Red Green Repeat Adventures of a Spec Driven Junkie

Emacs Window Management

Emacs is one piece of software I have consistently used throughout my programming career.

Honestly, I thought there would be a better programming editor by now but I haven’t found it.

I started with vim (which is great!), moved onto emacs after learning gdb is very well integrated with emacs, worked with webstorm on a freelance project, got annoyed with sublime text, and attended notepadconf.

Emacs is really an excellent programming tool for programmers and I highly recommend it for anyone that wants to be a better programmer (it’s definitely not for the faint of heart!)

Window management by Keyboard

One big reason I love emacs is there is a simple configuration which allows full control of windows in emacs by keyboard.

This allows one to program with their hands on the keyboard the majority of the time. No need to find the mouse or go to the trackpad. Just keep typing.

Sam Kleinman introduced me to this window setup initially and I’ve kept it ever since. Thank you!

With this configuration, I can reuse my vim muscle memory, which I highly recommend, but feel free to use different keys.

I will describe the commands needed for the associated key bindings and what to do to setup your own.

Items covered in this article

  • Assigning Keys
  • Making windows
  • Navigating windows
  • Resizing windows
  • Removing windows

init.el

Emacs key configuration is done through a configuration file init.el, which can be found in the .emacs.d in your home directory

If there is a .emacs file instead, the following information will still apply, but I highly recommend making the .emacs.d and moving the .emacs to be init.el in .emacs.d directory.

Command notation

This is the notation for commands in emacs:

C-x b Hold down control and press x, let go of control and then press b
C-x C-c Hold down control and press x, contribute to hold control and then press c
M-x d Hold down alt key and press x, let go of meta and then press d

As emacs is a chord based editor, compared to vim, which is a modal editor, the control key is used extensively. I highly recommending remapping caps lock key to be an extra control key.

Assigning Key bindings

The format to associate any key binding with a command in the init.el file is:

(global-set-key (key) 'command)

For example, to bind the key “Alt-h” to move to the left window, the command is:

(global-set-key (kbd "M-h") 'windmove-left)
(kbd "M-h") is the lisp command to get the keyboard code for “Alt-h”
windomove-left is the lisp command to move to the window to the left

With this, any command which can be executed at the emacs prompt (M-x windmove-up) can be easily mapped using this convention.

Mapping own keys

If you want to change the command to be Alt-left arrow key instead, the command would be:

(global-set-key (kbd "M-<left>") 'windmove-left)

Extensive details on key codes in emacs can be found here

Creating Windows

I still use the default window creation methods in emacs:

Key binding Meaning Emacs command
C-x 2 split the current window in half horizontally split-window-horizontally
C-x 3 split the current window in half vertically split-window-vertically

Navigate between Windows

The default set of commands to navigate between windows are

Key binding Meaning Emacs command
C-x o switch to other window other-window

This is a great command to remember. I use it whenever I am on a new install of emacs and need to move around.

Custom Navigation Keys

When windows become too numerous, a more efficient way to navigate windows is needed, so I set up these bindings in my init.el:

Key binding Meaning Emacs command
M-h switch to window left windmove-left
M-j switch to window below windmove-down
M-k switch to window above windmove-up
M-l switch to window right windmove-right

In the init.el file, the commands would be:

(global-set-key (kbd "M-h") 'windmove-left)
(global-set-key (kbd "M-j") 'windmove-down)
(global-set-key (kbd "M-k") 'windmove-up)
(global-set-key (kbd "M-l") 'windmove-right)

Here I take advantage of the vim key bindings. As most emacs commands use the control key and also a nmeonic. The alt key is relatively free, and the h-j-k-l keys are easy to remember and use.

Resizing Windows

When I want windows not to be exactly in halves, I map additional commands to interactively resize them by enlarging or shrinking the current window.

Key binding Meaning
M-H shrink window horizontally
M-J enlarge window vertically
M-K shrink window vertically
M-L enlarge window horizontally

The emacs command is a bit more involved. There’s a lambda required as other params. For simplicity, I use the format:

(lambda () (interactive) (command))

Which allows interactive usage of the command.

(global-set-key (kbd "M-H") (lambda () (interactive) (enlarge-window -1 t)))
(global-set-key (kbd "M-J") (lambda () (interactive) (enlarge-window  1)))
(global-set-key (kbd "M-K") (lambda () (interactive) (enlarge-window -1)))
(global-set-key (kbd "M-L") (lambda () (interactive) (enlarge-window  1 t)))

Removing Windows

When there are too many windows, these are commands to get rid of them.

Key binding Meaning Emacs command
C-x 0 destroy the current window delete-window
C-x 1 make current window the only window delete-other-windows

Like the window creation commands, I use these commands as is.

Benefits

The greatest benefit of emacs window management is that I can configure emacs’ windows easily for the task at hand.

Do I want to have two windows, one showing code and another showing specs? No problem.

Do I want to have four windows, one for code, another for specs, one for a terminal to run commands, and another for web page look up? Giddy-up!

All of these window configurations are easily done from the keyboard.

At the same time, anywhere emacs is running, locally as a native application, locally through the terminal, remote terminal, they are all the same.

The setup and configuration translates very well anywhere emacs is. :-)

Overall

A setup for managing windows in emacs has been presented. This is my default configuration in my emacs init.el file and use them every day I am on emacs.

Emacs really got me with its great gdb integration when I was coding C, but now it has a greater hold on me when I can completely configure the windows from my keyboard.

Go forth and own your editor!