Log in / create account | Login with OpenID
DocForge
Programmer's Wiki

Vi

From DocForge

Vi is a classic Unix text editor, comparable to emacs, and included in all Unix-like operating systems as part of the Posix standard.

The program was designed as a visual text editor improving on the line editor ex, which was in turn based on the ancient text editor ed. The syntax for editor commands matches that of these predecessors, and also resembles that of the script-friendly Unix stream editor sed.

Contents

[edit] Variants

  • Vim -- "Vi IMproved"; the dominant modern variant of vi. Many Linux-based systems link the vi command to vim by default.
  • gvim -- A graphical windowed version of vim, essentially identical except for its ability to exist as an independent window (with improved visual capabilities) rather than living in a terminal window.
  • Elvis
  • nvi
  • vile
  • bvi
  • Vigor -- vi plus Clippy from Microsoft Office (yes, it's a joke)

[edit] Relatives

[edit] Philosophy

As the Unix philosophy is for each tool to do one single thing well, vi is optimized for use as a fast visual text editor; for everything else, the shell should be used. For example, it's reasonable to open a shell script for editing in vi from the command line, make changes to the contents, exit vi, and run the script (possibly with a separate debugger) from the command line -- this contrasts with emacs and most IDEs, which encourage the user to run programs from within the editor. Yet this single-app-single-purpose approach is quite fast: vi is lightweight and will launch and exit quickly even on ancient or very cheap hardware, and all of the aforementioned operations can be done with a minimal number of keystrokes and without leaving the keyboard -- or even straying very far from the home row.

Vi commands emphasize staying close to the home row for two reasons: (1) It's fast and ergonomic, allowing a touch-typist to execute commands swiftly, accurately and without pausing; and (2) Commands can be relied on to exist on any keyboard made since 1970 (some keyboards like certain keys like Alt, Meta or Command).

[edit] Commands

vi is heavily modal. That is, depending on which mode you're in, the same keys do different things.

First, follow the tutorial, or you'll be terribly confused.

[edit] Normal mode

In this mode, where the editor begins, the keys are reserved for navigation and commands. You can leap around the document and perform complex text transformations with your hands in home position and with few modifier keys.

Powerful, but dangerous if you're unaware of it -- for example, if you open an existing file and immediately start typing "Dear John," you'll delete the first line, jump to the end of the first word on the next line, and insert "r John," immediately after that word. Typing "dapper John" accidentally will cut the current paragraph, paste it after the first line of the next paragraph, jump to the end of the first word after that, replace the first letter of the next word with a space, bring the entire next line onto the same line as the current one, open a new blank line below this, and insert the letters "hn" there, leaving you in Insert mode and probably a little dazed. Frankly, modal editors aren't for everyone.

Commands can be prefixed by a number to repeat the command that many times, e.g. 5dd deletes 5 lines.

In general, lowercase commands affect individual characters, uppercase (or Shift) commands affect entire lines, and suffixing a command with "ap" affects the current paragraph (text block delimited by blank lines).

To get back to Normal mode from any other mode, press Escape or Ctrl-c.

Navigation

  • h, j, k and l move the cursor left, down, up and right, respectively
  • / begins a search. Type an expression you want to match, and the cursor jumps there.
  •  ? begins a search just like /, but searching backward from the current point.
  • The characters * and # begin searches like / and ?, but using the current word as the search term
  • n and N jump to the next and previous matches of the most recent search
  • Ctrl-d and Ctrl-u scroll down and up by a half-screen
  • Ctrl-f and Ctrl-b scroll forward and back by a full screen
  • f and any character jumps to the next occurrence of that character on the current line
  • w moves to the start of the next word (word = alphanumeric string)
  • W moves to the start of the next word (word = anything other than whitespace)
  • e moves to the end of the current word (or next word, if on a non-alphanumeric character)
  • E moves to the last character before the next whitespace
  • b and B move back a word (alphanumeric or non-whitespace as above)
  • 0 moves to the first column of the current line
  • ^ moves to the start of the first word on the current line
  • $ moves to the end of the current line
  •  % jumps to the matching parenthesis, brace or bracket
  • gg jumps to the start of the document
  • G jumps to the end of the document
  • ...and many more.

Text transformation

Any of the navigation commands can be composed with most of these commands to change the affected area accordingly.

  • u undoes the previous action.
  • U undoes all actions on the current line.
  • x deletes the current character.
  • d and any motion deletes that area (e.g. dw deletes a word)
  • dd deletes the entire current line
  • D deletes to the end of the current line
  • c, C and cc perform likewise as d, deleting an area but dropping into insert mode afterward.
  • y, Y and yy copy (yank) the affected area into a register without deleting the area; deviates from the c/d pattern in that Y selects the whole line.
  • p inserts the contents of the register (the result of x, c, d or y) after the cursor. P does the same before the cursor.
  • r and any character replaces the current character with that character.
  • ~ switches the capitalization of the current character.
  • gw and any motion formats the area with line breaks according to predefined rules -- usually wrapping at 72 or 79 characters.
  • ...and many more.

Macros

  • q and any key begins recording a macro accessible through that key. Press q again to stop recording.
  • @ and a key where a macro is recorded applies that macro.
  • @@ repeats the last macro used.

[edit] Insert mode

This is the mode where you type text into the file.

How to get here from Normal or Visual modes:

  • i begins inserting at the cursor.
  • I begins inserting before the first word of the line.
  • a appends text after the cursor.
  • A appends text to the end of the line.
  • s kills the current character or selection and begins inserting.
  • c, C or cc kill an area as described above and begin inserting.

Return to Normal mode with Esc or Ctrl-c.

[edit] Replace mode

Overwrite as you type instead of inserting characters. Get here by pressing Ctrl-R and exit to Normal mode with Esc or Ctrl-c.

[edit] Visual mode

Press 'v' to enter visual mode. The modifications from normal mode apply:

  • v by itself selects the current character.
  • The selected area follows the normal navigation keys (l moves right, etc.)
  • V selects linewise.
  • Ctrl-v selects rectangular regions instead of by text flow. This is useful for formatting, e.g. deleting the comment characters at the start of several consecutive lines.

[edit] Minibuffer

(Is that what it's called?)

From Normal or Visual mode, press : to begin typing in the minibuffer. From here, more complex commands can be entered.

  • Line jump: Type the line number to jump to, and press Enter.
  • Search and replace: Syntax is like sed. Use s/[matching text]/[replacement text] to replace the first [matching text] with [replacement text] on the current line. The text entries are evaluated as regular expressions, so extensive magic is possible. Append with /g to affect all occurrences in the affected area (line or selection); append with c for yes/no confirmation on each match; prepend % to affect the entire file.
  • Shell access: Prefix a command with ! to send it to the supporting shell; anything that can be done from the shell can therefore be done within vi.

And many others. Consult the documentation for your particular variant of vi.

[edit] See Also

Retrieved from "http://docforge.com/wiki/Vi"