In this article, learn the basic use of the vi editor, which is almost always available on any Linux or UNIX system. Learn to:
- Navigate a document using vi
- Use basic vi modes
- Insert, edit, delete, copy, and find text
Starting vi
Most Linux distributions now ship with the vim (for Vi IMproved) editor rather than classic vi. Vim is upward compatible with vi and has a graphical mode available (gvim) as well as the standard vi text mode interface. The
vi
command is usually an alias or symbolic link to the vim program. There are several versions of vim: tiny, small, normal, big, and huge. You can find out what version of vim you are running and what features are included by using the command:vi --version
The vi editor dates from the time when not all terminal keyboards had cursor movement keys, so everything you can do in vi can be done with the keys typically found on a standard typewriter plus a couple of keys such as Esc and Insert. However, you can configure vi to use additional keys if they are available; most of the keys on your keyboard do something useful in vi. Because of this legacy and the slow nature of early terminal connections, vi has a well-deserved reputation for using very brief and cryptic commands. Let's start by looking at the keystrokes for navigation around your file.
Moving aroundh
Move left one character on the current line
j
Move down to the next line
k
Move up to the previous line
l
Move right one character on the current line
w
Move to the next word on the current line
e
Move to the next end of word on the current line
b
Move to the previous beginning of the word on the current line
Ctrl-f
Scroll forward one page
Ctrl-b
Scroll backward one page
Moving to lines
G
Moves to a specific line in your file. For example, 3G moves to line 3. With no parameter, G moves to the last line of the file.
H
Moves relative to the top line on the screen. For example, 3H moves to the line currently 3rd from the top of your screen.
L
Is like H, except that movement is relative to the last line on screen. Thus, 2L moves to the second-to-last line on your screen.
Practice these commands until you are comfortable moving around the file. If you get stuck and things aren't working as expected, read on and learn how to get out of the file
Getting out of vi
One of the most useful things to know about a new editor is how to get out of it before you do anything you shouldn't do, such as destroying an important configuration file. You can get out of vi by saving or abandoning your changes, or by restarting from the beginning. If these commands don't seem to work for you, you may be in insert mode, which you will learn about in a moment. If in doubt, pressing Esc will leave insert mode and return you to command mode where these commands should work.
:q!
Quit editing the file and abandon all changes. This is a very common idiom for getting out of trouble.
:w!
Write the file (whether modified or not). Attempt to overwrite existing files or read-only or other unwritable files. You may give a filename as a parameter, and that file will be written instead of the one your started with. It's generally safer to omit the ! unless you know what you're doing here.
ZZ
Write the file if it has been modified. Then exit. This is a very common idiom for normal vi exit.
:e!
Edit the current disk copy of the file. This will reload the file, abandoning changes you have made. You may also use this if the disk copy has changed for some other reason and you want the latest version.
:!
Run a shell command. Type the command and press Enter. When the command completes, you will see the output and a prompt to return to vi editing.
Quit editing the file and abandon all changes. This is a very common idiom for getting out of trouble.
:w!
Write the file (whether modified or not). Attempt to overwrite existing files or read-only or other unwritable files. You may give a filename as a parameter, and that file will be written instead of the one your started with. It's generally safer to omit the ! unless you know what you're doing here.
ZZ
Write the file if it has been modified. Then exit. This is a very common idiom for normal vi exit.
:e!
Edit the current disk copy of the file. This will reload the file, abandoning changes you have made. You may also use this if the disk copy has changed for some other reason and you want the latest version.
:!
Run a shell command. Type the command and press Enter. When the command completes, you will see the output and a prompt to return to vi editing.
Notes:
vi modes
The vi editor has two modes of operation:
- Command mode
- In command mode, you move around the file and perform editing operations such as searching for text, deleting text, changing text, and so on. You usually start in command mode.
- Insert mode
- In insert mode, you type new text into the file at the insertion point. To return to command mode, press the Esc (Escape) key.
These two modes determine the way the editor behaves. Anything you type in insert mode is considered text to be inserted into the file. If you are trying to type a command and nothing happens, or the character appears under the cursor, then you probably forgot to press Esc to escape from insert mode.
Editing text
Now that you can open a file in vi, move around it and get out, it's time to learn how to edit the text in the file.
Modifying text
Use the following commands when you need to insert, delete, or modify text. Note that some of these commands have an uppercase form that is similar to the lowercase form; see the descriptions below.
- i
- Enter insert mode before the character at the current position. Type your text and press Esc to return to command mode. Use I to insert at the beginning of the current line.
- a
- Enter insert mode after the character at the current position. Type your text and press Esc to return to command mode. Use A to insert at the end of the current line.
- c
- Use c to change the current character and enter insert mode to type replacement characters.
- o
- Open a new line for text insertion below the current line. Use O to open a line above the current line.
- cw
- Delete the remainder of the current word and enter insert mode to replace it. Use a repetition count to replace multiple words. Use c$ to replace to end of line.
- dw
- Same as for cw (and c$) above, except that insert mode is not entered.
- dd
- Delete the current line. Use a repetition count to delete multiple lines.
- x
- Delete the character at the cursor position. Use a repetition count to delete multiple characters.
- p
- Put the last deleted text after the current character. Use P to put it before the current character.
- xp
- This combination of x and p is a useful idiom. This swaps the character at the cursor position with the one on its right.
Searching text
You can search for text in your file using regular expressions:
- /
- Use / followed by a regular expression to search forward in your file.
- ?
- Use ? followed by a regular expression to search backward in your file.
- n
- Use n to repeat the last search in either direction.
You may precede any of the above search commands with a number indicating a repetition count. So 3/x will find the third occurrence of x from the current point, as will /x followed by 2n. Similarly, 2/^e will find the second line from the current position that starts with e.
0 comments:
Post a Comment