Using Command Line Editing

One of the most frustrating parts of learning to use Linux/Unix is when you mistype a command and then the error message pops up.  None of us are perfect typists, and Linux/Unix commands can be long with multiple options.  But there are ways to fix your mistakes without typing the whole command string again.

Linux/Unix has a feature called “command line history”.  Every time you type a command, it is stored in a text file in your home directory.  It doesn’t hold every command you ever typed, but is usually something reasonable for the server. And is definitely enough for the purposes of editing commands.

Linux/Unix  links has the capability to read the contents of the history file and edit it using the vieditor.(Note:  Raspberry Pi and some other distributions default to the nano editor.  That can be changed to the vi editor, but will not be discussed in this tutorial.)

This option is not active by default on most systems.  To turn on command line history editing, type     set -o vi Enter at the command line prompt.  You will return to the prompt once the command has executed.  Even though you didn’t see anything happen, the ability to edit commands (without retyping the entire line) has been turned on. 

vi Modes

There are two modes in vi/command line editing, command mode and editing mode.  Command mode is used for selecting the line you want to edit and moving the cursor within the line.  To activate command mode, hit the ESC key.  Then move your cursor to the place in the line that you need to make a change.  You are always either in command mode or editing mode.  You can only edit in edit mode and you can only move the cursor without changing text in command mode.

Command mode

For example, to edit the previous command, hit ESC and then the kkey to go up a line.  The first line you’ll see is “set -o vi”.  To edit the command before that, hit the kkey again to move to previous commands.  If you find you’ve gone too far back and need to move to a more recent command, hit the jkey to move down one line at a time to new commands.

Once you’ve found the command you need to edit, you need to move the cursor to the spot you need to change. The cursor is always put at the beginning of the line. To move left one letter at a time, hit the hkey.  To move right one letter at a time, hit the lkey.  To move forward a word, hit the wkey.  To move back a word, hit the bkey.

Editing mode

Editing mode allows you to actually change something in the history command you’ve selected.  After you’ve moved your cursor to where you need to make a change, you need to change the mode from command to editing mode.

If you want to insert a letter, type an iand then the letter(s) you need to insert. To delete the character under the cursor, hit x.

Getting out of trouble (AKA “Help!”)

It’s all too easy to get into a vi session when you don’t mean to.  All you have to do is hit the vkey while in command mode.  And if you’re not that experienced, when the full  vi editing window pops up, you might well feel as though you’re in trouble.  (“How do I get out of this?”) 

But getting out of that window is actually not hard.  Just do the following:

  1. Hit the ESCkey to get into command mode. 
  2. Then hit :(colon), q!(exclamation), Enter

You will return to the history command you were working on.

Real World Example

My teammate and I had to check if mysql was running on a server.  I originally typed the following:

                ps zux | grep mysql

That wasn’t what I wanted.  So I hit the kkey to go back to the command, then used the lkey to move the cursor until it was on top of the z.  Then I hit rand then a.  That replaced the zwith an a. Then I hit ESCto end editing and Enterto run the command.

                ps aux | grep mysql

I still wasn’t completely satisfied.  I didn’t want the grep command to show up in the resolution.  So I used the kkey to go back up to the ps aux | grep mysql.  I hit $to move the cursor to the end of the command and pressed ato append to it.  The final command was:

                ps aux | grep mysql | grep -v grep.

Final thoughts

The vi editor is deceptively simple and powerful.  While you can learn simple editing in a few sessions, there are too many commands and options to learn in a single session.

What I hoped to do is to give you the options that are likely to be used for command line editing.  This will help you avoid all the tedious re-typing.  I’ve put a small table of the most used options at the end of this tutorial.

Once you get comfortable with command line editing, don’t be afraid of starting to work in vi.





Useful vi  Commands



vi mode to allow you to move cursor



move down one line



move up one line



move left one letter at a time



move right one letter at a time



move to the end of the line



move to the beginning of the line



move to next word



move to previous word



vi mode to allow text editing



insert before cursor



append after the cursor



delete charactor currently under cursor



replace the character under cursor



delete from cursor to the end of the line



change from cursor to the end of the line