*motion.txt* For Vim version 5.6. Last change: 1999 Sep 17 VIM REFERENCE MANUAL by Bram Moolenaar Cursor motions *cursor-motions* *navigation* These commands move the cursor position. If the new position is off of the screen, the screen is scrolled to show the cursor (see also 'scrolljump' and 'scrolloff' options). 1. Motions and operators |operator| 2. Left-right motions |left-right-motions| 3. Up-down motions |up-down-motions| 4. Word motions |word-motions| 5. Text object motions |object-motions| 7. Various motions |various-motions| ============================================================================== 1. motions and operators *operator* The motion commands can be used after an operator command, to have the command operate on the text that was moved over. That is the text between the cursor position before and after the motion. Operators are generally used to delete or change text. The following operators are available: |c| c change |d| d delete |y| y yank into register (does not change the text) |~| ~ swap case (only if 'tildeop' is set) |!| ! filter through an external program |=| = filter through 'equalprg' or C-indenting if empty |gq| gq text formatting |Q| Q text formatting |>| > shift right |<| < shift left If the motion includes a count and the operator also had a count before it, the two counts are multiplied. For example: "2d3w" deletes six words. After applying the operator the cursor is mostly left at the start of the text that was operated upon. For example, "yfe" doesn't move the cursor, but "yFe" moves the cursor leftwards to the "e" where the yank started. *linewise* *characterwise* The operator either affects whole lines, or the characters between the start and end position. Generally, motions that move between lines affect lines (are linewise), and motions that move within a line affect characters (are characterwise). However, there are some exceptions. A character motion is either inclusive or exclusive. When inclusive, the start and end position of the motion are included in the operation. When exclusive, the last character towards the end of the buffer is not included. Linewise motions always include the start and end position. Which motions are linewise, inclusive or exclusive is mentioned below. There are however, two general exceptions: 1. If the motion is exclusive and the end of the motion is in column 1, the end of the motion is moved to the end of the previous line and the motion becomes inclusive. Example: "}" ends at the first line after a paragraph, but "V}" will not include that line. 2. If the motion is exclusive, the end of the motion is in column 1 and the start of the motion was at or before the first non-blank in the line, the motion becomes linewise. Example: If a paragraph begins with some blanks and you do "d}" while standing on the first non-blank, all the lines of the paragraph are deleted, including the blanks. If you do a put now, the deleted lines will be inserted below the cursor position. Note that when the operator is pending (the operator command is typed, but the motion isn't yet), a special set of mappings can be used. See |:omap|. Instead of first giving the operator and then a motion you can use Visual mode: mark the start of the text with "v", move the cursor to the end of the text that is to be affected and then hit the operator. The text between the start and the cursor position is highlighted, so you can see what text will be operated upon. This allows much more freedom, but requires more key strokes and has limited redo functionality. See the chapter on Visual mode |Visual-mode|. If you want to know where you are in the file use the "CTRL-G" command |CTRL-G| or the "g CTRL-G" command |g_CTRL-G|. If you set the 'ruler' option, the cursor position is continuously shown in the status line (which slows down Vim a little). NOTE: Experienced users prefer the hjkl keys because they are always right under their fingers. Beginners often prefer the arrow keys, because they do not know what the hjkl keys do. The mnemonic value of hjkl is clear from looking at the keyboard. Think of j as an arrow pointing downwards. ============================================================================== 2. Left-right motions *left-right-motions* h or *h* or ** CTRL-H or *CTRL-H* ** [count] characters to the left (exclusive). Note: If you prefer to delete a character, use the mapping: :map CTRL-V X (to enter "CTRL-V" type the CTRL-V key, followed by the key) See |:fixdel| if the key does not do what you want. l or *l* or ** ** [count] characters to the right (exclusive). *0* 0 To the first character of the line (exclusive). When moving up or down, stay in same screen column (if possible). ** ** To the first character of the line (exclusive). When moving up or down, stay in same text column (if possible). Works like "1|", which differs from "0" when the line starts with a . {not in Vi} *^* ^ To the first non-blank character of the line (exclusive). *$* ** ** $ or To the end of the line and [count - 1] lines downward (inclusive). *bar* | To screen column [count] in the current line (exclusive). *f* f{char} To [count]'th occurrence of {char} to the right. The cursor is placed on {char} (inclusive). *F* F{char} To the [count]'th occurrence of {char} to the left. The cursor is placed on {char} (inclusive). *t* t{char} Till before [count]'th occurrence of {char} to the right. The cursor is placed on the character left of {char} (inclusive). *T* T{char} Till after [count]'th occurrence of {char} to the left. The cursor is placed on the character right of {char} (inclusive). *;* ; Repeat latest f, t, F or T [count] times. *,* , Repeat latest f, t, F or T in opposite direction [count] times. These commands move the cursor to the specified column in the current line. They stop at the first column and at the end of the line, except "$", which may move to one of the next lines. See 'whichwrap' option to make some of the commands move across line boundaries. ============================================================================== 3. Up-down motions *up-down-motions* k or *k* or ** *CTRL-P* CTRL-P [count] lines upward (linewise). j or *j* or ** CTRL-J or *CTRL-J* or ** *CTRL-N* CTRL-N [count] lines downward (linewise). *-* - [count] lines upward, on the first non-blank character (linewise). + or *+* CTRL-M or *CTRL-M* ** [count] lines downward, on the first non-blank character (linewise). *_* _ [count] - 1 lines downward, on the first non-blank character (linewise). or *G* ** G Goto line [count], default last line, on the first non-blank character (linewise). If 'startofline' not set, keep the same column. *N%* {count}% Go to {count} percentage in the file, on the first non-blank in the line (linewise). To compute the new line number this formula is used: {count} * number-of-lines / 100. See also 'startofline' option. {not in Vi} These commands move to the specified line. They stop when reaching the first or the last line. The first two commands put the cursor in the same column (if possible) as it was after the last command that changed the column, except after the "$" command, then the cursor will be put on the last character of the line. ============================================================================== 4. Word motions *word-motions* or ** *w* w [count] words forward (exclusive). or ** *W* W [count] WORDS forward (exclusive). *e* e Forward to the end of word [count] (inclusive). *E* E Forward to the end of WORD [count] (inclusive). or ** *b* b [count] words backward (exclusive). or ** *B* B [count] WORDS backward (exclusive). These commands move over words or WORDS. *word* A word consists of a sequence of letters, digits and underscores, or a sequence of other non-blank characters, separated with white space (spaces, tabs, ). This can be changed with the 'iskeyword' option. *WORD* A WORD consists of a sequence of non-blank characters, separated with white space. An empty line is also considered to be a word and a WORD. Special case: "cw" and "cW" are treated like "ce" and "cE" if the cursor is on a non-blank. This is because "cw" is interpreted as change-word, and a word does not include the following white space. {Vi: "cw" when on a blank followed by other blanks changes only the first blank; this is probably a bug, because "dw" deletes all the blanks} Another special case: When using the "w" motion in combination with an operator and the last word moved over is at the end of a line, the end of that word becomes the end of the operated text, not the first word in the next line. The original Vi implementation of "e" is buggy. For example, the "e" command will stop on the first character of a line if the previous line was empty. But when you use "2e" this does not happen. In Vim "ee" and "2e" are the same, which is more logical. However, this causes a small incompatibility between Vi and Vim. ============================================================================== 5. Text object motions *object-motions* *(* ( [count] sentences backward (exclusive). *)* ) [count] sentences forward (exclusive). *{* { [count] paragraphs backward (exclusive). *}* } [count] paragraphs forward (exclusive). These commands move over three kinds of text objects. *sentence* A sentence is defined as ending at a '.', '!' or '?' followed by either the end of a line, or by a space or tab. Any number of closing ')', ']', '"' and ''' characters my appear after the '.', '!' or '?' before the spaces, tabs or end of line. A paragraph and section boundary is also a sentence boundary. If the 'J' flag is present is 'cpoptions', at least two spaces have to follow the punctuation mark; s are not recognized as white space. The definition of a sentence cannot be changed. *paragraph* A paragraph begins after each empty line. ============================================================================== 7. Various motions *various-motions* *m* *mark* *Mark* m{a-zA-Z} Set mark {a-zA-Z} at cursor position (does not move the cursor, this is not a motion command). m' or m` Set the previous context mark. This can be jumped to with the "''" or "``" command (does not move the cursor, this is not a motion command). m. Toggle an an anonymous mark on the current line. IDE's can set/clear "bookmarks" on a line. This command depends on platform functionality. {jVi only} m{<>} To the previous, next anonymous mark. This command depends on platform functionality. {jVi only} *'* *'a* '{a-z} To the first non-blank character on the line with mark {a-z} (linewise). *'A* *'0* '{A-Z0-9} To the first non-blank character on the line with mark {A-Z0-9} in the correct file (linewise when in same file, not a motion command when in other file). {not in Vi} *`* *`a* `{a-z} To the mark {a-z} (exclusive). *`A* *`0* `{A-Z0-9} To the mark {A-Z0-9} in the correct file (exclusive when in same file, not a motion command when in other file). {not in Vi} A mark is not visible in any way. It is just a position in the file that is remembered. Do not confuse marks with named registers, they are totally unrelated. 'a - 'z lowercase marks, valid within one file 'A - 'Z uppercase marks, also called file marks, valid between files '0 - '9 numbered marks, set from .viminfo file Lowercase marks 'a to 'z are remembered as long as the file remains in the buffer list. If you remove the file from the buffer list, all its marks are lost. If you delete a line that contains a mark, that mark is erased. Lowercase marks can be used in combination with operators. For example: "d't" deletes the lines from the cursor position to mark 't'. Hint: Use mark 't' for Top, 'b' for Bottom, etc.. Lowercase marks are restored when using undo and redo. Numbered marks '0 to '9 are quite different. They can not be set directly. They are only present when using a viminfo file |viminfo-file|. Basically '0 is the location of the cursor when you last exited Vim, '1 the last but one time, etc. See |viminfo-file-marks|. *'<* '< To the first non-blank character on the first line of the last selected Visual area in the current buffer. {not in Vi}. *`<* `< To the first character of the last selected Visual area in the current buffer. {not in Vi}. *'>* '> To the first non-blank character on the last line of the last selected Visual area in the current buffer. {not in Vi}. *`>* `> To the last character of the last selected Visual area in the current buffer. {not in Vi}. *''* '' To the first non-blank character of the line where the cursor was before the latest jump, or where the last "m'" or "m`" command was given (linewise). *``* `` To the position before latest jump, or where the last "m'" or "m`" command was given (exclusive). A "jump" is one of the following commands: "'", "`", "G", "/", "?", "n", "N", "%", "(", ")", "[[", "]]", "{", "}", ":s", ":tag", "L", "M", "H" and the commands that start editing a new file. If you make the cursor "jump" with one of these commands, the position of the cursor before the jump is remembered. You can return to that position with the "''" and "``" command, unless the line containing that position was changed or deleted. *%* % Find the next item in this line after or under the cursor and jump to its match (inclusive). Items can be: ([{}]) parenthesis or (curly/square) brackets (this can be changed with the 'matchpairs' option) /* */ start or end of C-style comment #if, #ifdef, #else, #elif, #endif C preprocessor conditionals Parens and braces preceded with a backslash are ignored. When the '%' character is not present in 'cpoptions', parens and braces inside double quotes are ignored, unless the number of parens/braces in a line is uneven and this line and the previous one does not end in a backslash. '(', '{', '[', ']', '}' and ')' are also ignored (parens and braces inside single quotes). Note that this works fine for C, but not for Perl, where single quotes are used for strings. No count is allowed ({count}% jumps to a line {count} percentage down the file). Using '%' on #if/#else/#endif makes the movement linewise. {jVi an extended mode that uses the platforms brace matching algorithms may be available} *H* H To line [count] from top (Home) of screen (default: first line on the screen) on the first non-blank character (linewise). See also 'startofline' option. Cursor is adjusted for 'scrolloff' option. *M* M To Middle line of screen, on the first non-blank character (linewise). See also 'startofline' option. *L* L To line [count] from bottom of screen (default: Last line on the screen) on the first non-blank character (linewise). See also 'startofline' option. Cursor is adjusted for 'scrolloff' option. Moves to the position on the screen where the mouse click is (inclusive). See also ||. If the position is in a status line, that window is made the active window and the cursor is not moved. {not in Vi} vim:tw=78:ts=8:sw=8: