Created by Bill Joy in 1976, vi is a text editor for the Unix/Linux command line. At first glance, it may seem crude by today’s standards for text editors, but it is useful for working in remote command-line sessions.
To open vi at the command line, simple type vi. If you want to open a specific file with vi, type vi and then the filename.
One thing to keep in mind about vi is that it operates in three modes. You must be aware of what mode you are in at any given time, because each reacts differently to what you type in. The three modes:
Command mode is the default mode. When you first start vi, you are in command mode. You can not enter text. Here you are entering commands. Most keystrokes have a command associated with them.
Input mode is where you actually enter text. the easiest way to get into input mode from command mode is to type the letter “i.” Then you can start typing. (“a” will also work). To get out of insert mode back into command mode, hit the escape key.
Ex mode is used for file handling duties, as well as performing substitution tasks. It is kicked off by typing a “:” from the command mode.
For instance , if you want to save a file, you’d hit escape type in “:w [filename]” If you want to quit, type in “:q” If you haven’t saved your file since making any changes however, it won’t let you quit, unless you put an “!” at the end of the command, “:q!”
You can also combine the commands for writing and quitting, i.e. “:wq”
While you will have to figure out which commands are worth memorizing for yourself, here are a few that I myself have found handy:
(all of these are executed from the command line, unless otherwise noted):
) and ( : Jump ahead one sentence or jump back one sentence, respectively.
:[Number]: This will allow you to jump ahead by the number of lines you designate. For instance. “:4″ will jump the cursor ahead 4 lines. Using a negative number will jump back by the number you designate.
ctrl-f, ctrl-b, and ctrl-u, ctrl-d: Jump a screen (24 lines) forward, back, up or down, respectively.
o and O: will move from command mode to insert mode, but insert a new blank line. This is also handy for adding a new line at the end of the document.
dd: delete a line. (Note: This is also the first step of a cut and paste operation. See below).
dw: delete a word.
p and P: This means to paste, as in cut and paste. When you delete something with dd or dw, it goes into the buffer. This command retrieves what is in the buffer.
yy: The command allows for copying and pasting, without the cutting of copy. Typing yy copies the line that the cursor is on.
u undoes the last command (though there seems to be no undo for the edit mode).
/ and ? are search operators. Type them in and then the text you are searching for. / looks for the next instance after the cursor, ? looks for the first instance before the cursor.
Taken from this book:
…as well as a class I’m taking on Unix. All mistakes are my own, however.–Joab Jackson