Emacs 27.1

M-x ansi-term

run some process that outputs lots of text.

Then C-c and type Linux command clear.

Only the current page is cleared, in the buffer. But I need to clear all text from the buffer.

1 Answer 1

(defun my-term-reset-terminal ()
  (let ((inhibit-read-only t))

In char mode you can call it after typing C-c (or maybe C-x). E.g.:

C-cM-x my-term-reset-terminal

Or you might prefer:

(with-eval-after-load "term"
  (define-key term-raw-escape-map (kbd "C-l") 'my-term-reset-terminal))

Which gives you the C-cC-l binding when in char mode.

I think you should M-x report-emacs-bug though, as the code for term-emulate-terminal comments that the clear command will result in a call to term-reset-terminal and seemingly that doesn't happen.

  • In my case this help: (define-key term-mode-map (kbd "C-l") #'my-term-reset-terminal) Commented Jul 7, 2023 at 7:47
  • If I execute my-term-reset-terminal when ansi-term have some live ouput then clear not working correctly. I need to stop output (e.g. by C-c) and the only then run my-term-reset-terminal. It not very convinient. Commented Jul 10, 2023 at 7:26
  • 1
    Typing clear in a terminal emulator also doesn't abort the process you're running in the terminal. It would not be expected behaviour for that to happen. If you want to programmatically send C-c to the inferior process you can use (term-send-raw-string "\C-c").
    – phils
    Commented Jul 10, 2023 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.