Emacs: Switch Buffer

By Xah Lee. Date: . Last updated: .

This page is a tutorial on switching buffers in emacs. If you don't know what buffers are, first see: Emacs: List Buffers.

Alt+x switch-to-bufferCtrl+x b】 to switch to a different buffer. Emacs will prompt you for a name, and you can press Tab for name completion.

A better command to switch buffer is ido-switch-buffer in ido-mode.

ido-switch-buffer will show a list of candidates AUTOMATICALLY as you type (no need to press Tab first).

emacs ido-mode show vertical 2016-06-27 52017
Emacs ido-mode's ido-switch-buffer in action.
  1. Alt+x ido-mode to turn it on.
  2. Alt+x ido-switch-bufferCtrl+x b】 to switch buffer.

When ido-mode is on, the key 【Ctrl+x b】 will call ido's version (ido-switch-buffer).

Ido Hotkeys

When ido-switch-buffer is called, it will prompt for a choice of buffer name.

Here's the most useful ido hotkeys when in the prompt:

  1. 】 → Select the next choice.
  2. 】 → Select the previous choice.
  3. 】 → Go to the buffer you switched to last time. 【】 for next.
  4. Enter ↵】 → Use the first choice.
  5. Ctrl+f】 → stop suggestion. useful for creating new file. 【Ctrl+b】 to go back to buffer selection mode.
  6. Ctrl+d】 → switch to open directory mode.
  7. Ctrl+g】 → cancel.

How to create a new file with ido-mode?

Alt+x ido-switch-buffer, then press 【Ctrl+f】 to stop suggestion. This is useful when creating new file.

When you want to open a directory, press 【Ctrl+d】.

Set ido-mode as Default

To make ido-mode default for switching buffer, put the following in your emacs init:

;; make buffer switch command auto suggestions, also for find-file command
(ido-mode 1)

Make Ido Suggestions Display Vertically

Ido can show suggestions vertically. This makes it easier to read.

Put the following in your emacs init file.

  (require 'ido)
  ;; make buffer switch command do suggestions, also for find-file command
  (ido-mode 1)
  ;; (ido-everywhere 1)
  (if ; make ido display choices vertically
      (version< emacs-version "25")
        (make-local-variable 'ido-separator)
        (setq ido-separator "\n"))
      (make-local-variable 'ido-decorations)
      (setf (nth 2 ido-decorations) "\n")))
  (setq ido-enable-flex-matching t) ; show any name that has the chars you typed
  (setq ido-default-file-method 'selected-window) ; use current pane for newly opened file
  (setq ido-default-buffer-method 'selected-window) ; use current pane for newly switched buffer
  (define-key (cdr ido-minor-mode-map-entry) [remap write-file] nil) ; stop ido from suggesting when naming new file

;; big minibuffer height, for ido to show choices vertically
(setq max-mini-window-height 0.5)

Stop ido Suggestion When Writing File

When you call write-fileCtrl+x Ctrl+w】 (aka “save as”), when ido is on, it'll make suggestions. This is annoying because usually you don't want the name to be a existing file.

Here's how to stop ido suggestion when Alt+x write-file, put this in emacs init:

(require 'ido)
;; stop ido suggestion when doing a save-as
(define-key (cdr ido-minor-mode-map-entry) [remap write-file] nil)

Switching Next/Previous User Buffers

See also: Emacs: Next/Previous User Buffer

Create New Empty Buffer

Emacs: New Empty Buffer

Open File topics

  1. Emacs: File Manager, dired
  2. Emacs: Bookmark Tutorial
  3. Emacs: Open Recently Opened File
  4. Emacs: Open File Fast
  5. Emacs: Open Last Closed File
  6. Emacs: Open File Path Under Cursor

Buffer topics

  1. Emacs: List Buffers
  2. Emacs: Switch Buffer
  3. Emacs: New Empty Buffer
  4. Emacs: Next/Previous User Buffer
Like it? Buy Xah Emacs Tutorial. Thanks.