In vanilla Emacs, I have to do C-x b
and then manually type out the buffer name to switch. If I forget the name of the buffer, I have to hit TAB
to list possible completions. Is there anything I can do to make this process faster?
25 Answers
I've recently started using C-x <left>
and C-x <right>
. Those are both standard Emacs bindings—nothing to configure. Left takes you back to the previous buffer (initially the same as C-x b RET
) but doing it again takes you to the 3rd most recently visited buffer. If you're popping back and forth between 3 or 4 buffers a lot then I find it a lot easier than trying to remember buffer names.
Also I just discovered C-x C-<left>
and C-x C-<right>
are bound to the same functions so that you can just hold down the control key and alternate between x and the arrows to flip through buffers. Pretty nice!
-
8I agree with you! I'm a newcomer to emacs, and don't want to be overloaded with extra packages and yet-more-configurations. This solution works for me! :D Commented Jul 8, 2015 at 4:24
-
C-x C-<left> and C-x C-<right> for the win! I used to have prev/next buffer bound to C-S-<left>/<right>. But somehow I find typing C-x C-left even easier then using two meta keys. C-S is awkward to press. Commented Nov 5, 2018 at 6:12
-
Nice! I learned these bindings a week ago, thought how awesome they were, and then forgot what they are. Couldn't find them on the net for a week, until now. Thanks! Commented Mar 30, 2019 at 20:36
-
There is ido mode that should help.
EDIT:
put this in your init file, to activate ido mode by default:
(ido-mode 1)
Now, C-x b
will show you a list of candidates.
Put this in your init file, if you prefer the items being displayed vertically:
(setq ido-separator "\n")
Courtesy of ergoemacs
Let me also suggest this post talking about switch to previous/next buffer.
-
2We should probably try to get most of the important information in that post as a response to this question. That way future users will not have to chase links to possibly dead sites.– nixeagleCommented Oct 6, 2014 at 19:13
-
@nixeagle I made some edits, I hope the most important informations are present. Commented Oct 6, 2014 at 19:26
-
I upvoted yours as it is complete now, but I will wait a few days before I accept in case others come with different and or better answers. Thanks!– nixeagleCommented Oct 6, 2014 at 19:30
-
-
1
You could use Helm.
In my setup, I have switched C-x b
for M-l
. This allied with helm-buffers-list
, makes the process pretty fast, at least for me.
-
This is basically the best answer now. Sure, you can still use
ido
oriswitchb
oricicles
, stuff like that, but they are basically obsoleted by Helm. Yeah,ace-jump-buffer
is cool, but it's not as fast as Helm, because before you know what key to press, you have to find the buffer in the list. Helm lets you narrow down the list with a few keystrokes which you already know (like the name of the buffer). I useC-TAB
to switch buffers, load recent files, bookmarks, etc. Nothing's faster.– user3871Commented Mar 29, 2015 at 10:42 -
5+1 for
helm
. But I prefer usinghelm-mini
, which also shows youRecentf
tohelm-buffers-list
.– joonCommented May 10, 2015 at 19:25 -
1
-
I find ivy much faster than helm, so I recommend it over helm. Commented Nov 5, 2018 at 6:08
Just to add tweaks that I use with already mentioned Ido:
(require 'ido)
(require 'ido-hacks)
(require 'flx-ido)
(require 'ido-vertical-mode)
(ido-mode 1)
(flx-ido-mode 1)
(ido-hacks-mode 1)
(ido-everywhere 1)
(ido-vertical-mode 1)
(setq ido-enable-flex-matching t
ido-use-filename-at-point 'guess
ido-vertical-define-keys 'C-n-C-p-up-down-left-right))
I'm also using ace-window
to switch between buffers together with ace-jump-mode
to jump between visible windows. Comes with some getting used to but worth trying.
I have combined it with key-chord-mode
and hit so
for quick jump:
(key-chord-define-global "so" 'ace-window)
All of the above mentioned packages are available via the package manager from Melpa.
-
I've edited your answer to replace
use-package
withrequire
to prevent confusion. In the given example, there is no specific need for a user to install an additional package to implement that solution. Commented Oct 30, 2014 at 11:03 -
1I'd suggest not to use letter combinations that occur frequently in English (assuming that you type mainly in English in emacs) as key-chords. In my opinion "so" is very easy to trigger unintentionally when you are typing stuff like "so", "some", "personal", etc very fast. Commented Oct 30, 2014 at 11:13
As @kuanyui says in a comment, next-buffer
& previous-buffer
are very quick, if you have only a few buffers to cycle among. (For more than a few, and for more buffer-choosing features, I use icicle-buffer
.)
However, by default, next-buffer
and previous-buffer
are on a prefix key, C-x
.
That means that they are not repeatable: you cannot just hit C-x <right> <right> <right>...
. You must instead either bind these commands to different, repeatable keys, or you must use C-x <right> C-x <right> C-x <right>...
, which is not very quick.
For repeatable versions of these commands, load library misc-cmds.el
and remap the vanilla, unrepeatable versions to the repeatable ones defined there:
(global-set-key [remap previous-buffer] 'previous-buffer-repeat)
(global-set-key [remap next-buffer] 'next-buffer-repeat)
(Similarly, in the same library you will find a version of undo
that is repeatable even when on a prefix key: undo-repeat
.)
BTW, you can make pretty much any command repeatable (even on a prefix key), by using function repeat-command
(from misc-cmds.el
). This is all that was needed to define the repeatable version of next-buffer
:
(defun previous-buffer-repeat ()
"Switch to the previous buffer in the selected window.
You can repeat this by hitting the last key again..."
(interactive)
(require 'repeat nil t) ; Library `repeat.el' is in Emacs 22.1 and later
(repeat-command 'next-buffer))
-
3...or simply bind next/previous buffer to
C-,
andC-.
to gain repeatability, and while you're on it anyway, setC-x.
andC-x,
toother-window
and(lambda()(interactive)(other-window -1))
. Cycling windows probably needs repeatability less, and you get away in a minute, without extra packages. Commented Jul 4, 2015 at 9:41 -
2@AndreasSpindler: You can of course put any command on a repeatable key and thus make it repeatable. That's not the point. Repeatable keys are few, and unbound ones are getting rarer. This is about how to make a command repeatable when it is on a prefix key.– DrewCommented Jul 4, 2015 at 18:27
-
1I unterstood your post, but referred more or less to the topic of this thread "How do I switch buffers quickly?". For this purpose I found your tip to use
next-buffer
andprevious-buffer
the best solution, independently from any specific key binding. Therefore my comment. Commented Jul 4, 2015 at 19:47 -
-
This is great, I many times need repeatable commands even when they have prefix keys. That said, I found prev/next buffer defaults to C-x C-left and C-x C-right which allows you to old C, and just repeat pressing x-left and x-right. Which is not too bad as default. Commented Nov 5, 2018 at 6:25
I'm surprised nobody mentioned iswitchb
yet. It may not be as powerful as ido
, but long before ido-mode
was born iswitchb-mode
was already standard for emacs, and it is still useful nowadays.
To try it just put
(iswitchb-mode 1)
into your .emacs or activate via M-x iswitchb-mode
.
-
4
iswitchb
is obsolete as of Emacs 24.4. All of its functionality is available viaido
now.– TylerCommented Oct 9, 2014 at 15:28 -
5@Tyler I like the default look of iswitchb better, because it's more colorful, prettier. Maybe ido can be configured to look the same, but there is no point (at least for me) while iswitchb is available.– TomCommented Nov 10, 2014 at 15:31
Use helm-buffer-list or helm-mini for visually selecting buffer entry and fuzzy matching. It can do a lot of things outside of switching buffers. You should see the demo in the link.
Others have discussed packages that are useful to make life easier with regards to buffer/window/frame switching. (I personally use helm
which is phenomenal.) I want to discuss simpler changes that can make for a more enjoyable Emacs experience.
For me, the solution to my buffer/frame woes (because I think C-x o
is the worst) was not a package; it was simply:
- mapping
C-;
toother-window
(cycling through windows) - mapping
C-'
toother-frame
(cycling through frames). I actually started using frames a lot more after adding this shortcut). - mapping other easy single-keystroke key-bindings to simple, custom functions for manipulating buffers in the visible frame. Examples include: throwing a buffer to the other window (in a 2-window set-up), toggling between the current buffer and last buffer in a given window, burying the other buffer (this one is great for replacing the
help
window that pops up (when reading man pages, apropos, etc) with whatever window was previously visible).
I also thought I'd share a few thoughts on key-bindings which newbies might find helpful. (Sorry if this is the wrong place.) I don't have specific suggestions but, rather, a "philosophy" that lets me maintain an effective set of keybindings for myself.
Unfrequently used commands do not deserve easy key-bindings.
- Download
keyfreq.el
and see what you really use a lot. I use a survival-of-the-fittest method to decide which commands are awarded the most valuable real-estate on my keyboard. - Don't be afraid to get rid of the default Emacs keybindings, some of which are just absurd.
C-\
is a great shortcut (for the reasons discussed in #2)...and it would be a crime to leave it mapped to its default command:change-input-method
. (Maybe some of you use it a lot, I certainly don't.)
- Download
The following factors determine the ease and speed of a keyboard shortcut (starting with the most important):
- Continuously pressing the modifier key (whatever it may be,
Ctrl
,Meta
, etc.). For example, it's way faster for me to hitC-c C-l C-p
than it is to hitC-x o
. I personally find it very inconvenient to have to lift off theCtrl
key in between successive key-strokes of a given key sequence; it's so much easier just to keep mashing the modifier button (in this case,Ctrl
). (This specific example might be influenced by the fact that I've remapped myCtrl
key and myCaps-Lock
key.) - Alternation in your friend. It's way faster for me to hit
C-c C-l C-p
than it is to hitC-p C-' C-;
. (Give it a try and it should be obvious.) Generally speaking, it's a lot easier to use keystroke sequences that use alternating hands (or, at the least, alternating fingers on the same hand). - Degree of departure from standard hand position. This doesn't apply to the hunt-and-peck enthusiasts out there. But for the rest of us, our most frequently used keyboard shortcuts should allow us to return to our standard touch-typing position quickly and easily.
- Continuously pressing the modifier key (whatever it may be,
Just a couple of thoughts that I thought might be helpful to newbies. I may add some more later.
I use windmove.el
and bs.el
to switch buffers quickly. Both modes
are included with Emacs.
windmove.el
- switches to a window (a buffer that is currently displayed) in a specified direction, e.g. left, right, up, down.bs.el
- I usebs-cycle-next
andbs-cycle-previous
to switch to the next buffer that isn't currently shown in a window.
You can setup windmove with (windmove-default-keybindings)
which
will bind the appropriate commands to Shift-Left Arrow
, Shift-Right Arrow
, etc.
For setting up bs.el
, something like this will work:
(global-set-key [(f9)] 'bs-cycle-previous)
(global-set-key [(f10)] 'bs-cycle-next)
-
-
2And a window isn't a frame. Regardless,
windmove.el
is under the EmacsWiki page "Switching Buffers", plusbs.el
operates on buffers, not windows.– JoeCommented Oct 7, 2014 at 22:01 -
I prefer browser-style tab navigation. I have bound Ctrl+Tab and Ctrl+Shift+Tab to next and previous buffers respectively:
(global-set-key [C-tab] 'next-buffer)
(global-set-key [C-S-tab] 'previous-buffer)
The fastest way to switch buffers I'm aware of is with ace-jump-buffer
. One key stroke opens the buffer menu and then select the letter next to the buffer you want with ace-jump-line-mode
. This works best for the 26 or so most recent buffers. For switching to buffers farther back in your usage history, ido-switch-buffer
is always a good choice.
Widely speaking, there are 2 ways to switch buffers and most of the solutions will fall into one of these.
- Using the mini buffer
This is what C-x b
does by default. Ido gives you nice auto completion so that
you don't need to TAB TAB TAB so much. Projects like helm, projectile etc makes
it easier by giving you abilities to work with files in a project etc, and along
with ido, they can be pretty useful.
- An entire buffer with all items listed.
This is useful when you have so many buffers and you want a list of them all. You can go up and down the buffer, and press enter to visit a particular file. Ibuffer is pretty good for that.
I use both and this is how you can set it up.
(global-set-key (kbd "C-x C-b") 'ido-switch-buffer)
(global-set-key (kbd "C-x b") 'ibuffer)`
Addition to ido, I have following setup.
In case of evil mode, use following key bindings to switch buffers
(define-key evil-normal-state-map (kbd "C-j") 'next-buffer) (define-key evil-normal-state-map (kbd "C-k") 'previous-buffer) (define-key evil-normal-state-map (kbd "C-S-h") 'evil-window-left) (define-key evil-normal-state-map (kbd "C-S-j") 'evil-window-down) (define-key evil-normal-state-map (kbd "C-S-k") 'evil-window-up) (define-key evil-normal-state-map (kbd "C-S-l") 'evil-window-right)
Set buffer name as title of window. This will help you when navigating with above keybindings
(setq frame-title-format "%b")
If you are using evil mode, you can try
evil-leader
. Bind frequently used keys to leader. e.g You can bindC-x b
to<leader>-b
.(global-evil-leader-mode) (evil-leader/set-leader ",") (evil-leader/set-key "b" 'switch-to-buffer)
Bind dired mode to
C-<f8>
. Now when you want to open file in directory, pressC-<f8>
, navigate to file name & pressa
instead ofRET
. So dired buffer will be killed immediately.(global-set-key (kbd "C-<f8>") (lambda () (interactive) (dired default-directory))) (put 'dired-find-alternate-file 'disabled nil)
Use
recentf
mode to navigate recent files.C-x C-r
One last important point is - use ergonomic mechanical keyboard. So you can press 'ctrl' key easily and quickly with wrist instead of hurting pinky.
Hope this might help someone.
Alternatively, you can click the buffer name in the mode line to cycle to the next buffer.
-
1You might like to clarify how to click, i.e. which buttons to use, especially since you can use that to go either to the next or to the previous buffer.– StefanCommented Apr 14, 2020 at 12:35
-
I didn't know about this until now. It's nice when you just use mouse/trackpad for whatever reason. would never believe this is possible in Emacs. Makes me wonder what else am missing. Right click move you to previous buffer. Commented Apr 14, 2020 at 20:37
-
I get a hint about the button to press when the mouse pointer is on the buffer name, in the mode line. I use Emacsformacosx on Apple Mac, not in a shell window.– MarcelloCommented Apr 20, 2020 at 23:13
Surprised no one mentioned iflipb. Much better than bare cycling through.
iflipb provides three commands:
iflipb-next-buffer
,iflipb-previous-buffer
andiflipb-kill-buffer
.
iflipb-next-buffer
behaves likeAlt-TAB
: it switches to the previously used buffer, just likeC-x b RET
(orC-M-l
in XEmacs). However, another consecutive call toiflipb-next-buffer
switches to the next buffer in the buffer list, and so on. When such a consecutive call is made, the skipped-over buffer is not regarded as visited.While flipping, the names of the most recent buffers are displayed in the minibuffer, and the currently visited buffer is surrounded by square brackets and marked with a bold face.
A key thing to notice here is that iflipb displays the buffer contents after each step forward/backwards (in addition to displaying the buffer names), unlike for instance the buffer switching model of ido-mode where only the buffer names are displayed.
-
Welcome to Emacs.SE! Please provide more detail to make your answer stand alone. We discourage link-only answers because the links can change at any time.– Dan ♦Commented Jan 17, 2021 at 20:03
As you can see from the other answers, there are many ways to streamline this particular task. I personally use Lusty Emacs as my buffer switching apparatus, as it handles fuzzy completion quite nicely. This minimizes the number of key-strokes required to select the new buffer; for example I need only type oxr<tab>
to switch to ox-reveal.el
, or even just o
the second time around, as it tracks the history of switches as well.
If you want to just keep brushing through your buffers, add this to your init file:
(bind-key* "M-l" 'next-buffer)
(bind-key* "M-k" 'previous-buffer)
You do need the bind-key package and you can change the key-bindings to your wish but I found these keybindings to be the most comfortable. Before adding this snippet, you do need to require bind-key by,
(require 'bind-key)
I using C-Tab and C-S-Tab like general applications.
(defun change-buffer-until-normal (change-fn)
(let (current (buffer-name))
(funcall change-fn)
(while (and (string-prefix-p "*" (buffer-name))
(not (eq current (buffer-name))))
(funcall change-fn))))
(defun my-next-buffer ()
(interactive)
(change-buffer-until-normal 'next-buffer))
(defun my-prev-buffer ()
(interactive)
(change-buffer-until-normal 'previous-buffer))
(global-set-key [C-tab] 'my-next-buffer)
(global-set-key [C-S-iso-lefttab] 'my-prev-buffer)
This is not a very elegant solution (since it uses macros instead of functions) and it does not answer how to jump to any buffer quickly, but only how to jump to buffers used recently. But it works very well for me:
I have a series of macros using ido-switch-buffer
to jump to the previous buffer, the buffer before that, the buffer before that, etc. And I bound those macros to key bindings that I find convenient.
To jump to the last buffer:
(fset 'my-goto-previous-buffer
[?\C-x ?b return])
To jump to the penultimate buffer:
(fset 'my-goto-previous-buffer
[?\C-x ?b ?\C-n return])
To jump to the antepenultimate buffer:
(fset 'my-goto-previous-buffer
[?\C-x ?b ?\C-n ?\C-n return])
Etc. (you can ad as many ?\C-n
as you want to go as far back the history of visited buffers as you want).
There is a built-in function to jump to the previous buffer, but the advantage of using macros with ido-switch-buffer
is that you can customize the variable ido-ignore-buffers
to remove special buffers such as *Messages*
or *scratch*
and any other unwanted buffer from the list. That way, the only buffers you jump to are the ones you are really interested in jumping to.
I see people have mentioned ido
and iswitchb
but oddly enough, noone mentioned icomplete-mode
yet, which is another similar alternative.
The main difference is that instead of changing the binding of C-x b
to some alternative command, it works by changing the way minibuffer completion works, so it affects all minibuffer completion operations rather than only buffers (or files).
In Emacs-27, there is also a variant of icomplete-mode
called fido-mode
which aims to mimick the ido
UI, but again by changing the whole minibuffer completion UI so it applies "everywhere".
There is also the package nswbuff which give you live preview of the buffer you want open.
ibuffer, classic package:
(require 'ibuffer) (require 'ibuf-ext) (defalias 'list-buffers 'ibuffer) (setq ibuffer-expert 1 ibuffer-hidden-filter-groups (list "Helm" "*Internal*") ibuffer-saved-filter-groups '( ("default" ("Dired" (mode . dired-mode)) ("Markdown" (mode . markdown-mode)) ("YAML" (mode . yaml-mode)) ("Org" (mode . org-mode)) ;; Some ;; settings for buffer ;; grouping ;; Emacs standard buffers in one place ("Emacs" (or (name . "^\\*scratch\\*$") (name . "^\\*Messages\\*$") (name . "^\\*\\(Customize\\|Help\\)") (name . "\\*\\(Echo\\|Minibuf\\)"))))) ibuffer-show-empty-filter-groups nil ;; Do not show empty groups ibuffer-truncate-lines nil ibuffer-sorting-mode 'filename/process ibuffer-formats ;; Columns order and size '( ( mark modified read-only locked " " (mode 8 -1 :left) " " (name 30 40 :left :elide) " " filename-and-process) ( mark " " (name 16 -1) " " filename)) ibuffer-use-other-window nil) (defun setup-ibuffer-mode () "Settings for 'ibuffer-mode'." (interactive) (hl-line-mode 1) (ibuffer-auto-mode 1) (ibuffer-switch-to-saved-filter-groups "default")) (global-set-key (kbd "<f2>") 'ibuffer) (add-hook 'ibuffer-mode-hook #'setup-ibuffer-mode)
Helm (all-in-one, ultimate package):
(require 'helm) (global-set-key (kbd "<f2>" 'helm-buffer-list))
nim-switch-buffers
Emacs buffer switching made easy.
Wouldn't it be nice if emacs buffer switching worked like Firefox tab switching?
- Never loose the existing order.
- Control-PgUp/Dn to switch buffers in order without end.
- Shift-Control-PgUp/Dn to shift current buffer in the list.
I didn't want tabs and the mess they make on the console. I just wanted the functionality.
Ordered tab switching without the tabs.
I created/uploaded the code at github.com/paulihano/nim-switch-buffers
Click the buffer name in the mode-line to switch to the previous (left click) or next (right-click) buffer in the list of current buffers.
-
Not clear what you're saying. I'm guessing you mean clicking the buffer name in the mode-line. If so, please say so.– DrewCommented Jan 15, 2023 at 18:45
On my VM, C-x ALT-RIGHT / LEFT is too slow. I have to wait for C-x to register in the status bar before I can do anything else. plus its just too many keys. So i mapped C-TAB and C-M-TAB to next and prev buffer as that's what I do in most windows programs anyway. Its easy to do from within emacs:
M-x
takes you to the status bar.
Then type global-set-key
which will prompt you for the key sequence you wish to bind (C-TAB
) and then for the command you wish to bind it to (next-buffer
). That's it!
-
1Welcome. Other answers seem to already cover mapping these commands to other keys. Commented Dec 17, 2017 at 13:00
-
Not exactly - everyone is talking about how to change your .emacs config file. I've described how to do it from within emacs session Commented Jan 11, 2018 at 3:51
C-x C-<left/right>
to switch buffers quickly. When needing to switch to "farther" buffer, I useIBuffer
.