Jump to content

vi (text editor)

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 82.225.154.2 (talk) at 11:20, 7 August 2007 (→‎Derivatives and clones). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

vi editing a temporary, empty file. Tildes signify lines not present in the file.

vi is a free software screen-oriented text editor computer program written by Bill Joy in 1976 for an early BSD release.

About vi

The name vi is an initialism derived from the shortest unambiguous abbreviation for the command visual in ex; the command in question switches the line editor ex to visual mode. (Ex was a Unix editor that could switch between visual and command-line modes - the command line mode was to maintain some backward compatibility with an even earlier Unix editor called ed. The name vi is pronounced in various different ways; according to Eric S. Raymond, the correct pronunciation is /viː.aɪ/,[1] but other pronunciations such as /vaɪ/ are also used.[2]

Typically, as a matter of convenience, the same program will start up in vi or ex mode, depending on the name with which it is started.

vi is a modal editor, which means that it operates in either insert mode (where any typed text becomes part of the document) and command mode (where typed in letters are interpreted as commands that manipulate the document). When in insert mode, pressing the escape key will switch the editor to command mode.

The main advantage of a modal editor is that the usage of <Alt>, <Ctrl> and arrow keys is reduced to a minimum (since normal keys are used as commands in the command mode), resulting in the user's hands not having to leave the main keyboard, and thus overall faster work.

Operation

Upon start-up, vi starts in command mode (unless instructed otherwise). Typing 'i' (the letter 'i' without the quotes) enters insert mode. Any text typed thence gets added to the document, until the escape key is pressed, at which point the insert mode is exited and vi switches to command mode. (There are few more commands that switch the editor into insert mode, but they differ only in where the new text will go - before the cursor, after the cursor, above current line, below current line, etc.).

Commands

There are two main classes of commands: cursor movements and text modification. Cursor movement commands move the cursor. Here are a few:

  • G - Go to (a specified line). E.g., "10G" moves the cursor to line 10.
  • /pattern - Search for pattern and place the cursor at the start of the matched text.
  • ?pattern - Search in the reverse direction.
  • h j k l - Move the cursor to the left, down, up, right, respectively. These also take a count: '8l' moves the cursor 8 letters to the right.
  • <Ret> + - - Move the cursor down (<Ret> and '+') or up ('-').
  • fletter - Move the cursor to the first occurrence of letter in the current line, starting from current position.
  • Fletter - Similar to 'f', but in the reverse direction.
  • w W b B - Move forward ('w' and 'W') or back ('b' and 'B') by a word. 'w' and 'b' treat anything non-alphanumeric as a word delimiter; 'W' and 'B' honor only white space.
  • } { - Move to end of current or previous paragraph, respectively.
  • ^ $ - Move to beginning or end of current line, respectively.

Most of the cursor movement commands accept a count.

Text modification commands operate on a region of text. A region is simply a text movement command:

  • d - delete a region. "d10G" deletes lines from current line until line 10 (inclusive). Recall that "10G" moves the cursor to line 10.
  • c - change a region. "cwnew"<Esc> - changes current word with the text 'new'. Note that "cw" essentially switches the editor to insert mode.
  • < > - indent a region. "<)" indents left all the lines from here until end of paragraph. ">/xxx" indents right lines from here until the line containing the pattern "xxx".
  • ! - Filter a region through an external program. "!Gsort" pipes all the lines from current line to the end of the document ('G') to the 'sort' program and replaces those lines in the document with the output of the sort program.

Instead of specifying a region, if the cursor movement command letter is repeated (as in 'dd'), the current line is treated as the region. Thus "dd" deletes the current line, and "ccnew<Esc>" replaces the entire current line with the text 'new'.

  • = - Format a region of text.

As usual, these commands can be prefixed with a count. E.g., "10dd" deletes 10 lines.

Regular Expressions

Support for regular expressions (or text patterns) is a big feature of vi. These can be used extensively in search, substitution and global search and replace commands.

Options

Several of vi's default behaviors can be altered using options. These take the form ":set option" or ":set nooption" and so on. Example: "set ignorecase" causes all searches to be case-insensitive.

File Commands

  • :w filename - Writes (saves) the current document. If the optional filename is provided, the text is saved in the specified file.
  • :q - Quits the editor.
  • :e filename - Loads the specified file into the editor.
  • :n - Loads the next file into the editor. Useful when vi was invoked from the command line with multiple file names.
  • :r filename - Reads (inserts) the content of the specified file into the current document.

Where applicable, these commands take a line number or a range of line numbers. E.g., "10,25w newfile" will write lines 10 through 25 (inclusive) into the file newfile; "$r newfile" will read the contents of file newfile after the last line of the current document ('$' stands for the last line).

Maps and Abbreviations

A frequently used command sequence can be mapped to a new command-letter. The sequence could even include any text to be inserted. E.g.,:

map * iAuthor: John Bull<Esc>

causes the '*' character to be a command that inserts "Author: John Bull" at the cursor position.

An abbreviation is like a mapping, but during insert mode. Example:

abbr US United States

This will insert "United States" whenever the word "US" is typed when in insert mode.

Miscellaneous

The above provide a flavor of vi commands, and are obviously not the complete set.

History

ADM3A keyboard layout

vi was written in Evans Hall at the University of California, Berkeley, on a Lear-Siegler ADM3A terminal. On this machine, the Escape key was where the Tab key is nowadays, thus enabling users to very efficiently switch modes. Also, the keys hjkl had arrows, thus explaining the usage of these keys for moving around.

vi became the de facto standard Unix editor and a nearly undisputed hacker favorite outside of MIT until the rise of Emacs after about 1984. As of 2007 either vi or one of its clones can still be found on nearly all installations of Unix. The Single UNIX Specification specifies vi, so any system conforming to the Single UNIX Specification will have vi.

vi is still widely used by users of Unix variants. About half the respondents in a 1991 USENET poll preferred vi.[1] It starts up faster than the bulkier versions of Emacs and uses less memory. Consequently, even some Emacs fans will resort to it as a mail editor and for small editing jobs. In 1999, Tim O'Reilly, founder of the eponymous computer book publisher, stated that his company sold more copies of its vi book than its emacs book [1].

Derivatives and clones

The startup screen of vi clone vim
  • vi is a port of the classic BSD vi 3.7 to modern Unix systems. It uses ed as a codebase, which is BSD-style free since January 2002. [2]
  • nvi is an implementation of the ex/vi text editor originally distributed as part of the final official Berkeley Software Distribution(4.4BSD). This is the version of vi that is shipped with all BSD-based open source distributions. It adds command history and editing, filename completions, multiple edit buffers, multi-windowing (including multiple windows on the same edit buffer).
  • Vim "Vi IMproved" has yet more features than nvi, including (scriptable) syntax highlighting, mouse support, graphical versions, visual mode and many new editing commands. It is the standard version of vi on most Linux systems.
  • Elvis is a free vi clone for Unix and other operating systems. This is the standard version of vi shipped on Slackware Linux and Kate OS.
  • Vigor is vi with the addition of the Vigor Assistant, a deliberately irritating animated character modelled on Microsoft Office's Clippy.[3]
  • vile was initially derived from an early version of Microemacs in an attempt to bring the Emacs multi-window/multi-buffer editing paradigm to vi users.
  • bvi "Binary VI" is an editor for binary files based on the vi text editor.[4]
  • viper-mode is a VI emulation mode for Emacs.
  • svicc is a small vi clone for the Commodore (64) [5]
  • BusyBox (a set of standard Linux utilities on a single executable) includes a tiny vi clone
  • Yzis provides all vi features in a library which in turn is used by several frontends (Qt,KDE, curses) for all main operating systems (Unix, Windows, MacOS X) [6]

See also

References

  1. ^ a b Guy L. Steele, Eric S. Raymond (1996). (ed.) (ed.). The New Hacker's Dictionary (3rd edition ed.). MIT Press. ISBN 0-262-68092-0. {{cite book}}: |edition= has extra text (help); |editor= has generic name (help)
  2. ^ Gross, Christian (2005). Open Source for Windows Administrators. Charles River Media. pp. p. 55. ISBN 1-584-50347-5. {{cite book}}: |pages= has extra text (help)

Further reading

Oualline, Steve (2001) Vi IMproved - Vim, New Riders Publishers, 572 pp.