VIM REFERENCE MANUAL - Part 2

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

4. Modes

4.1 Introduction
Vim has four BASIC modes:
Normal mode
In Normal 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). This is also know as command mode.

Visual mode
This is like Normal mode, but the movement commands extend a highlighted area. When a non-movement command is used it is executed for the highlighted area.

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. See Insert and Replace Mode

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 "/" and the filter command "!".

There are two ADDITIONAL modes:

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 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.

Insert command mode
Entered when CTRL-O given in Insert mode. This is like Normal mode, but after executing one command Vim returns to Insert mode. The string "-- INSERT COMMAND --" is show at the bottom of the window.
4.2 Switching from mode to mode
If for any reason you do not know in which mode you are, you can always get back to Normal mode by typing <Esc> twice. You will know you are back in Normal mode when you see the screen flash or hear the bell after you type <Esc>.
  • go from Normal mode to Visual mode by giving one of the commands "vV^V"
  • go from Normal mode to Insert mode by giving one of the commands "iIaAoOcCsS".
  • go from Normal mode to Replace mode with the "R" command (not the "r" command!).
  • go from Normal mode to Command_line mode with the one of the commands ":/?!".
  • go from Insert or Replace mode to Normal mode with <Esc> (twice in some rare cases).
  • go from Visual mode to Normal mode by giving a non-movement command, which causes the command to be executed, or by hitting <Esc> or 'v', which does nothing.
  • go from Command_line mode to Normal mode by:
    • hitting <CR> or <NL>, 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"}

  • go from Insert mode to Replace mode by hitting <Insert>.
  • go from Replace mode to Insert mode by hitting <Insert>.
  • go from Visual mode to Command_line mode by hitting ':'. The line numbers of the highlighted area will be inserted in the command line.
If the 'insertmode' option is set, editing a file will start in Insert mode.

4.3 Insert and Replace mode

If you are working in a special language mode when inserting text, see the 'langmap' option on how to avoid switching this mode on and off all the time.

4.3.1 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
<Esc> or CTRL-[ End insert or Replace mode, back to Normal mode. Finish abbreviation.
CTRL-C Quit insert mode, back to Normal mode. Do not check for abbreviations.

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}

<BS> or CTRL-H Delete the character before the cursor (see below). See :fixdel if your <BS> does not do what you want. {Vi: does not delete autoindents}
<Del> Delete the character under the cursor. If the cursor is at the end of the line, and the 'backspace' option is non-zero, delete the newline. The next line is appended after the current one. See :fixdel if your <Del> key does not do what you want. {not in Vi}
CTRL-W Delete the word before the cursor (see below). See the section "word_motions" for the definition of a word.
CTRL-U Delete all entered characters in the current line (see below).

<Tab> or CTRL-I 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 and section 4.3.4.
<NL> or CTRL-J Begin new line.
<CR> or CTRL-M Begin new line.
CTRL-K {char1} {char2} Enter digraph (see 4.7 Digraphs). When {char1} is a special key, the code for that key is inserted. Both chars are not considered for mapping. {not in Vi}

CTRL-N Find next keyword. {not in Vi}
CTRL-P Find previous keyword. {not in Vi}

CTRL-R <0-9a-z"%:> Insert the contents of register. Between typing CTRL-R and the second character '"' will be displayed to indicate that you are expected to enter the name of a register. The text is inserted as if you typed it, but mappings and abbreviations are not used. If you have options like 'textwidth', 'formatoptions' or 'autoindent' set, this will influence what will be inserted. Special registers:
'"' the unnamed register, containing the text of the last delete or yank
'%' the current file name
':' the last command line
'.' the last inserted text
See about registers. {not in Vi}

CTRL-T Insert one shiftwidth of indent at the start of the current line. The indent is always rounded to a 'shiftwidth' (this is vi compatible). {Vi: only when in indent}
CTRL-D Delete one shiftwidth of indent at the start of the current line. The indent is always rounded to a 'shiftwidth' (this is vi compatible). {Vi: CTRL-D works only when used after autoindent}
0 CTRL-D Delete all indent in the current line. {Vi: CTRL-D works only when used after autoindent}
^ CTRL-D Delete all indent in the current line. 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-V Insert next non-digit literally. For special keys the terminal code is inserted. Up to three digits form the decimal value of a single byte (see below CTRL-V_digit). The non-digit and the three digits are not considered for mapping. {Vi: no decimal byte entry}

CTRL-Q Same as CTRL-V.

CTRL-X Enter CTRL-X mode. This is a sub mode where a commands can be given to complete words or scroll the window. See below CTRL-X and in 4.3.6 Insert mode completion. {not in Vi}

CTRL-E Insert the character which is below the cursor. {not in Vi}
CTRL-Y Insert the character which is above the cursor. {not in Vi}

CTRL-B Toggle the 'revins' option (B for Backwards). Only if compiled with RIGHTLEFT (which is not the default). See ins_reverse. {not in Vi}
CTRL-_ This key is only available if Vim was compiled with RIGHTLEFT. It's purpose is to switch between languages while in insert mode, as follows:
  • When in a rightleft window, revins+nohkmap are toggled, since English will likely be inserted in this case.
  • When in a norightleft window, revins+hkmap are toggled, since Hebrew will likely be inserted in this case.
CTRL-_ moves the cursor to the end of the typed text, unlike CTRL-B that leaves the cursor in the same place. Please refer to vim_rlh.html for more information about right-to-left mode. {not in Vi}
<Insert> Toggle between insert and replace mode. {not in Vi}
The effect of the <BS>, CTRL-W and CTRL-U depends on the 'backspace' option (unless 'revins' is set):

backspace
option
action
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 <NL>, which is used internally to represent the <Nul> character. When writing the buffer to a file the <NL> character is translated into <Nul>. The <NL> character is written at the end of each line. Thus if you want to insert a <NL> character in a file you will have to make a line break.

CTRL-X enters a sub-mode, where several commands can be used. Most of these commands do keyword completion, see 4.3.6 ins_completion. These are only available when Vim was compiled with INSERT_EXPAND defined. If ":version" shows "+insert_expand" then is was, if it shows "-insert_expand" then these commands are not available. Two commands can be used to scroll the window up or down, without exiting insert mode:
CTRL-X CTRL-E scroll window one line up.
CTRL-X CTRL-Y scroll window one line down.
After CTRL-X is pressed each CTRL-E (CTRL-Y) scrolls the window up (down) by one line unless that would cause the cursor to move from its current position in the file. As soon as another key is pressed, CTRL-X mode is exited and that key is interpreted as in Insert mode.

4.3.2 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 Normal 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
<Up> cursor one line up
<Down> cursor one line down
<Left> cursor one character left
<Right> cursor one character right
<S-Left> cursor one word back (like "b" command)
<S-Right> cursor one word forward (like "w" command)
<Home> cursor to first char in the line
<End> cursor to after last char in the line
<C-Home> cursor to first char in the file
<C-End> cursor to after last char in the file
<LeftMouse> cursor to position of mouse click
<S-Up> move window one page up
<PageUp> move window one page up
<S-Down> move window one page down
<PageDown> move window one page down
CTRL-O execute one command and return to Insert mode
The CTRL-O command sometimes 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 <Left> and <Right> keys on the first/last character in the line make the cursor wrap to the previous/next line.

4.3.3 '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.

Long lines are broken if you enter a non white character after the margin. The situations where a line will be broken can be restricted by adding characters to the 'formatoptions' option:

"l"
Only break a line if it was not longer than 'textwidth' when the insert started.

"v"
Only break at a white character that has been entered during the current insert command. This is mostly Vi-compatible.

"lv"
Only break if the line was not longer than 'textwidth' when the insert started and only at a white character that has been entered during the current insert command. Only differs from "l" when entering non white characters when crossing the 'textwidth' boundary.
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". See also Q.

4.3.4 '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 with 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 character will be put back for only one space that you backspace over (the last one). {Vi does not have the 'expandtab' option}

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}

4.3.5 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. If a <NL> is typed, a line break is inserted and no character is deleted.

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>, CTRL-W or CTRL-U), what happens is that you delete the changes. The characters that were replaced are restored. If you had typed past the existing text, the characters you added are deleted. This is effectively a character-at-a-time undo. If the 'expandtab' option is set, a <Tab> will replace one character with 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 character will be put back for only one space that you backspace over (the last one). {Vi does not have the 'expandtab' option}

4.3.6 Insert mode completion

In insert and replace mode there are several commands to complete part of a keyword or line that has been typed. This is useful if you are using complicated keywords (e.g. function names with capitals and underscores). Completion can be done for:

  1. Whole lines
  2. keywords in the current file and 'dictionary'
  3. keywords in 'dictionary'
  4. keywords in the current and included files
  5. tags
  6. file names
  7. definitions or macros
All these (except 3) are done in CTRL-X mode. This is a sub-mode from insert/replace mode. You enter CTRL-X mode by typing CTRL-X and one of the CTRL-X commands. You exit CTRL-X mode by typing a key that is not a valid CTRL-X mode command. Valid keys are the CTRL-X command itself, CTRL-N (next) and CTRL-P (previous).

