VIM REFERENCE MANUAL - Part 5

By Bram Moolenaar

CONTENTS

[Note: The commands for multiple windows and buffers are explained in a different file, see vim_win.html]

  1. Introduction
  2. Notation
  3. Starting Vim
    1. Command line
    2. Workbench (Amiga only)
    3. Vim window (Amiga only)
    4. Initialization
    5. Suspending
    6. The viminfo file
  4. Modes
    1. Introduction
    2. Switching from mode to mode
    3. Insert and Replace mode
      1. special keys
      2. special special keys
      3. 'textwidth' option
      4. 'expandtab' option
      5. Replace mode
      6. Insert mode completion
    4. Command_line mode
      1. Command line editing
      2. Command line completion
      3. Ex command lines
      4. Ex command line ranges
      5. Ex special characters
    5. The window contents
    6. Abbreviations
    7. Digraphs
    8. Using the mouse
    9. On-line help
  5. Editing files
    1. Introduction
    2. Editing a file
    3. The argument list
    4. Writing and quitting
    5. Using the QuickFix mode
    6. Editing binary files
    7. Automatic commands
  6. Cursor motions
    1. Left-right motions
    2. Up-down motions
    3. Word motions
    4. Text object motions
    5. Text object selection
    6. Pattern searches
    7. Various motions
  7. Scrolling
  8. Tags and special searches
    1. Tags
    2. Identifier searches
  9. Inserting text
  10. Deleting text
  11. Changing text
    1. Delete and insert
    2. Simple changes
    3. Complex changes
    4. Formatting text
    5. Formatting C programs
  12. Copying and moving text
  13. Visual mode
  14. Various commands
  15. Repeating commands
    1. Single repeats
    2. Multiple repeats
    3. Complex repeats
  16. Undo and redo
  17. Key mapping
  18. Recovery after a crash
    1. The swap file
    2. Recovery
  19. Options
    1. Setting options
    2. Automatically setting options
    3. Saving settings
    4. Options summary
  20. Terminal information
    1. Startup
    2. Terminal options
    3. Window size
    4. Slow and fast terminals
  21. Differences from Vi and Ex
    1. Missing commands
    2. Missing options
    3. Limits

7. Scrolling

Move edit window (the part of the buffer that you see) downwards (this means that more lines downwards in the text buffer are seen):

CTRL-E
Scroll window [count] lines downwards in the buffer. Mnemonic: Extra lines.

CTRL-D
Scroll 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]. The cursor is moved the same number of lines down in the file (if possible; when lines wrap and when hitting the end of the file there may be a difference). When the cursor is on the last line of the buffer nothing happens and a beep is produced. See also 'startofline' option. {difference from vi: Vim scrolls 'scroll' screen lines, instead of file lines; makes a difference when lines wrap}

<S-Down> or
<PageDown> or
CTRL-F
Scroll window [count] pages Forwards (downwards) in the buffer. See also 'startofline' option.
Move edit window (the part of the buffer that you see) upwards (this means that more lines upwards in the text buffer are seen):
CTRL-Y
Scroll window [count] lines upwards in the buffer.

CTRL-U
Scroll window Upwards in the buffer. The number of lines comes from the 'scroll' option (default: half a screen). If [count] given, first set the 'scroll' option to [count]. The cursor is moved the same number of lines up in the file (if possible; when lines wrap and when hitting the end of the file there may be a difference). When the cursor is on the first line of the buffer nothing happens and a beep is produced. See also 'startofline' option. {difference from vi: Vim scrolls 'scroll' screen lines, instead of file lines; makes a difference when lines wrap}

<S-Up> or
<PageUp> or
CTRL-B
Scroll window [count] pages Backwards (upwards) in the buffer. See also 'startofline' option.
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. Careful: If caps-lock is on, this commands becomes "ZZ": write buffer and exit! {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 (with 'scrolloff' screen lines around it). 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. When the window moves upwards in the buffer, the text in the window moves downwards on your screen.
z<Right> or
zl
Scroll the screen [count] characters to the left. This only works when 'wrap' is off. {not in Vi}

z<Left> or
zh
Scroll the screen [count] characters to the right. This only works when 'wrap' is off. {not in Vi}
For these two commands the cursor follows the screen. If the character that the cursor is on is moved off the screen, the cursor is moved to the closest character that is on the screen. The value of 'sidescroll' is not used.
zs
Scroll the screen horizontally to position the cursor at the start (left side) of the screen. This only works when 'wrap' is off. {not in Vi}
ze
Scroll the screen horizontally to position the cursor at the end (right side) of the screen. This only works when 'wrap' is off. {not in Vi}
For these two commands the cursor is not moved in the text, only the text scrolls on the screen.

8. Tags and special searches

8.1 Tags
:ta[g][!] {ident}
Jump to the definition of {ident}, using the information in the tags file. Put {ident} in the tag stack. See below for [!].

g<LeftMouse>
<C-LeftMouse>
CTRL-]
":ta" to the keyword under or after cursor. Put the keyword in the tag stack. {Vi: identifier after the cursor}

{Visual}CTRL-]
":ta" to the text that is highlighted. {not in Vi}

g<RightMouse>
<C-RightMouse>
CTRL-T
Jump to [count] older entry in the tag stack (default 1). {not in Vi}

:[count]po[p][!]
Jump to [count] older entry in tag stack (default 1). See below for [!]. {not in Vi}

:[count]ta[g][!]
Jump to [count] newer entry in tag stack (default 1). See below for [!]. {not in Vi}

:tags
Show the contents of the tag stack. The active entry is marked with a '>'. {not in Vi}
A tag is an identifier that appears in the "tags" file. It is a sort of label that can be jumped to. For example: In C programs each function name can be used as a tag.

With the ":tag" command the cursor will be positioned on the tag. With the CTRL-] command, the keyword on which the cursor is standing is used as the tag. If the cursor is not on a keyword, the first keyword to the right of the cursor is used.

When there are multiple matches for a tag, this priority is used:

  1. The first matching static tag with a full matching tag for the current file.
  2. The first matching global tag with a full matching tag.
  3. The first matching static tag with a full matching tag for another file.
  4. The first matching static tag with an ignore-case matching tag for the current file.
  5. The first matching global tag with an ignore-case matching tag.
  6. The first matching static tag with an ignore-case matching tag for another file.
A static tag is a tag that is defined for a specific file. In a C program this could be a static function.

In Vi jumping to a tag sets the current search pattern. This means that the "n" command after jumping to a tag does not search for the same pattern that it did before jumping to the tag. Vim does not do this as we consider it to be a bug. You can still find the tag search pattern in the search history. If you really want the old Vi behaviour, set the 't' flag in 'cpoptions'.

If the tag is in the current file this will always work. Otherwise the performed actions depend on whether the current file was changed, whether a ! is added to the command and on the 'autowrite' option:
tag in current file file changed ! autowrite option action
yes x x x goto tag
no no x x read other file, goto tag
no yes yes x abandon current file, read other file, goto tag
no yes no on write current file, read other file, goto tag
no yes no off fail

  • If the tag is in the current file, the command will always work.
  • If the tag is in another file and the current file was not changed, the other file will be made the current file and read into the buffer.
  • If the tag is in another file, the current file was changed and a ! is added to the command, the changes to the current file are lost, the other file will be made the current file and read into the buffer.
  • If the tag is in another file, the current file was changed and the 'autowrite' option is on, the current file will be written, the other file will be made the current file and read into the buffer.
  • If the tag is in another file, the current file was changed and the 'autowrite' option is off, the command will fail. If you want to save the changes, use the ":w" command and then use ":tag" without an argument. This works because the tag is put on the stack anyway. If you want to lose the changes you can use the ":tag!" command.
The ":tag" command works very well for C programs. If you see a call to a function and wonder what that function does, position the cursor inside of the function name and hit CTRL-]. This will bring you to the function definition. An easy way back is with the CTRL-T command. Also read about the tag stack below.

A tags file can be created with the external command 'ctags'. It will contain a tag for each function. Some versions of 'ctags' will also make a tag for each "#defined" macro.

The lines in the tags file should have this format:

{tag}{separator}{filename}{separator}{command}

{tag} the identifier
{separator} one or more <Tab> or space characters
{filename} the file that contains the definition of {tag}
{command} the Ex command that positions the cursor on the tag.
The identifier normally is the name of a function, but it can be any identifier. There is one special form for local (static) functions: {filename}:{identifier}. Some ctags programs make use of this to separate local (static) functions from global functions. (Detail: Vim compares only the last part of the filename and ignores any path before it).

The command can be any Ex command, but normally it is a search command like

"/^main(argc, argv)"
If it is a search command, and the search fails, another try is done ignoring case. If that fails too, a search is done for:
"^main[ \t]*("
(the tag with '^' prepended and "[ \t]*(" appended). When using function names, this will find the function name when it is in column 0. This will help when the arguments to the function have changed since the tags file was made. If this search also fails another search is done with:
"^[#a-zA-Z_].*main[ \t]*("
This means: A line starting with '#' or an identifier and containing the tag followed by white space and a '('. This will find macro names and function names with a type prepended. {the extra searches are not in Vi}.

In Vi the ":tag" command sets the last search pattern when the tag is searched for. In Vim this is not done, the previous search pattern is still remembered, unless the 't' flag is present in 'cpoptions'. The search pattern is always put in the search history.

Emacs style tag files are supported if Vim was compiled with EMACS_TAGS defined. Check the output of ":version", if it contains "+emacs_tags" then it was. Sorry, there is no explanation about Emacs tag files here, it is only supported for backwards compatibility :-).

The 'tags' option is a list of file names. Each of these files is searched for the tag. This can be used to use a different file than the default file "tags". It can also be used to access a common tags file. The next file in the list is not used when: - A matching static tag for the current buffer has been found. - A matching global tag has been found. This also depends on the 'ignorecase' option. If it is off, and the tags file only has a match without matching case, the next tags file is searched for a match with matching case. If no tag with matching case is found, the first match without matching case is used. If 'ignorecase' is on, and a matching global tag with or without matching case is found, this one is used, no further tags files are searched. When a tag file name starts with "./", the '.' is replaced with the path of the current file. This makes it possible to use a tags file in the directory where the current file is (no matter what the current directory is). If the current directory is equal to the directory where the current file is, the tag file names that start with "./" are skipped. You should normally always have the name both with and without the "./". When using "./tags" the "tags" file will not be found when editing a file in the current directory. The idea of using "./" is that you can define which tag file is searched first: In the current directory ("tags,./tags") or in the directory of the current file ("./tags,tags"). For example: :set tags=./tags,tags,/home/user/commontags

In this example the tag will first be searched for in the file "tags" in the directory where the current file is. Next the "tags" file in the current directory. If it is not found there, then the file "/home/user/commontags" will be searched for the tag.

Instead of the comma a space may be used. Then a backslash is required for the space to be included in the string option:

:set tags=tags\ /home/user/commontags

To include a space in a file name use three backslashes. To include a comma in a file name use two backslashes. For example, use:

:set tags=tag\\\ file,/home/user/common\\,tags

for the files "tag file" and "/home/user/common,tags". The 'tags' option will have the value "tag\ file,/home/user/common\,tags".

If the 'tagrelative' option is on (which is the default) and using a tag file in another directory, file names in that tag file are relative to the directory where the tag file is.

The tags that you use are remembered in the tag stack. You can print this stack with the ":tags" command. The result looks like this:
# TO tag FROM line in file
1 main 1 harddisk2:text/vim/test
> 2 FuncA 58 -current-
3 FuncC 357 harddisk2:text/vim/src/amiga.c
This list shows the tags that you jumped to and the cursor position before that jump. The older tags are at the top, the newer at the bottom.

The '>' points to the active entry. This is the tag that will be used by the next ":tag" command. The CTRL-T and ":pop" command will use the position above the active entry.

The line number and file name are remembered to be able to get back to where you were before the tag command. The line number will be correct, also when deleting/inserting lines, unless this was done by another program (e.g. another instance of Vim).

You can jump to previously used tags with several commands. Some examples:

":pop" or CTRL-T to position before previous tag
{count}CTRL-T to position before {count} older tag
":tag" to newer tag
":0tag" to last used tag
The most obvious way to use this is while browsing through the call graph of a program. Consider the following call graph:
	main  --->  FuncA  --->  FuncC
	      --->  FuncB
(Explanation: main calls FuncA and FuncB; FuncA calls FuncC). You can get from main to FuncA by using CTRL-] on the call to FuncA. Then you can CTRL-] to get to FuncC. If you now want to go back to main you can use CTRL-T twice. Then you can CTRL-] to FuncB.

If you issue a ":ta {ident}" or CTRL-] command, this tag is inserted at the current position in the stack. If the stack was full (it can hold up to 20 entries), the oldest entry is deleted and the older entries shift one position up (their index number is decremented by one). If the last used entry was not at the bottom, the entries below the last used one are deleted. This means that an old branch in the call graph is lost. After the commands explained above the tag stack will look like this:
# TO tag FROM line in file
1 main 1 harddisk2:text/vim/test
2 FuncB 59 harddisk2:text/vim/src/main.c
>

8.2 Include file searches
These commands look for a string in the current file and in all encountered included files (recursively). This can be used to find the definition of a variable, function or macro. If you only want to search in the current buffer, use the commands listed at pattern_searches.

When a line is encountered that includes another file, that file is searched before continuing in the current buffer. Files included by included files are also searched. When an include file could not be found it is silently ignored. Use the ":checkpath" command to discover which files could not be found, possibly your 'path' option is not set up correctly. Note: the included file is searched, not a buffer that may be editing that file. Only for the current file the lines in the buffer are used. The string can be any keyword or a defined macro. For the keyword any match will be found. For defined macros only lines that match with the 'define' option will be found. The default is "\^#[ \t]*define", which is for C programs. Also, when a match is found for a defined macro, the displaying of lines continues with the next line when a line ends in a backslash. The commands that start with "[" start searching from the start of the current file. The commands that start with "]" start at the current cursor position. The 'include' option is used to define a line that includes another file. The default is "\^#[ \t]*include", which is for C programs. Note: Vim does not recognize C syntax, if the 'include' option matches a line inside "#ifdef/#endif" or inside a comment, it is searched anyway. The 'isfname' option is used to recognize the file name that comes after the matched pattern.

The 'path' option is used to find the directory for the include files that do not have an absolute path.

The 'comments' option is used for the commands that display a single line or jump to a line. It defines patterns that may start a comment. Those lines are ignored for the search, unless [!] is used. One exception: When the line matches the pattern "^# *define" it is not considered to be a comment.

[i
Display the first line that contains the keyword under the cursor. The search starts at the beginning of the file. Lines that look like a comment are ignored (see 'comments' option). If a count is given, the count'th matching line is displayed. {not in Vi}

]i
like "[i", but start at the current cursor position. {not in Vi}

:[range]is[earch][!] [count] [/]pattern[/]
Like "[i" and "]i", but search in [range] lines (default: whole file). Without [!] lines that are recognized as comments are skipped. Without [/] only whole words are matched, using the pattern "\<pattern\>". {not in Vi}

[I
Display all lines that contain the keyword under the cursor. File names and line numbers are displayed for the found lines. The search starts at the beginning of the file. {not in Vi}

]I
like "[I", but start at the current cursor position. {not in Vi}

:[range]il[ist][!] [/]pattern[/]
Like "[I" and "]I", but search in [range] lines (default: whole file). Without [!] lines that are recognized as comments are skipped. Without [/] only whole words are matched, using the pattern "\<pattern\>". {not in Vi}

[ CTRL-I
Jump to the first line that contains the keyword under the cursor. The search starts at the beginning of the file. Lines that look like a comment are ignored (see 'comments' option). If a count is given, the count'th matching line is jumped to. {not in Vi}

] CTRL-I
like "[ CTRL-I", but start at the current cursor position. {not in Vi}

:[range]ij[ump][!] [count] [/]pattern[/]
Like "[ CTRL-I" and "] CTRL-I", but search in [range] lines (default: whole file). Without [!] lines that are recognized as comments are skipped. Without [/] only whole words are matched, using the pattern "\<pattern\>". {not in Vi}

CTRL-W CTRL-I
CTRL-W i
Open a new window, with the cursor on the first line that contains the keyword under the cursor. The search starts at the beginning of the file. Lines that look like a comment line are ignored (see 'comments' option). If a count is given, the count'th matching line is jumped to. {not in Vi}

:[range]isp[lit][!] [count] [/]pattern[/]
Like "CTRL-W i" and "CTRL-W i", but search in [range] lines (default: whole file). Without [!] lines that are recognized as comments are skipped. Without [/] only whole words are matched, using the pattern "\<pattern\>". {not in Vi}

[d
Display the first macro definition that contains the macro under the cursor. The search starts from the beginning of the file. If a count is given, the count'th matching line is displayed. {not in Vi}

]d
like "[d", but start at the current cursor position. {not in Vi}

:[range]ds[earch][!] [count] [/]pattern[/]
Like "[d" and "]d", but search in [range] lines (default: whole file). Without [!] lines that are recognized as comments are skipped. Without [/] only whole words are matched, using the pattern "\<pattern\>". {not in Vi}

[D
Display all macro definitions that contain the macro under the cursor. File names and line numbers are displayed for the found lines. The search starts from the beginning of the file. {not in Vi}

]D
like "[D", but start at the current cursor position. {not in Vi}

:[range]dl[ist][!] [/]pattern[/]
Like "[D" and "]D", but search in [range] lines (default: whole file). Without [!] lines that are recognized as comments are skipped. Without [/] only whole words are matched, using the pattern "\<pattern\>". {not in Vi}

[ CTRL-D
Jump to the first macro definition that contains the keyword under the cursor. The search starts from the beginning of the file. If a count is given, the count'th matching line is jumped to. {not in Vi}

] CTRL-D
like "[ CTRL-D", but start at the current cursor position. {not in Vi}

:[range]dj[ump][!] [count] [/]pattern[/]
Like "[ CTRL-D" and "] CTRL-D", but search in [range] lines (default: whole file). Without [!] lines that are recognized as comments are skipped. Without [/] only whole words are matched, using the pattern "\<pattern\>". {not in Vi}

CTRL-W CTRL-D
CTRL-W d
Open a new window, with the cursor on the first macro definition line that contains the keyword under the cursor. The search starts from the beginning of the file. If a count is given, the count'th matching line is jumped to. {not in Vi}

:[range]dsp[lit][!] [count] [/]pattern[/]
Like "CTRL-W d", but search in [range] lines (default: whole file). Without [!] lines that are recognized as comments are skipped. Without [/] only whole words are matched, using the pattern "\<pattern\>". {not in Vi}

:che[ckpath]
List all the included files that could not be found. {not in Vi}

:che[ckpath]!
List all the included files. {not in Vi}

9. Inserting text

The following commands can be used to insert new text into the buffer. They can all be undone. The non-Ex commands can be repeated with the "." command.
a
Append text after the cursor [count] times.

A
Append text at the end of the line [count] times.

<Insert> or
i
Insert text before the cursor [count] times.

I
Insert text before the first CHAR on the line [count] times.

gI
Insert text in column 1 [count] times. {not in Vi}

o
Begin a new line below the cursor and insert text, repeat [count] times. {Vi: blank [count] screen lines}

O
Begin a new line above the cursor and insert text, repeat [count] times. {Vi: blank [count] screen lines}
These commands are used to start inserting text. They can be undone and repeated. You can end Insert mode with <Esc>. See the section "Insert and Replace mode" for the other special characters in Insert mode. The effect of [count] takes place after Insert mode is exited.

When 'autoindent' is on, the indent for a new line is obtained from the previous line. When 'smartindent' or 'cindent' is on, the indent for a line is automatically adjusted for C programs.

'textwidth' can be set to the maximum width for a line. When a line becomes too long when appending characters a line break is automatically inserted.

:r[ead] [name]
Insert the file [name] (default: current file) below the cursor.

:{range}r[ead] [name]
Insert the file [name] (default: current file) below the specified line.

:r[ead] !{cmd}
Execute {cmd} and insert its standard output below the cursor. A temporary file is used to store the output of the command which is then read into the buffer. 'shellredir' is used to save the output of the command, which can be set to include stderr or not.
These commands insert the contents of a file, or the output of a command, into the buffer. They can be undone. They cannot be repeated with the "." command. They work on a line basis, insertion starts below the line in which the cursor is, or below the specified line. To insert text above the first line use the command ":0r {name}".

The <NL> character is recognized as end-of-line marker. If the 'textmode' option is on, a <CR> in front of an <NL> is ignored and a CTRL-Z at the end of the file is ignored. The 'textmode' option is default on for MS-DOS.

If the 'textauto' option is on Vim tries to recognize the type of end-of-line marker (see 5.2 how this is done). However, the 'textmode' option will not be changed. Only while reading one file the text mode is used or not.

On non-MS-DOS systems the message "[textmode]" is shown if a file is read in text mode, to remind you that something unusual is done. On MS-DOS the message "[notextmode]" is shown if a file is read without text mode.

An example on how to use ":r !":

:r !uuencode binfile binfile
This command reads "binfile", uuencodes it and reads it into the current buffer. Useful when you are editing e-mail and want to include a binary file.

10. Deleting text

["x]<Del> or
["x]x
Delete [count] characters under and after the cursor [into register x] (not linewise). Does the same as "dl". See :fixdel if the <Del> key does not do what you want. Also see 'whichwrap'. {<Del> not in Vi}

["x]X
Delete [count] characters before the cursor [into register x] (not linewise). Does the same as "dh". Also see 'whichwrap'.

["x]d{motion}
Delete text that is moved over [into register x]. See below for exception.

["x]dd
Delete [count] lines [into register x] (linewise).

["x]D
Delete the characters under the cursor until the end of the line and [count]-1 more lines [into register x]; synonym for d$ (not linewise).

{Visual}["x]x or
{Visual}["x]d
Delete the highlighted text [into register x] (see the chapter on Visual_mode). {not in Vi}

{Visual}["x]X or
{Visual}["x]D
Delete the highlighted lines [into register x] (see the chapter on Visual_mode). {not in Vi}

:[range]d[elete] [x]
Delete [range] lines (default: current line) [into register x].

:[range]d[elete] [x] {count}
Delete {count} lines, starting with [range] (default: current line, See Command line_ranges) [into register x].
These commands delete text. They can be repeated with the "." command (except ":d") and undone. Use Visual mode to delete blocks of text. See registers for an explanation of registers.

An exception for the d{motion} command: If the motion is not linewise, the start and end of the motion are not in the same line and before the start and after the end are only blanks, the delete becomes linewise. This means that the blank line that would remain is also deleted.

J
Join [count] lines, with a minimum of two lines.

{Visual}J
Join the highlighted lines, with a minimum of two lines. {not in Vi}

:[range]j[oin][!]
Join [range] lines. Same as "J", except when [!] is given, then no spaces will be inserted or deleted. When [range] is given and the start and end of the range are equal, nothing happens. Default is to join two lines.

:[range]j[oin][!] {count}
Join {count} lines, starting with [range] (default: current line, See Command line_ranges). Same as "J", except when [!] is given, then no spaces will be inserted or deleted.
These commands delete the newline between lines. This has the effect of joining them into one line. They can be repeated (except ":j") and undone.

One space is inserted in place of the <NL>, unless the line ended with a space, <Tab> or the next line started with a ')'. If the next line has leading white space it is deleted first. If the 'joinspaces' option is on, two spaces are inserted after a period.


Part 1 Part 2 Part 3 Part 4 Part 5 Part 6 Part 7 Part 8 Part 9
Send feedback on this page to Rajesh Kallingal
For Vim version 3.24. Last modification: 1996 Apr 25