This article is about vi editor in Linux, it will cover below mentioned things :
- Describe vi Editor
- List different modes of vi Editor
- Manipulate (insert, delete and change) text within vi
- Navigate within the vi Editor
- Search text from the document
- Save files and quit from vi
- Summarize vi commands
- Print documents
Index
Overview of Vi Editor
The VI editor is a screen-based editor used by many Unix users.
Before You Begin
The VI editor uses the full screen, so it needs to know what kind of terminal you have. When you log in, wiliki should ask you what terminal you have. The prompt looks like this:
TERM = (vt100)
If you know your terminal is a vt100 (or an emulator that can do vt100), just hit return for the terminal type when you log in. If you have an hp terminal, type “hp” for the terminal type and hit return. If you are not sure what kind of terminal you have, ask a lab monitor, or have someone help you set the correct terminal type.
Starting the VI Editor
The VI editor lets a user create new files or edit existing files. The command to start the VI editor is vi, followed by the filename. For example to edit a file called temporary, you would type vi temporary and then return. You can start VI without a filename, but when you want to save your work, you will have to tell VI which filename to save it into later.
When you start VI for the first time, you will see a screen filled with tildes (A tilde looks like this: ~) on the left side of the screen. Any blank lines beyond the end of the file are shown this way. At the bottom of your screen, the filename should be shown, if you specified an existing file, and the size of the file will be shown as well, like this:
“filename” 21 lines, 385 characters
If the file you specified does not exist, then it will tell you that it is a new file, like this:
“newfile” [New file]
Three Modes of vi
The vi editor has three modes – command mode, insert mode and command line mode.
- Command mode: Letters or sequence of letters interactively command vi. Commands are case sensitive. The ESC key can end a command.
- Insert mode: Text is inserted. The ESC key ends insert mode and returns you to command mode. One can enter insert mode with the “i” (insert), “a” (insert after), “A” (insert at end of line), “o” (open new line after current line) or “O” (Open line above current line) commands.
- Command line mode: One enters this mode by typing “:” which puts the command line entry at the foot of the screen.
Inserting Text
The vi program is now in command mode. Insert text into the file by pressing i, which places the editor into insert mode, and begin typing.
Now is the time for all good men to come to the aid of the party.
~
~
Type as many lines as you want (pressing Enter after each). You may correct mistakes with the Backspace key. To end insert mode and return to command mode, press Esc.
In command mode, you can use the arrow keys to move around in the file. (If you have only one line of text, trying to use the up- or down-arrow keys will probably cause vi to beep at you.)
There are several ways to insert text other than the i command. The a command inserts text beginning after the current cursor position, instead of at the current cursor position. For example, use the left arrow key to move the cursor between the words “good” and “men.”
Now is the time for all good_men to come to the aid of the party.
~
~
Press a to start insert mode, type “wo”, and then press Esc to return to command mode.
Now is the time for all good women to come to the aid of the party.
~
~
To begin inserting text at the next line, use the o command. Press o and enter another
line or two:
Now is the time for all good humans to come to the aid of the party.
Afterwards, we’ll go out for pizza and beer.
~
~
Deleting Text
From command mode, the x command deletes the character under the cursor. If you press x five times, you’ll end up with:
Now is the time for all good humans to come to the aid of the party.
Afterwards, we’ll go out for pizza and_
~
~
Now press a and insert some text, followed by esc:
Now is the time for all good humans to come to the aid of the party.
Afterwards, we’ll go out for pizza and Diet Coke.
~
~
You can delete entire lines using the command dd (that is, press d twice in a row). If the cursor is on the second line and you type dd, you’ll see:
Now is the time for all good humans to come to the aid of the party.
~
~
To delete the word that the cursor is on, use the dw command. Place the cursor on the word “good”, and type dw.
Now is the time for all humans to come to the aid of the party.
~
~
Changing Text
You can replace sections of text using the R command. Place the cursor on the first letter in “party”, press R, and type the word “hungry”.
Now is the time for all good humans to come to the aid of the hungry.
~
~
Using R to edit text is like the i and a commands, but R overwrites, rather than inserts, text.
The r command replaces the single character under the cursor. For example, move the cursor to the beginning of the word “Now”, and press r followed by C, you’ll see:
Cow is the time for all good humans to come to the aid of the hungry.
~
~
The “~” command changes the case of the letter under the cursor from upper- to lower-case, and back. For example, if you place the cursor on the “o” in “Cow” above and repeatedly press ~, you’ll end up with:
COW IS THE TIME FOR ALL WOMEN TO COME TO THE AID OF THE HUNGRY.
~
~
Cursor Movement
You already know how to use the arrow keys to move around the document. In addition, you can use the h, j, k, and l commands to move the cursor left, down, up, and right, respectively. This comes in handy when (for some reason) your arrow keys aren’t working correctly.
The w command moves the cursor to the beginning of the next word; the b command
moves it to the beginning of the previous word.
The 0 command (that’s the zero key) moves the cursor to the beginning of the current
line, and the $ command moves it to the end of the line.
When editing large files, you’ll want to move forwards or backwards through the file a
screenful at a time. Pressing Ctrl-F moves the cursor one screenful forward, and Ctrl-B moves it a screenful back.
To move the cursor to the end of the file, press G. You can also move to an arbitrary
line; for example, typing the command 10G would move the cursor to line 10 in the file. To move to the beginning of the file, use 1G.
You can couple moving commands with other commands, such as those for deleting text. For example, the d$ command deletes everything from the cursor to the end of the line; dG deletes everything from the cursor to the end of the file, and so on.
Searching for Text
The Vi editor has two kinds of searches: string and character. For a string search, the / and ? commands are used. When you start these commands, the command just typed will be shown on the bottom line, where you type the particular string to look for. These two commands differ only in the direction where the search takes place.
The / command searches forwards (downwards) in the file, while the ? command searches backwards (upwards) in the file. The n and N commands repeat the previous search command in the same or opposite direction, respectively. Some characters have special meanings to Vi, so they must be preceded by a backslash (\) to be included as part of the search expression.
Special characters:
^ – Beginning of the line. (At the beginning of a search expression.)
. – Matches a single character.
* – Matches zero or more of the previous character.
$ – End of the line (At the end of the search expression.)
[ – Starts a set of matching, or non-matching expressions…
For example: /f[iae]t matches either of these: fit fat fet In this form, it matches anything except these: /a[^bcd] will not match any of these, but anything with an a and another letter: ab ac ad
< – Put in an expression escaped with the backslash to find the ending or beginning of a word. For example: /\<the\> should find only word the, but not words like these: there and other.
> – See the ‘<‘ character description above.
The character search searches within one line to find a character entered after the command. The f and F commands search for a character on the current line only. f searches forwards and F searches backwards and the cursor moves to the position of the found character.
The t and T commands search for a character on the current line only, but for t, the cursor moves to the position before the character, and T searches the line backwards to the position after the character.
These two sets of commands can be repeated using the ; or , command, where ; repeats the last character search command in the same direction, while , repeats the command in the reverse direction.
Saving Files and Quitting vi
To quit vi without making changes to the file, use the command :q!. When you press the “:”, the cursor moves to the last line on the screen and you’ll be in last line mode.
COW IS THE TIME FOR ALL WOMEN TO COME TO THE AID OF THE HUNGRY.
~
~
In last line mode, certain extended commands are available. One of them is q!, which quits vi without saving. The command :wq saves the file and then exits vi. The command ZZ (from command mode, without the “:”) is equivalent to :wq. If the file has not been changed since the last save, it merely exits, preserving the modification time of the last change. Remember that you must press Enter after a command entered in last line mode.
To save the file without quitting vi, use :w.