Also see the 'infercase' option if you want to adjust the case of the match.

Note: The keys that are valid in CTRL-X mode are not mapped. This allows for ":map ^F ^X^F" to work (where ^F is CTRL-F and ^X is CTRL-X). The key that ends CTRL-X mode (any key that is not a valid CTRL-X mode command) is mapped.

The following mappings are suggested to make typing the completion commands a bit easier (although they will hide other commands):

:inoremap ^] ^X^]
:inoremap ^F ^X^F
:inoremap ^D ^X^D
:inoremap ^L ^X^L

Completing whole lines:

CTRL-X CTRL-L
Search backwards for a line that starts with the same characters as in the current line before the cursor. Indent is ignored. The found line is inserted in front of the cursor.
CTRL-L or
CTRL-P
Search backwards for next matching line. This line replaces the previous matching line.

CTRL-N
Search forward for next matching line. This line replaces the previous matching line.
Completing keywords in current file:

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 a keyword in front of the cursor (a name made out of alphabetic characters and characters in 'iskeyword') it is used as the search pattern, with "\<" prepended (meaning: start of a word). Otherwise "\<\k\k" is used as search pattern (start of any keyword of at least two characters).

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.

In replace mode the number of characters that is replaced depends on the length of the matched string. This works like typing the characters of the matched string in replace mode.

If there is not a valid keyword character before the cursor, any keyword of at least two characters 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, thus a different match will be inserted at each CTRL-N and CTRL-P (unless there is only one matching keyword).

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 word 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.

Single character matches are never included, as they usually just get in the way of what you were really after.

eg. to get:
printf("name = %s\n", name);
just type:
printf("name = %s\n", n^P);
or even:
printf("name = %s\n", ^P);
The 'n' in '\n' is skipped.
Completing keywords in 'dictionary'
CTRL-X CTRL-K
Search the files given with the 'dictionary' option for words that start with the keyword in front of the cursor. This is like CTRL-N, but only the dictionary files are searched, not the current file. The found keyword is inserted in front of the cursor. This could potentially be pretty slow, since all matches are found before the first match is used. By default, the 'dictionary' option is empty.

CTRL-K or
CTRL-N
Search forward for next matching keyword. This keyword replaces the previous matching keyword.

CTRL-P
Search backwards for next matching keyword. This keyword replaces the previous matching keyword.
Completing keywords in the current and included files:

The 'include' option is used to specify a line that contains an include file name. The 'path' option is used to search for include files.

CTRL-X CTRL-I
Search for the first keyword in the current and included files that starts with the same characters as before the cursor. The matched keyword is inserted in front of the cursor.

CTRL-N
Search forwards for next matching keyword. This keyword replaces the previous matching keyword. Note: CTRL-I is the same as <Tab>, which is likely to be typed after a succesful completion, therefore CTRL-I is not used for searching for the next match.

CTRL-P
Search backward for previous matching keyword. This keyword replaces the previous matching keyword.
Completing tags:
CTRL-X CTRL-]
Search for the first tag that starts with the same characters as before the cursor. The matching tag is inserted in front of the cursor. Alphabetic characters and characters in 'iskeyword' are used to decide which characters are included in the tag name (same as for a keyword).

CTRL-] or
CTRL-N
Search forwards for next matching tag. This tag replaces the previous matching tag.

CTRL-P
Search backward for previous matching tag. This tag replaces the previous matching tag.
Completing file names:
CTRL-X CTRL-F
Search for the first file name that starts with the same characters as before the cursor. The matching file name is inserted in front of the cursor. Alphabetic characters and characters in 'isfname' are used to decide which characters are included in the file name. Note: the 'path' option is not used here (yet).

CTRL-F or
CTRL-N
Search forwards for next matching file name. This file name replaces the previous matching file name.

CTRL-P
Search backward for previous matching file name. This file name replaces the previous matching file name.
Completing definitions or macros:

The 'define' option is used to specify a line that contains a definition. The 'include' option is used to specify a line that contains an include file name. The 'path' option is used to search for include files.

CTRL-X CTRL-D
Search in the current and included files for the first definition (or macro) name that starts with the same characters as before the cursor. The found definition name is inserted in front of the cursor.

CTRL-D or
CTRL-N
Search forwards for next matching macro name. This macro name replaces the previous matching macro name.

CTRL-P
Search backward for previous matching macro name. This macro name replaces the previous matching macro name.
4.4 Command_line mode

