I'm once again an Emacs convert. I used Emacs a fair bit back in 2011 as my main editor (when vimpulse was a viable alternative to native Vim and I forced Emacs upon myself for as long as I could), I even wrote a few posts about extending it. But eventually I gave it up as it was starting to feel sluggish with everything I was throwing at it (looking back, it was more than likely just a bad minor-mode or two).

However in the time I've been away from Emacs I feel as though the eco-system has really modernized. We now have emacs-starter-kit and prelude to get you up and running quickly with sane defaults, we have package.el since Emacs 24, along with MELPA as a maintained repository of elisp packages, and even a set of decent color themes! And of course I can't leave out evil (the extensible vi layer) which I could not do without.

I could go on for hours about the mountain of features I've missed from Emacs while in Vim-land (although there are ports of the big ones), but I want to share the killer feature for me over the past few days: narrowing, and indirect buffers.


Emacs has a concept of narrowing buffers, which restricts your view of a buffer to an area that you specify, which is great for focusing on a particular method or for running commands that operate on the entire buffer. You can also change your major mode while in a narrow buffer, which is great for editing code in the middle of an email (for example).

Recently I've been working with a lot of client-side JavaScript and will often prototype in a single HTML file, beginning with CSS at the top, followed by markup, and ending with the JavaScript. This works really well to begin with but doesn't scale well, leading to a lot of jumping back and forth throughout the file. However using the concept of narrowing it is possible to treat the single HTML file as though it were actually three!

I begin by opening the HTML file, and then split Emacs into three panes. Then in each pane I select the appropriate code and run the narrow-to-region command, but oh no! This has actually narrowed all three of the editor panes at once, when I only wanted to narrow one.

Indirect Buffers

This is where indirect buffers come in. From the docs:

"An indirect buffer shares the text of some other buffer, which is called the base buffer of the indirect buffer. In some ways it is a buffer analogue of a symbolic link between files."

Put simply, by turning each pane into an indirect buffer, we will be able to narrow them separately! All we need to do is run clone-indirect-buffer on each pane to create an indirect buffer from this file. From here we can use the narrow-to-region command again, only this time each pane will remain separated as intended. Once the panes are narrowed they can each have their own major-mode set as well, in order to take advantage of things like less-css-mode and js2-mode without any distraction. When done you can simply kill the buffer like you would with a regular file.

A Handy Shortcut

Of course this wouldn't be much of an Emacs post without some lisp, so here is a shortcut command that combines cloning and narrowing:

(defun narrow-to-region-indirect (start end)
  "Restrict editing in this buffer to the current region, indirectly."
  (interactive "r")
  (let ((buf (clone-indirect-buffer nil nil)))
    (with-current-buffer buf
      (narrow-to-region start end))
      (switch-to-buffer buf)))

Got any questions? Leave a comment below!

(Maybe) Related posts: