|


| |
Vim reference manual
By Bram Moolenaar
version 3.0
There is a contents listing at the end of this document.
The commands and options for multiple windows and buffers are explained in a separate
document: windows.doc
- Introduction
Vim stands for Vi IMproved. It used to be Vi IMitation, but that does
not really cover it anymore. Vim is a text editor which includes almost all the commands
from the Unix program "Vi" (and a lot new ones). It is very useful for editing
programs and other ASCII text. All commands are given with the keyboard. There is no mouse
support and there are no menus. This gives the advantage that you can keep your fingers on
the keyboard and your eyes on the screen.
Throughout this manual the differences between Vi and Vim are mentioned in curly
braces. Read the file "difference.doc" for a summary of the differences.
This manual refers to Vim on the Commodore Amiga computer. On other computers and on
terminals there may be small differences. For MSDOS this is documented in msdos.doc. For
UNIX this is in unix.doc.
This manual is a reference for all the Vim commands and options. A basic knowledge of
"Vi" is assumed. A summary of this manual can be found in the file vim.hlp. It
can be accessed from within Vim with the <HELP> key (in MSDOS <F1>) and with
the command ":help". The 'helpfile' option can be set to the name of the help
file, so you can put it in any place you like.
- Notation
[] Characters in square brackets are optional.
[count] An optional number that may precede the command to multiply
or iterate the command. If no number is given a count of one
is used, unless otherwise noted. Note that in this manual
the [count] is not mentioned in the description of the
command, but only in the explanation. This was done to make
the commands easier to lookup. If the "sc" option is on, the
(partially) entered count is shown at the bottom of the
window. You can use <DEL> to erase the last digit.
["x] An optional register designation where text can be stored.
The x is a single character between <a> and <z> or <A> and
<Z> or <">, and in some cases (with the put command) between
<0> and <9>, <%>, <:> or <.>. The uppercase and lower case
letter designate the same register, but the lower case letter
is used to overwrite the previous register contents, while the
uppercase letter is used to append to the previous register
contents. Without the ""x" or with """" the stored text is
put into the unnamed register. See also "Copying and moving
text".
{} Curly braces denote parts of the command which must appear,
but can take a number of different values. The differences
between Vim and Vi are also given in curly braces (this will
be clear from the context).
{motion} A command that moves the cursor. They are listed in chapter
6. This is used after an "operator" command to move over the
text that is to be operated upon. If the motion includes a
count and the operator also had a count, the two counts are
multiplied. For example: "2d3w" deletes six words.
{visual} A piece of text that is started with the "v", "V" or CTRL-V
command and ended by the cursor position. This is used
before an "operator" to highlight the text that is to be
operated upon. See the chapter on Visual mode.
<character> A special character from the table below or a single ASCII
character.
<char1-char2> A single character from the range <char1> to <char2>. For
example: <a-z> is a lower case letter. Multiple ranges may be
concatenated. For example: <a-zA-Z0-9> is any alphanumeric
character.
CTRL-<char> <char> typed as a control character, that is, typing <char>
while holding the CTRL key down. The case of <char> does not
matter, thus CTRL-A and CTRL-a are equivalent.
'option' An option, or parameter, that can be set to a value is
enclosed in single quotes. See chapter 19.
"command" In examples the commands you can type are enclosed in double
quotes.
notation meaning equivalent decimal value
<NUL> zero CTRL_@ 000 (internally 010) <BELL> bell CTRL-G 007 <BS>
backspace CTRL-H 008 <TAB> tab CTRL-I 009 <LF> linefeed CTRL-J 010 <FF>
formfeed CTRL-L 012 <CR> carriage return CTRL-M 013 <ESC> escape CTRL-[ 027
<SPACE> space 032 <DEL> delete 127 <C_UP> cursor-up 128 (msdos: 176)
<C_DOWN> cursor-down 129 (msdos: 177) <C_LEFT> cursor-left 130 (msdos: 178)
<C_RIGHT> cursor-right 131 (msdos: 179) <SC_UP> shift-cursor-up 132 (msdos:
180) <SC_DOWN> shift-cursor-down 133 (msdos: 181) <SC_LEFT> shift-cursor-left
134 (msdos: 182) <SC_RIGHT> shift-cursor-right 135 (msdos: 183) <F1> -
<F10> function keys 1 to 10 136 - 145 (msdos: 184 - 193) <SF1> - <SF10>
shift-function keys 1 to 10 146 - 155 (msdos: 194 - 203) HELP> help key 156 (msdos:
204) <UNDO> undo key 157 (msdos: 205)
Note: The shifted cursor keys, the help key and the undo key are only available on a
few terminals. On some terminals the function keys 11 to 20 are used instead of the
shifted function keys. On the Amiga shifted function key 10 produces a code (CSI) that is
also used by key sequences. It will be recognized only after typing another key.
- Starting Vim
-
- Command line
Most often Vim is started to edit a single file with the command
vim file
More generally Vim is started with:
vim [options] [filelist]
If the filelist is missing, the editor will start with an empty buffer. Otherwise
exactly one out of the following three may be used to choose one or more files to be
edited.
file .. A list of file names. The first one will be the current file and read into the
buffer. The cursor will be positioned on the first line of the buffer.
-t {tag} A tag. "tag" is looked up in the tags file, the associated file
becomes the current file and the associated command is executed. Mostly this is used for C
programs. In that case "tag" should be a function name. The effect is that the
file containing that function becomes the current file and the cursor is positioned on the
start of the function (see the section "tag searches").
-e [errorfile] QuickFix mode. The file with the name [errorfile] is read and the first
error is displayed. If [errorfile] is not given, the 'errorfile' option is used for the
file name (default "AztecC.Err" for the Amiga, "errors" for other
systems). See section 5.5: "using the QuickFix mode".
The options, if present, must precede the filelist. The options may be given in any
order.
+[num] The cursor will be positioned on line "num" for the first file being
edited. If "num" is missing, the cursor will be positioned on the last line.
+/{pat} The cursor will be positioned on the first line containing "pat" in
the first file being edited (see the section "pattern searches" for the
available search patterns).
+{command}
-c {command} "command" will be executed after the first file has been read.
"command" is interpreted as an Ex command. If the "command" contains
spaces it must be enclosed in double quotes (this depends on the shell that is used).
Example: vim "+set si" main.c
-r Recovery mode. The swap file is read to recover a crashed editing session. See the
chapter "Recovery after a crash".
-v View mode. The 'readonly' option will be set and no swap file will be written (see
-n below). You can still edit the buffer, but will be prevented from accidentally
overwriting a file. If you forgot that you are in View mode and did make some changes, you
can overwrite a file by adding an exclamation mark to the Ex command, as in
":w!". The 'readonly' option can be reset with ":set noro" (see the
options chapter). Calling the executable "view" has the same effect as the -v
option. If your system does not support links and you do not want to have the executable
twice you could make an alias: "alias view vim -v".
-b Binary mode. The 'textauto', 'textmode' and 'expandtab' options will be reset. The
'textwidth' option is set to 0. 'modelines' is set to 0. The 'binary' option is set. This
is done after reading the .vimrc/.exrc files but before reading a file. See also 5.6:
"Editing binary files".
-n No swap file will be used. Recovery after a crash will be impossible. Handy if you
want to view or edit a file on a very slow medium (e.g. floppy). Can also be done with
":set uc=0". You can switch it on again by setting the 'uc' option to some
value, e.g. ":set uc=100". Any files already being edited will not be affected
by this.
-o[N] Open N windows. If [N] is not given, one window is opened for every file given as
argument. If there is not enough room, only the first few files get a window. If there are
more windows than arguments, the last few windows will be editing an empty file.
-T {terminal} Set the terminal type to "terminal". This influences the codes
that Vim will send to your terminal. This is normally not needed, because Vim will be able
to find out what type of terminal you are using (See chapter 20).
-d {device} Amiga only: The "device" is opened to be used for editing.
Normally you would use this to set the window position and size: "-d
con:x/y/width/height", e.g. "-d con:30/10/600/150". But you can also use it
to start editing on another device, e.g. AUX:.
-x Amiga only: Do not restart Vim to open a new window. This option should be used when
Vim is started by a program that will wait for the edit session to finish (e.g. mail or
readnews). See section 3.3.
-s {scriptin} The script file "scriptin" is read. The characters in the file
are interpreted as if you had typed them. The same can be done with the command
":source! {scriptin}". If the end of the file is reached before the editor
exits, further characters are read from the keyboard. See also the section "complex
repeats".
-w {scriptout} All the characters that you type are recorded in the file
"scriptout", until you exit Vim. This is useful if you want to create a script
file to be used with "vim -s" or ":source!". See also the section
"complex repeats".
Example for using a script file to change a name in several files: Create a file
"subs.vi" containing substitute commands and a :wq command:
:%s/Jones/Smith/g
:%s/Allen/Peter/g
:wq
Execute Vim on all files you want to change:
foreach i ( *.let ) vim -s subs.vi $i
If the executable is called "view" Vim will start in Readonly mode. This is
useful if you can make a hard or symbolic link from "view" to "vim".
Starting in Readonly mode can also be done with "vim -v".
- Workbench (Amiga only)
Vim can be started from the workbench by clicking on its icon
twice. It will then start with an empty buffer.
Vim can be started to edit one or more files by using a "Project" icon. The
"Default Tool" of the icon must be the full pathname of the Vim executable. The
name of the ".info" file must be the same as the name of the text file. By
clicking on this icon twice, Vim will be started with the filename as current filename,
which will be read into the buffer (if it exists). You can edit multiple files by pressing
the shift key while clicking on icons, and clicking twice on the last one. The
"Default Tool" for all these icons must be the same.
It is not possible to give arguments to Vim, other than filenames, from the workbench.
- Vim window (Amiga only)
Vim will run in the CLI window where it was started. If Vim
was started with the "run" or "runback" command, or if Vim was started
from the workbench, it will open a window of its own.
Technical detail:
To open the new window a little trick is used. As soon as Vim recognizes that it does not
run in a normal CLI window, it will create a script file in t:. This script file contains
the same command as how Vim was started, and an "endcli" command. This script
file is then executed with a "newcli" command (the "c:run" and
"c:newcli" commands are required for this to work). The script file will hang
around until reboot, or until you delete it. This method is required to get the
":sh" and ":!" commands to work correctly. But when Vim was started
with the -e option (Quickfix mode) or with the -x option, this method is not used. The
reason for this is that when a compiler starts Vim with the -e option it will wait for a
return code. With the script trick the compiler cannot get the return code. The -x option
can be used when Vim is started by a mail program which also waits for the edit session to
finish. As a consequence the ":sh" and ":!" commands are not available
when the -e or -x option is used.
Vim will automatically recognize the window size and react to window resizing. Under
Amiga DOS 1.3 it is advised to use the fastfonts program "FF" to speed up
display redrawing.
- Initialization
When Vim starts running it does initializations in the following
order. If an environment variable is used, it is executed as a single Ex command line.
Multiple commands must be separated with <|> or <LF>. If a file is used, each
line is executed as an Ex command line.
- Four places are searched for initializations. The first that exists is used, the others
are ignored.
- The environment variable VIMINIT
- The file "s:.vimrc" (for Unix: "$HOME/.vimrc")
- The environment variable EXINIT
- The file "s:.exrc" (for Unix: "$HOME/.exrc")
- If the 'exrc' option is set (default is 'noexrc'), the current directory is searched for
two files. The first that exists is used, the other is ignored.
- The file ".vimrc"
- The file ".exrc"
- The environment variable SHELL, if it exists, is used to set the 'shell' option. With
MSDOS the COMPSPEC variable is used if SHELL does not exist. The 'shellpipe' option is set
according to the name of the shell.
- The environment variable TERM, if it exists, is used to set the 'term' option.
The first can be used to set your default settings and mappings for all edit sessions.
The second one for sessions in a certain directory (note that the 'exrc' option is default
off). See the section "Saving settings" for how to create a file with commands
to recreate the current settings.
If the VIMINIT environment variable or ".vimrc" exist the EXINIT and
".exrc" are skipped. This can be used to initialize Vim without interfering with
another version of Vi.
On the Amiga two types of environment variables exist. The ones set with the DOS 1.3
(or later) setenv command are recognized. See the AmigaDos 1.3 manual. The environment
variables set with the old Manx Set command (before version 5.0) are not recognized.
On MS-DOS systems Vim assumes that all the "_vimrc" and "_exrc"
files have <CR><LF> pairs as line separators. This will give problems if you
have a file with only <LF>s and have a line like ":map xx yy^M". The
trailing ^M will be ignored.
While reading the ".vimrc" or the ".exrc" file in the current
directory some commands can be disabled for security reasons by setting the 'secure'
option. Otherwise it would be possible to create a .exrc that contains nasty commands,
which another user may automatically execute when he starts Vim it that directory. The
disabled commands are the ones that start a shell and the ones that write to a file. The
":map" commands are echoed, so you can see which keys are being mapped.
You can reset the 'secure' option in the EXINIT or VIMINIT environment variable or in
the global ".exrc" or ".vimrc" file. This is not possible in
".vimrc" or ".exrc" in the current directory, for obvious reasons.
On unix systems this only happens if you are not the owner of the ".vimrc" or
".exrc" file. Warning: If you unpack an archive that contains a .exrc file, it
will be owned by you. You won't have the security protection. Check the .exrc file before
you start Vim in that directory, or reset the 'exrc' option.
- Suspending
CTRL-Z Suspend the editor. Same as ":stop".
:sus[pend][!] or
:st[op][!] Suspend the editor. If the <!> is not given, the buffer was changed,
autowrite is set and a filename is known, the buffer will be written.
On many UNIX systems it is possible to suspend Vim with CTRL-Z. This is only possible
in Command mode (see next chapter). Vim will continue if you make it the foreground job
again. On other systems CTRL-Z will start a new shell. This is the same as the
":sh" command. Vim will continue if you exit from the shell.
4. Modes
-
- Introduction
Vim has four basic modes:
Command mode In Command mode you can enter all the editor commands. If you start the
editor you are in this mode (unless you have set the 'insertmode' option, see below).
Insert mode In Insert mode the text you type is inserted into the buffer. If the
'showmode' option is set (which is default), the string "-- INSERT --" is shown
at the bottom of the window.
Replace mode Replace mode is a special case of Insert mode. You can do the same things
as in Insert mode, but for each character you enter (except some special characters) one
character of the existing text is deleted. If the 'showmode' option is set (which is
default), the string "-- REPLACE --" is shown at the bottom of the window.
Command_line mode In Command_line mode you can enter one line of text at the bottom of
the window. This is for the Ex commands <:>, the pattern search commands
<?></> and the filter command <!>.
More explanation on the insert, replace and Command_line mode is further on in this
chapter.
- Switching from mode to mode
If for any reason you do not know in which mode you are,
you can always get back to Command mode by typing <ESC> twice. You will know you are
back in Command mode when you see the screen flash or hear the bell after you type
<ESC>.
- go from Command mode to Insert mode by giving one of the commands
"iIaAoOcCsS".
- go from Command mode to Replace mode with the "R" command (not the
"r" command!).
- go from Command mode to Command_line mode with the one of the commands ":/?!".
- go from insert or Replace mode to Command mode with <ESC> (twice in some rare
cases).
- go from Command_line mode to Command mode by:
- hitting <CR> or <LF>, which causes the entered command to be executed
- deleting the complete line (e.g. with CTRL-U) and giving a final <BS>
- hitting CTRL-C or <ESC>, which quits the command line without executing the
command.
In the last case <ESC> may be the character defined with the 'wildchar' option,
and start command line completion. You can ignore that and type <ESC> again. {vi:
when hitting <ESC> the command line is executed. This is unexpected for most people,
therefore it was changed in Vim. But when the <ESC> is part of a mapping the command
line is executed. If you want the vi behaviour also when typing <ESC> use
":cmap ^V<ESC> ^V^M"}
If the 'insertmode' option is set, editing a file will start in Insert mode.
- Insert and Replace mode
- special keys
In insert and Replace mode the following characters have a special
meaning, other characters are inserted directly. To insert one of these special characters
into the buffer, precede it with CTRL-V. To insert a <NUL> character use
"CTRL-V CTRL-@" or "CTRL-V 000". On some systems you have to use
"CTRL-V 003" to insert a CTRL-C.
char action
CTRL-@ Insert previously inserted text and stop insert. {Vi: only when typed as first
char, only up to 128 chars} CTRL-A Insert previously inserted text. {not in Vi} CTRL-B
Toggle the 'revins' option (B for Backwards). {not in Vi} CTRL-C Quit insert mode, back to
command mode. Do not check for abbreviations. CTRL-D Delete one shiftwidth of indent at
the start of the current line. See also 'shiftround' option. When preceded with <^>
or <0> delete all indent in the current line. With <^> the indent is restored
in the next line. This is useful when inserting a label. {Vi: CTRL-D works only when used
after autoindent} CTRL-E Insert the character which is below the cursor. {not in Vi}
CTRL-H <BS> Delete the character before the cursor (see below). {Vi: does not delete
autoindents} CTRL-I <TAB> Insert a tab. If the 'expandtab' option is on, the
equivalent number of spaces is inserted (use CTRL-V <TAB> to avoid the expansion).
See also the 'smarttab' option, section 4.3.4. CTRL-J <LF> Begin new line. CTRL-K
{char1} {char2} Enter digraph (see 4.7). {not in Vi} CTRL-M <CR> Begin new line.
CTRL-N Find next keyword (see 4.3.7). {not in Vi} CTRL-O Execute one Command mode command.
See below. {not in Vi} CTRL-P Find previous keyword (see 4.3.7). {not in Vi} CTRL-R
<0-9a-z"%:> Insert the contents of a numbered or named register. The text is
inserted as if you typed it, but mappings and abbreviations are not used. If you have
options like 'textwidht' or 'autoindent' set, this will influence what will be inserted.
Use <"> for the unnamed register, containing the text of the last delete or
yank. Use <%> for the current file name. Use <:> for the last command line.
See the chapter on copying and moving text about registers. {<%>, <"> and
<:> not in Vi} CTRL-T Insert one shiftwidth of indent at the start of the current
line. See also 'shiftround' option. {Vi: only when in autoindent} CTRL-U Delete all
entered characters in the current line (see below). CTRL-V Insert next non-digit
literally. Up to three digits form the decimal value of a single byte. The non-digit and
the three digits are not considered for mapping. {Vi: no decimal byte entry} CTRL-W Delete
the word before the cursor (see below). See the section "word motions" for the
definition of a word. CTRL-Y Insert the character which is above the cursor. {not in Vi}
CTRL-[ or <ESC> End insert or Replace mode, back to Command mode. <DEL> Same
as CTRL-H <BS>
The effect of the <BS>, <DEL>, CTRL-W and CTRL-U depends on the 'backspace'
option (unless 'revins' is set):
backspace action
option
0 delete stops in column 1 and start position of insert 1 delete stops at start position
of insert 2 delete always, CTRL-W and CTRL-U stop once at start position of insert
If the 'backspace' option is non-zero and the cursor is in column 1 when one of the
three keys is used, the current line is joined with the previous line. This effectively
deletes the newline in front of the cursor. {Vi: does not cross lines, does not delete
past start position of insert}
With CTRL-V followed by one, two or three digits you can enter the decimal value of a
byte, except 10. Normally CTRL-V is followed by three digits. The formed byte is inserted
as soon as you type the third digit. If you type only one or two digits and then a
non-digit, the decimal value of those one or two digits form the byte. After that the
non-digit is dealt with in the normal way. If you enter a value of 10, it will end up in
the file as a 0. The 10 is a <LF>, which is used internally to represent the
<NUL> character. When writing the buffer to a file the <LF> character is
translated into <NUL>. The <LF> character is written at the end of each line.
Thus if you want to insert a <LF> character in a file you will have to make a line
break.
- special special keys
The following keys are special. They stop the current insert, do
something and then restart insertion. This means you can do something without getting out
of Insert mode. This is very handy if you prefer to use the Insert mode all the time, just
like editors that don't have a separate Command mode. You may also want to set the
'backspace' option to 2 and set the 'insertmode' option. You can use CTRL-O if you want to
map a function key to a command.
The changes (inserted or deleted characters) before and after these keys can be undone
separately. Only the last change can be redone and always behaves like an "i"
command.
char action
<C_UP> cursor one line up <C_DOWN> cursor one line down <C_LEFT>
cursor one character left <C_RIGHT> cursor one character right <SC_UP> move
window one page up <SC_DOWN> move window one page down <SC_LEFT> cursor one
word back (like "b" command) <SC_RIGHT> cursor one word forward (like
"w" command) CTRL-O execute one command and return to Insert mode
The CTRL-O command has one side effect: If the cursor was beyond the end of the line it
will be put on the last character in the line. The shifted cursor keys are not available
on all terminals.
When the 'whichwrap' option is set appropriately, the <C_LEFT> and
<C_RIGHT> keys on the first/last character in the line make the cursor wrap to the
previous/next line.
- 'textwidth' and 'wrapmargin' option
The 'textwidth' option can be used to
automatically break a line before it gets too long. Set the 'textwidth' option to the
desired maximum line length. If you then type more characters (not spaces or tabs), the
last word will be put on a new line (unless it is the only word on the line). If you set
'textwidth' to 0, this feature is disabled.
The 'wrapmargin' option does almost the same. The difference is that 'textwidth' has a
fixed width while 'wrapmargin' depends on the width of the screen. When using 'wrapmargin'
this is equal to using 'textwidth' with a value equal to (columns - 'wrapmargin'), where
columns is the width of the screen.
When 'textwidth' and 'wrapmargin' are both set, 'textwidth' is used.
The line is only broken automatically when using insert mode, or when appending to a
line. When in replace mode and the line length is not changed, the line will not be
broken.
If you want to format a block of text you can use the "Q" operator. Type
"Q" and a movement command to move the cursor to the end of the block. In many
cases the command "Q}" will do what you want (format until the end of
paragraph). Or you can use visual mode: hit "v", move to the end of the block
and hit "Q".
- 'expandtab' and 'smarttab' options
If the 'expandtab' option is set, spaces will be
used to fill the amount of whitespace of the tab. If you want to enter a real <TAB>
type CTRL-V first. The 'expandtab' option is default off. Note that in Replace mode a
single character is replaced by several spaces. The result of this is that the number of
characters in the line increases. Backspacing will delete one space at a time. The
original text will be put back in a place where you would not expect it. {not in Vi}
When the 'smarttab' option is set a TAB in front of a line inserts 'shiftwidth'
positions, 'tabstop' in other places. This means that often spaces instead of a TAB
character is inserted. When not set a TAB always inserts 'tabstop' positions, 'shiftwidth'
is only used for ">>" and the like. {not in Vi}
- typing backwards
If the 'revins' (reverse insert) option is set, inserting happens
backwards. This can be used to type Hebrew. When inserting characters the cursor is not
moved and the text moves rightwards. A <BS> deletes the character under the cursor.
CTRL-W and CTRL-U also work in the opposite direction. <BS>, CTRL-W and CTRL-U do
not stop at the start of insert or end of line, no matter how the 'backspace' option is
set.
In Replace mode the cursor is moved leftwards. <BS> will restore the character
right of the cursor.
In insert or Replace mode the 'revins' option can be toggled with CTRL-B.
If the 'showmode' option is set, "-- REVERSE INSERT --" or "-- REVERSE
REPLACE --" will be shown in the status line.
- Replace mode
In Replace mode one character in the line is deleted for every character
you type. If there is no character to delete (at the end of the line), the typed character
is appended (as in Insert mode). Thus the number of characters in a line stays the same
until you get to the end of the line.
Be careful with <TAB> characters. If you type a normal printing character in its
place, the number of characters is still the same, but the number of columns will become
smaller.
If you delete characters in Replace mode (with <BS>, <DEL>, CTRL-W or
CTRL-U), you really delete your changes. The characters that were replaced are restored.
If you had typed past the existing text, the characters you added are deleted.
All this only works in the current line. If you have started a new line (replaced a
character with a <CR>) Vim no longer remembers what happened in the previous line.
If you backspace over the newline (only possible if the 'backspace' option is non-zero),
the two lines will be joined again, but typing further backspaces will not restore the
original text. Only the cursor is moved.
If the 'expandtab' option is set, a <TAB> will replace one character with several
spaces. When backspacing over these spaces, the original text will appear in a position
where you would not expect it.
- Keyword completion
In insert and replace mode the keys CTRL-N and CTRL-P can be used
to complete the keyword that is in front of the cursor. This is useful if you are writing
a program that has complicated variable names, and you want to copy a name from the text
before of after the cursor.
If there is an identifier in front of the cursor (a name made out of alphanumeric
characters and <_>) it is used as the search pattern, with "\<"
prepended (meaning: start of a word). Otherwise "\<[a-zA-Z_]" is used as
search pattern (start of any identifier).
With CTRL-N (next) the search goes forward, with CTRL-P (previous) the search goes
backward. The first time the search starts where the cursor is. The next times the search
starts at the last found position. If you type any other character than CTRL-P or CTRL-N
the current text is accepted and the search pattern is forgotten.
If the search found a match, it is inserted at the cursor position. Any previous match
is replaced. If no match was found, Vim will beep.
If there is not a valid identifier character before the cursor, any identifier is
matched.
eg. to get:
printf("(%g, %g, %g)", vector[0], vector[1], vector[2]);
just type:
printf("(%g, %g, %g)", vector[0], ^P[1], ^P[2]);
Multiple repeats of the same completion are skipped.
If there is only one completion found, then a second CTRL-P or CTRL-N will give the
message 'No other matches'.
If the only match in the file is an exact match, where no extra characters would be
typed, then the message 'Exact match only' is given (this is also useful for checking that
you typed the symbol correctly).
The mode (--INSERT--) is shown, unless there is another more important message (eg
Pattern not found). This other message will stay until another key is hit, and then the
mode is shown again.
Only matches where something extra will be added are used.
eg. to get:
printf("name = %s\n", name);
just type:
printf("name = %s\n", n^P);
The 'n' in '\n' is skipped.
- Command_line mode
Command_line mode is used to enter Ex commands <:>, search
patterns </><?> and filter commands <!>.
- Command line editing
Normal characters are inserted in front of the cursor position.
You can move around in the command line with the left and right cursor keys. {Vi: can only
alter the last character in the line}
The command lines that you enter are remembered in a history table. You can recall them
with the up and down cursor keys. Use the 'history' option to set the number of lines that
are remembered (default 20).
There is an automatic completion of names on the command line, see 4.4.2.
CTRL-V Insert next non-digit literally. Up to three digits form the decimal value of a
single byte. The non-digit and the three digits are not considered for mapping. This works
the same way as in Insert mode (see above). <C_LEFT> cursor left
<C_RIGHT> cursor right
<SC_LEFT> cursor one word left
<SC_RIGHT> cursor one word right
CTRL-B cursor to begin of command line CTRL-E cursor to end of command line
<BS> delete the character in front of the cursor <DEL> delete the character
under the cursor (at end of line: character in front of the cursor) CTRL-W delete the word
in front of the cursor CTRL-U remove all characters
Note: if the command line becomes empty with one of the delete commands, command line
mode is quit.
{char1} <BS> {char2} or
CTRL-K {char1} {char2}
enter digraph (see 4.7). {not in Vi}
<CR> or <LF> start entered command
<ESC> when typed: quit command line without executing in macros: start entered
command CTRL-C quit command line without executing
<C_UP> recall older command line from history <C_DOWN> recall more recent
command line from history
<SC_UP> recall older command line from history, which begin matches the current
command line (see below). <SC_DOWN> recall more recent command line from history,
which begin matches the current command line (see below).
CTRL-D command line completion (see 4.4.2) 'wildchar' option
command line completion (see 4.4.2) CTRL-N command line completion (see 4.4.2) CTRL-P
command line completion (see 4.4.2) CTRL-A command line completion (see 4.4.2) CTRL-L
command line completion (see 4.4.2)
The <SC_UP> and <SC_DOWN> keys take the current command line as search
string. The beginning of the next/previous command lines are compared against this string.
The fist line that matches is the new command line. When typing these two keys repeatedly,
the same string is used again. For example this can be used to find the previous
substitute command: Type ":s" and then <SC_UP>. The same could be done by
typing <C_UP> a number of times until the desired command line is shown. (Note: the
shifted arrow keys do not work on all terminals)
- Command line completion
When editing the command line a few commands can be used to
complete the word before the cursor. This is available for:
- Command names, at the start of the command line. Works always.
- tags, only after the ":tag" command.
- file names, only after a command that accepts a file name or a setting for an option
that can be set to a file name. This is called file name completion.
- options, only after the ":set" command.
These are the commands that can be used:
CTRL-D List names that match the pattern in front of the cursor. When showing file
names, directories are highlighted (see 'highlight' option)
'wildchar' option
A match is done on the pattern in front of the cursor. The match, or if there are several,
the first match, is inserted in place of the pattern. (Note: does not work inside a macro,
because TAB or ESC is mostly used as 'wildchar', and these have a special meaning in some
macros) When typed again, and there were multiple matches, the next match is inserted.
After the last match the first is used again (wrap around).
CTRL-N After using 'wildchar' which got multiple matches: go to next match. Otherwise:
Recall more recent command line from history. CTRL-P After using 'wildchar' which got
multiple matches: go to previous match. Otherwise: Recall older command line from history.
CTRL-A All names that match the pattern in front of the cursor are inserted.
CTRL-L A match is done on the pattern in front of the cursor. If there is one match, it is
inserted in place of the pattern. If there are multiple matches the longest common part is
inserted in place of the pattern.
The 'wildchar' option defaults to <TAB> (CTRL-E when compiled with COMPATIBLE; in
a previous version <ESC> was used). In the pattern standard wildcards <*> and
<?> are accepted. <*> matches any string, <?> matches exactly one
character.
For filename completion you can use the 'suffixes' option to set a priority between
files with almost the same name. If there are multiple matches, those files with an
extension that is in the 'suffixes' option are ignored. The default is
".bak.o.h.info.swp", which means that files with the extensions
".bak", ".o", ".h", ".info" and ".swp"
are sometimes ignored. It is impossible to ignore suffixes with two dots. Examples:
pattern: files: match:
test* test.c test.h test.o test.c
test* test.h test.o test.h and test.o test* test.i test.h test.c test.i and test.c
If there is more than one matching file (after ignoring the ones matching the
'suffixes' option) the first file name is inserted. You can see that there is only one
match when you type 'wildchar' twice and the completed match stays the same. You can get
to the other matches by entering 'wildchar', CTRL-N or CTRL-P. All files are included,
also the ones with extensions matching the 'suffixes' option.
- Ex command lines
The Ex commands have a few specialties:
<"> at the start of a line causes the whole line to be ignored.
<"> after a command causes the rest of the line to be ignored. This can be used
to add comments. Example:
:set ai "set 'autoindent' option It is not possible to add a comment to a shell
command ":!cmd" or to the ":map" command and friends, because they see
the <"> as part of their argument.
<|> can be used to separate commands, so you can give multiple commands in one
line. The commands ":global", "vglobal" and ":!" see the
<|> as their argument, and can therefore not be followed by another command. If you
want <|> to be included in a command, precede it with <\>. Note that this is
confusing (inherited from vi). If you give a command with ":!" you don't have to
use a backslash, with ":r !" you have to. And with ":g" the <|>
is included in the command, with ":s" it is not. Examples: :!ls | wc view the
output of two commands :r !ls \| wc insert the same output in the text :%g/foo/p|>
moves all matching lines one shiftwidth :%s/foo/bar/|> moves one line one shiftwidth
You can also use <LF> to separate commands in the same way as with <|>. But
using <|> is the preferred method.
When the character <%> or <#> is used where a filename is expected, they
are expanded to the current and alternate filename (see the chapter "editing
files").
Embedded spaces in filenames are allowed if one filename is expected as argument.
Trailing spaces will be ignored, unless escaped with a backslash or CTRL-V. Note that the
":next" command uses spaces to separate file names. Escape the spaces to include
them in a file name. Example: :next foo\ bar goes\ to School starts editing the three
files "foo bar", "goes to" and "School quot;.
When you want to use the special characters <"> or <|> in a command,
or want to use <%> or <#> in a filename, precede them with a backslash. The
backslash is not required in a range and in the ":substitute" command.
- Ex command line ranges
Some Ex commands accept a line range in front of them. This is
noted as [range]. It consists of one or more line specifiers, separated with <,> or
<;>. When separated with <;> the cursor position will be set to that line
before interpreting the next line specifier. The default line specifier for most commands
is the cursor position, but the commands ":write" and ":global" have
the whole file (1,$) as default. If more line specifiers are given than required for the
command, the first one(s) will be ignored.
Line numbers may be specified with:
{number} an absolute line number . the current line
$ the last line in the file
% equal to 1,$ (the entire file) 't position of mark t (lower case) /{pattern}[/] the next
line where {pattern} matches ?{pattern}[?] the previous line where {pattern} matches
Each may be followed (several times) by <+> or <-> and an optional number.
This number is added or subtracted from the preceding line number. If the number is
omitted, 1 is used.
The "/" and "?" may be preceded with another address. The search
starts from there. The "/" and "?" after {pattern} are required to
separate the pattern from anything that follows.
The {number} must be between 0 and the number of lines in the file. A 0 is interpreted
as a 1, except with the commands tag, pop and read.
Examples:
.+3 three lines below the cursor /that/+1 the line below the next line containing
"that" .,$ from current line until end of file 0/that the first line containing
"that"
Some commands allow for a count after the command. This count is used as the number of
lines to be used, starting with the line given in the last line specifier (the default is
the cursor line). The commands that accept a count are the ones that use a range but do
not have a file name argument (because a file name can also be a number).
Examples:
:s/x/X/g 5 substitute <x> by <X> in the current line and four following lines
:23d 4 delete lines 23, 24, 25 and 26
A range should have the lower line number first. If this is not the case, Vim will ask
you if it should swap the line numbers. This is not done within the global command
":g".
When giving a count before entering ":", this is translated into: :.,.+(count
- 1)
In words: The 'count' lines at and after the cursor. Example: To delete three lines:
3:d<CR> is translated into: .,.+2d<CR>
- The window contents
In command and Insert/Replace mode the screen window will show
the current contents of the buffer: What You See Is What You Get. {Vi: when changing text
a <$> is placed on the last changed character; The window is not always updated on
slow terminals} Lines longer than the window width will wrap, unless the 'wrap' option is
off (see below). The bottom lines in the window may start with one of these two
characters:
<@> The next line is too long to fit in the window. <~> Below the last line
in the buffer.
If the bottom line is completely filled with <@>, the line that is at the top of
the window is too long to fit in the window. If the cursor is on this line you can't see
what you are doing, because this part of the line is not shown. However, the part of the
line before the <@>s can be edited normally. {Vi: gives an "internal
error" on lines that do not fit in the window}
If the 'wrap' option is off, long lines will not wrap. Only the part that fits on the
screen is shown. If the cursor is moved to a part of the line that is not shown, the
screen is scrolled horizontally. The advantage of this method is that columns are shown as
they are and lines that cannot fit on the screen can be edited. The disadvantage is that
you cannot see all the characters of a line at once. The 'sidescroll' option can be set to
the minimal number of columns to scroll. {Vi: has no 'wrap' option}
All normal ASCII characters are displayed directly on the screen. The <TAB> is
replaced by the number of spaces that it represents. Other non-printing characters are
replaced by "^<char>", where <char> is the non-printing character
with 64 added. Thus character 7 (bell) will be shown as "^G". Characters between
127 and 160 are replaced by "~<char>", where <char> is the character
with 64 subtracted. These characters occupy more than one position on the screen. The
cursor can only be positioned on the first one.
If you set the 'number' option, all lines will be preceded with their number.
If you set the 'list' option, <TAB> characters will not be shown as several
spaces, but as "^I". A <$> will be placed at the end of the line, so you
can find trailing blanks.
In Command_line mode only the command line itself is shown correctly. The display of
the buffer contents is updated as soon as you go back to Command mode.
Some commands hand over the window to external commands (e.g. ":shell" and
"="). After these commands are finished the window may be clobbered with output
from the external command, so it needs to be redrawn. This is also the case if something
is displayed on the status line that is longer than the width of the window. If you are
expected to have a look at the screen before it is redrawn, you get this message:
Press RETURN or enter command to continue
After you type a key the screen will be redrawn and Vim continues. If you type
<CR>, <SP> or <LF> nothing else happens. If you type any other key, it
will be interpreted as (the start of) a new command. {Vi: only <:> commands are
interpreted}
The last line of the window is used for status and other messages. The status messages
will only be used if an option is on:
status message option default unix default
current mode 'showmode' on on
command characters 'showcmd' on off
cursor position 'ruler' off off
The current mode is "-- INSERT --" or "-- REPLACE --". The command
characters are those that you typed but were not used yet. {Vi: does not show the
characters you typed or the cursor position}
If you have a slow terminal you can switch off the status messages to speed up editing:
:set nosc noru nosm
If there is an error, an error message will be shown for at least one second (in
reverse video). {Vi: error messages may be overwritten with other messages before you have
a chance to read them}
Some commands show how many lines were affected. Above which threshold this happens can
be controlled with the 'report' option (default 2).
On the Amiga Vim will run in a CLI window. The name Vim and the full name of the
current filename will be shown in the title bar. When the window is resized, Vim will
automatically redraw the window. You may make the window as small as you like, but if it
gets too small not a single line will fit in it. Make it at least 40 characters wide to be
able to read most messages on the last line.
On most Unix systems window resize works ok. {Vi: not ok}
- Abbreviations
Abbreviations are used in insert mode, Replace mode and Command_line
mode. If you enter a word that is an abbreviation, it is replaced by the word it stands
for. This can be used to save typing for often used long words.
There are two types of abbreviations: The "full-id" type consists entirely of
id characters (letters, digits and <_> characters). This is the most common
abbreviation. The "non-id" type ends in an id character, but all the other
characters are not id characters. Examples of a "full-id" type are
"foo" and "c_1". Examples of a "non-id" type are
"#i" and "$/7". Examples of strings that will not be recognized as an
abbreviation are "a.b", "a b" and "_$ar".
The "full-id" abbreviation is recognized if:
- A character is typed that is not an id character. This can also be the <ESC> that
ends insert mode or the <CR> that ends a command.
- The characters in front of the cursor match the abbreviation.
- In front of the match is a non-id character, or this is where the line or insertion
starts. Exception: when the abbreviation is only one character, it is not recognized if
there is a non-id character in front of it, other than a space or a TAB.
The "non-id" abbreviation is recognized if:
- A character is typed that is not an id character. This can also be the <ESC> that
ends insert mode or the <CR> that ends a command.
- The characters in front of the cursor match the abbreviation.
- In front of the match is an id character, or a space or a TAB, or this is where the line
or insertion starts.
Example: ":ab foo four old otters". Note that spaces in the <rhs> are
allowed and included in the replacement string. If you now insert the word "foo"
with a space before and after it, it will be replaced by "four old otters". If
you would type "foobar" or "barfoo" nothing happens.
To avoid the abbreviation in insert mode type part of the abbreviation, exit insert
mode with <ESC>, re-enter insert mode with "a" and type the rest. In
Command_line mode you can type CTRL-V twice somewhere in the abbreviation to avoid it to
be replaced. A CTRL-V in front of a normal character is mostly ignored otherwise.
There are no default abbreviations.
Abbreviations are never recursive. You can use ":ab f f-o-o" without any
problem. But abbreviations can be mapped. {some versions of vi support recursive
abbreviations, for no apparent reason}
Abbreviations are disabled if the 'paste' option is set.
:ab[breviate] list all abbreviations. The character in the first column indicates the
mode where the abbreviation is used: 'i' for insert mode, 'c' for Command_line mode, '!'
for both.
:ab[breviate] <lhs> list the abbreviations that start with <lhs>
:ab[breviate] <lhs> <rhs>
add abbreviation for <lhs> to <rhs>. If <lhs> already existed it is
replaced with the new <rhs>. <rhs> may contain spaces.
:una[bbreviate] <lhs> remove abbreviation for <lhs> from the list
:norea[bbrev] [lhs] [rhs]
same as ":ab", but no remapping for this <rhs> {not in Vi}
:ca[bbrev] [lhs] [rhs] same as ":ab", but for Command_line mode only. {not in
Vi}
:cuna[bbrev] <lhs> same as ":una", but for Command_line mode only. {not
in Vi}
:cnorea[bbrev] [lhs] [rhs]
same as ":ab", but for Command_line mode only and no remapping for this
<rhs> {not in Vi}
:ia[bbrev] [lhs] [rhs] same as ":ab", but for insert mode only. {not in Vi}
:iuna[bbrev] <lhs> same as ":una", but for insert mode only. {not in
Vi}
:inorea[bbrev] [lhs] [rhs]
same as ":ab", but for insert mode only and no remapping for this <rhs>
{not in Vi}
- Digraphs
:dig[raphs] show currently defined digraphs. {not in Vi}
:dig[raphs] {char1}{char2} {number} ... Add digraph {char1}{char2} to the list.
{number} is the decimal representation of the character.
Digraphs are used to enter characters that normally cannot be entered by an ordinary
keyboard. These are mostly accented characters which have the eighth bit set. The digraphs
are easier to remember than the decimal number that can be entered with CTRL-V (see
above).
Vim must have been compiled with the 'digraphs' option enabled. If not, the
":digraph" command will display an error message.
There are two methods to enter digraphs: CTRL-K {char1} {char2} or
{char1} <BS> {char2}
The first is always available. The second only when the 'digraph' option is set.
Once you have entered the digraph the character is treated like a normal character,
taking up only one character in the file and on the screen. Example:
<|> <BS> <|> will enter the double <|> character (166) <a>
<BS> <^> will enter an <a> with a hat (226) CTRL-K <-> <->
will enter a minus sign (173)
The default digraphs are listed in the file digraph.doc. They are meant for the Amiga
character set, which is some international standard. With another character set they may
be illogical.
For CTRL-K there is one general digraph: CTRL-K <SPACE> {char} will enter {char}
with the highest bit set. This can be used to enter meta-characters.
The <ESC> character cannot be part of a digraph. When hitting <ESC>
entering the digraph is aborted and insert mode too.
If you accidently typed an <a> that should be an <e>, you will type
<a> <BS> <e>. But that is a digraph, so you will not get what you want.
To avoid this, use <DEL> instead of <BS>. Or don't set the 'digraph' option
and use CTRL-K to enter digraphs.
- Editing files
- Introduction
Editing a file with Vim means:
- reading the file into the internal buffer
- changing the buffer with editor commands
- writing the buffer into a file
As long as you don't write the buffer, the original file remains unchanged. If you
start editing a file (read a file into the buffer), the file name is remembered as the
"current filename".
If there already was a current filename, then that one becomes the alternate file name.
All filenames are remembered in the file list. When you enter a filename, for editing
(e.g. with ":e filename") or writing (e.g. with (:w filename"), the
filename is added to the list. You can use this list to remember which files you edited
and to quickly switch from one file to another with the CTRL-^ command (e.g. to copy
text). First type the number of the file and then hit CTRL-^. {Vi: only one alternate
filename}
In Ex commands (the ones that start with a colon) <%> is replaced by the current
filename and <#> is replaced by the alternate filename. The older alternate
filenames are "#1", "#2", etc. "#0" is the same as
"#". If a "<" is appended to <%>, <#> or "#n"
the extension of the file name is removed (everything after and including the last '.' in
the file name).
% current file name
%< current file name without extension # alternate file name for current window #<
idem, without extension
#31 alternate file number 31
#31< idem, without extension
CTRL-G or
:f[ile] Prints the current filename (as typed) and the cursor position. {vi does not
include column number}
{count}CTRL-G Prints the current filename with full path and the cursor position.
:f[ile] {name} Sets the current filename to {name}.
:buffers
:files List all the currently known file names. See 'windows.doc'. {not in vi}
Vim will remember the full path name of a file name that you enter. In most cases when
the file name is displayed only the name you typed is shown, but the full path name is
being used if you used the ":cd" command.
If the environment variable 'HOME' is set, and the file name starts with that string,
it is often displayed with HOME replaced by "~". This was done to keep file
names short. When reading or writing files the full name is still used, the "~"
is only used when displaying file names.
When writing the buffer, the default is to use the current filename. Thus when you give
the "ZZ" or ":wq" command, the original file will be overwritten. If
you do not want this, the buffer can be written into another file by giving a filename
argument to the ":write" command. For example:
vim testfile
[change the buffer with editor commands] :w newfile
:q
This will create a file "newfile", that is a modified copy of
"testfile". The file "testfile" will remain unchanged. Anyway, if the
'backup' option is set, Vim renames the original file before it will be overwritten. You
can use this file if you discover that you need the original file. See also the
'patchmode' option. The name of the backup file is the same as the original file with
".bak" appended. Any <.> is replaced by <_> on MSDOS machines, when
Vim has detected that an MSDOS-like filesystem is being used (e.g. messydos or crossdos)
and when the 'shortname' option is set.
Technical: On the Amiga you can use 30 characters for a file name. But on an
MSDOS-compatible filesystem only 8 plus 3 characters are available. Vim tries to detect
the type of filesystem when it is creating the .swp file. If an MSDOS-like filesystem is
suspected, a flag is set that has the same effect as setting the 'shortname' option. This
flag will be reset as soon as you start editing a new file. The flag will be used when
making the filename for the ".swp" and ".bak" files for the current
file. But when you are editing a file in a normal filesystem and write to an MSDOS-like
filesystem the flag will not have been set. In that case the creation of the
".bak" file may fail and you will get an error message. Use the 'shortname'
option in this case.
When you started editing without giving a file name, "No File" is displayed
in messages. If a ":write file" or ":read file" command is used, the
file name for the current file is set to the file name in that command. This is useful
when starting Vim without an argument and then doing ":read file" to start
editing a file. Or when entering text in an empty buffer and then writing it to a file.
Because the file name was set without really starting to edit that file, you are protected
from overwriting that file. This is done by setting the "notedited" flag. You
can see if this flag is set with the CTRL-G or ":file" command. It will include
"[Not edited]" when the "notedited" flag is set. When writing the
buffer to the current file name (with ":w!"), the "notedited" flag is
reset.
Vim remembers whether you have changed the buffer. You are protected from losing the
changes you made. If you try to quit without writing, or want to start editing another
file, this will be refused. In order to overrule this protection add a <!> to the
command. The changes will then be lost. For example: ":q" will not work if the
buffer was changed, but ":q!" will. To see whether the buffer was changed use
the "CTRL-G" command. The message includes the string "[Modified]" if
the buffer has been changed.
- Editing a file
:e[dit] [+cmd] Edit the current file, unless changes have been made.
:e[dit]! [+cmd] Edit the current file always. Discard any changes to
the buffer.
:e[dit] [+cmd] {file} Edit {file}, unless changes have been made.
:e[dit]! [+cmd] {file} Edit {file} always. Discard any changes to the
buffer.
:e[dit] #[count] Edit the [count]th alternate filename (as shown by
:files). This command does the same as [count] CTRL-^.
:ex [+cmd] [file] Same as :edit. {Vi: go from visual to Ex mode}
:vi[sual] [+cmd] [file] Same as :edit. {Vi: go from Ex to Visual mode}
[count]CTRL-^ Edit [count]th alternate file (equivalent to ":e
#[count]"). Without count this gets you to the
previously edited file. This is a quick way to
toggle between two (or more) files. If the
'autowrite' option is set and the buffer was
changed, write it.
]f
[f
gf Edit the file whose name is under or after the
cursor. Mnemonic: "goto file". This fails if the
current file cannot be abandoned. {not in Vi}
:cd On non-Unix systems: Print the current directory
name. On Unix systems: Change the current directory
to the home directory.
:cd {path} Change the current directory to {path}. Does not
change the meaning of an already entered file name,
because its full path name is remembered.
:chd[ir] [path] Same as :cd.
:pwd Print the current directory name. {Vi: no pwd}
- These commands are used to start editing a single file. This means that the file is read
into the buffer and the current filename is set. You may use the ":cd" command
to get to another directory, so you will not have to type that directory name in front of
the filenames. One warning: After using ":cd" the full path name will be used
for reading and writing files. On some networked file systems this may cause problems. The
result of using the full path name is that the file names currently in use will remain
referring to the same file. Example: If you have a file a:test and a directory a:vim the
commands ":e test" ":cd vim" ":w" will overwrite the file
a:test and not write a:vim/test. But if you do ":w test" the file a:vim/test
will be written, because you gave a new file name and did not refer to a file name before
the ":cd".
You can use the ":e!" command if you messed up the
buffer and want to start all over again. The ":e" command is only useful if you
have changed the current filename.
The [+cmd] can be used to position the cursor in the newly opened file: + Start at the
last line.
+{num} Start at line {num}.
+/{pat} Start at first line containing {pat}. {pat} must not contain any spaces.
+{command} Execute {command} after opening the new file. {command} is an Ex command. It
must not contain spaces.
When reading a file when the 'textmode' option is off (default for non-MSDOS) the
<LF> character is interpreted as end-of-line. If 'textmode' is on (default for
MSDOS), <CR><LF> is also interpreted as end-of-line.
When writing a file when the 'textmode' option is off a <LF> character is used to
separate lines. When the 'textmode' option is on <CR><LF> is used.
You can read a file with 'textmode' set and write it with 'textmode' reset. This will
replace all <CR><LF> pairs by <LF>. If you read a file with 'textmode'
reset and write with 'textmode' set, all <LF> characters will be replaced by
<CR><LF>.
If you start editing a new file and the 'textauto' option is set, Vim will try to
detect whether the lines in the file are separated by a single <LF> (as used on Unix
and Amiga) or by a <CR><LF> pair (MSDOS). It reads up to the first <LF>
and checks if there is a <CR> in front of it. If there is the 'textmode' option is
set, otherwise it is reset. If the 'textmode' option is set on non-MSDOS systems the
message "[textmode]" is shown to remind you that something unusual is happening.
On MSDOS systems you get the message "[notextmode]" if the 'textmode' option is
not set.
Before editing binary, executable or Vim script files you should set the 'textmode' and
'textauto' options off. With 'textmode' on you risk that single <LF> characters are
unexpectedly replaced with <CR><LF>. A simple way to do this is by starting
Vim with the "-b" option.
- The argument list
If you give more than one filename when starting Vim, this list is
remembered as the argument list. Do not confuse this with the file list, which you can see
with the ":files" command. The argument list was already present in vi, the file
list is new in Vim. The file names in the argument list will also be present in the file
list (unless they were deleted with ":bdel").
You can use the argument list with the following commands:
:ar[gs] Print the argument list, with the current file in square brackets.
:[count]argu[ment] [count] [+cmd]
Edit file [count] in the argument list, unless changes have been made and the 'autowrite'
option is off. {Vi: no such command}
:[count]argu[ment]! [count] [+cmd]
Edit file [count] in the argument list, discard any changes to the current buffer. {Vi: no
such command}
:[count]n[ext] [+cmd] Edit [count] next file, unless changes have been made and the
'autowrite' option is off {Vi: no count}.
:[count]n[ext]! [+cmd] Edit [count] next file, discard any changes to the buffer {Vi:
no count}.
:ar[gs] [+cmd] {filelist}
:n[ext] [+cmd] {filelist}
Define {filelist} as the new argument list and edit the first one, unless changes have
been made and the 'autowrite' option is off.
:ar[gs]! [+cmd] {filelist}
:n[ext]! [+cmd] {filelist}
Define {filelist} as the new argument list and edit the first one. Discard any changes to
the buffer.
:[count]N[ext] [count] [+cmd]
Edit [count] previous file in argument list, unless changes have been made and the
'autowrite' option is off {Vi: no count}.
:[count]N[ext]! [count] [+cmd]
Edit [count] previous file in argument list. Discard any changes to the buffer {Vi: no
count}.
:[count]pre[vious] [count] [+cmd]
Same as :Next {Vi: only in some versions}
:rew[ind] [+cmd] Start editing the first file in the argument list, unless changes have
been made and the 'autowrite' option is off.
:rew[ind]! [+cmd] Start editing the first file in the argument list. Discard any
changes to the buffer.
:la[st] [+cmd] Start editing the last file in the argument list, unless changes have
been made and the 'autowrite' option is off. {not in Vi}
:la[st]! [+cmd] Start editing the last file in the argument list. Discard any changes
to the buffer. {not in Vi}
:[count]wn[ext] [+cmd] Write current file and start editing the [count] next file. {not
in Vi}
:[count]wn[ext] [+cmd] {file}
Write current file to {file} and start editing the [count] next file, unless {file}
already exists and the 'writeany' option is off. {not in Vi}
:[count]wn[ext]! [+cmd] {file}
Write current file to {file} and start editing the [count] next file. {not in Vi}
:[count]wN[ext][!] [+cmd] [file]
:[count]wp[revous][!] [+cmd] [file]
Same as :wnext, but go to previous file instead of next. {not in Vi}
The [count] in the commands above defaults to one. For some commands it is possible to
use two counts. The last one (rightmost one) is used.
For [+cmd] see 5.2.
The wildcards in the argument list are expanded and the filenames are sorted. Thus you
can use the command "vim *.c" to edit all the C files. From within Vim the
command ":n *.c" does the same.
You are protected from leaving Vim if you are not editing the last file in the argument
list. This prevents you from forgetting that you were editing one out of several files. To
exit anyway try to exit twice. If there are changes in the current buffer this will fail.
You can exit anyway, and save any changes, with the ":wq!" command. To lose any
changes use the ":q!" command.
- Writing and quitting
:[range]w[rite][!] Write the specified lines to the current
file.
:[range]w[rite] {file} Write the specified lines to {file}, unless it already exists
and the 'writeany' option is off.
:[range]w[rite]! {file} Write the specified lines to {file}. Overwrite an existing
file.
:[range]w[rite][!] >> Append the specified lines to the current file.
:[range]w[rite][!] >> {file}
Append the specified lines to {file}. <!> forces the write even if file does not
exist.
:[range]w[rite] !{cmd} Execute {cmd} with [range] lines as standard input (note the
space in front of the <!>).
The default [range] for the ":w" command is the whole buffer (1,$). If a file
name is give with ":w" it becomes the alternate file. This can be used when the
write fails and you want to try again later with ":w #".
:q[uit] Quit, unless changes have been made or not editing the last file in the
argument list.
:q[uit]! Quit always, without writing.
:cq Quit always, without writing, and return an error code. Used for Manx's QuickFix
mode (see 5.5).
:wq Write the current file. Exit if not editing the last file in the argument list.
:wq! Write the current file and exit.
:wq {file} Write to {file}. Exit if not editing the last file in the argument list.
:wq! {file} Write to {file} and exit.
:x[it][!] [file] Like ":wq", but write only when changes have been made.
:exi[t][!] [file] Same as :xit.
ZZ Write current file, if modified, and exit (same as ":x").
If you write to an existing file (but do not append) while the 'backup' or
'writebackup' option is on, a backup of the original file is made. On Unix systems the
file is copied, on other systems the file is renamed. After the file has been successfully
written and when the 'writebackup' option is on and the 'backup' option is off, the backup
file is deleted.
'backup' 'writebackup' action
off off no backup made
off on backup made, deleted afterwards
on off backup made, not deleted
on on backup made, not deleted (default)
On Unix systems:
When you write to an existing file, that file is truncated and then filled with the new
text. This means that protection bits, owner and symbolic links are unmodified. The backup
file however, is a new file, owned by the user who edited the file. If it is not possible
to create the backup file in the same directory as the original file, the directory given
with the 'backupdir' option is used (default: home directory).
If the creation of a backup file fails, the write is not done. If you want to write
anyway add a <!> to the command.
If the 'textmode' option is set <CR><LF> is used for end-of-line. This is
default for MSDOS. On other systems the message "[textmode]" is shown to remind
you that an usual end-of-line marker was used. If the 'textmode' is not set LF is used for
end-of-line. On MSDOS the message "[notextmode]" is shown. See also the
'textmode' and 'textauto' options.
- Using the QuickFix mode
Vim has a special mode to speedup the edit-compile-edit
cycle. This is inspired by the quickfix option of the Manx's Aztec C compiler on the
Amiga. The idea is to save the error messages from the compiler in a file and use Vim to
jump to the errors one by one. You can then examine each problem and fix it, without
having to remember all the error messages.
If you are using Manx's Aztec C compiler on the Amiga you should do the following:
- Set the CCEDIT environment variable with the command mset "CCEDIT=vim -e"
- Compile with the -qf option. If the compiler finds any errors, Vim is started and the
cursor is positioned on the first error. The error message will be displayed on the last
line. You can go to other errors with the commands mentioned below. You can fix the errors
and write the file(s).
- If you exit Vim normally the compiler will re-compile the same file. If you exit with
the :cq command, the compiler will terminate. Do this if you cannot fix the error, or if
another file needs to be compiled first.
If you are using another compiler you should save the error messages in a file and
start Vim with "vim -e filename". An easy way to do this is with the
":make" command (see below). The 'errorformat' option should be set to match the
error messages from your compiler (see below).
The following commands can be used if you are in QuickFix mode:
:cc [nr] Display error [nr]. If [nr] is omitted, the same
error is displayed again. {not in Vi}
:[count]cn Display the [count] next error in the list that
includes a file name. If there are no file names at
all, go the the [count] next error. {not in Vi}
:[count]cp Display the [count] previous error in the list that
includes a file name. If there are no file names at
all, go the the [count] previous error. {not in Vi}
:cq Quit Vim with an error code, so that the compiler
will not compile the same file again. {not in Vi}
:cf [errorfile] Read the error file and jump to the first error.
This is done automatically when Vim is started with
the -e option. You can use this command when you
keep Vim running while compiling. If you give the
name of the errorfile, the 'errorfile' option will
be set to [errorfile] {not in Vi}
:cl List all errors. {not in Vi}
:make [arguments] 1. If the 'autowrite' option is set and the buffer
was changed, write it.
2. Any existing 'errorfile' is deleted.
3. The program given with the 'makeprg' option is
started (default "make") with the optional
[arguments] and the output is saved in
'errorfile' (for Unix it is also echoed on the
screen).
4. The 'errorfile' is then read and the first error
is jumped to.
5. The 'errorfile' is deleted.
{not in Vi}
The name of the file can be set with the 'errorfile' option. The default is
"AztecC.Err" for the Amiga and "errors" for other systems. The format
of the file from the Aztec compiler is:
filename>linenumber:columnnumber:errortype:errornumber:errormessage
filename name of the file in which the error was detected
linenumber line number where the error was detected
columnnumber column number where the error was detected
errortype type of the error, normally a single <E> or <W>
errornumber number of the error (for lookup in the manual)
errormessage description of the error
Another compiler is likely to use a different format. You should set the 'errorformat'
option to a scanf-like string that describes the format. First you need to know how scanf
works. Look in the documentation of your C compiler. Vim will understand eight conversion
characters. Others are invalid.
%f file name (finds a string)
%l line number (finds a number)
%c column number (finds a number)
%t error type (finds a single character)
%n error number (finds a number)
%m error message (finds a string)
%*<conv> any scanf non-assignable conversion
%% the single <%> character
Examples:
"%f>%l:%c:%t:%n:%m" for the AztecC.Err file
"%f:%l:\ %t%*[^0123456789]%n:\ %m" for Aztec C error messages
"%f\ %l\ %t%*[^0123456789]%n:\ %m" for SAS C
"\"%f\",%*[^0123456789]%l:\ %m" default for generic C compilers
"%f:%l:%m" for GCC
Note the backslash in front of a space and double quote. It is required for the :set
command.
The "%f" and "%m" conversions have to detect the end of the string.
They should be followed by a character that cannot be in the string. Everything up to that
character is included in the string. Be careful: "%f%l" will include everything
up to the first <%> in the file name. If the "%f" or "%m" is at
the end, everything up to the end of the line is included.
If a line is detected that does not completely match the 'errorformat', the whole line
is put in the error message and the entry is marked "not valid" These lines are
skipped with the ":cn" and ":cp" commands (unless there is no valid
line at all). You can use ":cl" to display all the error messages.
If the error format does not contain a file name Vim cannot switch to the correct file.
You will have to do this by hand.
If you have a compiler that produces error messages that do not fit in the format
string, you could write a program that translates the error messages into this format. You
can use this program with the ":make" command by changing the 'makeprg' option.
For example: ":set mp=make\ \\\|&\ error_filter". The backslashes before the
pipe character are required to avoid it to be recognized as a command separator. The
backslash before each space is required for the set command.
The ":make" command executes the command given with the 'makeprg' option.
This is done by passing the command to the shell given with the 'shell' option. This works
almost like typing
":!{makeprg} [arguments] {shellpipe} {errorfile}".
{makeprg} is the string given with the 'makeprg' option. Any command can be used, not
just "make". Characters <%> and <#> are expanded as usual on a
command line. You can use "#<" to insert the current filename without
extension, for example ":set makeprg=make\ #<.o".
[arguments] is anything that is typed after ":make".
{shellpipe} is the 'shellpipe' option.
{errorfile} is the 'errorfile' option.
The 'shellpipe' option defaults to ">" for the Amiga and MSDOS. This means
that the output of the compiler is saved in a file and not shown on the screen directly.
For Unix "| tee" is used. The compiler output is shown on the screen and saved
in a file the same time. Depending on the shell used "|& tee" or
"2>&1| tee" is the default, so stderr output will be included.
There are some restrictions to the Quickfix mode on the Amiga. The compiler only writes
the first 25 errors to the errorfile (Manx's documentation does not say how to get more).
If you want to find the others, you will have to fix a few errors and exit the editor.
After recompiling, up to 25 remaining errors will be found.
On the Amiga, if Vim was started from the compiler, the :sh and :! commands will not
work, because Vim is then running in the same process as the compiler and these two
commands may guru the machine then.
If you insert or delete lines, mostly the correct error location is still found because
hidden marks are used (Manx's Z editor does not do this). Sometimes, when the mark has
been deleted for some reason, the message "line changed" is shown to warn you
that the error location may not be correct. If you quit Vim and start again the marks are
lost and the error locations may not be correct anymore.
- Editing binary files
Although Vim was made to edit text files, it is possible to edit
binary files. The "-b" command line option (b for binary) sets some options for
editing binary files ('binary' on, 'textwidth' to 0, 'textmode' and 'textauto' off,
'modelines' to 0, 'expandtab' off). Setting the 'binary' option has the same effect. Don't
forget to do this before reading the file.
There are a few things to remember when editing binary files:
- When editing executable files the number of characters must not change. Use only the
"R" or "r" command to change text. Do not delete characters with
"x" or by backspacing.
- Set the 'textwidth' option to 0. Otherwise lines will unexpectedly be split in two.
- When there are not many end-of-line characters, the lines will become very long. If you
want to edit a line that does not fit on the screen reset the 'wrap' option. Horizontal
scrolling is used then. If a line becomes too long (more than about 32767 characters on
the Amiga, much more on 32-bit systems) you cannot edit that line. The line will be split
when reading the file. It is also possible that you get an "out of memory" error
when reading the file.
- Make sure the 'textmode' and 'textauto' options are off before loading the file. In
'textmode' both <CR><LF> and <LF> are considered to end a line and when
the file is written the <LF> will be replaced by <CR><LF>. The
'modelines' option should also be off, because there may be a string like ":vi:"
in the file that would give unpredictable results.
- <NUL> characters are shown on the screen as ^@. You can enter them with
"CTRL-V CTRL-@" or "CTRL-V 000" {vi cannot handle <NUL>
characters in the file}
- To insert a <LF> character in the file split up a line. When writing the buffer to
a file a <LF> will be written for the end of line.
- Vim normally appends an end-of-line character at the end of the file if there is none.
Setting the 'binary' option prevents this. If you want to add the final end-of-line, set
the 'endofline' option. You can also read the value of this option to see if there was an
end-of-line character for the last line (you cannot see this in the text).
- Cursor motions
These commands move the cursor position. If the new position is off of
the screen, the screen is scrolled to show the cursor (see also 'scrolljump' option).
The motion commands can be used after other commands, called operators, to have the
command operate on the text that was moved over. That is the text between the cursor
position before and after the motion. If the motion includes a count and the operator also
had a count, the two counts are multiplied. For example: "2d3w" deletes six
words. The operator either affects whole lines, or the characters between the start and
end position. Generally, motions that move between lines affect lines (are linewise), and
motions that move within a line affect characters. However, there are some exceptions. A
character motion is either inclusive or exclusive. When inclusive, the start and end
position of the motion are included in the operation. When exclusive, the last character
towards the end of the buffer is not included. Linewise motions always include the start
and end position. Which motions are linewise, inclusive or exclusive is mentioned below.
There are however, two general exceptions:
- If the motion is exclusive and the end of the motion is in column 1, the end of the
motion is moved to the end of the previous line and the motion becomes inclusive. Example:
"}" ends at the first line after a paragraph, but "V}" will not
include that line.
- If the motion is exclusive, the end of the motion is in column 1 and the start of the
motion was at or before the first non-blank in the line, the motion becomes linewise.
Example: If a paragraph begins with some blanks and you do "d}" while standing
on the first non-blank, all the lines of the paragraph are deleted, including the blanks.
If you do a put now, the deleted lines will be inserted below the cursor position.
Instead of first giving the operator and then a motion you can use Visual mode: mark
the start of the text with <v>, move the cursor to the end of the text that is to be
affected and then hit the operator. The text between the start and the cursor position is
highlighted, so you can see what text will be operated upon. This allows much more
freedom, but requires more key strokes and has limited redo functionality. See the chapter
on Visual mode.
If you want to know where you are in the file use the "CTRL-G" command. If
you set the 'ruler' option, the cursor position is continuously shown in the status line
(which slows down Vim a little).
NOTE: Experienced users prefer the hjkl keys because they are always right under their
fingers. Beginners often prefer the arrow keys, because they do not know what the hjkl
keys do. The mnemonic value of hjkl is clear from looking at the keyboard. Think of j as
an arrow pointing downwards.
- Left-right motions
h or
<C_LEFT> or
CTRL-H or
<BS> [count] characters to the left (exclusive).
l or
<C_RIGHT> or
<SPACE> [count] characters to the right (exclusive).
0 To the first character of the line (exclusive).
^ To the first non-blank character of the line
(exclusive).
$ To the end of line [count] from the cursor
(inclusive).
| To column [count] (inclusive).
f<char> To [count]'th occurrence of <char> to the right. The
cursor is placed on <char> (inclusive).
F<char> To the [count]'th occurrence of <char> to the left.
The cursor is placed on <char> (inclusive).
t<char> Till before [count]'th occurrence of <char> to the
right. The cursor is placed on the character left of
<char> (inclusive).
T<char> Till after [count]'th occurrence of <char> to the
left. The cursor is placed on the character right of
<char> (inclusive).
; Repeat latest f, t, F or T [count] times.
, Repeat latest f, t, F or T in opposite direction
[count] times.
These commands move the cursor to the specified column in the current line. They stop
at the first column and at the end of the line, except "$", which may move to
one of the next lines.
- Up-down motions
k or
<C_UP> or
CTRL-P [count] lines upward (linewise).
j or
<C_DOWN> or
CTRL-J or
<LF> or
CTRL-N [count] lines downward (linewise).
- <minus> [count] lines upward, on the first non-blank
character (linewise).
+ or
CTRL-M or
<CR> [count] lines downward, on the first non-blank
character (linewise).
_ <underscore> [count] - 1 lines downward, on the first non-blank
character (linewise).
G Goto line [count], default last line, on the first
non-blank character (linewise).
:[range] Set the cursor on the (last) specified line number
(cannot be used with an operator).
{count}% Go to {count} percentage in the file, on the first
non-blank in the line (linewise). To compute the new
line number this formula is used: {count} *
number-of-lines / 100. {not in Vi}
These commands move to the specified line. They stop when reaching the first or the
last line. The first two commands put the cursor in the same column (if possible) as it
was after the last command that changed the column, except after the "$"
command, then the cursor will be put on the last character of the line.
- Word motions
<SC_RIGHT> or
w [count] words forward (exclusive).
W [count] WORDS forward (exclusive).
e Forward to the end of word [count] (inclusive).
E Forward to the end of WORD [count] (inclusive).
<SC_LEFT> or
b [count] words backward (exclusive).
B [count] WORDS backward (exclusive).
These commands move over words or WORDS. A word consists of a sequence of letters,
digits and underscores, or a sequence of other non-blank characters, separated with white
space (spaces, tabs, end of line). A WORD consists of a sequence of non-blank characters,
separated with white space. An empty line is also considered to be a word and a WORD.
Special case: "cw" and "cW" are treated like "ce" and
"cE" if the cursor is on a non-blank. This is because "cw" is
interpreted as change-word, and a word does not include the following white space. {Vi:
"cw" when on a blank followed by other blanks changes only the first blank; this
is probably a bug, because "dw" deletes all the blanks}
Another special case: When using the "w" motion in combination with an
operator and the last word moved over is at the end of a line, the end of that word
becomes the end of the operated text, not the first word in the next line.
The original vi implementation of "e" is buggy. For example, the
"e" command will stop on the first character of a line if the previous line was
empty. But when you use "2e" this does not happen. In Vim "ee" and
"2e" are the same, which is more logical. However, this causes a small
incompatibility between vi and Vim.
- Text object motions
( [count] sentences backward (exclusive).
) [count] sentences forward (exclusive).
{ [count] paragraphs backward (exclusive).
} [count] paragraphs forward (exclusive).
]] [count] sections forward or to the next <{> in the first column. When used
after an operator, then the <}> in the first column. (linewise).
][ [count] sections forward or to the next <}> in the first column (linewise).
[[ [count] sections backward or to the previous <{> in the first column
(linewise).
[] [count] sections backward or to the previous <}> in the first column
(linewise).
These commands move over three kinds of text objects.
A sentence is defined as ending at a <.>, <!> or <?> followed by
either the end of a line, or by a space. {Vi: two spaces} Any number of closing <)>,
<]>, <"> and <'> characters my appear after the <.>,
<!> or <?> before the spaces or end of line. A paragraph and section boundary
is also a sentence boundary.
A paragraph begins after each empty line, and also at each of a set of paragraph
macros, specified by the pairs of characters in the 'paragraphs' option. The default is
"IPLPPPQPP LIpplpipbp", which corresponds to the macros ".IP",
".LP", etc. (these are nroff macros, the dot must be in the first column). A
section boundary is also a paragraph boundary. Note that this does not include a <{>
or <}> in the first column.
A section begins after a form-feed in the first column and at each of a set of section
macros, specified by the pairs of characters in the 'sections' option. The default is
"SHNHH HUnhsh".
The "]" and "[" commands stop at the <{> or <}" in
the first column. This is useful to find the start or end of a function in a C program.
Note that the first character of the command determines the search direction and the
second character the type of brace found.
- Pattern searches
/{pattern}[/] Search forward for the [count]'th occurrence of
{pattern} (exclusive).
/{pattern}/{offset} Search forward for the [count]'th occurrence of {pattern} and go
{offset} lines up or down (see below). (linewise).
/ Search forward for the [count]'th latest used pattern with latest used {offset}.
//{offset} Search forward for the [count]'th latest used pattern with new {offset}. If
{offset} is empty no offset is used. * Search forward for the [count]'th occurrence of the
ident after or under the cursor (exclusive). Only whole words are search for, like with
the command "/\<indent\>". If there is no identifier after or under the
cursor, any non-blank word is used to search for. {not in Vi} # Same as "*", but
search backward. {not in Vi}
?{pattern}[?] Search backward for the [count]'th previous occurrence of {pattern}
(exclusive).
?{pattern}?{offset} Search backward for the [count]'th previous occurrence of {pattern}
and go {offset} lines up or down (see below) (linewise).
? Search backward for the [count]'th latest used pattern with latest used {offset}.
??{offset} Search backward for the [count]'th latest used pattern with new {offset}. If
{offset} is empty no offset is used.
n Repeat the latest "/" or "?" [count] times. {Vi: no count}
N Repeat the latest "/" or "?" [count] times in opposite direction.
{Vi: no count}
CTRL-C Interrupt current (search) command.
These commands search for the specified pattern. With "/" and "?"
an additional offset may be given. There are two types of offsets: line offsets and
character offsets. {the character offsets are not in Vi}
The offset gives the cursor position relative to the found match: [num] [num] lines
downwards, in column 1 +[num] [num] lines downwards, in column 1 -[num] [num] lines
upwards, in column 1 e[+num] [num] characters to the right of the end of the match e[-num]
[num] characters to the left of the end of the match s[+num] [num] characters to the right
of the start of the match s[-num] [num] characters to the left of the start of the match
b[+num] [num] characters to the right of the start (begin) of the match b[-num] [num]
characters to the left of the start (begin) of the match
If a <-> or <+> is given but [num] is omitted, a count of one will be used.
When including an offset with 'e', the search becomes inclusive (the character the cursor
lands on is included in operations).
Examples:
pattern cursor position
/test/+1 one line below "test", in column 1
/test/e on the last t of "test"
/test/s+2 on the <s> of "test"
/test/b-3 three characters before "test"
If one of these commands is used after an operator, the characters between the cursor
position before and after the search is affected. However, if a line offset is given, the
whole lines between the two cursor positions are affected.
The last used <pattern> and <offset> are remembered. They can be used to
repeat the search, possibly in another direction or with another count. Note that two
patterns are remembered: one for 'normal' search commands and one for the substitute
command ":s". Each time an empty <pattern> is given, the previously used
<pattern> is used.
{In vi the :tag command sets a new search pattern when the tag is searched for. In Vim
this is not done, the previous search pattern is still remembered}.
If the 'wrapscan' option is set (which is the default), searches wrap around the end of
the buffer. If 'wrapscan' is not set, the backward search stops at the beginning and the
forward search stops at the end of the buffer. If 'wrapscan' is set and the pattern was
not found the error message "pattern not found" is given, and the cursor will
not be moved. If 'wrapscan' is not set the message becomes "search hit BOTTOM without
match" when searching forward, or "search hit TOP without match" when
searching backward. If wrapscan is set and the search wraps around the end of the file the
message "search hit TOP, continuing at BOTTOM" or "search hit BOTTOM,
continuing at TOP" is given when searching backwards or forwards respectively.
The "*" and "#" commands search for the identifier currently under
the cursor. If there is no identifier under the cursor, the first one to the right is
used. This identifier may only contain letters, digits and underscores. Note that if you
type with ten fingers, the characters are easy to remember: the "#" is under
your left hand middle finger (search to the left and up) and the "*" is under
your right hand middle finger (search to the right and down). If there is no identifier
under or after the cursor, a search is done for any word under or after the cursor. Blanks
(<TAB>s and/or <SPACE>s) are recognized as delimiters for this word.
The definition of a pattern:
Patterns may contain special characters, depending on the setting of the 'magic'
option.
- A pattern is one or more branches, separated by '\|'. It matches anything that matches
one of the branches. Example: "foo\|bar" matches "foo" and "bar.
- A branch is one or more pieces, concatenated. It matches a match for the first, followed
by a match for the second, etc. Example: "foo[0-9]bar", first match
"foo", then a digit and then "bar".
- A piece is an atom, possibly followed by: 'magic' 'nomagic'
option option
* \* matches 0 or more of the preceding atom
\+ \+ matches 1 or more of the preceding atom {not
in Vi}
\= \= matches 0 or 1 of the preceding atom {not in
Vi}
Examples:
.* .\* match anything, also empty string
.\+ .\+ match any non-empty string
foo\= foo\= match "fo" and "foo"
- An atom can be:
- One of these five:
magic nomagic
. \. matches any single character
\< \< matches the beginning of a word
\> \> matches the end of a word
^ ^ at beginning of pattern, matches start of
line
$ $ at end of pattern or in front of '\|',
matches end of line
A pattern enclosed by escaped parentheses (e.g. "\(^a\)").
A single character, with no special meaning, matches itself
A backslash followed by a single character, with no special meaning,
matches the single character.
A range. This is a sequence of characters enclosed in '[]' with the
'magic' option, or enclosed in '\[]' with the 'nomagic' option. It
normally matches any single character from the sequence. If the
sequence begins with <^>, it matches any single character NOT in the
sequence. If two characters in the sequence are separated by <->, this
is shorthand for the full list of ASCII characters between them (e.g.
'[0-9]' matches any decimal digit). To include a literal <]> in the
sequence, make it the first character (following a possible <^>). To
include a literal '\-', make it the first or last character.
If the 'ignorecase' option is set, the case of letters is ignored.
It is impossible to have a pattern that contains a line break.
Examples:
^beep( Probably the start of the C function "beep".
[a-zA-Z]$ Any alphabetic character at the end of a line.
\(^\|[^a-zA-Z0-9_]\)[a-zA-Z_]\+[a-zA-Z0-9_]*
A C identifier (will stop in front of it).
\(\.$\|\. \) A period followed by end-of-line or a space.
Note that "\(\. \|\.$\)" does not do the same,
because '$' is not end-of-line in front of '\)'.
This was done to remain vi-compatible.
[.!?][])"']*\($\|[ ]\) A search pattern that finds the end of a sentence,
with almost the same definition as the <)> command.
Technical detail:
<NUL> characters in the file are stored as <LF> in memory. In the display they
are shown as "^@". The translation is done when reading and writing files. To
match a <NUL> with a search pattern you can just enter CTRL-@ or "CTRL-V
000". This is probably just what you expect. Internally the character is replaced by
a <LF> in the search pattern. What is unusual is that typing CTRL_V CTRL_J also
inserts a <LF>, thus also searches for a <NUL> in the file. {vi cannot handle
<NUL> characters in the file at all}
Various motions
m<a-zA-Z> Set mark <a-zA-Z> at cursor position (does not move the cursor,
this is not a motion command).
:[range]mar[k] <a-zA-Z> Set mark <a-zA-Z> at last line number in [range],
column 0. Default is cursor line.
:[range]k<a-zA-Z> Same as :mark, but the space before the mark name can be
omitted.
'<a-z> To the first non-blank character on the line with mark <a-z>
(linewise).
'<A-Z> To the first non-blank character on the line with mark <A-Z> in the
correct file (linewise when in same file, not a motion command when in other file). {not
in Vi}
`<a-z> To the mark <a-z> (exclusive).
`<A-Z> To the mark <A-Z> in the correct file (exclusive when in same file,
not a motion command when in other file). {not in Vi}
:marks List the current marks (not a motion command). {not in Vi}
A mark is not visible in any way. It is just a position in the file that is remembered.
Do not confuse marks with named registers, they are totally unrelated.
Lowercase marks are only remembered as long as the file remains loaded. If you quit
editing the file, change a character in a line or delete a line that contains a mark, that
mark is erased. Lowercase marks can be used in combination with operators. For example:
"d't" deletes the lines from the cursor position to mark <t>. Hint: Use
mark <t> for Top, <b> for Bottom, etc..
Marks are restored when using undo and redo.
Marks are remembered as long as the file remains in the buffer list.
Uppercase marks include the file name. {Vi: no uppercase marks} You can use them to
jump from file to file. You can only use an uppercase mark with an operator if the mark is
in the current file. The line number of the mark remains correct, even if you
insert/delete lines or edit another file for a moment.
'[ To the first non-blank character on the first line of the previously operated text
or start of the last putted text. {not in Vi}
`[ To the first character of the previously operated text or start of the last putted
text. {not in Vi}
'] To the first non-blank character on the last line of the previously operated text or
end of the last putted text. {not in Vi}
`] To the last character of the previously operated text or end of the last putted
text. {not in Vi}
After executing an operator the Cursor is put at the beginning of the text that was
operated upon. After a put command ("p" or "P") the cursor is
sometimes placed at the first inserted line and sometimes on the last inserted character.
The four commands above put the cursor at either end. Example: After yanking 10 lines you
want to go to the last one of them: "10Y']". After inserting several lines with
the "p" command you want to jump to the lowest inserted line: "p']".
Note: After deleting text, the start and end positions are the same, except when using
blockwise Visual mode. These commands do not work when no operator or put command has been
used yet in the current file. The position may be incorrect after inserting text and
".p.
'' To the first non-blank character of the line where the cursor was before the latest
jump (linewise).
`` To the position before latest jump (exclusive).
A "jump" is one of the following commands: "'", "`",
"G", "/", "?", "n", "N", "%",
"(", ")", "[[", "]]", "{",
"}", ":s", ":tag", "L", "M",
"H" and the commands that start editing a new file. If you make the cursor
"jump" with one of these commands, the position of the cursor before the jump is
remembered. You can return to that position with the "''" and "``"
command, unless the line containing that position was changed or deleted.
CTRL-O Go to [count] Older cursor position in jump list (not a motion command). {not in
Vi}
CTRL-I Go to [count] newer cursor position in jump list (not a motion command). {not in
Vi}
:jumps Print the jump list (not a motion command). {not in Vi}
Jumps are remembered in a jump list. With the CTRL-O and CTRL-I command you can go to
cursor positions before older jumps, and back again. Thus you can move up and down the
list.
For example, after three jump commands you have this jump list:
jump line file
1 1 -current-
2 70 -current-
3 1154 -current-
>
You are currently in line 1167. If you then use the CTRL-O command, the cursor is put
in line 1154. This results in:
jump line file
1 1 -current-
2 70 -current-
> 3 1154 -current-
4 1167 -current-
The pointer will be set at the last used jump position. The next CTRL-O command will
use the entry above it, the next CTRL-I command will use the entry below it. If the
pointer is below the last entry, this indicates that you did not use a CTRL-I or CTRL-O
before. In this case the CTRL-O command will cause the cursor position to be added to the
jump list, so you can get back to the position before the CTRL-O. In this case this is
line 1167.
With more CTRL-O commands you will go to lines 70 and 1. If you use CTRL-I you can go
back to 1154 and 1167 again.
If you use a jump command, the current line number is inserted at the end of the jump
list. If you used CTRL-O or CTRL-I just before that, the same line may be in the list
twice. This may be a bit strange. Just try it and look at the jump list with the :jumps
command. Note that this behaviour is different from the tag stack.
After the CTRL-O command that got you into line 1154 you could give another jump
command (e.g. "G"). The jump list would then become:
jump line file
1 1 -current-
2 70 -current-
3 1154 -current-
4 1167 -current-
5 1154 -current-
>
As long as you stay in the same file, the line numbers will be adjusted for deleted and
inserted lines. If you go to another file the line numbers may be wrong (keeping them
correct would take too much time). If you want to avoid this use a :jumps command before
changing files. This will update the line numbers in the current file.
% Find the next item in this line after or under the cursor and jump to its match
(inclusive). Items can be:
([{}]) parenthesis or (curly/square) brackets /* */ start or end of C-style comment
#if, #ifdef, #else, #elif, #endif
C preprocessor conditionals
Parens and braces preceded with a backslash are
ignored. Parens and braces inside quotes are ignored,
unless the number of parens/braces in a line is uneven
and this line and the previous one does not end in a
backslash. No count is allowed ({count}% jumps to a
line {count} percentage down the file).
Using '%' on #if/#else/#endif makes the movement
linewise.
[( go to [count] previous unmatched <(>. {not in Vi}
[{ go to [count] previous unmatched <{>. {not in Vi}
]) go to [count] next unmatched <)>. {not in Vi}
]} go to [count] next unmatched <}>. {not in Vi}
The above four commands can be used to go to the start or end of the current code
block. It is like doing "%" on the <(>, <)>, <{> or <}>
at the other end of the code block, but you can do this from anywhere in the code block.
Very useful for C programs. Example: When standing on "case x:", "[{"
will bring you back to the switch statement.
H To line [count] from top (Home) of screen (default: first line on the screen) on the
first non-blank character (linewise).
M To Middle line of screen, on the first non-blank character (linewise).
L To line [count] from bottom of screen (default: Last line on the screen) on the first
non-blank character (linewise).
Scrolling
Move edit window downwards (this means that more lines downwards in the text buffer are
seen):
CTRL-E Window [count] lines downwards in the buffer
CTRL-D Window Downwards in the buffer. The number of lines comes from the 'scroll'
option (default: half a screen). If [count] given, first set 'scroll' option to [count].
<SC_DOWN> or
CTRL-F Window [count] pages Forwards (downwards) in the buffer.
Move edit window upwards (this means that more lines upwards in the text buffer are
seen):
CTRL-Y Window [count] lines upwards in the buffer.
CTRL-U Window Upwards in the buffer. The number of lines comes from the 'scroll' option
(default: half a screen). If [count] given, first set 'scroll' option to [count].
<SC_UP> or
CTRL-B Window [count] pages Backwards (upwards) in the buffer.
Window repositioning:
z<CR> Redraw, line [count] at top of window (default cursor line). Put cursor at
first non-blank in the line.
zt Like "z<CR>", but leave the cursor in the same column. {not in Vi}
z{height}<CR> Redraw, make window {height} lines tall. This is useful to make the
number of lines small when screen updating is very slow. Cannot make the height more than
the physical screen height.
z. Redraw, line [count] at center of window (default cursor line). Put cursor at first
non-blank in the line.
zz Like "z.", but leave the cursor in the same column. {not in Vi}
z- Redraw, line [count] at bottom of window (default cursor line). Put cursor at first
non-blank in the line.
zb Like "z-", but leave the cursor in the same column. {not in Vi}
These commands move the contents of the window. If the cursor position is moved off of
the window, the cursor is moved onto the window. A page is the number of lines in the
window minus two. The mnemonics for these commands may be a bit confusing. Remember that
the commands refer to moving the window upwards or downwards in the buffer. But when the
window moves upwards in the buffer, the text in the window moves downwards on your screen.
Tags
:ta[g] |