Kaviraj       Archive       Tags

Emacs: Split window with different buffer

It’s common to split windows while working in emacs. By default, whenever we split windows in emacs the newly created window will also have the same buffer(same buffer in two windows). So it is always needed to change to another buffer by C-x b after splitting it.

We can customize it by little elisp code as below.

put the below code in either ~/.emacs file or init.el file in ~/.emacs.d directory.

(eval-when-compile (require 'cl))

(defun split-window-func-with-other-buffer (split-function)
  (lexical-let ((s-f split-function))
    (lambda ()
      (funcall s-f)
      (set-window-buffer (next-window) (other-buffer)))))

All this code does is pretty straight forward. lexical-let needs common lisp libraries. Thats what (eval-when-compile (require 'cl)) does.

'split-function' is an argument which will be default elisp split function. In (funcall s-f) we call the default split function then using(set-window-buffer)elisp function we change the buffer of newly created window (next-window) with different buffer (other-buffer).

Also rebind the default split keys C-x2(horizontal) and C-x3(vertical) to our new custom split function.

(global-set-key "\C-x2" (split-window-func-with-other-buffer 'split-window-vertically))
(global-set-key "\C-x3" (split-window-func-with-other-buffer 'split-window-horizontally))

here 'split-window-vertically' and 'split-window-horizontally' are default elisp split functions which we pass as argument to our new custom split function 'split-window-func-with-other-buffer'.

I got this code from here (I am not elisp master. Well! atleast not yet 😜). Thanks to Purcell. In fact his .emacs.d is well organised and very good resource to learn how to customize emacs and he is also helpful via mail 😊.

I also found this tutorial is very helpful in getting started on Emacs lisp.

Happy hacking emacs!! πŸ‘βœŒ.

If you liked this post, you can share it with your followers or follow me on Twitter!

comments powered by Disqus