ZZ: Saves the current changes to the file and exits from vi.
:w: Saves the current file, but you can continue editing.
:wq: Works the same as ZZ.
:q: Quits the current file. This works only if you don't have any unsaved changes.
:q!: Quits the current file and doesn't save the changes you just made to the file.TIPIf you've really trashed the file by mistake, the :q! command is the best way to exit and abandon your changes. The file reverts to the most recently changed version. So, if you just saved with :w, you are stuck with the changes up to that point. However, despite having saved the file, you can type u to back out of changes (all the way back to the beginning of the editing session if you like) and then save again.
You have learned a few vi
editing commands. I describe more commands in the following sections. First, however, consider the following tips to smooth out your first trials with vi
:
Esc: Remember that Esc gets you back to command mode. (I've watched people press every key on the keyboard trying to get out of a file.) Esc followed by ZZ gets you out of command mode, saves the file, and exits.
u: Press u to undo the previous change you made. Continue to press u to undo the change before that and the one before that.
Ctrl+R: If you decide that you didn't want to undo the previous undo command, use Ctrl+R for Redo. Essentially, this command undoes your undo.
Caps Lock: Beware of hitting Caps Lock by mistake. Everything that you type in vi has a different meaning when the letters are capitalized. You don't get a warning that you are typing capitals; things just start acting weird.
:!command: You can run a shell command while you are in vi using :! followed by a shell command name. For example, type :!date to see the current date and time, type :!pwd to see what your current directory is, or type :!jobs to see whether you have any jobs running in the background. When the command completes, press Enter and you are back to editing the file. You could even use this technique to launch a shell (:!bash) from vi, run a few commands from that shell, and then type exit to return to vi. (I recommend doing a save before escaping to the shell, just in case you forget to go back to vi.)
Ctrl+g: If you forget what you are editing, pressing these keys displays the name of the file that you are editing and the current line that you are on at the bottom of the screen. It also displays the total number of lines in the file, the percentage of how far you are through the file, and the column number the cursor is on. This just helps you get your bearings after you've stopped for a cup of coffee at 3 a.m.
Skipping around in the file
Besides the few movement commands described earlier, there are other ways of moving around a vi
file. To try these out, open a large file that you can't damage too much. (Try copying /var/log/messages
to /tmp
and opening it in vi
.) Here are some movement commands that you can use:
Ctrl+f: Pages ahead one page at a time.
Ctrl+b: Pages back one page at a time.
Ctrl+d: Pages ahead one-half page at a time.
Ctrl+u: Pages back one-half page at a time.
G: Goes to the last line of the file.
1G: Goes to the first line of the file.
35G: Goes to any line number (35, in this case).
Searching for text
To search for the next or previous occurrence of text in the file, use either the slash (/
) or the question mark (?
) character. Follow the slash or question mark with a pattern (string of text) to search forward or backward, respectively, for that pattern. Within the search, you can also use metacharacters. Here are some examples:
/hello: Searches forward for the word hello.
?goodbye: Searches backward for the word goodbye.
/The.*foot: Searches forward for a line that has the word The in it and also, after that at some point, the word foot.
?[pP]rint: Searches backward for either print or Print. Remember that case matters in Linux, so make use of brackets to search for words that could have different capitalization.
After you have entered a search term, simply type n or N to search again in the same direction (n
) or the opposite direction (N
) for the term.
Using ex mode
The vi
editor was originally based on the ex
editor, which didn't let you work in full-screen mode. However, it did enable you to run commands that let you find and change text on one or more lines at a time. When you type a colon and the cursor goes to the bottom of the screen, you are essentially in ex
mode. The following are examples of some of those ex
commands for searching for and changing text. (I chose the words Local
and Remote
to search for, but you can use any appropriate words.)
:g/Local: Searches for the word Local and prints every occurrence of that line from the file. (If there is more than a screenful, the output is piped to the more command.)
:s/Local/Remote: Substitutes Remote for the first occurrence of the word Local on the current line.
:g/Local/s//Remote: Substitutes the first occurrence of the word Local on every line of the file with the word Remote.
:g/Local/s//Remote/g: Substitutes every occurrence of the word Local with the word Remote in the entire file.
:g/Local/s//Remote/gp: Substitutes every occurrence of the word Local with the word Remote in the entire file and then prints each line so that you can see the changes (piping it through less if output fills more than one page).
Learning more about vi and vim
To learn more about the vi
editor, try typing vimtutor. The vimtutor
command opens a tutorial in the vim
editor that steps you through common commands and features you can use in vim
. To use vimtutor
, install the vim
-enhanced package.
Finding Files
Even a basic Linux installation can have thousands of files installed on it. To help you find files on your system, you can use commands such as locate
(to find commands by name), find
(to find files based on lots of different attributes), and grep
(to search within text files to find lines in files that contain search text).
Using locate to find files by name
On most Linux systems (Fedora and RHEL included), the updatedb
command runs once per day to gather the names of files throughout your Linux system into a database. By running the locate
command, you can search that database to find the location of files stored in it.
Here are a few things that you should know about searching for files using the locate
command:
There are advantages and disadvantages to using locate to find filenames instead of the find command. A locate command finds files faster because it searches a database instead of having to search the whole filesystem live. A disadvantage is that the locate command cannot find any files added to the system since the previous time the database was updated.
Not