For example in gdb mode, there is a buffer named
*input/output of <program name>*, that I want to clear each time I press
In general, if you don't know the exact name of the buffer, you need to call
buffer-list to obtain the list of buffers and filter the result to only act on the desired buffer(s).
(defun erase-buffers-matching (regexp) "Erase the content of all buffers whose name matches REGEXP." (interactive "sErase buffers matching: ") (save-match-data (mapc (lambda (buffer) (cond ((not (string-match regexp (buffer-name buffer)))) ((and (buffer-file-name buffer) (buffer-modified-p buffer)) (message "Skipping buffer %s with unsaved data" (buffer-name buffer)) (t (with-current-buffer buffer (erase-buffer))))) (buffer-list))))
For the specific case of the GDB program output buffer, GDB keeps track of it. It's the
(defun erase-gdb-inferior-io-buffer () (interactive) (with-current-buffer (gdb-get-buffer 'gdb-inferior-io) (erase-buffer))) (global-set-key (kbd "C-c l") 'erase-gdb-inferior-io-buffer)
If this is something you want to do often, you could define a function for it in your .emacs file. Otherwise you could type it out in scratch, evaluate it and then assign it a key binding?
You could try:
(defun my/clear_buffer () (interactive) (let ((tmp (buffer-name))) (if (get-buffer "<buffer name>") ;; check if buffer exists (progn (switch-to-buffer "<buffer name>") ;; switch to the buffer (erase-buffer) ;; erase it (switch-to-buffer tmp))))) ;; switch back