3

I am trying the clear the inferior emacs lisp mode(ielm) buffer. Solutions applicable to shell are not applicable here - the erase-buffer command says "Test is read only"

5

inferior-emacs-lisp-mode (aka ielm), like various other subshell modes in Emacs, derives from comint-mode, a general COMmand INTerpreter. This means that ielm inherits a lot of comint's functionality and key bindings.

Emacs 25 added the command comint-clear-buffer, by default bound to C-c M-o, which works in any comint derivative, including ielm.

In older Emacsen I propose two solutions:

  1. Define comint-clear-buffer and its binding yourself:

    (with-eval-after-load 'comint
      (unless (fboundp 'comint-clear-buffer)
        (defun comint-clear-buffer ()
          "Clear the comint buffer."
          (interactive)
          (let ((comint-buffer-maximum-size 0))
            (comint-truncate-buffer)))
    
        (define-key comint-mode-map "\C-c\M-o" #'comint-clear-buffer)))
    

    or, in even older Emacsen than that:

    (eval-after-load 'comint
      (lambda ()
        (unless (fboundp 'comint-clear-buffer)
          (defun comint-clear-buffer ()
            "Clear the comint buffer."
            (interactive)
            (let ((comint-buffer-maximum-size 0))
              (comint-truncate-buffer)))
    
          (define-key comint-mode-map "\C-c\M-o" #'comint-clear-buffer))))
    
  2. Make the best of what is available:

    1. M-x mark-whole-buffer (C-x h) - Mark whole buffer.
    2. M-x comint-kill-region - Kill buffer contents, including read-only prompts.
    3. M-x ielm-return (RET/<return>) - Create new prompt.
2
  • there is no comint-clear-buffer command, and no C-c M-o key binding – Draif Kroneg May 18 '17 at 7:05
  • 1
    @DraifKroneg Sorry about that, it seems this function was only added in Emacs 25. I'll update my answer. – Basil May 18 '17 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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