vi Commands

vi has two modes: command mode and insert mode. As soon as you enter a file, you are in command mode, and the editor is waiting for you to enter a command. Commands enable you to move anywhere in the file, to perform edits, or to enter insert mode to add new text. Commands can also be given to exit the file (saving or ignoring your edits) in order to return to the Unix prompt.

You can think of the different modes as representing two different keyboards. In insert mode, your keyboard functions like a typewriter. In command mode, each key has a new meaning or initiates some instruction.

There are several ways to tell vi that you want to begin insert mode. One of the most common is to press i. The i doesn’t appear on the screen, but after you press it, whatever you type will appear on the screen and will be entered into the buffer. The cursor marks the current insertion point.[10] To tell vi that you want to stop inserting text, pressESC. Pressing ESC moves the cursor back one space (so that it is on the last character you typed) and returns vi to command mode.

For example, suppose you have opened a new file and want to insert the word “introduction.” If you type the keystrokes iintroduction, what appears on the screen is:

introduction

When you open a new file, vi starts in command mode and interprets the first keystroke (i) as the insert command. All keystrokes made after the ...

Get Learning the vi and Vim Editors, 7th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.