Command_line mode is used to enter Ex commands ":", search patterns "/" and "?" and filter commands "!".

4.4.1 Command line editing
Normally characters are inserted in front of the cursor position. You can move around in the command line with the left and right cursor keys. With the <Insert> key you can toggle between inserting and overstriking characters. {Vi: can only alter the last character in the line} Note that if your keyboard does not have working cursor keys, or any of the other special keys, you can use ":cnoremap" to define another key for them. For example, to define tcsh style editing keys: :cnoremap ^A <Home> :cnoremap ^F <Right> :cnoremap ^B <Left> :cnoremap ^[b <S-Left> :cnoremap ^[f <S-Right> (All ^x characters entered with CTRL-V CTRL-x, <xx> typed literally).

The command line that you enter is remembered in a history table. You can recall them with the up and down cursor keys. There are actually two history tables: One for ':' commands and one for search strings. These are completely separate. The search strings history can only be accessed when entering a search string, the ':' history only when entering a command line for the ":" command. Use the 'history' option to set the number of lines that are remembered (default 20). Note that when you enter a command line that is excactly the same as an older one, the old one is removed (to avoid repeated commands moving older commands out of the history). Only commands that are typed are remembered. Ones that come from mappings are not put in the history (detail: the descision is made from the last key that was typed for the line, normally <CR>). All searches are put in the search history, also the ones that come from commands like "*" and "#". {Vi: no history}

There is an automatic completion of names on the command line, see 4.4.2 Command line completion.

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 CTRL-V).

CTRL-Q
Same as CTRL-V.

<Left>
cursor left

<Right>
cursor right

<S-Left>
cursor one word left

<S-Right>
cursor one word right

CTRL-B or
<Home>
cursor to begin of command line

CTRL-E
<End>
cursor to end of command line

<LeftMouse>
cursor to position of mouse click.

CTRL-H
<BS>
delete the character in front of the cursor (see :fixdel if your <BS> key does not do what you want).

<Del>
delete the character under the cursor (at end of line: character in front of the cursor) (see :fixdel if your <Del> key does not do what you want).

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.

<Insert>
Toggle between insert and overstrike. {not in Vi}

{char1} <BS> {char2} or
CTRL-K {char1} {char2}
enter digraph (see 4.7 Digraphs). When {char1} is a special key, the code for that key is inserted. {not in Vi}

CTRL-R <0-9a-z"%:>
Insert the contents of a numbered or named register. Between typing CTRL-R and the second character '"' will be displayed to indicate that you are expected to enter the name of a register. The text is inserted as if you typed it, but mappings are not used. Special registers:
'"' the unnamed register, containing the text of the last delete or yank
'%' the current file name
':' the last command line
'.' the last inserted text
See about registers. {not in Vi}

CTRL-J
<CR> or
<NL>
start entered command

<Esc>
When typed and 'x' not present in 'cpoptions': quit command line without executing. In macros or when 'x' present in 'cpoptions': start entered command.

CTRL-C
quit command line without executing

<Up>
recall older command line from history, which begin matches the current command line (see below).

<Down>
recall more recent command line from history, which begin matches the current command line (see below).

<S-Up> or
<PageUp>
recall older command line from history

<S-Down> or
<PageDown>
recall more recent command line from history

CTRL-D
command line completion (see 4.4.2 Command line completion)

'wildchar' option
command line completion (see 4.4.2 Command line completion)

CTRL-N
command line completion (see 4.4.2 Command line completion)

CTRL-P
command line completion (see 4.4.2 Command line completion)

CTRL-A
command line completion (see 4.4.2 Command line completion)

CTRL-L
command line completion (see 4.4.2 Command line completion)

CTRL-_
switch between Hebrew and English keyboard mode, which is private to the command line, and not related to hkmap. This is usefull when hebrew text entry is required in the command line: searches, abbreviations etc. Applies only if Vim is compiled with RIGHTLEFT. See vim_rlh.html.
The <Up> and <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 <Up>. The same could be done by typing <S-Up> a number of times until the desired command line is shown. (Note: the shifted arrow keys do not work on all terminals)

4.4.2 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.

<S-Tab>
CTRL-P
After using 'wildchar' which got multiple matches: go to previous match. Otherwise: Recall older command line from history. only works with the GUI.

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 ending in ".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.

The old value of an option can be obtained by hitting 'wildchar' just after the '='. For example, typing 'wildchar' after ":set dir=" will insert the current value of 'dir'. This overrules filename completion for the options that take a file name.

If you would like using for CTRL-P in an xterm, put this command in your .cshrc:

xmodmap -e "keysym Tab = Tab Find"
And this in your .vimrc:
cmap [1~ ( is CTRL-P)
4.4.3 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", ":!", ":help" and ":autocmd" see the '|' as their argument, and can therefore not be followed by another command. If you want '|' to be included in one of the other commands, 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. There is one execption: When the 'b' flag is present in 'cpoptions', with the ":map" and ":abbr" commands and friends CTRL-V needs to be used instead of '\'. See also map_bar. 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
:map q 10^V| map "q" to "10|"
:map q 10\| map \ l map "q" to "10\" and map "\" to "l" (when 'b' is present in 'cpoptions')
You can also use <NL> to separate commands in the same way as with '|'. But using '|' is the preferred method.

Because of vi compatibility the following strange commands are supported:
:| print current line (like ":p")
:3| print line 3 (like ":3p")
:3 goto line 3
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 on the Amiga 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 ".

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.

The '!' (bang) character after an Ex command makes the command behave in a different way. The '!' should be placed immediately after the command, without any blanks in between. If you insert blanks the '!' will be seen as an argument for the command, which has a different meaning. For example:
:w! name write the current buffer to file "name", overwriting any existing file
:w !name send the current buffer as standard input to command "name"

4.4.4 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)
* equal to '<,'> (the Visual area)
't position of mark t (lower case)
/{pattern}[/] the next line where {pattern} matches
?{pattern}[?] the previous line where {pattern} matches
\/ the next line where the previously used search pattern matches
\? the previous line where the previously used search pattern matches
\& the next line where the previously used substitute 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>

{Visual}:
Starts a command line with the Visual selected lines as a range. The code ":'<,'>" is used for this range, which makes it possible to select a similar line from the command line history for repeating a command on different Visually selected lines.
4.4.5 Ex special characters
In Ex commands, at places where a file name can be used, the following characters have a special meaning. To avoid the special meaning of '%' and '#' insert a backslash before it.
% is replaced with the current filename
# is replaced with the alternate filename
#n (where n is a number) is replaced with the filename of buffer n. "#0" is the same as "#"
CTRL-V w is replaced with the word under the cursor
CTRL-V W is replaced with the WORD under the cursor (see WORD)
CTRL-V p is replaced with the path name under the cursor
CTRL-V f when executing autocommands, is replaced with the file name for a file read or write
After "%", "#", "#n", "CTRL-V p" or "CTRL-V f" modifiers can be given (in this order):
:p Make file name a full path. Must be the first modifier.
:h Head of the file name (the last component and any separators removed). Cannot be used with :e, :r or :t. Can be repeated to remove several components at the end. When there is no head the result is empty.
:t Tail of the file name (last component of the name). Must precede any :r or :e.
:r Root of the file name (the last extension removed). When there is only an extension (file name that starts with '.', e.g. ".vimrc"), it is not removed. Can be repeated to remove several extensions (last one first).
:e Extension of the file name. Only makes sense when used alone. When there is no extension the result is empty. When there is only an extension (file name that starts with '.'), the result is empty. Can be repeated to include more extensions. If there are not enough extensions (but at least one) as much as possible are included.
Examples, when the file name is "src/version.c":
	:p	   /home/mool/vim/src/version.c
	:h			  src
	:p:h	   /home/mool/vim/src
	:p:h:h	   /home/mool/vim
	:t			      version.c
	:p:t			      version.c
	:r			  src/version
	:p:r	   /home/mool/vim/src/version
	:t:r			      version
	:e				      c

Examples, when the file name is "src/version.c.gz": :p /home/mool/vim/src/version.c.gz :e gz :e:e c.gz :e:e:e c.gz :e:e:r c :r src/version.c :r:e c :r:r src/version :r:r:r src/version

If a "<" is appended to "%", "#", "#n" or "CTRL-V p" the extension of the file name is removed (everything after and including the last '.' in the file name). This is included for backwards compatibility with version 3.0, the ":r" form is preferred. Examples:
% 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-V w word under the cursor
CTRL-V W WORD under the cursor (see WORD)
CTRL-V p path name under the cursor
CTRL-V p< idem, without extension
Note: Where a file name is expected wildcards expansion is done. On Unix the shell is used for this. Backticks also work, like in ":n `echo *.c`". But expansion is only done if there are any wildcards before expanding the '%', '#', etc.. This avoids expanding wildcards inside a file name. Examples: (alternate filename is "?readme?")
command expands to
:e # :e ?readme?
:e `ls #` :e {files matching "?readme?"}
:e #.* :e {files matching "?readme?.*"}
4.5 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. There are two exceptions:

  • When the 'cpoptions' option contains '$', and the change is within one line, the text is not directly deleted, but a '$' is put at the last deleted character.
  • When inserting text in one window, other windows on the same text are not updated until the insert is finished.
{Vi: The screen is not always updated on slow terminals}

Lines longer than the window width will wrap, unless the 'wrap' option is off (see below). The 'linebreak' option can be set to wrap at a blank character.

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}

