Jump to content

Vi (text editor): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
undid major deletion that was made with incomprehensible comment
Well let me make it clear, we don't provide a detailed user manual, The operation of the langauge is irrelevent to us.
Line 46: Line 46:
vi is still widely used by users of Unix variants. About half the respondents in a [[1991]] [[Usenet|USENET]] poll preferred vi.<ref name="hackers" /> 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.<ref>{{ cite web | url = http://www.oreilly.com/pub/a/oreilly/ask_tim/1999/unix_editor.html | title = Ask Tim Archive | publisher = O'Reilly | date = Jun 21 1999 }}</ref>
vi is still widely used by users of Unix variants. About half the respondents in a [[1991]] [[Usenet|USENET]] poll preferred vi.<ref name="hackers" /> 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.<ref>{{ cite web | url = http://www.oreilly.com/pub/a/oreilly/ask_tim/1999/unix_editor.html | title = Ask Tim Archive | publisher = O'Reilly | date = Jun 21 1999 }}</ref>


== Operation ==
{{manual}}


Upon start-up, vi starts in normal mode (unless instructed otherwise). Typing the letter 'i' (without 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 '''normal 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.).

===Vi commands===

There are two main classes of commands: cursor movements and text modification. Vi is the fullscreen counterpart to ex, and cursor movement is a key part of the design.

; Motions
These commands move the cursor, either relative to its current position, or to an absolute position.

Relative motions can be prefixed with a count, to tell vi to repeat the motion. These are relative motion commands:
{|
|- valign="top"
|width="75pt"|<code>h</code>&nbsp;<code>j</code>&nbsp;<code>k</code>&nbsp;<code>l</code>
|Move the cursor to the left, down, up, right, respectively. These also take a count: '<code>8h</code>' moves the cursor 8 letters to the left.
|- valign="top"
|''Return''&nbsp;<code>+</code>&nbsp;<code>-</code>
|Move the cursor down (''Return'' and '<code>+</code>') or up ('<code>-</code>').
|- valign="top"
|<code>w</code>&nbsp;<code>W</code>&nbsp;<code>b</code>&nbsp;<code>B</code>
|Move forward ('<code>w</code>' and '<code>W</code>') or back ('<code>b</code>' and '<code>B</code>') by a word. '<code>w</code>' and '<code>b</code>' treat anything non-alphanumeric as a word delimiter; '<code>W</code>' and '<code>B</code>' honor only white space.
|- valign="top"
|<code>}</code>&nbsp;<code>{</code>
|Move to end of current or previous paragraph, respectively.
|- valign="top"
|<code>)</code>&nbsp;<code>(</code>
|Move to end of current or previous sentence, respectively.
|}

Absolute motions do not accept a count except for special cases where it acts as a line or column number. These are absolute motion commands:
{|
|- valign="top"
|width="75pt"|<code>G</code>&nbsp;&nbsp;
|Go to (a specified line). E.g., "10G" moves the cursor to line 10. Without a count, vi goes to the last line of the buffer.
|- valign="top"
|<code>^</code>
|Move to first nonblank character on the current line.
|- valign="top"
|<code>$</code>
|Move to end of current line.
|- valign="top"
|<code>0</code>
|(zero) Move to beginning of current line.
|}

; Operators
Many of the text modification commands form a special category known as ''operators''. They can be prefixed with a count (to repeat the operator), and are suffixed with a ''motion'' command. The text between the current position and the final position (after the motion) is called a ''region''.

These are examples of operators:
{|
|- valign="top"
|width="75pt"|<code>d</code>&nbsp;&nbsp;
|delete a region. "d10G" deletes lines from current line until line 10 (inclusive). Recall that "10G" moves the cursor to line 10.
|- valign="top"
|<code>c</code>
|change a region. "cw''new''"<Esc> - changes current word with the text 'new'. Note that "cw" essentially switches the editor to '''insert mode'''.
|- valign="top"
|<code>&lt;</code>&nbsp;<code>&gt;</code>
|indent a region. "<code>&lt;}</code>" indents left all the lines from here until end of paragraph. "<code>&gt;/xxx</code>" indents right lines from here until the line containing the pattern "xxx".
|}

In some instances, if the motion command repeats the operator character (as in 'dd'), the region is a whole line. Thus "dd" deletes the current line, and "cc''new''<Esc>" replaces the entire current line with the text 'new'. Prefixing it with a count repeats (or makes it apply to more than one), e.g., "10dd" deletes 10 lines.

; Inline Commands
A few commands move the cursor only within the current line. Like operators, they accept a repeat count:
{|
|- valign="top"
|width="75pt"|<code>f</code>''letter''&nbsp;&nbsp;
|Move the cursor to the first occurrence of ''letter'' in the current line, starting from current position.
|- valign="top"
|<code>F</code>''letter''
|Similar to 'f', but in the reverse direction.
|- valign="top"
|<code>t</code>''letter''
|Move the cursor before the first occurrence of ''letter'' in the current line, starting from current position.
|- valign="top"
|<code>T</code>''letter''
|Similar to 't', but in the reverse direction.
|- valign="top"
|<code>;</code>
|Repeat the last forward scan.
|- valign="top"
|<code>,</code>
|Repeat the last reverse scan.
|}

; Other Commands
Other commands do not fall neatly into a category:
{|
|- valign="top"
|width="75pt"|<code>/</code>''pattern''&nbsp;&nbsp;&nbsp;&nbsp;
|Search for pattern and place the cursor at the start of the matched text.
|- valign="top"
|<code>?</code>''pattern''
|Search in the reverse direction.
|- valign="top"
|<code>%</code>
|Move the cursor to the matching brace or bracket: {} [] ().
|- valign="top"
|<code>.</code>
|Repeat the last command which has modified the file. This may be used in a map. For example, suppose one has substituted an underscore for a space as the last command. Then ''<escape>''':map g j0.''''' will set a macro-command ''g'' to perform the last . command after moving to the first column in a line (0) and then down one line (j). From then on, one may simply press the ''g'' key to repeat the last command.
|- valign="top"
|<code>!</code>''program''&nbsp;&nbsp;
|Filter a region through an external program. "<code>!Gsort</code>" pipes all the lines from current line to the end of the document (<code>G</code>) to the <code>sort</code> program and replaces those lines in the document with the output of the sort program.
|}

=== Regular expressions ===
Support for [[regular expression]]s (or text patterns) is an important feature of vi. These can be used extensively in search, substitution and global search and replace commands.

===Ex commands===
All of the ex commands can be used in vi by typing ":" to get a prompt, and entering the command. Exit temporarily (or permanently) from visual mode to ex by "Q", return to visual mode by ":vi".

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

; File Commands
{|
|- valign="top"
|width="75pt"|<code>:w</code>&nbsp;''filename''&nbsp;&nbsp;
|Writes (saves) the current document. If the optional ''filename'' is provided, the text is saved in the specified file.
|- valign="top"
|<code>:q</code>
|Quits the editor.
|- valign="top"
|<code>:wq</code> or <code>:x</code>
|Save and Quit.
|- valign="top"
|<code>:e</code>&nbsp;''filename''
|Loads the specified file into the editor.
|- valign="top"
|<code>:n</code>
|Loads the next file into the editor. Useful when vi was invoked from the command line with multiple file names.
|- valign="top"
|<code>:r</code>&nbsp;''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.,:
<code>
:map * iAuthor: John Bull<Esc>
</code>
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:
<code>
:abbr US United States
</code>
This will insert "United States" whenever the word "US" is typed when in '''insert mode'''.


==Derivatives and clones==
==Derivatives and clones==

Revision as of 20:02, 27 October 2008

Vi
Developer(s)Bill Joy
Repository
Typetext editor
Websiteex-vi.sourceforge.net Edit this on Wikidata

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

The name vi is derived from the shortest unambiguous abbreviation for the command visual in ex; the command in question switches the line editor ex to visual mode. The name vi is Template:PronEng,[1] or /vaɪ/.[2]

Current releases of vi are free and open source software, usually released under permissive free software licenses such as the BSD License.

Interface

vi is a modal editor: it operates in either insert mode (where typed text becomes part of the document) or normal mode (where keystrokes are interpreted as commands that control the edit session). Typing 'i' while in normal mode switches the editor to insert mode. Typing 'i' again at this point places an 'i' character in the document. How the 'i' keystroke is processed depends on the editor mode. (From insert mode, pressing the escape key switches the editor back to normal mode.)

vi can process compound commands that embed text for insertion in the document. For example, the command:

20iHello world! <Enter> <escape>

would insert 20 lines in the document with the text 'Hello world!'. Rather than grapple with the notion of two mode switches while executing this command, some users view vi as a stateful filter. After processing the third character, vi changes state and begins processing input as text to be added to the file. On processing the escape, vi returns to the state in which it is ready to receive a new command.

Whether viewed as modal or stateful, vi's processing of the same keystroke in different ways depending on the history of the edit session distinguishes it from editors which are generally considered non-modal.

A perceived advantage of a modal editor is that the use of keyboard chords (multiple keys pressed simultaneously, typically a modifier plus a letter key) is reduced or eliminated. Instead, in normal mode, single keystrokes serve as commands. This results in the user's hands not having to take up awkward positions, which some find results in faster work. [citation needed]

History

ADM3A keyboard layout

Bill Joy wrote vi at the University of California, Berkeley, on a Lear-Siegler ADM3A terminal. On this terminal, the Escape key was on the left, where the Tab key is now on the widely-used IBM PC keyboard, thus enabling users to very efficiently switch modes. Also, the keys h,j,k,l had arrows, explaining the usage of these keys for moving around. The ADM3A had no other keys that corresponded to arrows.

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 2008 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] 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.[3]


Derivatives and clones

The startup screen of vi clone vim
  • 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 vi, 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.
  • 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.
  • BusyBox, a set of standard Linux utilities on a single executable, includes a tiny vi clone.
  • Viper, an emacs package providing Vi emulation on top of Emacs.

See also

References

  1. ^ a b Raymond, Eric S (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); Unknown parameter |coauthors= ignored (|author= suggested) (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)
  3. ^ "Ask Tim Archive". O'Reilly. Jun 21 1999. {{cite web}}: Check date values in: |date= (help)

Further reading