Skip to content

Insight and analysis of technology and business strategy

Emacs Keybindings in Bash

Or, How to Be a Command-Line Commando

Does it surprise you to learn that I’m a Linux guy? I’ve been using Linux, to the exclusion more-or-less of everything else, since about 1999. In the past, I’ve done a little programming and some junior system administration. I’m even LPI-certified.

With this background, I’m quite comfortable working in the shell (AKA the command-line), the natural habitat of the sysadmin[1]. I frequently open a shell to do some quick work, and when I do, I use GNU’s Bash, which is the default on most Linux distributions. (I believe it’s also the default shell in Mac OS X.)

One of Bash’s features is editable command-line history, which makes your current command-line and its entire history available to you as an editable buffer. That offers a great way to streamline your work in the shell.

I suspect, however, that many shell users don’t even know about this better way. And it baffles me that many SAs I have seen in action — including some of Pythian’s own — don’t use this. They almost seem to prefer unnecessary effort — smashing away at their keyboards, repeating themselves, deleting with the Backspace key, scrolling, forwarding their cursor one character at a time, copying and pasting with the mouse, and so on. That’s a lot of elbow grease.

With Bash, or any other shell that uses the GNU readline library, you can use the following Emacs-like key-chords to make your life better. The point of this (as with so many things sysadmins and programmers do) is to save you effort, viz. typing. These aren’t all of them; they’re the ones I use:

Keys Effect
CTRL-P go to the Previous command in your history
CTRL-N go to the Next command in your history
CTRL-R Reverse-search through your history
CTRL-S Search forward through your history
CTRL-A Move the cursor to the beginning of the line
CTRL-E Move the cursor to the end of the line
CTRL-W delete a Word backwards
ALT-D delete a word forwards
CTRL-F move the cursor Forward 1 character
CTRL-B move the cursor Backward 1 character
ALT-F move the cursor Forward 1 word
ALT-B move the cursor Backward 1 word
ALT-_ undo

It takes a little learning to get these under your fingers, but it’s worth it. Too often, easy-to-use GUIs get us started quickly, and then leave us handicapped in our work. Spending the time to learn how to use capable tools pays off.

If you don’t want the default Emacsisms, there’s a vi-mode too — try set -o vi. I am a vi guy (in addition to a Linux guy), so I don’t mind modal editing, except that in the shell, there’s no way to tell which mode I’m in at any moment. Some commando out there might have put together a fancy custom PS1 prompt that shows vi-modes, but I really haven’t gone searching.

If you’d like a more thorough read about this, try the Bash Emacs Editing Mode (readline) Cheat Sheet or “man readline”.

1. As it happens, knowing how to use Unix text tools such as grep, sed, and wc (to name just three) is very helpful to a writer, too.

Top Categories

  • There are no suggestions because the search field is empty.

Tell us how we can help!

dba-cloud-services
Upcoming-Events-banner