The 'showbreak' option contains the string to put in front of wrapped lines.

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 with the number of spaces that it represents. Other non-printing characters are replaced with "^{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 with "~{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. Tip: If you don't like wrapping lines to mix with the line numbers, set the 'showbreak' option to eight spaces:

":set showbreak=\ \ \ \ \ \ \ \ "

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 <NL> 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 --", see 'showmode'. 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}

4.6 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 with the word it stands for. This can be used to save typing for often used long words.

There are three types of abbreviations:

full-id
The "full-id" type consists entirely of keyword characters (letters and characters from 'iskeyword' option). This is the most common abbreviation.

Examples: "foo", "g3", "c_1"

end-id
The "end-id" type ends in a keyword character, but all the other characters are not keyword characters.

Examples: "#i", "..f", "$/7"

non-id
The "non-id" type ends in a non-keyword character, the other characters may be of any type, excluding space and Tab. {this type is not supported by Vi}

Examples: "def#", "4/7$"

Examples of strings that cannot be abbreviations: "a.b", "#def", "a b", "_$r"

An abbreviation is only recognized when you type a non-keyword character. This can also be the <Esc> that ends insert mode or the <CR> that ends a command. The characters before the cursor must match the abbreviation. Each type has an additional rule:

full-id
In front of the match is a non-keyword 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-keyword character in front of it, other than a space or a <Tab>.

end-id
In front of the match is a keyword character, or a space or a <Tab>, or this is where the line or insertion starts.

non-id
In front of the match is a space, <Tab> or the start of the line or the insertion.

Examples: (<CURSOR> is where you type a non-keyword character)
":ab foo four old otters" (Note that spaces in the <rhs> are allowed and included in the replacement string.)
" foo<CURSOR>" is expanded to " four old otters"
" foobar<CURSOR>" is not expanded
"barfoo<CURSOR>" is not expanded
":ab #i #include"
"#i<CURSOR>" is expanded to "#include"
">#i<CURSOR>" is not expanded
":ab ;; <endofline>"
"test;;" is not expanded
"test ;;" is expanded to "test <endofline>"
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. Or type CTRL-V before the character after the abbreviation. To avoid the abbreviation in Command_line mode: 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.

:un[abbreviate] <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}

