Basics of the Vi Text Editor
Vi (and its updated cousin, vim) is a text editor that is available on most Linux and Unix servers. Once you get comfortable with it you'll find vi is a fast and powerful editor.
Since you'll routinely be editing files on a Linux server it's important to know how to use a text editor. In addition to vi there are other text editors such as emacs, nano, pico, and joe; however this article will deal with vi.
Vi has the advantage of being installed on just about any Unix system you may use, Linux or otherwise. Once you learn vi you'll never be without a familiar editing environment. Some platforms actually use vi as the default editor for some tasks, like visudo, so even if you prefer another editor it's useful to know enough about vi to get by.
Opening a file
To get started editing a file, just type:
If the file doesn't already exist a new one will be created.
Vi is a modal editor; this means that it functions either in insert mode or in command mode. Some people find this concept confusing, however just think of it as either typing text (insert mode), or telling the editor what to do (command mode).
For example, if you have ever used Notepad in Windows, you are always in insert mode. When you want to issue a command to Notepad like finding some text, you use a menu - that's your command mode. Since you can't easily use a mouse and menus from the command line, vi provides command mode to let you accomplish similar tasks.
First we'll look at some tasks you can perform in command mode.
At your fingertips
A unique feature of vi is that everything you need to do can be done with your hands in the "home" position on the keyboard.
For example, to move forward word by word, just hit w. To move back word by word, hit b. To move down a line hit enter, or hit the j key. To move up, hit the k key (notice that j and k are right next to each other on the keyboard). To jump to the end of the file, hit shift-G.
To search for a word (like "foo") in a file, just type a forward slash followed by the search term:
If there are multiple occurrences of that word you can hit "n" to go to the next one, or "N" (shift-n) to go back to the previous occurrence.
You can also delete one character, one word, or an entire line with one or two keystrokes.
To delete a single character, move the cursor over it and hit the "x" key. To delete a word, move the cursor to it then type "dw" (for delete word). To delete an entire line, type "dd".
Any of these commands can have a number placed in front of them to delete multiple times. For example, "2x" or "2dw" or "2dd" (to delete 2 characters, 2 words, or 2 lines, respectively).
Another handy tool in Vi is to hit u (for undo) if you have made a mistake and want to revert back.
Up to this point we have been in command mode. To enter insert mode so that you can type new text, just hit "i" (for insert). You can then type your text into the file. When you are done typing hit the escape key to go back to command mode.
If you want to add text after the cursor, like at the end of a line, you can hit "a" (for append) instead.
If you want to replace (type over) some existing text from command mode, hit shift-R (for replace) and type your new text, then hit the escape key when you are done.
Closing a file
When you exit the file you can either save your work or exit without saving.
To save, type:
That's a colon followed by "wq" for "write and quit".
If you made changes but want to exit without saving them, type:
That's colon q, followed by an exclamation point.
If you were just reading the file and made no changes, just type:
That's colon q, for quit. If you type ":q" but have made any changes the system will prompt you to use one of the other commands to quit.
The vi text editor is a very powerful tool, and there are many other things that you can do in it. This should be enough to get started.
For more on what you can do with vi and how to do it, you'll find plenty of information at the official vim documentation site.
© 2011-2013 Rackspace US, Inc.
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License
See license specifics and DISCLAIMER