Читать книгу Linux Bible - Christopher Negus - Страница 153
Exiting vi
ОглавлениеTo wrap things up, use the following commands to save or quit the file:
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.