Do you know how to clear ansi term in Emacs so it actually clear the terminal and not change the scroll position?

Yes, in Emacs’ ansi-term mode, when you try to clear the screen using clear or Ctrl+L, it typically just inserts blank lines instead of actually resetting the terminal display. To properly clear the terminal without affecting the scroll position, try one of the following approaches:

1. Use tput reset

Run this inside ansi-term:

sh
tput reset

This sends a proper terminal reset sequence that should fully clear the screen.

2. Use printf with the ANSI Escape Code

sh
printf "\033c"

This sends the ESC c sequence, which performs a terminal reset.

3. Use clear && echo -en "\e[3J"

sh
clear && echo -en "\e[3J"

The \e[3J ensures the terminal history is wiped instead of just inserting blank lines.

4. Create a Shell Alias

If you're using Bash or Zsh inside ansi-term, add this to your .bashrc or .zshrc:

sh
alias cls='clear && echo -en "\e[3J"'

Then, just run cls inside ansi-term.

Would you like a more Emacs Lisp-based solution, or does this work for your use case?