Introduction to vim Features


Introduction to vim Features

This section covers modes of operation, online help, the Work buffer, emergency procedures, and other vim features. To see which features are incorporated in a particular build, give a vim command followed by the version option.

Online Help

As covered briefly earlier, vim provides help while you are using it. Give the command :help feature to display information about feature. As you scroll through the various help texts you will see words with a bar on either side, such as |tutor|. These words are active links: Move the cursor on top of an active link and press CONTROL-] to jump to the linked text. Use CONTROL-O (lowercase "o") to jump back to where you were in the help text. You can also use the active link words in place of feature. For example, you might see the reference |credits|; you could enter :help credits RETURN to read more about credits. Enter :q! to close a help window.

Some common features that you may want to look up using the help system are insert, delete, and opening-window. Although opening-window is not intuitive, you will get to know the names of features as you spend more time with vim. You can also give the command :help doc-file-list to view a complete list of the help files. Although vim is a free program, the author requests that you donate the money you would have spent on similar software to help the kids in Uganda (:help uganda for more information).

Modes of Operation

The vim editor is part of the ex editor, which has five modes of operation:

  • ex Command mode

  • ex Input mode

  • vim Command mode

  • vim Input mode

  • vim Last Line mode

While in Command mode, vim accepts keystrokes as commands, responding to each command as you enter it. It does not display the characters you type in this mode. While in Input mode, vim accepts and displays keystrokes as text that it eventually puts into the file you are editing. All commands that start with a colon (:) put vim in Last Line mode. The colon moves the cursor to the bottom line of the screen, where you enter the rest of the command.

In addition to the position of the cursor, there is another important difference between Last Line mode and Command mode. When you give a command in Command mode, you do not terminate the command with a RETURN. However, you must terminate all Last Line mode commands with a RETURN.

You do not normally use the ex modes. When this chapter refers to Input and Command modes, it means the vim modes, not the ex modes.

At the start of an editing session, vim is in Command mode. Several commands, including Insert and Append, put vim in Input mode. When you press the ESCAPE key, vim always reverts to Command mode.

The Change and Replace commands combine the Command and Input modes. The Change command deletes the text you want to change and puts vim in Input mode so you can insert new text. The Replace command deletes the character(s) you overwrite and inserts the new one(s) you enter. Figure 6-3 on page 149 shows the modes and the methods for changing between them.

Tip: Watch the mode and the CAPS LOCK key

Almost anything you type in Command mode means something to vim. If you think that vim is in Input mode when it is actually in Command mode, typing in text can produce confusing results. When learning vim, make sure that the showmode parameter (page 184) is set (it is by default) to remind you which mode you are using. You may also find it useful to turn on the status line by giving a :set laststatus=2 command (page 182).

Also keep your eye on the CAPS LOCK key. In Command mode typing uppercase letters produces different results than typing lowercase ones. It can be disorienting to give commands and have vim give the "wrong" responses.


The Display

The vim editor uses the status line and several special symbols to give information about what is happening during an editing session.

Status Line

The vim editor displays status information on the bottom line of the display area. This information includes error messages, information about the deletion or addition of blocks of text, and file status information. In addition, vim displays Last Line mode commands on the status line.

Redrawing the Screen

Sometimes the screen becomes garbled or overwritten. When vim puts characters on the screen, it sometimes leaves @ on a line instead of deleting the line. When output from a program becomes intermixed with the display of the Work buffer things can get confusing. The output does not become part of the Work buffer but affects only the display. If the screen gets overwritten, press ESCAPE to make sure vim is in Command mode, and press CONTROL-L to redraw (refresh) the screen.

Tilde (~) Symbol

If the end of the file is displayed on the screen, vim marks lines that would appear past the end of the file with a tilde (~) at the left of the screen. When you start editing a new file, the vim editor marks each line on the screen (except for the first line) with this symbol.

Correcting Text as You Insert It

While vim is in Input mode, you can use the erase and line kill keys to back up over text so you can correct it. You can also use CONTROL-W to back up over words.

Work Buffer

The vim editor does all of its work in the Work buffer. At the start of an editing session, vim reads the file you are editing from the disk into the Work buffer. During the editing session, it makes all changes to this copy of the file but does not change the file on the disk until you write the contents of the Work buffer back to the disk. Normally when you end an editing session, you command vim to write out the contents of the Work buffer, which makes the changes to the text final. When you edit a new file, vim creates the file when it writes the contents of the Work buffer to the disk, usually at the end of the editing session.

Storing the text you are editing in the Work buffer has both advantages and disadvantages. If you accidentally end an editing session without writing out the contents of the Work buffer, your work is lost. However, if you unintentionally make some major changes (such as deleting the entire contents of the Work buffer), you can end the editing session without implementing the changes.

If you want to use vim to look at a file but not to change it, you can use the view utility:

$ view filename


Calling the view utility is the same as calling the vim editor with the R (readonly) option. Once you have invoked the editor in this way, you cannot write the contents of the Work buffer back to the file whose name appeared on the command line. You can always write the Work buffer out to a file with a different name.

