Join David D. Levine for an in-depth discussion in this video Understanding vi's modes: Command, Insert, Colon, part of Up and Running with vi.
- View Offline
VI is unlike most modern editors. In that, it has modes. A mode determines what happens when you press a key on the keyboard. VI begins in Command mode. In Command mode, the keys that you press on the keyboard are interpreted as commands that tells VI to do things. For example, H and L move backwards and forwards through the current line, and O opens a new line for typing. When you press certain commands, including i, a, o and c, or their uppercase versions, it puts you into Insert mode.
When you're in Insert mode, the keys that you press on the keyboard result in inserting characters into the file. You can keep typing as much as you like in Insert mode, but when you're done inserting and you want to go back to editing the file, you press the Esc key. That returns you to Command mode. Let me give you an example of that. We'll start by editing our Duke of York example again. So we've just entered VI and now we're in Command mode, which is where you start. In this mode, the letters that you type at the keyboard are interpreted as commands. So, for example, j moves the cursor down one character, j, j.
If I type an l, it moves the cursor to the right, l, l, l, l. Or an h to move it to the left, h, h, h, h, l, l, l. And now I will type i to enter Insert mode. Note that when I type the i, the word Insert appeared at the bottom of the screen, telling me that I'm in Insert mode. Now that I am in Insert mode, if I type an h, it actually is inserted into the file, l, k and so on. So I just hit backspace to delete those characters I just typed. To leave Insert mode, I hit the Esc key.
Now you see that the word Insert has vanished from the bottom of the screen. In addition to Command and Insert mode, VI has another mode, a kind of a pseudo mode, which I call Prompt mode. When you type certain commands including colon, slash, exclamation point or question mark, it puts you into what I call, Prompt mode. But which the developers of VI called EX mode because it returns you to the same line editing prompt that the EX editor had before the visual mode VI was developed. When you're in Prompt mode, a prompt, usually a colon or a slash, appears at the bottom of the screen.
At this point, you can type additional, more lengthy commands or a search string, and then hit Return to execute the command and return to Command mode. So let me give you an example of that. So, here we are, in Command mode, and you can see as I type h, and l, that the cursor moves around, it doesn't insert. And notice that there also is not the word Insert at the bottom of the screen. If I type a colon, that puts me into prompt mode, and there you see that colon has appeared at the bottom of the screen and also the cursor has gone to that last line. So here I can type w, q to write in quit.
If I don't want to do that, I could press Esc twice in order to terminate the command. Or I can hit Backspace to terminate the command. Or, if I actually do want to perform the command Write and Quit, I just hit Enter, which writes out the file, and then quits VI
- Entering and leaving vi
- Understanding the Command, Insert, and Colon modes
- Moving around in files
- Editing text
- Moving content with delete, yank, and put
- Searching with regular expressions
- Customizing vi
- Filtering text through shell commands