Backbeat Software
Photo by dhe haivan on Unsplash

The editor hotkeys hiding in plain sight

The vim and emacs hotkeys embedded in your everyday applications.

Glynn Forrest
Saturday, November 30, 2019

If you’ve ever used the vim or emacs family of editors, you’ll know they each contain hundreds or even thousands of commands bound to different keys. What many people don’t know is that these keybindings are also used in dozens of everyday applications - command line tools, text inputs, and even web applications. Let’s explore where vim or emacs knowledge can be used in your everyday tools.

Vim vs emacs

The editor war is a long-running, light-hearted battle between the vi-style of editing and the emacs style of editing.

The vi-style makes use of ‘modes’, allowing for concise, composable commands. For example, to swap the current line and the line below, then save the current file, you would:

  • Make sure you’re in ‘normal mode’ by pressing <escape>;
  • Type dd to delete the current line;
  • Type p to paste what you just deleted after the cursor;
  • Type :w and hit the <return> key to save the file.

The emacs style is ‘modeless’ (like most editing styles), using modifier keys such as Control and Meta (Alt) combined with a character for different commands. To accomplish the same example in emacs, you would:

  • Move down a line with C-n;
  • Type C-x then C-t to execute the command transpose-lines;
  • Type C-x then C-s to save the file.

Shells and readline

Out of the box both bash and zsh support emacs-style cursor navigation:

  • C-a and C-e - move to the start and end of the line;
  • C-f and C-b - move forward and backward a character;
  • C-n and C-p - move forwards and backwards through your command history;
  • C-h - delete the previous character.

This is also supported by tools that use readline, such as the python, ruby, or node interactive REPLs.

If you prefer vim style, you can also change the input mode in .inputrc:

set editing-mode vi     # use vim style readline editing
set show-mode-in-prompt # show an indicator when in insert / normal mode

With vi-mode enabled, you can press escape at any point to change to normal mode, where most of the composable vim commands are available.

Both bash and zsh also have the emacs hotkey C-x C-e to open the current command in $EDITOR.

Less, man, and other pagers

Pagers such as less, more, man, git log, etc, all support vim keys.

I like to scroll through man pages using C-d and C-u and search for a phrase with / and ?.

Commandline applications

There are many commandline applications specifically designed with vim keys in mind:

  • tig, a git client;
  • ranger, a file manager;
  • tmux, a terminal multiplexer;
  • ncmpcpp, a client to the music player daemon.

See also the Big Pile of Vim-like software.

OS-level hotkeys

If you’re on Mac OS, most text input boxes support emacs-style navigation. C-a, C-e, C-f, C-b, and C-h all work, as well as C-F and C-B to highlight text.

In a browser address bar you can additionally use C-n and C-p to navigate between different options in the search bar.

Web sites and applications

Even some web applications have familiar hotkeys:

  • The Gmail interface has a large amount of hotkeys, including j and k to move between emails, x to select an email, e to archive, and # to delete. Make sure keyboard shortcuts are enabled in your gmail settings, then press ? to see all the available options.
  • DuckDuckGo also uses j and k to navigate between search results, o to open a link, and / to focus the search box.
  • Some of the Github hotkeys have parallels with vim, including / to search, the g prefix to go to various places, and j and k to move things up and down on certain pages.

Keyboard powered web browsing

To go even further, web browsers can be powered entirely by vim keys! Firefox and Chrome have the Tridactyl and Vimium extensions, and luakit is a standalone browser that uses vim keys for navigation.

It’s possible to fully embrace the vim way of navigating the web with these extensions, almost completely removing the need for a mouse.

Conclusion

It’s tempting to pick one side in the editor war and ignore the other, but I feel that having the basics mastered in both editors helps you tremendously when using different unix tools and applications.

Once you become proficient with both styles, you’ll start to notice vim and emacs shortcuts in many applications you use every day. You’ll start to feel the pain when your application doesn’t have these hotkeys built in, and want to seek out alternatives that do!

To get started, simply type vimtutor or emacs -Q -f help-with-tutorial in your unix shell.

More from the blog

Sending emails with Symfony: Swift Mailer or the Mailer component? cover image

Sending emails with Symfony: Swift Mailer or the Mailer component?

The evolution of Symfony’s email tooling, and what to use for a brand new application.


Glynn Forrest
Thursday, October 31, 2019

Why Symfony's container is fast cover image

Why Symfony's container is fast

Why Symfony’s dependency injection container is both full of features and lightning fast.


Glynn Forrest
Monday, September 30, 2019

Rotating pet servers with SaltStack cover image

Rotating pet servers with SaltStack

How to rotate a machine with minimal downtime.


Glynn Forrest
Saturday, August 31, 2019