11.8. Improved Editing FacilitiesThis section describes the features of vim that make simple text editing easier and more powerful. 11.8.1. Command-Line History and Completionvim keeps a history of your ex commands, search strings, and expressions in its extended command language. These are three separate histories. The size of each is controlled by the history option; the default is 20. You may wish to increase it in your .vimrc file, although vim does take steps to maintain only unique commands. To access the history, use the cursor key on the colon command line. This will move backwards through the saved commands (most recent first). The key will move forwards. You can move around on the command line using the and keys. By default, text that you type is inserted into the command line. You can use the INS (Insert) key on your keyboard to toggle this mode, in which case what you type will replace what's on the command line. The BACKSPACE key will erase characters. You can use the SHIFT or CTRL key in combination with the and keys to move the cursor left or right one word at time. This may or may not work on all keyboards, though. You can use ^B or HOME to move the cursor to the beginning of the command line, and ^E or END to move to the end of the command line. The control key versions should always work. The behavior of the ESC character can vary. If vim is in vi compatibility mode, ESC acts likes RETURN and executes the command. When vi-compatibility is turned off, ESC will exit the command line without executing anything. vim also provides completion facilities on the ex command line. The wildchar option contains the character that you type when you want vim to do a completion. The default value is the tab character. You can use completion for all of the following:
Besides just the TAB key to do an expansion, a number of other control keys provide additional functionality. Table 11.3 describes the commands and what they do. Table 11.3. vim Command-Line Completion Commands
The completion facilities are extensive; see :help cmdline for the full details. Besides command-line completion, vim also provides insert mode completion. When typing text, especially in programs, the same words appear quite often. vim has commands that search backwards or forwards for a match with a half-finished word. For example, if you were typing this text and had entered ex, giving the ^P command would have completed it to example. This is a nice way to reduce the number of typed characters and to avoid spelling mistakes. Completion works not only with words in the text where you are typing, you can also fetch words from much further away. Table 11.4 shows an overview of the relevant commands. Table 11.4. vim Insert Mode Completion Commands
See :help ins-completion for more details. 11.8.2. Tag StacksTag stacking is described in Section 8.5.3. vim provides the richest set of facilities for working with tags. Besides just the ability to stack tags, if there are multiple matching tags, you can choose among them. You can also do a tag selection and window splitting operation in one command. See Table 11.5 for a list of vim tag commands. Table 11.5. vim Tag Commands
Normally, vim shows you which matching tag, out of how many, has been jumped to: tag 1 of >3 It uses a greater-than sign (>) to indicate that it has not yet tried all the matches. You can use :tnext or :tlast to try more matches. If this message is not displayed because of some other message, use :0tn to see it. The output of the :tags command is shown below. The current location is marked with a greater than sign (>): # TO tag FROM line in file 1 1 main 1 harddisk2:text/vim/test > 2 2 FuncA 58 -current- 3 1 FuncC 357 harddisk2:text/vim/src/amiga.c The :tselect command lets you pick from more than one matching tag. The "priority" (pri field) indicates the quality of the match (global versus static, exact case versus case-independent, etc.); this is described more fully in the vim documentation. nr pri kind tag file ~ 1 F f mch_delay os_amiga.c mch_delay(msec, ignoreinput) > 2 F f mch_delay os_msdos.c mch_delay(msec, ignoreinput) 3 F f mch_delay os_unix.c mch_delay(msec, ignoreinput) Enter nr of choice (<CR> to abort): The :tag and :tselect commands can be given an argument that starts with /. In that case, this argument is treated as a regular expression. vim will find all the tags that match the given regular expression.[57] For example, :tag /normal will find the macro NORMAL, the function normal_cmd, and so on. Use :tselect /normal and enter the number of the tag you want.
The vi command mode commands are described in Table 11.6. Besides using the keyboard, as in the other editors, you can also use the mouse, if mouse support is enabled in your version of vim. Table 11.6. vim Command Mode Tag Commands
The vim options that affect tag searching are described in Table 11.7. Table 11.7. vim Options for Tag Management
The vim 5.1 distribution comes with Version 2.0.3 of the Exuberant ctags program. As of this writing, this is the current version of Exuberant ctags. vim can use emacs style etags files, but this is only for backwards compatibility; the format is not documented in the vim documentation, nor is the use of etags files encouraged. Finally, like elvis, vim also looks up the entire word containing the cursor, not just the part of the word from the cursor location forward. 11.8.3. Infinite UndoIn vim, being able to undo and redo multiple levels of changes is controlled by the undolevels option. This option is a number indicating how many levels of "undo" that vim should allow. A negative value disallows any undoing (which is not terribly useful). When undolevels is set to a non-zero value, you enter text as normal. Then each successive u command undoes one change. To redo (undo the undo), you use the (rather mnemonic) CTRL-R command. vim is different from elvis; it starts out with a default value for undolevels of 1,000, which should be close enough to infinite for any given editing session. Also, the option is global, and not per buffer. Once undolevels has been set, a count to either the u or ^R commands undoes or redoes the given number of changes. vim actually implements undoing and redoing in two different ways. When the cpoptions (compatibility options) option contains the letter u, the u command works like in vi, and ^R repeats the previous action (like . in nvi). When u is absent from cpoptions, u undoes one step and ^R redoes one step. This is easier to use, but not vi-compatible. 11.8.4. Arbitrary Length Lines and Binary Datavim does not have a limit on the number or lengths of lines. When editing a binary file, you should either use the -b command-line option or :set binary. These set several other vim options that make it easier to edit binary files. To enter 8-bit text, use ^V followed by three decimal digits. 11.8.5. Incremental SearchingAs mentioned in Section 8.6.4, you enable incremental searching in vim using :set incsearch. The cursor moves through the file as you type. vim highlights the text that matches what you've typed so far. You may wish to use this with the hlsearch option, which highlights all matches of the most recent search pattern. This option is particularly useful when looking for all uses of a particular variable or function in program source code. 11.8.6. Left-Right ScrollingAs mentioned in Section 8.6.5, you enable left-right scrolling in vim using :set nowrap. The value of sidescroll controls the number of characters by which vim shifts the screen when scrolling left to right. With sidescroll set to zero, each scroll puts the cursor in the middle of the screen. Otherwise, the screen scrolls by the desired number of characters. vim also has several commands that scroll the window sideways, shown in Table 11.8. Table 11.8. vim Sideways Scrolling Commands
11.8.7. Visual Modevim allows you to select regions one character at a time, one line at a time, or rectangularly, using the commands shown in Table 11.9. Table 11.9. vim Block Mode Command Characters
vim highlights (using reverse video) the text as you are selecting. To make your selection, simply use the normal motion keys. If showmode is set, vim will indicate the mode as one of visual, visual line, or visual block. If vim is running inside an xterm, you can also use the mouse to select text (see :help mouse-using for the details). This also works in the GUI versions. The screen below shows a rectangular region: The 6th edition of <citetitle>Learning the vi Editor</citetitle> brings the book into the late 1990’s. In particular, besides the “original” version of <command>vi</command> that comes as a standard part of every UNIX system, there are now a number of freely available “clones” or work-alike editors. After applying the ~ operator, the screen looks like this: The 6th edition of <citetitle>Learning the vi Editor</citetitle> brings the BOOK INTO THE LATE 1990’s. In particulAR, BESIDES THE &LDQUo;original” version of <command>vi</COMMAND> THAT COMES as a standard part of every UNIX system, there are nOW A NUMBER OF FREELY available “clones” or work-alike editors. vim permits many operations on the selected text. Some operations work only on whole lines, even if you've selected a region that does not contain whole lines. vim has special commands for increasing the "swept out" area, and it allows you to apply almost any vi mode command to the highlighted text, as well as some commands that are unique to visual mode. When defining the area to be operated on, a number of commands make it easy to treat words, sentences, or blocks of C/C++ code as single objects. These are described in Table 11.10. These commands can be used by themselves to extend the region, or they can be used in conjunction with an operator. For example, daB deletes a brace-enclosed block of text, including the braces. Table 11.10. vim Block Mode Object Selectors
The terms "word" and "WORD" have the same meaning as for the w and W motion commands. vim allows you to use many operators on highlighted text. The available operators are summarized in Table 11.11. Table 11.11. vim Block Mode Operations
Copyright © 2003 O'Reilly & Associates. All rights reserved. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|