0

I have this kbd in my init.el

(global-set-key (kbd "C-l") 'comint-clear-buffer)

When I use Emacs-ess mode fro programming in R, I use this kbd to clear the R output buffer, this is usually called *R*. That works, but it requires that I go into the *R* buffer and do C-l from there.

Can this be modded so that wherever I am into some other buffer, the comint-clear-buffer command is applied specifically to the *R* buffer when I am into an ESS mode buffer?

1

This switches to the R REPL buffer in its window, or in the the current one if no window is displaying it, and clears the screen.

(defun my-r-clear-buffer ()
  (interactive)
  (pop-to-buffer (ess-get-process-buffer) '((display-buffer-reuse-window
                                             display-buffer-same-window)))
  (comint-clear-buffer))

(define-key ess-r-mode-map (kbd "C-l") #'my-r-clear-buffer)

However, C-l is normally already bound to recenter-top-bottom, which is a pretty useful function. I suggest you to use

(define-key ess-r-mode-map (kbd "C-c l") #'my-r-clear-buffer)

instead.


(ess-get-process-buffer) retrieves the R REPL buffer associated with the current R source code buffer. IOW, the function above clears the buffer that C-RET sends lines to.

This version should be more stubborn about finding an R REPL buffer to clear.

(defun my-r-clear-buffer ()
  (interactive)
  (let ((r-repl-buffer (seq-find (lambda (buf)
                                   (string-prefix-p "*R" (buffer-name buf)))
                                 (buffer-list))))
    (if r-repl-buffer
        (pop-to-buffer r-repl-buffer '((display-buffer-reuse-window
                                        display-buffer-same-window)))
      (user-error "No R REPL buffers found"))
    (comint-clear-buffer)))

It just switches to the first buffer in the buffer list whose name begins with "*R" and clears that.

Note: the reason why I search for a buffer whose name begins with "*R" instead of just aiming at the buffer named *R* is that in my Emacs (v. 27.1, ESS v. 18.10) R REPL buffers have paths in their names, like *R:~*.


it doesn't move back to the original associated buffer.

You just have to ask.

(defun my-r-clear-buffer ()
  (interactive)
  (let ((r-repl-buffer (seq-find (lambda (buf)
                                   (string-prefix-p "*R" (buffer-name buf)))
                                 (buffer-list))))
    (if r-repl-buffer
        (with-current-buffer r-repl-buffer
          (comint-clear-buffer))
      (user-error "No R REPL buffers found"))))

(global-set-key (kbd "C-c l") #'my-r-clear-buffer)
6
  • thanks but it doesn't work, says: "No ESS process is associated with this buffer now" – PinkCollins Feb 10 at 15:21
  • @PinkCollins I've updated the answer – Arch Stanton Feb 10 at 16:38
  • This one works althogh after cleaning the R process buffer it doesn't not move back to the original associated buffer. – PinkCollins Feb 12 at 13:53
  • Your kbd didn't work in my case, I don't know why. I did my usual way: (eval-after-load "ess-mode" '(define-key ess-mode-map (kbd "C-c l") 'my-R-clear-buffer)) and it now works – PinkCollins Feb 12 at 13:53
  • @PinkCollins If you want to be able to call the function from any buffer, in any mode, you have to bind it in the global map. See my updated answer. (If you read my now deleted comments, forget them, they didn't solve your problem.) – Arch Stanton Feb 12 at 15:19

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.