67

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?

  • 10
    This should get marked Community Wiki. But Emacs already has a giant wiki. For instance, my top Google result is this page on Switching Buffers. CW-style questions feel, to me, particularly out of place on emacs.SE. – purple_arrows Oct 6 '14 at 22:16
  • 1
    I like using C-x C-<left/right> to switch buffers quickly. When needing to switch to "farther" buffer, I use IBuffer. – kuanyui Oct 7 '14 at 15:35
  • I use 'C-x x' to switch buffers (with helm and with caps mapped to control). I've never felt comfortable typing 'C-x b'. – wdkrnls Oct 7 '14 at 23:55
  • 1
    How can I more easily switch between buffers in Emacs?, Emacs: help me understand file/buffer management, along with numerous linked Q&As from each. – phils Oct 9 '14 at 5:50
  • 1
    Use one of the solutions in the answers and don't use C-x b. 3 keys are too many for such a frequent operation. I personally use a single key, the right windows key which I don't use otherwise. – Tom Nov 10 '14 at 13:12

17 Answers 17

49

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.

  • 2
    We 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. – nixeagle Oct 6 '14 at 19:13
  • @nixeagle I made some edits, I hope the most important informations are present. – Nsukami _ Oct 6 '14 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! – nixeagle Oct 6 '14 at 19:30
  • @nixeagle it's nothing! – Nsukami _ Oct 6 '14 at 19:40
  • 1
    @nixeagle have you found a better answer? – Emacs User Nov 13 '15 at 16:34
42

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!

  • 4
    I 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 – ankush981 Jul 8 '15 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. – Didier A. Nov 5 '18 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! – Kirill Yunussov Mar 30 at 20:36
40

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 or iswitchb or icicles, 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 use C-TAB to switch buffers, load recent files, bookmarks, etc. Nothing's faster. – blujay Mar 29 '15 at 10:42
  • 4
    +1 for helm. But I prefer using helm-mini, which also shows you Recentf to helm-buffers-list. – joon May 10 '15 at 19:25
  • 1
    There is also ivy with ivy-switch-buffer. – Oleksii Filonenko Aug 25 '18 at 18:21
  • I find ivy much faster than helm, so I recommend it over helm. – Didier A. Nov 5 '18 at 6:08
10

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 with require to prevent confusion. In the given example, there is no specific need for a user to install an additional package to implement that solution. – Kaushal Modi Oct 30 '14 at 11:03
  • 1
    I'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. – Kaushal Modi Oct 30 '14 at 11:13
10

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-, and C-. to gain repeatability, and while you're on it anyway, set C-x. and C-x, to other-window and (lambda()(interactive)(other-window -1)). Cycling windows probably needs repeatability less, and you get away in a minute, without extra packages. – Andreas Spindler Jul 4 '15 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. – Drew Jul 4 '15 at 18:27
  • 1
    I 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 and previous-buffer the best solution, independently from any specific key binding. Therefore my comment. – Andreas Spindler Jul 4 '15 at 19:47
  • @AndreasSpindler: Got it; thx. – Drew Jul 4 '15 at 20:33
  • 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. – Didier A. Nov 5 '18 at 6:25
9

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 via ido now. – Tyler Oct 9 '14 at 15:28
  • 4
    @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. – Tom Nov 10 '14 at 15:31
7

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.

6

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-; to other-window (cycling through windows)
  • mapping C-' to other-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.

  1. 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.)
  2. 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 hit C-c C-l C-p than it is to hit C-x o. I personally find it very inconvenient to have to lift off the Ctrl 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 my Ctrl key and my Caps-Lock key.)
    • Alternation in your friend. It's way faster for me to hit C-c C-l C-p than it is to hit C-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.

Just a couple of thoughts that I thought might be helpful to newbies. I may add some more later.

3

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 use bs-cycle-next and bs-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)
  • Buffer is NOT Window (Frame)! Don't confound them! – kuanyui Oct 7 '14 at 15:33
  • 2
    And a window isn't a frame. Regardless, windmove.el is under the EmacsWiki page "Switching Buffers", plus bs.el operates on buffers, not windows. – Joe Oct 7 '14 at 22:01
  • I clarified what windmove does. – Joe Oct 7 '14 at 22:10
3

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.

3

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)
2

Widely speaking, there are 2 ways to switch buffers and most of the solutions will fall into one of these.

  1. 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.

  1. 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)`
  • For the second Helm is also a nice option – Tom Nov 10 '14 at 15:33
2

Addition to ido, I have following setup.

  1. 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)
    
  2. Set buffer name as title of window. This will help you when navigating with above keybindings

    (setq frame-title-format "%b")
    
  3. If you are using evil mode, you can try evil-leader. Bind frequently used keys to leader. e.g You can bind C-x b to <leader>-b.

    (global-evil-leader-mode)
    (evil-leader/set-leader ",")
    (evil-leader/set-key
     "b" 'switch-to-buffer)
    
  4. Bind dired mode to C-<f8>. Now when you want to open file in directory, press C-<f8>, navigate to file name & press a instead of RET. 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)
    
  5. Use recentf mode to navigate recent files. C-x C-r

  6. 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.

1

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 minimises the number of key-strokes required to select the new buffer, for example I need only type oxr to switch to ox-reveal.el, or even just o the second time round as it does track history of switches as well.

0

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)
0

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)
-1

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!

  • 1
    Welcome. Other answers seem to already cover mapping these commands to other keys. – Andrew Swann Dec 17 '17 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 – S.N. Jan 11 '18 at 3:51

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.