Tiny Code

Tiny Code

Sunday, March 29, 2015

Command line editing

A great many tools (including the Unix/Linux login) use a standard "readline" library which by default uses many key bindings from the Emacs editor.  It's also possible to configure readline to use Vi editor bindings.  While I'm not an Emacs user, I find it much easier to use the default bindings and to memorize a small subset of those shortcuts.  That way you don't have to worry about whether the program taking your input recognizes that you prefer Vi commands.  The login process won't know that since you are stuck using system default settings prior to logging in.

The following list shows the readline shortcuts I find most useful.  There are more of these available but many deal with situations I don't find useful very often so they're not worth memorizing for me.  If you're interested, you can easily find the rest by typing "Unix readline shortcuts" into Google.  In the following list, the "Ctrl-a" means to hold the control key down while also typing the "a" key.
  • Ctrl-a – go to the start of the command line
  • Ctrl-e – go to the end of the command line
  • Ctrl-k – delete from cursor to the end of the command line
  • Ctrl-u – delete from cursor to the start of the command line
  • Ctrl-l – clear the screen
  • Alt-b – move backward on the command line by one word
  • Alt-f – move forward on the command line by one word
I can't tell you how often I use the Ctrl-u shortcut when typing a long password during a Unix/Linux login.  There's something about not being able to see how far along you've gotten in the password entry which makes it easy to forget what's next.

Now we can combine this readline editing with the Bash command history.  You can use the up arrow to recall previously issued commands and then use these handy readline shortcuts to edit one of the commands and issue the modified command.  Sometimes that can make for a significant time savings.

No comments: