In the following commands, file may be either a filename, or a shell command if prefixed with <b>!</b>
. Filenames are globbed by the shell before <b>vi</b>
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 the 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 " |
: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 (possibly as 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 |
Globally search for regular expression and execute cmd for each line containing the pattern. |
:s/RE |
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 |
Here is an example of what my .exrc startup file in my home directory looks like:
set report=1 shiftwidth=4 tabstop=8 wrapmargin=10
set ai bf exrc magic nomesg modelines showmode nowrapscan
map! #1 `x=%; echo ${x\%/*}/
Some other command settings are ignorecase (
ic),
autowrite (
aw), and
showmatch (
sm).