Usr_03
Nvim:help pages,generated fromsource using thetree-sitter-vimdoc parser.
VIM USER MANUALby Bram Moolenaar
Moving around
Before you can insert or delete text the cursor has to be moved to the rightplace. Vim has a large number of commands to position the cursor. Thischapter shows you how to use the most important ones. You can find a list ofthese commands below
Q_lr.
03.1 Word movement
03.2 Moving to the start or end of a line
03.3 Moving to a character
03.4 Matching a parenthesis
03.5 Moving to a specific line
03.6 Telling where you are
03.7 Scrolling around
03.8 Simple searches
03.9 Simple search patterns
03.10 Using marks
Word movement
To move the cursor forward one word, use the "w" command. Like most Vimcommands, you can use a numeric prefix to move past multiple words. Forexample, "3w" moves three words. This figure shows how it works (starting atthe position marked with "x"):
This is a line with example text
x-->-->->-----------------> w w w 3w
Notice that "w" moves to the start of the next word if it already is at thestart of a word. The "b" command moves backward to the start of the previous word:
This is a line with example text
<----<--<-<---------<--x b b b 2b b
There is also the "e" command that moves to the next end of a word and "ge",which moves to the previous end of a word:
This is a line with example text
<----<----x---->------------> 2ge ge e 2e
If you are at the last word of a line, the "w" command will take you to thefirst word in the next line. Thus you can use this to move through aparagraph, much faster than using "l". "b" does the same in the otherdirection.
A word ends at a non-word character, such as a ".", "-" or ")". To changewhat Vim considers to be a word, see the
'iskeyword' option. If you try thisout in the help directly,
'iskeyword' needs to be reset for the examples towork:
:set iskeyword&
It is also possible to move by white-space separated WORDs. This is not aword in the normal sense, that's why the uppercase is used. The commands formoving by WORDs are also uppercase, as this figure shows:
ge b we <- <- ---> --->
This is-a line, with special/separated/words (and some more).
<----- <----- --------------------> -----> gE B W E
With this mix of lowercase and uppercase commands, you can quickly moveforward and backward through a paragraph.
03.2 Moving to the start or end of a line
The "$" command moves the cursor to the end of a line. If your keyboard hasan<End> key it will do the same thing.
The "^" command moves to the first non-blank character of the line. The "0"command (zero) moves to the very first character of the line, and the<Home>key does the same thing. In a picture ("." indicates a space):
^ <-----------x
.....This is a line with example text
<----------------x x-------------->0 $
(the "....." indicates blanks here)
The "$" command takes a count, like most movement commands. But moving tothe end of the line several times doesn't make sense. Therefore it causes theeditor to move to the end of another line. For example, "1$" moves you tothe end of the first line (the one you're on), "2$" to the end of the nextline, and so on. The "0" command doesn't take a count argument, because the "0" would bepart of the count. Unexpectedly, using a count with "^" doesn't have anyeffect.
03.3 Moving to a character
One of the most useful movement commands is the single-character searchcommand. The command "fx" searches forward in the line for the singlecharacter x. Hint: "f" stands for "Find". For example, you are at the beginning of the following line. Suppose youwant to go to the h of human. Just execute the command "fh" and the cursorwill be positioned over the h:
To err is human. To really foul up you need a computer.
---------->---------------> fh fy
This also shows that the command "fy" moves to the end of the word really. You can specify a count; therefore, you can go to the "l" of "foul" with"3fl":
To err is human. To really foul up you need a computer.
---------------------> 3fl
The "F" command searches to the left:
To err is human. To really foul up you need a computer.
<--------------------- Fh
The "tx" command works like the "fx" command, except it stops one characterbefore the searched character. Hint: "t" stands for "To". The backwardversion of this command is "Tx".
To err is human. To really foul up you need a computer.
<------------ ------------->Thtn
These four commands can be repeated with ";". "," repeats in the otherdirection. The cursor is never moved to another line. Not even when thesentence continues.
Sometimes you will start a search, only to realize that you have typed thewrong command. You type "f" to search backward, for example, only to realizethat you really meant "F". To abort a search, press<Esc>. So "f<Esc>" is anaborted forward search and doesn't do anything. Note:<Esc> cancels mostoperations, not just searches.
03.4 Matching a parenthesis
When writing a program you often end up with nested () constructs. Then the"%" command is very handy: It moves to the matching paren. If the cursor ison a "(" it will move to the matching ")". If it's on a ")" it will move tothe matching "(".
%
<----->if (a == (b * c) / d)
<----------------> %
This also works for [] and {} pairs. (This can be defined with the
'matchpairs' option.)
When the cursor is not on a useful character, "%" will search forward to findone. Thus if the cursor is at the start of the line of the previous example,"%" will search forward and find the first "(". Then it moves to its match:
if (a == (b * c) / d)
---+----------------> %
Other ways to move around code can be found in
usr_29.txt.
03.5 Moving to a specific line
If you are a C or C++ programmer, you are familiar with error messages such asthe following:
prog.c:33: j undeclared (first use in this function)
This tells you that you might want to fix something on line 33. So how do youfind line 33? One way is to do "9999k" to go to the top of the file and "32j"to go down thirty-two lines. It is not a good way, but it works. A muchbetter way of doing things is to use the "G" command. With a count, thiscommand positions you at the given line number. For example, "33G" puts youon line 33. (For a better way of going through a compiler's error list, see
usr_30.txt, for information on the :make command.) With no argument, "G" positions you at the end of the file. A quick way togo to the start of a file use "gg". "1G" will do the same, but is a tiny bitmore typing.
|first line of a file ^ |text text text text | |text text text text | gg7G |text text text text | |text text text text |text text text text Vtext text text text |text text text text | Gtext text text text |last line of a file V
Another way to move to a line is using the "%" command with a count. Forexample, "50%" moves you halfway through the file, and "90%" goes to near theend.
The previous assumes that you want to move to a line in the file, no matter ifit's currently visible or not. What if you want to move to one of the linesyou can see? This figure shows the three commands you can use:
+---------------------------+H -->| text sample text || sample text || text sample text || sample text |M -->| text sample text || sample text || text sample text || sample text |L -->| text sample text |+---------------------------+
Hints: "H" stands for Home, "M" for Middle and "L" for Last. Alternatively,"H" for High, "M" for Middle and "L" for Low.
03.6 Telling where you are
To see where you are in a file, there are three ways:
1. Use the
CTRL-G command. You get a message like this (assuming the
'ruler' option is off):
"usr_03.txt" line 233 of 650 --35%-- col 45-52
This shows the name of the file you are editing, the line number where the cursor is, the total number of lines, the percentage of the way through the file and the column of the cursor. Sometimes you will see a split column number. For example, "col 2-9". This indicates that the cursor is positioned on the second character, but because character one is a tab, occupying eight spaces worth of columns, the screen column is 9.
2. Set the
'number' option. This will display a line number in front of every line:
:set number
To switch this off again:
:set nonumber
Since
'number' is a boolean option, prepending "no" to its name has the effect of switching it off. A boolean option has only these two values, it is either on or off. Vim has many options. Besides the boolean ones there are options with a numerical value and string options. You will see examples of this where they are used.
3. Set the
'ruler' option. This will display the cursor position in the lower right corner of the Vim window:
:set ruler
Using the
'ruler' option has the advantage that it doesn't take much room,thus there is more space for your text.
TheCTRL-U command scrolls down half a screen of text. Think of lookingthrough a viewing window at the text and moving this window up by half theheight of the window. Thus the window moves up over the text, which isbackward in the file. Don't worry if you have a little trouble rememberingwhich end is up. Most users have the same problem. TheCTRL-D command moves the viewing window down half a screen in the file,thus scrolls the text up half a screen.
+----------------+ | some text| | some text| | some text|+---------------+ | some text|| some text|CTRL-U --> |||| | 123456|| 123456| +----------------+| 7890||| +----------------+| example|CTRL-D --> | 7890|+---------------+ || | example| | example| | example| | example| +----------------+
To scroll one line at a time useCTRL-E (scroll up) andCTRL-Y (scroll down).Think ofCTRL-E to give you one line Extra. (If you use MS-Windows compatiblekey mappingsCTRL-Y will redo a change instead of scroll.)
To scroll forward by a whole screen (except for two lines) useCTRL-F. Toscroll backwards, useCTRL-B. These should be easy to remember: F forForwards and B for Backwards.
A common issue is that after moving down many lines with "j" your cursor is atthe bottom of the screen. You would like to see the context of the line withthe cursor. That's done with the "zz" command.
+------------------+ +------------------+| earlier text | | earlier text || earlier text | | earlier text || earlier text | | earlier text || earlier text | zz --> | line with cursor || earlier text | | later text || earlier text | | later text || line with cursor | | later text |+------------------+ +------------------+
The "zt" command puts the cursor line at the top, "zb" at the bottom. Thereare a few more scrolling commands, see
Q_sc. To always keep a few lines ofcontext around the cursor, use the
'scrolloff' option.
To search for a string, use the "/string" command. To find the word include,for example, use the command:
/include
You will notice that when you type the "/" the cursor jumps to the last lineof the Vim window, like with colon commands. That is where you type the word.You can press the backspace key (backarrow or
<BS>) to make corrections. Usethe
<Left> and
<Right> cursor keys when necessary. Pressing
<Enter> executes the command.
Note:The characters .*[]^%/\?~$ have special meanings. If you want to usethem in a search you must put a \ in front of them. See below.
To find the next occurrence of the same string use the "n" command. Use thisto find the first #include after the cursor:
/#include
And then type "n" several times. You will move to each #include in the text.You can also use a count if you know which match you want. Thus "3n" findsthe third match. You can also use a count with "/": "4/the" goes to thefourth match of "the".
The "?" command works like "/" but searches backwards:
?word
The "N" command repeats the last search the opposite direction. Thus using"N" after a "/" command searches backwards, using "N" after "?" searchesforwards.
IGNORING CASE
Normally you have to type exactly what you want to find. If you don't careabout upper or lowercase in a word, set the
'ignorecase' option:
:set ignorecase
If you now search for "word", it will also match "Word" and "WORD". To matchcase again:
:set noignorecase
HISTORY
Suppose you do three searches:
/one/two/three
Now let's start searching by typing a simple "/" without pressing
<Enter>. Ifyou press
<Up> (the cursor key), Vim puts "/three" on the command line.Pressing
<Enter> at this point searches for three. If you do not press
<Enter>, but press
<Up> instead, Vim changes the prompt to "/two". Anotherpress of
<Up> moves you to "/one". You can also use the
<Down> cursor key to move through the history ofsearch commands in the other direction.
If you know what a previously used pattern starts with, and you want to use itagain, type that character before pressing<Up>. With the previous example,you can type "/o<Up>" and Vim will put "/one" on the command line.
The commands starting with ":" also have a history. That allows you to recalla previous command and execute it again. These two histories are separate.
SEARCHING FOR A WORD IN THE TEXT
Suppose you see the word "TheLongFunctionName" in the text and you want tofind the next occurrence of it. You could type "/TheLongFunctionName", butthat's a lot of typing. And when you make a mistake Vim won't find it. There is an easier way: Position the cursor on the word and use the "*"command. Vim will grab the word under the cursor and use it as the searchstring. The "#" command does the same in the other direction. You can prepend acount: "3*" searches for the third occurrence of the word under the cursor.
SEARCHING FOR WHOLE WORDS
If you type "/the" it will also match "there". To only find words that endin "the" use:
/the\>
The "\>" item is a special marker that only matches at the end of a word.Similarly "\<" only matches at the beginning of a word. Thus to search forthe word "the" only:
/\<the\>
This does not match "there" or "soothe". Notice that the "*" and "#" commandsuse these start-of-word and end-of-word markers to only find whole words (youcan use "g*" and "g#" to match partial words).
HIGHLIGHTING MATCHES
While editing a program you see a variable called "nr". You want to checkwhere it's used. You could move the cursor to "nr" and use the "*" commandand press "n" to go along all the matches.
Vim will highlight all matches. That is a very good way to see where thevariable is used, without the need to type commands. To switch this off:
:set nohlsearch
Then you need to switch it on again if you want to use it for the next searchcommand:
:set hlsearch
If you only want to remove the highlighting, use this command:
:nohlsearch
This doesn't reset the option. Instead, it disables the highlighting. Assoon as you execute a search command, the highlighting will be used again.Also for the "n" and "N" commands.
TUNING SEARCHES
There are a few options that change how searching works. These are theessential ones:
:set nowrapscan
This stops the search at the end of the file. Or, when you are searchingbackwards, it stops the search at the start of the file. The
'wrapscan'option is on by default, thus searching wraps around the end of the file.
:set noincsearch
This disables the display of the matches while you are still typing yoursearch.
INTERMEZZO
If you like one of the options mentioned before, and set it each time you useVim, you can put the command in your Vim startup file. Edit the file, forexample with:
:edit ~/.config/nvim/init.vim
Then add a line with the command to set the option, just like you typed it inVim. Example:
Go:set hlsearch<Esc>
"G" moves to the end of the file. "o" starts a new line, where you type the":set" command. You end insert mode with
<Esc>. Then write and close thefile:
ZZ
If you now start Vim again, the
'hlsearch' option will already be set.
03.9 Simple search patterns
The Vim editor uses regular expressions to specify what to search for.Regular expressions are an extremely powerful and compact way to specify asearch pattern. Unfortunately, this power comes at a price, because regularexpressions are a bit tricky to specify. In this section we mention only a few essential ones. More about searchpatterns and commands can be found in chapter 27
usr_27.txt. You can findthe full explanation here:
pattern.
BEGINNING AND END OF A LINE
The ^ character matches the beginning of a line. On an English-US keyboardyou find it above the 6. The pattern "include" matches the word includeanywhere on the line. But the pattern "^include" matches the word includeonly if it is at the beginning of a line. The $ character matches the end of a line. Therefore, "was$" matches theword was only if it is at the end of a line.
Let's mark the places where "/the" matches in this example line with "x"s:
the solder holding one of the chips melted and the
xxx xxx xxx
Using "/the$" we find this match:
the solder holding one of the chips melted and the
xxx
And with "/^the" we find this one:
the solder holding one of the chips melted and the
xxx
You can try searching with "/^the$"; it will only match a single lineconsisting entirely of "the". White space does matter here, thus if a linecontains a space after the word, like "the ", the pattern will not match.
MATCHING ANY SINGLE CHARACTER
The . (dot) character matches any existing character. For example, thepattern "c.m" matches a string whose first character is a c, whose secondcharacter is anything, and whose third character is m. Example:
We use a computer that became the cummin winter.
xxx xxx xxx
MATCHING SPECIAL CHARACTERS
If you really want to match a dot, you must avoid its special meaning byputting a backslash before it. If you search for "ter.", you will find these matches:
We use a computer that became the cummin winter.
xxxx xxxx
Searching for "ter\." only finds the second match.
When you make a jump to a position with the "G" command, Vim remembers theposition from before this jump. This position is called a mark. To go backwhere you came from, use this command:
``
This is a backtick or open single-quote character. If you use the same command a second time you will jump back again. That'sbecause the "`" command is a jump itself, and the position from before thisjump is remembered.
Generally, every time you do a command that can move the cursor further thanwithin the same line, this is called a jump. This includes the searchcommands "/" and "n" (it doesn't matter how far away the match is). But notthe character searches with "fx" and "tx" or the word movements "w" and "e". Also, "j" and "k" are not considered to be a jump, even when you use acount to make them move the cursor quite a long way away.
The "``" command jumps back and forth, between two points. The
CTRL-O commandjumps to older positions (Hint: O for older).
CTRL-I then jumps back to newerpositions (Hint: for many common keyboard layouts, I is just next to O).Consider this sequence of commands:
33G/^TheCTRL-O
You first jump to line 33, then search for a line that starts with "The".Then with
CTRL-O you jump back to line 33. Another
CTRL-O takes you back towhere you started. If you now use
CTRL-I you jump to line 33 again. Andto the match for "The" with another
CTRL-I.
|example text ^ |33G |example text |CTRL-O |CTRL-I |example text | | Vline 33 text ^ V |example text | | /^The |example text |CTRL-O |CTRL-I VThere you are | Vexample text
Note:CTRL-I is the same as<Tab>.
The ":jumps" command gives a list of positions you jumped to. The entry whichyou used last is marked with a ">".
Vim enables you to place your own marks in the text. The command "ma" marksthe place under the cursor as mark a. You can place 26 marks (a through z) inyour text. You can't see them, it's just a position that Vim remembers. To go to a mark, use the command{mark}, where {mark} is the mark letter.Thus to move to the a mark:
`a
The command "'mark" (single quotation mark, or apostrophe) moves you to thebeginning of the line containing the mark. This differs from the "`mark"command, which also moves you to the marked column.
The marks can be very useful when working on two related parts in a file.Suppose you have some text near the start of the file you need to look at,while working on some text near the end of the file. Move to the text at the start and place the s (start) mark there:
ms
Then move to the text you want to work on and put the e (end) mark there:
me
Now you can move around, and when you want to look at the start of the file,you use this to jump there:
's
Then you can use '' to jump back to where you were, or 'e to jump to the textyou were working on at the end. There is nothing special about using s for start and e for end, they arejust easy to remember.
You can use this command to get a list of marks:
:marks
You will notice a few special marks. These include:
'The cursor position before doing a jump"The cursor position when last editing the file[Start of the last change]End of the last change