:abc[lear]
Remove all abbreviations. {not in Vi}

:iabc[lear]
Remove all abbreviations for Insert mode. {not in Vi}

:cabc[lear]
Remove all abbreviations for Command_line mode. {not in Vi}
4.7 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 DIGRAPHS defined. If this wasn't done, the ":digraph" command will display an error message. You can also check this with the ":version" command. If it shows "+digraphs" then it's included, "-digraphs" means it's not included.

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.

If a digraph with {char1}{char2} does not exist, a digraph {char2}{char1} is searched for. This will help when you don't remember which character comes first.

Note that when you enter CTRL-K {char1}, where {char1} is a special key, the code for that special key is entered. This is not a digraph.

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 "vim_digr.html". There are two sets: One that is used for MS-DOS and one for the international standard character set that is mostly used on Unix systems and the Amiga. With the wrong character set they will look 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> the entering of the digraph is aborted and Insert mode or command line mode is ended, just like hitting an <Esc>.

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 correct this, you will have to type <BS> e again. To avoid this don't set the 'digraph' option and use CTRL-K to enter digraphs.

You may have problems using Vim with characters which have an ascii value > 128. For example: You insert ue (u-umlaut) and the editor echoes \334 in Insert mode. After leaving the Insert mode everything is fine. Also fmt removes all characters with ascii > 128 from the text being formated. On some Unix systems this means you have to define the environment-variable LC_CTYPE. If you are using csh then put in your .cshrc following line:

setenv LC_CTYPE=iso_8859_1
4.8 Using the mouse
This section is about using the mouse on a terminal or a terminal window. How to use the mouse in a GUI window is explained in gui_mouse. Don't forget to do ":set mouse=a", otherwise Vim won't recognize the mouse in all modes (See 'mouse').

Currently the mouse is supported for Unix in an xterm window and for MS-DOS. Mouse clicks can be used to position the cursor, select the Visual area and paste. There are no menus, use the GUI version for that.

