User:Hydratab/vi-commands

Useful Commands and Options

edit
  • A. Beginner
  • B. Basic
  • C. Advanced


A. Beginner

  • 1. Type: vi mynewfile
  • 2. Type: i (this enters insert (ie. edit) mode)
  • 3. Type: The quick brown fox jumps over the lazy cow.
  • 4. Type: <escape> (this exits edit mode back to command mode)
  • 5. Type: :w (this saves (writes) the file)
  • 6. Type: :q (this quits vi, back to the shell)
  • 7. Type: cat mynewfile (you should see the new file. for Win32, use "type mynewfile")

B. Basic

Below is a (small) list of most common commands and options. Anything after the '#' is a comment. The options should apply to all versions of vi, but this list is based on "Vim", so may differ from standard vi, or some version of vi differently compiled or configured, like those on Debian, Solaris etc. This is not intended be a comprehensive list of options, but rather a list of the most useful options in order to get started with vi.

(Hope someone can format the commands into a table format for easier reading, with command and description in seperate columns) At the vi command line:

 vi filename        (open 'filename' for editing in vi, or create as new if it doesn't exist)

At the vi command mode:

 Working with files:
   :e filename      (open 'filename' for editing)
   :q!              (exit without saving)
   :wq              (save and exit)
   :w filename      (save as 'filename')
   :r !ls           (reads result of ls into the file - replace 'ls' with your choice!)
 Moving in the file:
   0                (start of line)
   $                (end of line)
   :999             (goto line 999)
   G                (goto last line)
 Inserting/writing text:
   i                (insert)
   I                (insert at start of line)
   a                (append)
   A                (append at end of line)
   o                (open new line below and insert)
   O                (open new line above and insert)
   C                (change rest of current line)
 Deleting text:
   x                (deletes like DELETE key)
   X                (deletes like BACKSPACE key)
 Editing blocks of text:
   v                (starts highlighing)
   V                (starts highlighing lines)
   y                (yank (copy) marked text)
   yy               (copy line)
   9yy              (copy 9 lines)
   9dd              (delete 9 lines)
   p                (paste the yank'ed text)
   u                (undo last action)
 Search:
   /pattern         (search for pattern)
   n                (next match)
   N                (prev match)
   :%s/foo/bar/g    (replace all foo with bar in whole file)
   :%s/foo/bar/gc   (replace all foo with bar in whole file, confirming each)

At the vi edit mode:

 (Note: enter edit mode in many ways, such as pressing 'i' in command mode)
 <escape>           (stop editing)
 <up>               (move cursor up)
 <down>             (move cursor down)
 <left>             (move cursor left)
 <right>            (move cursor right)
 <backspace>        (delete before cursor)
 <delete>           (delete after cursor)

C. Advanced

After you are confortable with the basics, you can get more advanced, such as regular expressions, splitting windows, search and replace, command-line options, in the following ways:

  • Type: man vi (vi manual pages)
  • Type: info vi (vi info pages)
  • Get a vi or Unix book
  • Check out one of the detailed reference URLs below
  • Check out the source code, http://ex-vi.sourceforge.net/