How to Edit Files in Linux Using vi
If you have the opportunity to dabble with
ed in Linux, you’ll find that
vi is a dream come true, even though it’s still a command-line editor. In Linux. the
vi editor is a full-screen text editor, so you can view several lines at the same time. Most Unix systems, including Linux, come with
vi. Therefore, if you know the basic features of
vi, you can edit text files on almost any Unix-based system.
vi edits a file, it reads the file into a buffer — a block of memory — so you can change the text in the buffer. The
vi editor also uses temporary files during editing, but the original file isn’t altered until you save the changes.
To start the editor in a Linux system, type vi followed by the name of the file you want to edit, like this:
vi editor loads the file into memory, displays the first few lines in a text screen, and positions the cursor on the first line.
The last line shows the pathname of the file as well as the number of lines (
2) and the number of characters (
59) in the file. In this case, the text
[readonly] appears after the filename because
/etc/fstab file is opened while the user is logged in as a normal user (which means that this person doesn’t have permission to modify the file). Later, the last line in the
vi display functions as a command entry area. The rest of the lines display the file. If the file contains fewer lines than the screen,
vi displays the empty lines with a tilde (
~) in the first column.
The current line is marked by the cursor, which appears as a small black rectangle. The cursor appears on top of a character.
When using vi, you work in one of three modes in Linux:
- Visual command mode: This mode is the default. In this mode, anything you type is interpreted as a command that applies to the line containing the cursor. The
vicommands are similar to the
- Colon command mode: You use this mode for reading or writing files, setting
vioptions, and quitting
vi. All colon commands start with a colon (
:). When you type the colon,
vipositions the cursor on the last line and waits for you to type a command. The command takes effect when you press Enter.
- Text-input mode: This mode is for typing text. You can enter text-input mode with the command
a(insert after cursor),
A(append at end of line),
o(open a line below the current one),
O(open a line above the current one), or
i(insert after cursor). After entering lines of text, you have to press Esc to leave text-input mode and reenter visual command mode.
One problem with all these modes is that you can’t easily tell the current mode that vi is in. You may begin typing only to realize that
vi isn’t in text-input mode, which can be frustrating.
If you want to make sure that
vi is in command mode, press Esc a few times. (Pressing Esc more than once doesn’t hurt.)
To view online help in
vi, type :help while in colon command mode. When you’re finished with help, type :q to exit the Help screen and return to the file you’re editing.
vi editor initially positions the cursor on the first character of the first line, and one of the handiest things you can know is how to move the cursor around. To get a bit of practice, try the following command.
|Key||Moves the Cursor|
|↓||One line down|
|↑||One line up|
|←||One character to the left|
|→||One character to the right|
|W||One word forward|
|B||One word backward|
|Ctrl+D||Half a screen down|
|Ctrl+U||Half a screen up|
You can go to a specific line number at any time by using the handy colon command. To go to line 6, for example, type the following and then press Enter:
When you type the colon,
vi displays the colon on the last line of the screen. From then on,
vi uses any text you type as a command. You have to press Enter to submit the command to
vi. In colon command mode,
vi accepts all commands that the
ed editor accepts, and then some.
To search for a string, first type a slash (
vi editor displays the slash on the last line of the screen. Type the search string and then press Enter. The
vi editor locates the string and positions the cursor at the beginning of that string. To locate the string
cdrom in the file
To delete the line that contains the cursor, type dd. The
vi editor deletes that line of text and makes the next line the current one.
To begin entering text in front of the cursor, type i. The
vi editor switches to text-input mode. Now you can enter text. When you finish entering text, press Esc to return to visual command mode.
After you finish editing the file, you can save the changes in the file with the
:w command. To quit the editor without saving any changes, use the
:q! command. If you want to save the changes and exit, you can use the
:wq command to perform both steps at the same time. The
vi editor saves the changes in the file and exits. You can also save the changes and exit the editor by pressing Shift+ZZ (that is, hold Shift down and press Z twice).
vi accepts a large number of commands in addition to the commands just mentioned. The table below lists some commonly used
vi commands, organized by task.
|Command||Does the Following|
||Inserts text after the cursor|
||Inserts text at the end of the current line|
||Inserts text at the beginning of the current line|
||Inserts text before the cursor|
||Deletes up to the end of the current line|
||Deletes the current line|
||Deletes from the current line to the end of the file|
||Deletes the current word where the cursor presently resides|
||Deletes the character on which the cursor rests|
||Changes up to the end of the current line|
||Changes the current line|
||Joins the current line with the next one|
|rx||Replaces the character under the cursor with x (where x is any character)|
||Moves one character to the left|
||Moves one line down|
||Moves one line up|
||Moves to the end of the screen|
||Moves one character to the right|
||Moves to the beginning of the following word|
||Moves to the beginning of the previous word|
|Ctrl+D||Scrolls forward by half a screen|
|Ctrl+U||Scrolls backward by half a screen|
|Ctrl+L||Redraws the screen|
|Cut and paste text|
||Yanks (copies) current line to an unnamed buffer|
||Puts the yanked line above the current line|
|p||Puts the yanked line below the current line|
||Executes a shell command|
|:q||Quits the editor|
||Quits without saving changes|
||Reads the file and inserts it after the current line|
||Writes a buffer to the file|
|:wq||Saves changes and exits|
||Searches forward for a string|
||Searches backward for a string|
||Undoes the last command|
||Ends input mode and enters visual command mode|
||Undoes recent changes to the current line|