The characters in the 'mouse' option tell in which situations the mouse will be used by Vim:
n Normal mode
v Visual mode
i Insert mode
c Command line mode
h all previous modes when in a help file
a all previous modes
r for "Hit return ..." question

The default for 'mouse' is empty, the mouse is not used. Normally you would do

:set mouse=a
to start using the mouse (this is equivalent to setting 'mouse' to "nvich"). If you only want to use the mouse in a few modes or also want to use it for the two questions you will have to concatenate the letters for those modes. For example:
:set mouse=nv
Will make the mouse work in Normal mode and Visual mode.
:set mouse=h
Will make the mouse work in help files only (so you can use "g<LeftMouse>" to jump to tags).

In an xterm, with the currently active mode included in the 'mouse' option, normal mouse clicks are used by Vim, mouse clicks with the shift or ctrl key pressed go the the xterm. With the currently active mode not included in 'mouse' all mouse clicks go to the xterm.

Here is how you copy and paste a piece of text:

Copy/paste with the mouse and Visual mode ('mouse' option must be set, see above):

  1. Press left mouse button on first letter of text, move mouse pointer to last letter of the text and release the button. This will start Visual mode and highlight the selected area.
  2. Press "y" to yank the Visual text in the unnamed register.
  3. Click the left mouse button at the insert position.
  4. Click the middle mouse button.

Shortcut: If the insert position is on the screen at the same time as the Visual text, you can do 2, 3 and 4 all in one: Click the middle mouse button at the insert position.

Copy/paste in xterm with (current mode NOT included in 'mouse'):

  1. Press left mouse button on first letter of text, move mouse pointer to last letter of the text and release the button.
  2. Use normal Vim commands to put the cursor at the insert position.
  3. Press "a" to start Insert mode.
  4. Click the middle mouse button.
  5. Press ESC to end Insert mode.
(The same can be done with anything in 'mouse' if you keep the shift key pressed while using the mouse)

Thus in an xterm the shift and ctrl keys cannot be used with the mouse. To make it possible to do the mouse commands that require the ctrl modifier, the "g" key can be typed before using the mouse:
"g<LeftMouse>" is "<C-LeftMouse> (jump to tag under mouse click)
"g<RightMouse>" is "<C-RightMouse> ("CTRL-T")

A short overview of what the mouse buttons do:

Normal Mode:
event position cursor Visual change window action
<LeftMouse> yes end yes
<C-LeftMouse> yes end yes "CTRL-]" (2)
<S-LeftMouse> yes no change yes "*" (2)
<LeftDrag> yes start or extend (1) no
<LeftRelease> yes start or extend (1) no
<MiddleMouse> yes if not active no put
<MiddleMouse> yes if active no yank and put
<RightMouse> yes start or extend yes
<S-RightMouse> yes no change yes "#" (2)
<C-RightMouse> no no change no "CTRL-T"
<RightDrag> yes extend no
<RightRelease> yes extend no

Insert or Replace Mode:
event position cursor Visual change window action
<LeftMouse> yes (cannot be active) yes
<C-LeftMouse> yes (cannot be active) yes "CTRL-O^]" (2)
<S-LeftMouse> yes (cannot be active) yes "CTRL-O*" (2)
<LeftDrag> yes start or extend (1) no like CTRL-O (1)
<LeftRelease> yes start or extend (1) no like CTRL-O (1)
<MiddleMouse> no (cannot be active) no put register
<RightMouse> yes start or extend yes like CTRL-O
<S-RightMouse> yes (cannot be active) yes "CTRL-O#" (2)
<C-RightMouse> no (cannot be active) no "CTRL-O CTRL-T"

(1) only if mouse pointer moved since press
(2) only if click is in same buffer

Clicking the left mouse button causes the cursor to be positioned. If the click is in another window that window is made the active window. When editing the command line the cursor can only be positioned on the command line. When in Insert mode Vim remains in Insert mode. If 'scrolloff' is set, and the cursor is positioned within 'scrolloff' lines from the window border, the text is scrolled.

A Visual area can be selected by pressing the left mouse button on the first character, moving the mouse to the last character, then releasing the mouse button. You will not always see the Visual selection until you release the button, only in some versions (GUI, MS-DOS, WIN32) will the dragging be shown immediately. Note that you can make the text scroll by moving the mouse at least one character in the first/last line in the window when 'scrolloff' is non-zero.

In Normal and Visual mode clicking the right mouse button causes the Visual area to be extended. When clicking in a window which is editing another buffer, the Visual mode is stopped.

