I recommend IswitchBuffers, because it is easy to understand. I don’t need the power of InteractivelyDoThings, which does the same thing for opening files, and many unrelated things besides. – AlexSchroeder
‘icicle-buffer’ – see Icicles - Buffer-Name Input. You can:
Nothing is simpler or quicker. You are not limited to cycling: you can also use completion. And you can open a new, non-existant buffer. – DrewAdams
These libraries let you switch among buffers and files based on matching their content, not just their names:
There are several libraries that let you cycle among buffers:
‘next-buffer’; if you have XEmacs or an older Emacs, you could use Lisp:prev-next-buffer.el to define them. The Emacs 22.1 key bindings for these functions (by default, `
C-x <left>’ and `
C-x <right>’ respectively) are inconvenient, so you may want to rebind them (see KeyBindingNotation) regardless of Emacs version. DrewAdams: or look at Lisp:setup-keys.el, in which I bind them to chords `
C-<pause>’ and `
M-<pause>’ — chords are convenient and easily to repeat single-gesture keystrokes, see ChordDefined. (However, I still prefer
‘doremi-buffers’to cycle among buffers directly.
‘C-TAB’cycling of buffers.
‘M-x anything-for-buffers’, narrow the buffer list by typing some patterns (multiple patterns are space-delimited string), select with up/down/pgup/pgdown/C-p/C-n/C-v/M-v, choose with enter, With
‘TAB’actions can be selected if the selected candidate has more than one possible action. With
‘C-z’contents of selected candidate are displayed.
With the “switching” or “buffer flipping” technique, the challenge is to do as little buffer-switching as possible. That is, you would like the buffers you switch between most to be near to each other. One solution is to sort buffers in the order they were visited. Then the most-recently-used buffer is always just below the current one. You hit a problem, though, when you want to switch between two buffers which are further apart in the stack. Imagine you are at the first buffer. As you move through the stack, each buffer is visited and gets put on top, so by the time you get to the target buffer there are a bunch of buffers between you and the first. If you want to go back, you have to switch through them again.
A solution without this problem is demonstrated by Microsoft Windows. On this platform, ALT-TAB lets you switch through windows (”Frames” in Emacs lingo) to pick the one you want, then pulls that window to the top. This comes much closer to the ideal of stacking in order of use because it distinguishes between “using” and “switching” – unlike the simple model described above. Several libraries provide something similar for Emacs buffers:
(defun switch-to-previous-buffer () (interactive) (switch-to-buffer (other-buffer (current-buffer) 1)))
I have this (or something similar) bound to F1 and it is really handy. Saves me from the Ctrl-key sickness also… ☺ – MaDa
‘icicle-buffer’to alternate between two buffers – trivial.
In Emacs you can do many things at once in multiple frames (outside Emacs frames are sometimes called “windows”). For instance: If you have multiple screens on your machine you can open individual Emacs frames for each screen. Each frame contains its own buffers and of course each frame can be split into Emacs windows.
I wrote the following bit of Emacs lisp in order to switch the contents of two open frames. This is very useful for me at work since I use a two monitor setup. Sometimes I want to edit whatever is on the other monitor on my “main” monitor and vise-versa.
(defun switch-buffers-between-frames () "switch-buffers-between-frames switches the buffers between the two last frames" (interactive) (let ((this-frame-buffer nil) (other-frame-buffer nil)) (setq this-frame-buffer (car (frame-parameter nil 'buffer-list))) (other-frame 1) (setq other-frame-buffer (car (frame-parameter nil 'buffer-list))) (switch-to-buffer this-frame-buffer) (other-frame 1) (switch-to-buffer other-frame-buffer)))
If you have a window split showing two buffers, you can transpose the two buffers:
(defun transpose-buffers (arg) "Transpose the buffers shown in two windows." (interactive "p") (let ((selector (if (>= arg 0) 'next-window 'previous-window))) (while (/= arg 0) (let ((this-win (window-buffer)) (next-win (window-buffer (funcall selector)))) (set-window-buffer (selected-window) next-win) (set-window-buffer (funcall selector) this-win) (select-window (funcall selector))) (setq arg (if (plusp arg) (1- arg) (1+ arg))))))
See also buffer-move.el written by Lukhas
If switching through windows rapidly would be useful…
(global-set-key [s-left] 'windmove-left) (global-set-key [s-right] 'windmove-right) (global-set-key [s-up] 'windmove-up) (global-set-key [s-down] 'windmove-down)
Question: I really like TextMate’s “Go To File” (Navigation > Go To File, or Command-T) functionality. How can I replicate it in emacs? None of the buffer switching techniques above come close to touching it. Of course, TextMate’s “GTF” only works if you have a “project” loaded – but I’d want the emacs equivalent to work underneath some directory that I’ve specified. – DavePeck
Question: Is there a way to find out that the user has switched to a buffer, i.e., someting like switch-to-buffer-hook? The manual seems to say no, which is surprising. I would like to set some frame-specific variables according to the buffer which has been selected…. JoergVolbers?
‘current-buffer’. You might also want to look at variable
‘major-mode’and the hooks
‘after-change-major-mode-hook’. The last of these is in Emacs 22 only. – DrewAdams
Thank you for the fast reply, but what I want to do is to catch, so to speak, the switch-event. My aim is to set the font (via set-frame-font) according to the biggest current buffer’s major-mode. I already wrote a small defun which hooks itself in
(if (one-window-p) (set-frame-font (get-fontname-according-to-major-mode)))
Unfortunately, the condition (one-window-p) does not filter out displaying the menu and tooltips. Also, some resizing events trigger this function. But all I want is to change the frame font if one switches to another buffer… Maybe I should advice switch-buffer, but that seems to be a rather unelegant way.
Question: I don’t like how emacs handles showing the same buffer in two windows at once: if I switch away from that buffer, then (a) switching back to that buffer moves point to its location in the other window, and (b) I can’t switch back with “C-x b RET” because it isn’t the “default” target. Are there any packages that remember the location of point when switching buffers and/or allow me to quickly switch back to a buffer even if another window is viewing it?
If you use Icicles, then, in Icicle mode,
‘C-u C-x o’ gives you a multi-command that lets you choose a window (or a frame) by its buffer name (with numeric suffix to distinguish multiple windows for the same buffer). You can also cycle among windows whose buffers match a given pattern. If you also use library crosshairs.el, then it’s easier to see which window is selected as you cycle. – DrewAdams
What I mean is this. Load a file and split the main window (
‘C-x 2’). Move point in one of the windows around, for instance by pressing C-v a few times. Now each window has its own point. Now, type
‘C-x b’ in one window and switch to another buffer in it. Now, type
‘C-x b’ and switch back to the buffer you were previously viewing (the one that’s available in the other window). Point will be moved to its location in the other window.
This comes up a lot for me when I’m working on two parts of a file at once. I split the window and scroll each half until it’s showing the part that I want. Then I start working in one of the windows – but sometimes I want to refer to another file temporarily. If I just switch to a new file in the window I’m using, I have to move point back to where I just was, which is a bit annoying. The only solution I’ve found is to split the window again, switch buffers in the active half, and then delete the split when I’m done. It works, but it takes more keypresses and it means I don’t have as large a viewing area.
For exactly this use case there is pointback-mode: http://stud4.tuwien.ac.at/~e0225855/pointback/pointback.html
I’m often facing the same problem (working on part 1 of a single file in window1 and part 2 of the same file in window2, switching temporarily to another file in window2 and wanting to switch back to part 2 of the single file in window2). I noticed that many commands which read from the MiniBuffer, like
‘C-x b’), for example, record (on the execution stack) the window configuration (maybe using something like
‘save-window-excursion’). So I often use this very simple trick. First, check
‘enable-recursive-minibuffers’ is T, then, from inside window2, type:
‘C-x b’or any similar command),
‘C-- C-x o’WITHOUT ANY RETURN to go back to window2 (this lets the
‘M-x ’prompt in the MiniBuffer),
‘C-x b RET’(or select one from the menu) in window2,
‘M-x something RET’),
‘C-x o’to switch back to the MiniBuffer (or click inside it),
‘C-g’to abort the MiniBuffer command,
This “trick” does not require any external module! HTH – JackDuthen?
Question I would like to have buffers sticked to the frames, i.e. when I use C-x b, then I see only buffers that belongs to current frame. This could be convenient when work on different projects simultaneously and do not want to mix buffers. Somebody has an idea? – [[konsy?]]
Yes. Use Icicles. Using
‘icicle-buffer’ (bound to
‘C-x b’ in Icicle mode, by default) with a negative prefix argument will do what you want:
‘C-- C-x b’.
And with a positive prefix arg (e.g.
‘C-u C-x b’), only buffers visiting files are candidates. – DrewAdams
Thanks for the answer. Just for the record - there is a small package that allows to do that without icicles - flobl.el. Very handy. – Konsy
This functionality is also provided by the FrameBufs package.