This section presents several commands that you may find useful once you have become comfortable using vim.
optional: Using Markers
While you are using vim, you can set and use markers to make addressing more convenient. Set a marker by giving the command mc, where c is any character. (Letters are preferred because some characters, such as a single quotation mark, have special meanings when used as markers.) The vim editor does not preserve markers when you exit from vim.
Once you have set a marker, you can use it in a manner similar to a line number. You can move the cursor to the beginning of a line that contains a marker by preceding the marker name with a single quotation mark. For example, to set marker t , position the cursor on the line you want to mark and give the command mt. During this editing session, unless you reset marker t or delete the line it marks, you can return to the beginning of the line you marked with the command 't.
You can delete all text from the current line through the line containing marker r with the following command:
You can use a back tick (', also called a grave accent or reverse single quotation mark) to go to the exact position of the mark on the line. After setting marker t, you can move the cursor to the location of this marker (not the beginning of the line that holds the marker) with the command 't. The following command deletes all the text from the current line up to the character where the mark r was placed; the rest of the line containing the marker remains intact:
You can use markers in addresses of commands instead of line numbers. The following command replaces all occurrences of The with THE on all lines from marker m to the current line (marker m must precede the current line):
EDITING OTHER FILES
The following command causes vim to edit the file you specify with filename:
If you want to save the contents of the Work buffer, you must write it out (using :w) before you give this command. If you do not want to save the contents of the Work buffer, vim insists that you use an exclamation point to acknowledge that you will lose the work you did since the last time you wrote out the Work buffer. If you do not supply a filename, vim edits the same file you are currently working on.
The command :e! starts an editing session over again. This command returns the Work buffer to the state it was in the last time you wrote it out or, if you have not written it out, the state it was in when you started editing the file. It is useful when you make mistakes while editing a file and decide that it would be easier to start over than to fix the mistakes.
Because this command does not destroy the contents of the General-Purpose or Named buffers, you can store text from one file in a buffer, use a :e command to edit a second file, and put text from the buffer in the second file.
The command :e# closes the current file and opens the last file you were editing, placing the cursor on the line that it was on when you last closed the file. If you do not save the file you are working on before you give this command, vim prompts you to do so. Setting the autowrite parameter (page 178) will not stop vim from prompting you.
The :e# command can help you copy blocks of text from one file to another. Call vim with the names of several files as arguments. You can use :n to edit the next file, :e# to edit the file you just edited, and :rew to rewind the sequence of files so that you are editing the first file again. As you move between files, you can copy text from one file into a buffer and paste that text into another file. You can use :n! to force vim to close a file without writing out changes before it opens the next file.
MACROS AND SHORTCUTS
The vim editor allows you to create macros and shortcuts. The :map command defines a key or sequence of keys that perform some action in Command mode. The following command maps CONTROL-X to the commands that will find the next left bracket on the line the cursor is on (f[ ), delete all characters from that bracket to the next right bracket (df] ) on the same line, delete the next character (x), move the cursor down two lines (2j), and finally move the cursor to the beginning of the line (0):
:map ^X f[df]x2j0
You can use ESCAPE and CONTROL sequences but try to avoid remapping characters or sequences that are vim commands. Type :map by itself to see a list of the current mappings. You may need to use CONTROL-V (page 159) to quote some of the characters you want to enter into the :map string.
The :abbrev command is similar to :map but creates abbreviations you can use while in Input mode. When you are in Input mode and type a string you have defined with :abbrev, followed by a SPACE, vim replaces the string and the SPACE with the characters you specified when you defined the string. For ease of use, do not use common sequences of characters when creating abbreviations. The following command defines ZZ as an abbreviation for Mark G. Sobell:
:abbrev ZZ Mark G. Sobell
Even though ZZ is a vim command, it is used only in Command mode. It has no special meaning in Input mode, where you use abbreviations.
Executing Shell Commands from Within vim
You can execute shell commands in several ways while you are using vim. You can spawn a new interactive shell by giving the following command and pressing RETURN:
The vim shell parameter (page 179) determines which shell is spawned (usually bash or tcsh). By default shell is the same as your login shell.
After you have finished your work in the shell, you can return to vim by exiting from the shell (press CONTROL-D or give an exit command).
tip: If :sh does not work correctly
The :sh command may behave strangely depending on how your shell has been configured. You may get warnings with the :sh command or it may even hang. Experiment with the :sh command to be sure it works with your configuration. If it does not, then you might want to set the vim shell parameter to another shell before using :sh. For example, the following command causes vim to use tcsh with the :sh command:
You may need to change the SHELL environment variable after starting :sh to show the correct shell.
caution: Edit only one copy of a file
When you create a new shell by using :sh, remember that you are still using vim. A common mistake is to try to edit the same file from the new shell, forgetting that vim is already editing the file from a different shell. Because you can lose information by editing the same file from two instances of an editor, vim warns you when you make this mistake. Refer to "File Locks" on page 152 to see an example of the message that vim displays.
You can execute a shell command line from vim by giving the following command, replacing command with the command line you want to execute and terminating the command with a RETURN:
The vim editor spawns a new shell that executes the command. When the command runs to completion, the newly spawned shell returns control to the editor.
You can execute a command from vim and have it replace the current line with the output from the command. If you do not want to replace any text, put the cursor on a blank line before giving the following command:
Nothing happens when you enter the first exclamation point. When you enter the second one, vim moves the cursor to the status line and allows you to enter the command you want to execute. Because this command puts vim in Last Line mode, you must end the command with a RETURN (as you would end most shell commands).
Finally you can execute a command from vim with standard input to the command coming from all or part of the file you are editing and standard output from the command replacing the input in the file you are editing. You can use this type of command to sort a list in place within a file.
To specify the block of text that will become standard input for the command, move the cursor to one end of the block of text. Then enter an exclamation point followed by a command that would normally move the cursor to the other end of the block of text. For example, if the cursor is at the beginning of the file and you want to specify the whole file, give the command !G. If you want to specify the part of the file between the cursor and marker b, give the command !'b. After you give the cursor-movement command, vim displays an exclamation point on the status line and waits for you to enter a shell command.
To sort a list of names in a file, move the cursor to the beginning of the list and set marker q with an mq command. Then move the cursor to the end of the list and give the following command:
Press RETURN and wait. After a few seconds, the sorted list should replace the original list on the screen. If the command did not do what you expected, you can usually undo the change with a u command. Refer to page 762 for more information on sort.
caution: ! can destroy a file
If you enter the wrong command or mistype a command, you can destroy a file (for example, if the command hangs or stops vim from working). For this reason it is a good idea to save your file before using this command. The Undo command (page 160) can be a lifesaver. A :e! command (page 181) will get rid of your changes, returning the buffer to the state it was in last time you saved it.
As with the :sh command, the default shell may not work properly with the ! command. You may want to test the shell with a simple file before executing this command with your real work. If the usual shell does not work properly, change the shell parameter.