BACKGROUND IMAGE: iSTOCK/GETTY IMAGES
In a world of readily available nice graphical text editors, the default editor on Linux is still vi or vim. The Linux vi commands discussed in this article will give new administrators enough information to get started with vi.
You can't do without Linux vi or its updated cousin vi Improved (vim). Why? Because vi is always available, no matter the state of a Linux server. If your server runs into problems and nothing works anymore, vi is there to fix it.
Some commands are based on vi and use vi as their default editor. Commands like crontab to schedule jobs, visudo to delegate administration privileges and edquota to edit file system quotas are just some examples where vi is vital. The vi editor's features facilitate script writing, so new Linux administrators will find vi helpful.
Whenever possible, choose Linux vim, the new version of the text editor, to take advantage of usability features such as syntax highlighting. On some Linux distributions, vim starts automatically over vi, while the administrator will have to specify vi or vim on others.
The vi commands discussed in this article all work in both Linux vi and vim editors.
The two modes of Linux vi
Linux vi has two modes, which is difficult for first-time users. When first starting vi to edit a document -- vi ~/mydocument -- you'll enter command mode. Before entering commands, however, you might want to type some text. To enter text, you need to get into input mode.
The following commands can help admins move from command to input mode in vi and vim:
Use a to enter input mode after the current cursor position
Use i to enter input mode at the current cursor position
Use o to open a new line
After you have entered text in input mode, use :wq! to write changes to disk and quit. Don't forget the colon in the beginning of the command, and be sure to enter the letters in the right order; you cannot quit then write to disk. Or use its alternative: ZZ.
Sometimes all doesn't go well during a text session. In that case, do this:
Use u to undo the last modification (can be repeated multiple times)
Use Ctrl-R to redo any action that you've just rolled back with undo
Use :q! as an emergency exit. It gets you out of the current document without saving anything.
The text files that you'll be editing on Linux can grow really large. To navigate the cursor to the right position, use these:
gg to go to the first line in the document
:number to go to a line with a specific number
/text to go to the first occurrence of "text"
G to go to the end of the current document
Some editing commands are also available from vi's command mode:
v enters visual mode. In visual mode, you can select text by moving the arrow keys.
d deletes the current selection
y copies the current selection
p pastes a selection
dd removes a line. Linux text editing often requires you to remove an entire line.
To search and replace a given text, use :%s/oldtext/newtext/g
The "oldtext" part of the vi command should be the text you're looking for. The "newtext" part will be the text you want to replace it with. This is an easy way to make mass replacements in a document.
You will find many other commands available, but as a vi newbie, it's a good idea to focus on a bare minimum. Trying to navigate text editing with every single Linux vi command available increases the risk of getting lost.