Emacs: Switch Buffer
This page is a tutorial on switching buffers in emacs. If you don't know what buffers are, first see: Emacs: List Buffers.
switch-to-buffer 【Ctrl+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 will show a list of candidates AUTOMATICALLY as you type (no need to press Tab ⇥ first).
- First, call
ido-modeto turn it on.
- Then, call
ido-switch-buffer【Ctrl+x b】 to switch buffer.
When ido-mode is on, the key 【Ctrl+x b】 will call ido's version (
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:
- 【→】 → Select the next choice.
- 【←】 → Select the previous choice.
- 【↑】 → Go to the buffer you switched to last time. 【↓】 for next.
- 【Enter ↵】 → Use the first choice.
- 【Ctrl+f】 → stop suggestion. useful for creating new file. 【Ctrl+b】 to go back to buffer selection mode.
- 【Ctrl+d】 → switch to open directory mode.
- 【Ctrl+g】 → cancel.
How to create a new file with ido-mode?
ido-switch-buffer, then press 【Ctrl+f】 to stop suggestion. This is useful when creating new file.
When you want to open a directory, you can 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.
(progn (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") (progn (make-local-variable 'ido-separator) (setq ido-separator "\n")) (progn (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-file 【Ctrl+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 calling
(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
or, buy something from Best Keyboard for Emacs