Double, triple and quadruple clicks are supported when the GUI is active, for MSDOS and Win32, and for an xterm (if the gettimeofday() function is available). Double clicking may be done to make the selection word-wise, triple clicking makes it line-wise, and quadruple clicking makes it rectangular block-wise. For MSDOS and xterm the time for double clicking can be set with the 'mousetime' option. For the other systems this time is defined outside of Vim.

In Insert mode, when a Visual area is selected, Vim goes into Normal mode temporarily. When Visual mode ends, it returns to Insert mode. This is like using CTRL-O in Insert mode.

When working with several windows, the size of the windows can be changed by dragging the status line with the mouse. Point the mouse at a status line, press the left button, move the mouse to the new position of the status line, release the button. Just clicking the mouse in a status line makes that window the current window, without moving the cursor. If by selecting a window it will change position or size, the dragging of the status line will look confusing, but it will work (just try it).

Mouse clicks can be mapped. The codes for mouse clicks are:
code mouse button normal action
<LeftMouse> left pressed set cursor position
<LeftDrag> left moved while pressed extend Visual area
<LeftRelease> left released set Visual area end
<MiddleMouse> middle pressed paste text at cursor position
<MiddleDrag> middle moved while pressed -
<MiddleRelease> middle released -
<RightMouse> right pressed extend Visual area
<RightDrag> right moved while pressed extend Visual area
<RightRelease> right released set Visual area end

Examples:

:noremap <MiddleMouse> <LeftMouse><MiddleMouse>
Paste at the position of the middle mouse button click (otherwise the paste would be done at the cursor position).

:noremap <LeftRelease> <LeftRelease>y
Immediately yank the Visually highlighted text.

Note the use of ":noremap" instead of "map" to avoid a recursive mapping.

To swap the meaning of the left and right mouse buttons:
:noremap <LeftMouse> <RightMouse>
:noremap <LeftDrag> <RightDrag>
:noremap <LeftRelease> <RightRelease>
:noremap <RightMouse> <LeftMouse>
:noremap <RightDrag> <LeftDrag>
:noremap <RightRelease> <LeftRelease>
:noremap g<LeftMouse> <C-RightMouse>
:noremap g<RightMouse> <C-LeftMouse>
:noremap! <LeftMouse> <RightMouse>
:noremap! <LeftDrag> <RightDrag>
:noremap! <LeftRelease> <RightRelease>
:noremap! <RightMouse> <LeftMouse>
:noremap! <RightDrag> <LeftDrag>
:noremap! <RightRelease> <LeftRelease>

4.9 On-line help
<Help> or
:h[elp]
Split the window and display the help file in read-only mode. If there is a help window open already, use that one. {not in Vi}

:h[elp] {subject}
Like ":help", additionally jump to the tag {subject}. {subject} can be a regular expression. :help z. jump to help for any "z" command :help z\. jump to the help for "z." If there is no full match for the pattern, or there are several matches, the "best" match will be used. {not in Vi}
The help file name can be set with the 'helpfile' option. The initial height of the help window can be set with the 'helpheight' option (default 20). Jump to specific subjects by using tags. This can be done in two ways:
  • Use the "CTRL-]" command while standing on the name of a command or option. This only works when the tag is a keyword. "<C-Leftmouse>" and "g<LeftMouse>" work just like "CTRL-]".
  • use the ":ta {subject}" command. This works with all characters.
Use "CTRL-T" to jump back.
Use ":q" to close the help window.

All the help files must be in the same directory. The files are:
vim_help.html overview and quick reference
vim_idx.html alphabetical index of all commands
vim_ref1.html reference manual (Part 1)
vim_ref2.html reference manual (Part 2)
vim_ref3.html reference manual (Part 3)
vim_ref4.html reference manual (Part 4)
vim_ref5.html reference manual (Part 5)
vim_ref6.html reference manual (Part 6)
vim_ref6.html reference manual (Part 7)
vim_ref8.html reference manual (Part 8)
vim_ref9.html reference manual (Part 9)
vim_win.html reference manual for windows commands
vim_diff.html main differences between Vim and Vi
vim_digr.html list of available digraphs
vim_tips.html tips on using Vim
vim_gui.html about the Graphical User Interface
vim_40.html about version 4.0
vim_rlh.html about the 'rightleft' option

vim_unix.html Unix specific remarks
vim_ami.html Amiga specific remarks
vim_dos.html MS-DOS specific remarks
vim_w32.html Windows-NT/95 specific remarks
vim_arch.html Archimedes specific remarks
vim_mac.html Macintosh specific remarks

vim_tags tags file for documentation


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