Using Command Line History
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 vi editor. (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.
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.
For example, to edit the previous command, hit ESC and then the k key to go up a line. The first line you’ll see is “set -o vi”. To edit the command before that, hit the k key 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 j key 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 h key. To move right one letter at a time, hit the l key. To move forward a word, hit the w key. To move back a word, hit the b key.
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 i and 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 v key 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:
- Hit the ESC key to get into command mode.
- 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 k key to go back to the command, then used the l key to move the cursor until it was on top of the z. Then I hit r and then a. That replaced the z with an a. Then I hit ESC to end editing and Enter to 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 k key to go back up to the ps aux | grep mysql. I hit $ to move the cursor to the end of the command and pressed a to append to it. The final command was:
ps aux | grep mysql | grep -v grep.
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.
|Command||ESC||vi mode to allow you to move cursor|
|j||move down one line|
|k||move up one line|
|h||move left one letter at a time|
|l||move right one letter at a time|
|$||move to the end of the line|
|0||move to the beginning of the line|
|w||move to next word|
|b||move to previous word|
|Editing||vi mode to allow text editing|
|i||insert before cursor|
|a||append after the cursor|
|x||delete charactor currently under cursor|
|r||replace the character under cursor|
|D||delete from cursor to the end of the line|
|C||change from cursor to the end of the line|