Graeme's Home   Graeme's Math Help
 Graeme's email
Search Engines
Kids' Stuff
Sports
Music
Movies
Tools
Find a Host
Web Design
Fun Sites to Explore
Funnies
Job, Career
Colleges, Education
Food
Running
Information Sources
The BibleLanguage
For School Reports

e-Books, Catalogs
Math, Science, Philosophy
High School Reunion
Financial / Investment Info
Banks
Brokers, Exchanges
Stock Prices, News
Web Publishers
Newspapers
Television Networks
Technical Journals
Web-Zines
Financial Magazines
Retailers
Consumer Guides
Books, etc.
Computer
Auctions, Other
Shipping
Local Guide
Restaurants
Travel
Airlines
Discount Tickets
Computers
vi, ASP, JavaScript
Consulting
Software Windows

Hardware Vendors
IBM Mainframe, AS/400
Training

# `vi` Cheat Sheet

This document is a vi cheat sheet, designed to be kept nearby while using the vi editor. In general, vi commands follow the convention of "one from column A and one from column B", using the two tables below, Operators and Operands as columns A and B.

Numeric arguments may prefix any operator; the command is repeated the given number of times or until it fails. Numeric arguments prefixing an operand execute the operand the given number of times, effectively just moving the cursor.

Operators Description
`d` operand delete the operand into the (delete) buffer
`p` paste the contents of the (delete) buffer
`y` operand yank the operand into the (delete) buffer
`i` operand inserts the operand
`a` operand appends the operand (insert after current character)
`r` operand replaces current character with operand
`s` operand substitute the operand with typed-in text
`c` operand change the operand to typed-in text
`!` operand pass the operand to a (Unix) shell as standard input;
standard output replaces the operand.
Common Macros Description
`I` insert at beginning of line (same as ^i)
`A` append at end of line (same as \$a)
`D` delete to end of line (same as d\$)
`C` change to end of line (same as c\$)
`x` delete one character (same as dl)
`ZZ` save and exit
`:w` filename save as filename without exiting
`:q!` quit immediately (without save)
Miscellaneous
`R` enter replace (overstrike) mode
`o` open line below
`O` open line above
`"` n n is 0-9: delete buffers
`"` x x is lowercase a-z: overwrite user buffers
`"` x x is uppercase A-Z: append to user buffers
`.` perform last change again
`u` undo last change
`U` undo all changes to current line
Operands Description
`h j k l` left, down, up, right; one character/line at a time
`w b e` next word, back word, end of word
`W B E` (same as above, but ignores punctuation)
`/`string search for string (use ? for reverse search)
`%` find matching ( ), { }, or [ ]
`( )` beginning of current/previous sentence
`{ }` beginning of current/previous paragraph (two adjacent newlines) (see also set paragraphs)
`[[ ]]` beginning of current/previous section (mostly user-defined; see also set sections)
line `G` goto particular line (or end-of-file)
`0 ^ \$` move to column 0, move to first non-whitespace, move to end of line
`f` x forward to character x on same line (inclusive)
`t` x to character x on same line (not inclusive)
`;` last f or t again in the same direction
`,` last f or t again in the opposite direction
`m` x set mark x at current position
`'` x move to line containing mark x
``` x move to exact position of mark x
`''` move to line of last jump point
```` move to exact position of last jump point

Interesting examples of numeric prefixes would be `36i-*<ESC>` and `20r_`.

## Ex (colon-mode) commands

In the following commands, file may be either a filename, or a shell command if prefixed with `!`. Filenames are globbed by the shell before `vi` uses them (shell wildcards are processed before the filenames are used). Address ranges may be used immediately after the colon in the commands below. Example address ranges are:

Range Description
`1,\$` From line 1 to the end of the file.
`10,20` From line 10 to line 20, inclusive.
`.,.+10` From the current line to current line + 10 (11 lines total).
`'a,'d` From the line containing mark a to the line containing mark d.
`/from/,/to/` From the line containing "from" to line containing "to", inclusive.
Commands which change the file being edited.
`:e` filename Change from the current file being edited to filename. "`%`" means current file, and "`#`" means alternate file.
Use `:e #` to edit the file most recently edited during the same session.
`:n` [filename(s)] Edits the next file from the command line. With optional list of filenames, changes command parameters and edits the first file in the list. Filenames are passed to the shell for wildcard substitution. Also consider command substitution:
`:n `grep -l pattern *.c``
`:args` Lists the files from the command line (modified by `:n`, above).
`:rew` Restarts editing at the first filename from the command line.
Commands which modify the text buffer or disk file being edited.
`:g/`RE`/cmd` Globally search for regular expression and execute `cmd` for each line containing the pattern.
`:s/`RE`/string/opt` Search-and-replace; string is the replacement. Use `opt` to specify options c (confirm), g (globally on each line), and p (print after making change).
`:w `file Write the contents of the buffer to file. If file starts with an exclamation mark, the filename is interpreted as a shell command instead, and the buffer is piped into the command as stdin.
`:r `file Reads the contents of the file into the current buffer. If file starts with an exclamation mark, the filename is interpreted as a shell command instead, and the stdout of the command is read into the buffer.
These commands control the environment of the vi session.
`:set `opt Turns on boolean option opt.
`:set `noopt Turns off boolean option opt.
`:set `opt=val Sets option opt to val.
`:set `opt? Queries the setting of option opt.
Miscellaneous commands.
`:abbr `string phrase Creates abbreviation string for the phrase phrase. Abbreviations are replaced immediately as soon as recognized during text or command input. Use `:unab string` to remove an abbreviation.
`:map `key string Creates a mapping from key to string. This is different from an abbreviation in two ways: abbreviations are recognized as complete units only (for example, a word with surrounding whitespace) while mappings are based strictly on keystrokes, and mappings can apply to function keys by using a pound-sign followed by the function key number, i.e. #8 would map function key 8. If the terminal doesn't have an <F8> key, the mapping can be invoked by typing "#8" directly.

Here is an example of what my `.exrc` startup file in my home directory looks like:

```set report=1 sw=4 ts=8 wm=10 set ai bf exrc magic nomesg modelines opt smd nows map! #1 `x=%; echo \${x\%/*}/ ```

Some other command settings are `ignorecase` (`ic`), `autowrite` (`aw`), and `showmatch` (`sm`).