Search code examples
emacswindowbuffer

switch to buffer with C-x C-b without changing window


I usually have 2-3 windows open in emacs, and am particular about which buffer is shown where. While C-x C-f shows the buffer (file visited) in the current window, C-x C-b (and recentf-open-files from the recentf package) sometimes end up displaying it in another window, which then makes complicated rearrangements necessary.

Is there a way to force these commands to end up displaying the buffer in the window that was active when the C-x C-f or C-x C-b where issued?


Solution

  • The function at issue list-buffers is a one-liner -- so just change display-buffer to switch-to-buffer and redefine the keyboard shortcut to point to the new function:

    (defun my-list-buffers (&optional arg)
    "Display a list of existing buffers.
    The list is displayed in a buffer named \"*Buffer List*\".
    See `buffer-menu' for a description of the Buffer Menu.
    By default, all buffers are listed except those whose names start
    with a space (which are for internal use).  With prefix argument
    ARG, show only buffers that are visiting files."
      (interactive "P")
      (switch-to-buffer (list-buffers-noselect arg)))
    
    (define-key ctl-x-map "\C-b" 'my-list-buffers)