Blog Series Tags

Emacs - Frames, Windows and Buffers

First for some terminology. What Emacs called “frames” is what everyone else now calls windows. This is because Emacs started out as a full screen terminal tool and GUI’s came along later and because of that what Emacs calls windows is now what most visual studio programmers would know as split views. You can experiment with creating new frames, which are by default bound to [C-x 5 2] and deleting the created frame by selecting it and pressing [C-x 5 0]. It’s like opening a new instance of visual studio except that your on the same project as you were when you opened it.

The combination of frames with windows is very powerful. Inside a given frame you can split your view horizontally (creating 2 Emacs “windows”) by pressing [C-x 2]. Now you have a split view of the same buffer that you were editing. You can split vertically by pressing [C-x 3] and you can delete the window you are in by pressing [C-x 0] (alternatively you can maximize the window to fill the whole Emacs frame by pressing [C-x 1].

Ah buffers… Buffers are the actual contents of your windows. They represent text in memory. They can be the contents of a file or some other kind of generated content (like the view of a directory) or it can even by scratch buffers – buffers that are created in memory and not saved to disk unless you specify otherwise. A buffer is a generic term used in Emacs to say something that contains “stuff”. Buffers are displayed inside Emacs windows, which are themselves displayed inside frames. Comprende?

Here’s a screenshot just in case.

(The original image was shamelessly plagiarized from here…)

You can create new buffers by [C-x, b] and typing a name. Buffers have names and you use them to switch buffers inside a given window. If you press [C-x, b] and then type a name of a buffer that already exists it will switch to that buffer instead of creating a new one. Pressing [C-x, k] will kill (delete) the existing buffer.

One of the keys to being productive inside Emacs is knowing how to manage frames, windows and buffers. So it’s useful to tabulate the commands that we have seen so far.

Create frame   C-x 5 2
Delete frame   C-x 5 0
Split Horizontally   C-x 2
Split Vertically   C-x 3
Delete Split   C-x 0
Maximize Split   C-x 1
Create or Switch buffer   C-x b
Delete buffer   C-x k

One exception to the buffer command shortcuts that you may wish to reassign is the switch buffer shortcut. It’s by default set to [C-x o], which can be a little annoying at times, specially because CUA-Mode which we discussed earlier binds C-x to cut when text is selected, which can happen more than you expect when working with multiple buffers. Fortunately rebinding this to an easier and more familiar key (like C-tab) is relatively straightforward. Simply add the following to your .emacs and your good to go.

(global-set-key (kbd "<C-tab>") 'other-window)

Do a [M-x, load-file, enter enter] to load the current .el file that your editing and you should be done.

For the time being switching buffers may seem a little difficult, and you might find yourself wishing for a familiar set of tabs at the top of the Emacs frame (everybody else’s “window”) like you have on Visual Studio but fret not, this is but a temporary pain. There is a mode that allows it but be warned it has some performance issues. There is a better alternative, an alternative which will have to wait till the next episode.

This is a post in the Emacs series.