Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buffer cycling in Emacs: avoiding scratch and Messages buffer

Tags:

emacs

Hi I have turned on buffer cycling placing following commands in my .emacs

(global-set-key (kbd "<C-tab>") 'bury-buffer)

But while cycling how do I avoid cycling through the Messages and the scrratch buffers which are always present in any emacs buffer list. I never use those buffers and become an eye-sore when cycling through my buffer-list

like image 485
smilingbuddha Avatar asked Oct 08 '11 21:10

smilingbuddha


People also ask

How do I stop buffering in Emacs?

Here are some commands for killing buffers: C-x k buffer RET. Kill buffer buffer ( kill-buffer ). M-x kill-some-buffers.

How do I navigate between buffers in Emacs?

To move between the buffers, type C-x b. Emacs shows you a default buffer name. Press Enter if that's the buffer you want, or type the first few characters of the correct buffer name and press Tab. Emacs fills in the rest of the name.

How many buffers can be open in Emacs at a time?

At any time, one and only one buffer is selected. It is also called the current buffer. Often we say that a command operates on "the buffer" as if there were only one; but really this means that the command operates on the selected buffer (most commands do).

What is scratch buffer Emacs?

At startup, the *scratch* buffer contains a short message, in the form of a Lisp comment, that explains what it is for. This message is controlled by the variable initial-scratch-message , which should be either a documentation string, or nil (which means to suppress the message).


4 Answers

If you never use the scratch buffer, just add this to your .emacs to automatically close it:

(kill-buffer "*scratch*")

I also found this code on the Emacs wiki which should do what you want:

; necessary support function for buffer burial
(defun crs-delete-these (delete-these from-this-list)
  "Delete DELETE-THESE FROM-THIS-LIST."
  (cond
   ((car delete-these)
    (if (member (car delete-these) from-this-list)
        (crs-delete-these (cdr delete-these) (delete (car delete-these)
                                                 from-this-list))
      (crs-delete-these (cdr delete-these) from-this-list)))
   (t from-this-list)))
; this is the list of buffers I never want to see
(defvar crs-hated-buffers
  '("KILL" "*Compile-Log*"))
; might as well use this for both
(setq iswitchb-buffer-ignore (append '("^ " "*Buffer") crs-hated-buffers))
(defun crs-hated-buffers ()
  "List of buffers I never want to see, converted from names to buffers."
  (delete nil
          (append
           (mapcar 'get-buffer crs-hated-buffers)
           (mapcar (lambda (this-buffer)
                     (if (string-match "^ " (buffer-name this-buffer))
                         this-buffer))
                   (buffer-list)))))
; I'm sick of switching buffers only to find KILL right in front of me
(defun crs-bury-buffer (&optional n)
  (interactive)
  (unless n
    (setq n 1))
  (let ((my-buffer-list (crs-delete-these (crs-hated-buffers)
                                          (buffer-list (selected-frame)))))
    (switch-to-buffer
     (if (< n 0)
         (nth (+ (length my-buffer-list) n)
              my-buffer-list)
       (bury-buffer)
       (nth n my-buffer-list)))))
(global-set-key [(control tab)] 'crs-bury-buffer)
(global-set-key [(control meta tab)] (lambda ()
                                       (interactive)
                                       (crs-bury-buffer -1)))

You will need to add the scratch and message buffers to the variable crs-hated-buffers, e.g.:

(add-to-list 'crs-hated-buffers "*Messages*")
(add-to-list 'crs-hated-buffers "*scratch*")
like image 175
Luke Girvin Avatar answered Sep 21 '22 23:09

Luke Girvin


Luke's answered your specific question. In my personal experience buffer cycling is more useful as a most-recently-used stack instead of the Emacs default cycling functions. That is, the buffers you most recently used should bubble to the top of the stack, similar to how alt-tab works in Windows.

There are quite a few packages that implement this on the wiki. buffer-stack is the one I recommend. It has a list of excluded buffers by default, I've included my buffer-stack-suppl configuration, which does same major-mode filtering. If you ask questions about buffer-stack, I'll try my best to help.

like image 26
event_jr Avatar answered Sep 21 '22 23:09

event_jr


All the buffers I did not use, such as scratch, messages and completions, messed with my workflow.

I've managed to completely get rid of them, without breaking emacs in any way.

Posted first here, and pasted bellow:

Place this in your .emacs:

;; Makes *scratch* empty.
(setq initial-scratch-message "")

;; Removes *scratch* from buffer after the mode has been set.
(defun remove-scratch-buffer ()
  (if (get-buffer "*scratch*")
      (kill-buffer "*scratch*")))
(add-hook 'after-change-major-mode-hook 'remove-scratch-buffer)

;; Removes *messages* from the buffer.
(setq-default message-log-max nil)
(kill-buffer "*Messages*")

;; Removes *Completions* from buffer after you've opened a file.
(add-hook 'minibuffer-exit-hook
      '(lambda ()
         (let ((buffer "*Completions*"))
           (and (get-buffer buffer)
                (kill-buffer buffer)))))

;; Don't show *Buffer list* when opening multiple files at the same time.
(setq inhibit-startup-buffer-menu t)

;; Show only one active window when opening multiple files at the same time.
(add-hook 'window-setup-hook 'delete-other-windows)

Bonus:

;; No more typing the whole yes or no. Just y or n will do.
(fset 'yes-or-no-p 'y-or-n-p)
like image 33
Ole Avatar answered Sep 19 '22 23:09

Ole


Well, if you use ido-mode to cycle between buffers you can set ido-ignore-buffers to match the buffers you want to ignore. From the documentation:

List of regexps or functions matching buffer names to ignore. For example, traditional behavior is not to list buffers whose names begin with a space, for which the regexp is "` ". See the source file for example functions that filter buffer names.

For more info on ido-mode see: Introduction to ido-mode

like image 27
PuercoPop Avatar answered Sep 21 '22 23:09

PuercoPop