Before you start make sure you are in vi mode.
When you enter bash commands while in vi editing mode, you are in Input mode (page 174). As you enter a command, if you discover an error before you press RETURN, you can press ESCAPE to switch to vi Command mode. This setup is different from the stand-alone vi(m) editor's initial mode. While in Command mode you can use many vi(m) commands to edit the command line. It is as though you were using vi(m) to edit a copy of the history file with a screen that has room for only one command. When you use the k command or the UP ARROW to move up a line, you access the previous command. If you then use the j command or the DOWN ARROW to move down a line, you will return to the original command. To use the k and j keys to move between commands you must be in Command mode; you can use the ARROW keys in both Command and Input modes.
The stand-alone editor starts in Command mode tip The stand-alone vim editor starts in Command mode, whereas the command line vi(m) editor starts in Input mode. If commands display characters and do not work properly, you are in Input mode. Press escape and enter the command again.
In addition to cursor-positioning commands, you can use the search-backward (?) command followed by a search string to look back through your history list for the most recent command containing that string. If you have moved back in your history list, use a forward slash (/) to search forward toward your most recent command. Unlike the search strings in the stand-alone vi(m) editor, these search strings cannot contain regular expressions. You can, however, start the search string with a caret (A) to force the shell to locate commands that start with the search string. As in vi(m), pressing n after a successful search looks for the next occurrence of the same string.
You can also access events in the history list by using event numbers. While you are in Command mode (press ESCAPE), enter the event number followed by a G to go to the command with that event number.
When you use /, ?, or G to move to a command line, you are in Command mode, not Input mode. Now you can edit the command as you like or press RETURN to execute it.
Once the command you want to edit is displayed, you can modify the command line using vi(m) Command mode editing commands such as x (delete character), r (replace character), ~ (change case), and . (repeat last change). To change to Input mode, use an Insert (i, I), Append (a, A), Replace (R), or Change (c, C) command. You do not have to return to Command mode to run a command; simply press RETURN, even if the cursor is in the middle of the command line.
Was this article helpful?