JanosH
JanosH

Reputation: 67

emacs switch buffer per window

All,

I'm looking for a way to switch buffers in multiple windows similar to other IDEs. Let's say I split my frame into two windows (C-x 3). I'd like to handle buffers for .c filesalways in the left window and .h files in the right. I'm looking for a way to couple a list of buffers to a specific window, and cycle buffers only belonging to the current window. So if I'm editing a .h file on the right side, and press a key combination I get the next .h file.

Thanks for any advice, Janos

Upvotes: 5

Views: 335

Answers (2)

juanleon
juanleon

Reputation: 9410

I use buffer-stack (available via MELPA) for something similar. buffer-stack allows you to cycle between buffers in a very flexible way (it remembers your most recent used buffers, and it uses separates stacks per frame).

You can add filter functions to buffer switching commands. So you could add a filter function for considering only buffers with filenames with same extension than the current one. I use a filtering function to switch among buffers of the same mode. Here is it as an example that shows my keybindings to switch filtering by current mode and also swith to dired buffers. It would be easy to add another filter based on file extension:

(defmacro command (&rest body)
  `(lambda ()
     (interactive)
     ,@body))

(defvar buffer-stack-mode)

(defun buffer-op-by-mode (op &optional mode)
  (let ((buffer-stack-filter 'buffer-stack-filter-by-mode)
        (buffer-stack-mode (or mode major-mode)))
    (funcall op)))

(defun buffer-stack-filter-by-mode (buffer)
  (with-current-buffer buffer
    (equal major-mode buffer-stack-mode)))

(global-set-key [(meta kp-7)]
                (command (buffer-op-by-mode 'buffer-stack-up)))
(global-set-key [(meta kp-9)]
                (command (buffer-op-by-mode 'buffer-stack-down)))
(global-set-key [(meta kp-3)]
                (command (buffer-op-by-mode 'buffer-stack-down 'dired-mode)))
(global-set-key [(meta kp-1)]
                (command (buffer-op-by-mode 'buffer-stack-up 'dired-mode)))

Upvotes: 1

abo-abo
abo-abo

Reputation: 20362

You can use ff-find-other-file. With a prefix argument C-u, it will open another window.

As for your idea of getting the next .h file, you're just limiting yourself by trying to bring a bad idea from IDE into Emacs. Use projectile or ido-switch-buffer or helm-buffers-list: these tools can manage hundreds of files at a time, while the approach of "getting the next file" (i.e. tabs) fails around 20.

Upvotes: 1

Related Questions