Line Length and File Size

The vim editor operates on any format file, provided the length of a single "line" (that is, the characters between two NEWLINE characters) can fit into available memory. The total length of the file is limited only by available disk space and memory.

Windows

The vim editor allows you to open, close, and hide multiple windows, each of which allows you to edit a different file. Most of the window commands consist of CONTROL-W followed by another letter. For example, CONTROL-W s opens another window (splits the screen) that is editing the same file. CONTROL-W n opens a second window that is editing an empty file. CONTROL-W w moves the cursor between windows, and CONTROL-W q (or :q) quits (closes) a window. Give the command :help windows to display a complete list of windows commands.

File Locks

When you edit an existing file, vim displays the first few lines of the file, gives status information about the file on the status line, and locks the file. When you try to open a locked file with vim, you will see a message similar to the one shown in Figure 6-7. You will see this type of message in two cases: when you try to edit a file that someone is already editing (perhaps you are editing it in another window or on another terminal) or when you try to edit a file that you were editing when vim or the system crashed.

Figure 6-7. Attempting to open a locked file


Although it is advisable to follow the instructions that vim displays, a second user can edit a file and write it out with a different filename. Refer to the next sections for more information.

Abnormal Termination of an Editing Session

You can end an editing session in one of two ways: When you exit from vim, you can save the changes you made during the editing session or you can abandon those changes. You can use the ZZ or :wq command from Command mode to save your changes and exit from vim (page 153).

To end an editing session without writing out the contents of the Work buffer, give the following command:

:q!


When you use this command to end an editing session, vim does not preserve the contents of the Work buffer, so you will lose all the work you did since the last time you wrote the Work buffer to disk. The next time you edit or use the file, it will appear as it did the last time you wrote the Work buffer to disk. Use the :q! command cautiously.

Sometimes you may find that you created or edited a file but vim will not let you exit. For example, if you forgot to specify a filename when you first called vim, you will get a message saying No file name when you give a ZZ command. If vim does not let you exit normally, you can use the Write command (:w) to name the file and write it to disk before you quit vim. Give the following command, substituting the name of the file for filename (remember to follow the command with a RETURN):

:w filename


After you give the Write command, you can use :q to quit using vim. You do not need to use the exclamation point (as in q!); it is necessary only when you have made changes since the last time you wrote the Work buffer to disk. Refer to page 178 for more information about the Write command.

Tip: When you cannot write to a file

It may be necessary to write a file using :w filename if you do not have write permission for the file you are editing. If you give a ZZ command and see the message "filename" is read only, you do not have write permission for the file. Use the Write command with a temporary filename to write the file to disk under a different filename. If you do not have write permission for the working directory, vim may still not be able to write the file to the disk. Give the command again, using an absolute pathname of a dummy (nonexistent) file in your home directory in place of the filename. (For example, Alex might give the command :w /Users/alex/temp or :w ~/temp.)

If vim reports File exists, you will need to use :w! filename to overwrite the existing file (make sure that you want to do this). Refer to page 179.


Recovering Text After a Crash

The vim editor temporarily stores the file you are working on in a swap file. If the system crashes while you are editing a file with vim, you can often recover its text from the swap file. When you attempt to edit a file that has a swap file, you will see a message similar to the one shown in Figure 6-7 on page 157. If someone else is editing the file, quit or open the file as a readonly file.

Alex checks whether the swap file exists for a file named memo, which he was editing when the system went down:

$ vim -r Swap files found:    In current directory: 1.    .memo.swp              dated: Mon Oct 18 13:16:06 2004           owned by: alex          file name: ~alex/memo          host name: bravo.example.com          user name: alex         process ID: 19786    In directory ~/tmp:       -- none --    In directory /var/tmp:       -- none --    In directory /tmp:       -- none -- 


With the r option vim displays a list of any swap files that it has saved (some may be old). If your work was saved, give the same command followed by a SPACE and the name of the file. You will then be editing a recent copy of your Work buffer. Use :w filename immediately to save the salvaged copy of the Work buffer to disk under a name different from the original file. Then check the recovered file to make sure it is OK. Following is Alex's exchange with vim as he recovers memo. Subsequently he deletes the swap file:

$ vim -r memo Using swap file ".memo.swp" Original file "~/memo" Recovery completed. You should check if everything is OK. (You might want to write out this file under another name and run diff with the original file to check for changes) Delete the .swp file afterwards. Hit ENTER or type command to continue :w memo2 :q $ rm .memo.swp 


Tip: You must recover files on the system you were using

The recovery feature of vim is specific to the system you were using when the crash occurred. If you are running on a cluster, you must log in on the system you were using before the crash to use the r option.





A Practical Guide to UNIX[r] for Mac OS[r] X Users
A Practical Guide to UNIX for Mac OS X Users
ISBN: 0131863339
EAN: 2147483647
Year: 2005
Pages: 234

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net