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.
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.
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!! πβ.