Learning the vi Editor

Learning the vi EditorSearch this book
Previous: 8.5 Enhanced TagsChapter 8
vi Clones Feature Summary
Next: 8.7 Programming Assistance
 

8.6 Improved Facilities

The four clones all provide additional features that make simple text editing easier and more powerful.

Editing the ex command line

The ability to edit ex mode commands as you type them, possibly including a saved history of ex commands. Also, the ability to complete filenames and possibly other things, such as commands and options.

No line length limit

The ability to edit lines of essentially arbitrary length. Also, the ability to edit files containing any 8-bit character.

Infinite undo

The ability to successively undo all of the changes you've made to a file.

Incremental searching

The ability to search for text while you are typing the search pattern.

Left/right scrolling

The ability to let long lines trail off the edge of screen instead of wrapping.

Visual mode

The ability to select arbitrary contiguous chunks of texts upon which some operation will be done.

Mode indicators

A visible indication of insert mode versus command mode, as well as indicators of the current line and column.

8.6.1 Command-Line History and Completion

Users of the csh, tcsh, ksh, and bash shells have known for years that being able to recall previous commands, edit them slightly, and resubmit them makes them more productive.

This is no less true for editor users than it is for shell users; unfortunately, UNIX vi does not have any facility to save and recall ex commands.

This lack is remedied in each of the clones. Although each one provides a different way of saving and recalling the command history, each one's mechanism is usable and useful.

In addition to a command history, all of the editors can do some kind of completion. This is where you type the beginning of, for example, a filename. You then type a special character (such as tab), and the editor completes the filename for you. All of the editors can do filename completion, some of them can complete other things as well. Details are provided in each editor's chapter.

8.6.2 Arbitrary Length Lines and Binary Data

All four clones can handle lines of any length.[7] Historic versions of vi often had limits of around 1,000 characters per line; longer lines would be truncated.

[7] Well, up to the maximum value of a C long, 2,147,483,647.

All four are also 8-bit clean, meaning that they can edit files containing any 8-bit character. It is even possible to edit binary and/or executable files, if necessary. This can be really useful, at times. You may or may not have to tell each editor that a file is binary.

nvi

Automatically handles binary data. No special command-line or ex options are required.

elvis

Under UNIX, does not treat a binary file differently from any other file. On other systems, it uses the elvis.brf file to set the binary option, to avoid newline translation issues. (The elvis.brf file and hex display modes are described in Section 10.10, "Interesting Features" in Chapter 10.)

vim

Does not have a limit on the length of a line. When binary is not set, vim is like nvi, and automatically handles binary data. However, 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.

vile

Automatically handles binary data. No special command-line or ex options are required.

Finally, there is one tricky detail. Traditional vi always writes the file with a final newline appended. When editing a binary file, this might add one character to the file and cause problems. nvi and vim are compatible with vi by default, and add that newline. In vim you can set the binary option, so this doesn't happen. elvis and vile never append the extra newline.

8.6.3 Infinite Undo

UNIX vi allows you to undo only your last change, or to restore the current line to the state it was in before you started making any changes. All of the clones provide "infinite undo," the ability to keep undoing your changes, all the way back to the state the file was in before you started any editing.

8.6.4 Incremental Searching

When incremental searching is used, the editor moves the cursor through the file, matching text as you type the search pattern. When you finally type [RETURN], the search is finished.[8] If you've never seen it before, it is rather disconcerting at first, but after a while you get used to it.

[8] emacs has always had incremental searching.

elvis does not support incremental searching. nvi and vim enable incremental searching with an option, and vile uses two special vi mode commands. vile can be compiled with incremental searching disabled, but it is enabled by default. Table 8.6 shows the options each editor provides.


Table 8.6: Incremental Searching
EditorOptionCommandAction
nvisearchincr

The cursor moves through the file as you type, always being placed on the first character of the text that matches.

vimincsearch

The cursor moves through the file as you type. vim highlights the text that matches what you've typed so far.

vile^X S, ^X R

The cursor moves through the file as you type, always being placed on the first character of the text that matches. ^X S incrementally searches forward through the file, while ^X R incrementally searches backward.

8.6.5 Left-Right Scrolling

By default, vi and most of the clones wrap long lines around the screen. Thus, a single logical line of the file may occupy multiple physical lines on your screen.

There are times when it might be preferable if a long line simply disappeared off the right-hand edge of the screen, instead of wrapping. Moving onto that line and then moving to the right would "scroll" the screen sideways. This feature is available in all of the clones. Typically, a numeric option controls how much to scroll the screen, and a Boolean option controls whether lines wrap or disappear off the edge of the screen. vile also has command keys to perform sideways scrolling of the entire screen. Table 8.7 shows how to use horizontal scrolling with each editor.


Table 8.7: Sideways Scrolling
EditorScroll AmountOptionAction
nvisidescroll = 16leftright

Off by default. When set, long lines simply go off the edge of the screen. The screen scrolls left or right by 16 characters at a time.

elvissidescroll = 8wrap

Off by default. When set, long lines simply go off the edge of the screen. The screen scrolls left or right by 8 characters at a time.

vimsidescroll = 0wrap

Off by default. When set, long lines simply go off the edge of the screen. 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.

vilesideways = 0linewrap

Off by default. When set, long lines wrap. Thus, the default is to have long lines go off the edge of the screen. Long lines are marked at the left and right edges with < and >. With sideways set to zero, each scroll moves the screen by 1/3. Otherwise the screen scrolls by the desired number of characters.

horizscroll

On by default. When set, moving the cursor along a long line off-screen shifts the whole screen. When not set, only the current line shifts; this may be desirable on slower displays.

vile has two additional commands, ^X ^R and ^X ^L. These two commands scroll the screen right and left, respectively, leaving the cursor in its current location on the line. You cannot scroll so far that the cursor position would go off the screen.

8.6.6 Visual Mode

Typically, operations in vi apply to units of text such lines, words, or characters, or to sections of text from the current cursor position to a position specified by a search command. For example, d/^} deletes up to the next line that starts with a right brace. elvis, vim, and vile all provide a mechanism to explicitly select a region of text to which an operation will apply. In particular, it is possible to select a rectangular block of text and apply an operation to all the text within the rectangle! See each editor's respective chapter for the details.

8.6.7 Mode Indicators

As you know by now, vi has two modes, command mode and insert mode. Usually, you can't tell by looking at the screen which mode you're in. Furthermore, often it's useful to know where in the file you are, without having to use the ^G or ex := commands.

Two options address these issues, showmode and ruler. All four clones agree on the option names and meanings, and even Solaris vi has the showmode option.

Table 8.8 lists the special features in each editor.


Table 8.8: Position and Mode Indicators
EditorWith ruler, displaysWith showmode, displays
nvirow and column

insert, change, replace, and command mode indicators

elvisrow and column

input and command mode indicators

vimrow and column

insert, replace, and visual mode indicators

vilerow, column, and percent of file

insert, replace, and overwrite mode indicators

viN/A

separate mode indicators for open, input, insert, append, change, replace, replace one character, and substitute modes

The GUI versions of elvis and vim change the cursor shape depending upon the current mode.


Previous: 8.5 Enhanced TagsLearning the vi EditorNext: 8.7 Programming Assistance
8.5 Enhanced TagsBook Index8.7 Programming Assistance

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System