usr_03.txt 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. *usr_03.txt* For Vim version 9.0. Last change: 2022 Oct 30
  2. VIM USER MANUAL - by Bram Moolenaar
  3. Moving around
  4. Before you can insert or delete text the cursor has to be moved to the right
  5. place. Vim has a large number of commands to position the cursor. This
  6. chapter shows you how to use the most important ones. You can find a list of
  7. these commands below |Q_lr|.
  8. |03.1| Word movement
  9. |03.2| Moving to the start or end of a line
  10. |03.3| Moving to a character
  11. |03.4| Matching a parenthesis
  12. |03.5| Moving to a specific line
  13. |03.6| Telling where you are
  14. |03.7| Scrolling around
  15. |03.8| Simple searches
  16. |03.9| Simple search patterns
  17. |03.10| Using marks
  18. Next chapter: |usr_04.txt| Making small changes
  19. Previous chapter: |usr_02.txt| The first steps in Vim
  20. Table of contents: |usr_toc.txt|
  21. ==============================================================================
  22. *03.1* Word movement
  23. To move the cursor forward one word, use the "w" command. Like most Vim
  24. commands, you can use a numeric prefix to move past multiple words. For
  25. example, "3w" moves three words. This figure shows how it works (starting at
  26. the position marked with "x"):
  27. This is a line with example text ~
  28. x-->-->->----------------->
  29. w w w 3w
  30. Notice that "w" moves to the start of the next word if it already is at the
  31. start of a word.
  32. The "b" command moves backward to the start of the previous word:
  33. This is a line with example text ~
  34. <----<--<-<---------<--x
  35. b b b 2b b
  36. There is also the "e" command that moves to the next end of a word and "ge",
  37. which moves to the previous end of a word:
  38. This is a line with example text ~
  39. <----<----x---->------------>
  40. 2ge ge e 2e
  41. If you are at the last word of a line, the "w" command will take you to the
  42. first word in the next line. Thus you can use this to move through a
  43. paragraph, much faster than using "l". "b" does the same in the other
  44. direction.
  45. A word ends at a non-word character, such as a ".", "-" or ")". To change
  46. what Vim considers to be a word, see the 'iskeyword' option. If you try this
  47. out in the help directly, 'iskeyword' needs to be reset for the examples to
  48. work: >
  49. :set iskeyword&
  50. It is also possible to move by white-space separated WORDs. This is not a
  51. word in the normal sense, that's why the uppercase is used. The commands for
  52. moving by WORDs are also uppercase, as this figure shows:
  53. ge b w e
  54. <- <- ---> --->
  55. This is-a line, with special/separated/words (and some more). ~
  56. <----- <----- --------------------> ----->
  57. gE B W E
  58. With this mix of lowercase and uppercase commands, you can quickly move
  59. forward and backward through a paragraph.
  60. ==============================================================================
  61. *03.2* Moving to the start or end of a line
  62. The "$" command moves the cursor to the end of a line. If your keyboard has
  63. an <End> key it will do the same thing.
  64. The "^" command moves to the first non-blank character of the line. The "0"
  65. command (zero) moves to the very first character of the line, and the <Home>
  66. key does the same thing. In a picture ("." indicates a space):
  67. ^
  68. <-----------x
  69. .....This is a line with example text ~
  70. <----------------x x-------------->
  71. 0 $
  72. (the "....." indicates blanks here)
  73. The "$" command takes a count, like most movement commands. But moving to
  74. the end of the line several times doesn't make sense. Therefore it causes the
  75. editor to move to the end of another line. For example, "1$" moves you to
  76. the end of the first line (the one you're on), "2$" to the end of the next
  77. line, and so on.
  78. The "0" command doesn't take a count argument, because the "0" would be
  79. part of the count. Unexpectedly, using a count with "^" doesn't have any
  80. effect.
  81. ==============================================================================
  82. *03.3* Moving to a character
  83. One of the most useful movement commands is the single-character search
  84. command. The command "fx" searches forward in the line for the single
  85. character x. Hint: "f" stands for "Find".
  86. For example, you are at the beginning of the following line. Suppose you
  87. want to go to the h of human. Just execute the command "fh" and the cursor
  88. will be positioned over the h:
  89. To err is human. To really foul up you need a computer. ~
  90. ---------->--------------->
  91. fh fy
  92. This also shows that the command "fy" moves to the end of the word really.
  93. You can specify a count; therefore, you can go to the "l" of "foul" with
  94. "3fl":
  95. To err is human. To really foul up you need a computer. ~
  96. --------------------->
  97. 3fl
  98. The "F" command searches to the left:
  99. To err is human. To really foul up you need a computer. ~
  100. <---------------------
  101. Fh
  102. The "tx" command works like the "fx" command, except it stops one character
  103. before the searched character. Hint: "t" stands for "To". The backward
  104. version of this command is "Tx".
  105. To err is human. To really foul up you need a computer. ~
  106. <------------ ------------->
  107. Th tn
  108. These four commands can be repeated with ";". "," repeats in the other
  109. direction. The cursor is never moved to another line. Not even when the
  110. sentence continues.
  111. Sometimes you will start a search, only to realize that you have typed the
  112. wrong command. You type "f" to search backward, for example, only to realize
  113. that you really meant "F". To abort a search, press <Esc>. So "f<Esc>" is an
  114. aborted forward search and doesn't do anything. Note: <Esc> cancels most
  115. operations, not just searches.
  116. ==============================================================================
  117. *03.4* Matching a parenthesis
  118. When writing a program you often end up with nested () constructs. Then the
  119. "%" command is very handy: It moves to the matching paren. If the cursor is
  120. on a "(" it will move to the matching ")". If it's on a ")" it will move to
  121. the matching "(".
  122. %
  123. <----->
  124. if (a == (b * c) / d) ~
  125. <---------------->
  126. %
  127. This also works for [] and {} pairs. (This can be defined with the
  128. 'matchpairs' option.)
  129. When the cursor is not on a useful character, "%" will search forward to find
  130. one. Thus if the cursor is at the start of the line of the previous example,
  131. "%" will search forward and find the first "(". Then it moves to its match:
  132. if (a == (b * c) / d) ~
  133. ---+---------------->
  134. %
  135. ==============================================================================
  136. *03.5* Moving to a specific line
  137. If you are a C or C++ programmer, you are familiar with error messages such as
  138. the following:
  139. prog.c:33: j undeclared (first use in this function) ~
  140. This tells you that you might want to fix something on line 33. So how do you
  141. find line 33? One way is to do "9999k" to go to the top of the file and "32j"
  142. to go down thirty-two lines. It is not a good way, but it works. A much
  143. better way of doing things is to use the "G" command. With a count, this
  144. command positions you at the given line number. For example, "33G" puts you
  145. on line 33. (For a better way of going through a compiler's error list, see
  146. |usr_30.txt|, for information on the :make command.)
  147. With no argument, "G" positions you at the end of the file. A quick way to
  148. go to the start of a file use "gg". "1G" will do the same, but is a tiny bit
  149. more typing.
  150. | first line of a file ^
  151. | text text text text |
  152. | text text text text | gg
  153. 7G | text text text text |
  154. | text text text text
  155. | text text text text
  156. V text text text text |
  157. text text text text | G
  158. text text text text |
  159. last line of a file V
  160. Another way to move to a line is using the "%" command with a count. For
  161. example "50%" moves you to halfway the file. "90%" goes to near the end.
  162. The previous assumes that you want to move to a line in the file, no matter if
  163. it's currently visible or not. What if you want to move to one of the lines
  164. you can see? This figure shows the three commands you can use:
  165. +---------------------------+
  166. H --> | text sample text |
  167. | sample text |
  168. | text sample text |
  169. | sample text |
  170. M --> | text sample text |
  171. | sample text |
  172. | text sample text |
  173. | sample text |
  174. L --> | text sample text |
  175. +---------------------------+
  176. Hints: "H" stands for Home, "M" for Middle and "L" for Last. Alternatively,
  177. "H" for High, "M" for Middle and "L" for Low.
  178. ==============================================================================
  179. *03.6* Telling where you are
  180. To see where you are in a file, there are three ways:
  181. 1. Use the CTRL-G command. You get a message like this (assuming the 'ruler'
  182. option is off):
  183. "usr_03.txt" line 233 of 650 --35%-- col 45-52 ~
  184. This shows the name of the file you are editing, the line number where the
  185. cursor is, the total number of lines, the percentage of the way through
  186. the file and the column of the cursor.
  187. Sometimes you will see a split column number. For example, "col 2-9".
  188. This indicates that the cursor is positioned on the second character, but
  189. because character one is a tab, occupying eight spaces worth of columns,
  190. the screen column is 9.
  191. 2. Set the 'number' option. This will display a line number in front of
  192. every line: >
  193. :set number
  194. <
  195. To switch this off again: >
  196. :set nonumber
  197. <
  198. Since 'number' is a boolean option, prepending "no" to its name has the
  199. effect of switching it off. A boolean option has only these two values,
  200. it is either on or off.
  201. Vim has many options. Besides the boolean ones there are options with
  202. a numerical value and string options. You will see examples of this where
  203. they are used.
  204. 3. Set the 'ruler' option. This will display the cursor position in the
  205. lower right corner of the Vim window: >
  206. :set ruler
  207. Using the 'ruler' option has the advantage that it doesn't take much room,
  208. thus there is more space for your text.
  209. ==============================================================================
  210. *03.7* Scrolling around
  211. The CTRL-U command scrolls down half a screen of text. Think of looking
  212. through a viewing window at the text and moving this window up by half the
  213. height of the window. Thus the window moves up over the text, which is
  214. backward in the file. Don't worry if you have a little trouble remembering
  215. which end is up. Most users have the same problem.
  216. The CTRL-D command moves the viewing window down half a screen in the file,
  217. thus scrolls the text up half a screen.
  218. +----------------+
  219. | some text |
  220. | some text |
  221. | some text |
  222. +---------------+ | some text |
  223. | some text | CTRL-U --> | |
  224. | | | 123456 |
  225. | 123456 | +----------------+
  226. | 7890 |
  227. | | +----------------+
  228. | example | CTRL-D --> | 7890 |
  229. +---------------+ | |
  230. | example |
  231. | example |
  232. | example |
  233. | example |
  234. +----------------+
  235. To scroll one line at a time use CTRL-E (scroll up) and CTRL-Y (scroll down).
  236. Think of CTRL-E to give you one line Extra. (If you use MS-Windows compatible
  237. key mappings CTRL-Y will redo a change instead of scroll.)
  238. To scroll forward by a whole screen (except for two lines) use CTRL-F. To
  239. scroll backwards, use CTRL-B. These should be easy to remember: F for
  240. Forwards and B for Backwards.
  241. A common issue is that after moving down many lines with "j" your cursor is at
  242. the bottom of the screen. You would like to see the context of the line with
  243. the cursor. That's done with the "zz" command.
  244. +------------------+ +------------------+
  245. | earlier text | | earlier text |
  246. | earlier text | | earlier text |
  247. | earlier text | | earlier text |
  248. | earlier text | zz --> | line with cursor |
  249. | earlier text | | later text |
  250. | earlier text | | later text |
  251. | line with cursor | | later text |
  252. +------------------+ +------------------+
  253. The "zt" command puts the cursor line at the top, "zb" at the bottom. There
  254. are a few more scrolling commands, see |Q_sc|. To always keep a few lines of
  255. context around the cursor, use the 'scrolloff' option.
  256. ==============================================================================
  257. *03.8* Simple searches
  258. To search for a string, use the "/string" command. To find the word include,
  259. for example, use the command: >
  260. /include
  261. You will notice that when you type the "/" the cursor jumps to the last line
  262. of the Vim window, like with colon commands. That is where you type the word.
  263. You can press the backspace key (backarrow or <BS>) to make corrections. Use
  264. the <Left> and <Right> cursor keys when necessary.
  265. Pressing <Enter> executes the command.
  266. Note:
  267. The characters .*[]^%/\?~$ have special meanings. If you want to use
  268. them in a search you must put a \ in front of them. See below.
  269. To find the next occurrence of the same string use the "n" command. Use this
  270. to find the first #include after the cursor: >
  271. /#include
  272. And then type "n" several times. You will move to each #include in the text.
  273. You can also use a count if you know which match you want. Thus "3n" finds
  274. the third match. You can also use a count with "/": "4/the" goes to the
  275. fourth match of "the".
  276. The "?" command works like "/" but searches backwards: >
  277. ?word
  278. The "N" command repeats the last search the opposite direction. Thus using
  279. "N" after a "/" command searches backwards, using "N" after "?" searches
  280. forwards.
  281. IGNORING CASE
  282. Normally you have to type exactly what you want to find. If you don't care
  283. about upper or lowercase in a word, set the 'ignorecase' option: >
  284. :set ignorecase
  285. If you now search for "word", it will also match "Word" and "WORD". To match
  286. case again: >
  287. :set noignorecase
  288. HISTORY
  289. Suppose you do three searches: >
  290. /one
  291. /two
  292. /three
  293. Now let's start searching by typing a simple "/" without pressing <Enter>. If
  294. you press <Up> (the cursor key), Vim puts "/three" on the command line.
  295. Pressing <Enter> at this point searches for three. If you do not press
  296. <Enter>, but press <Up> instead, Vim changes the prompt to "/two". Another
  297. press of <Up> moves you to "/one".
  298. You can also use the <Down> cursor key to move through the history of
  299. search commands in the other direction.
  300. If you know what a previously used pattern starts with, and you want to use it
  301. again, type that character before pressing <Up>. With the previous example,
  302. you can type "/o<Up>" and Vim will put "/one" on the command line.
  303. The commands starting with ":" also have a history. That allows you to recall
  304. a previous command and execute it again. These two histories are separate.
  305. SEARCHING FOR A WORD IN THE TEXT
  306. Suppose you see the word "TheLongFunctionName" in the text and you want to
  307. find the next occurrence of it. You could type "/TheLongFunctionName", but
  308. that's a lot of typing. And when you make a mistake Vim won't find it.
  309. There is an easier way: Position the cursor on the word and use the "*"
  310. command. Vim will grab the word under the cursor and use it as the search
  311. string.
  312. The "#" command does the same in the other direction. You can prepend a
  313. count: "3*" searches for the third occurrence of the word under the cursor.
  314. SEARCHING FOR WHOLE WORDS
  315. If you type "/the" it will also match "there". To only find words that end
  316. in "the" use: >
  317. /the\>
  318. The "\>" item is a special marker that only matches at the end of a word.
  319. Similarly "\<" only matches at the beginning of a word. Thus to search for
  320. the word "the" only: >
  321. /\<the\>
  322. This does not match "there" or "soothe". Notice that the "*" and "#" commands
  323. use these start-of-word and end-of-word markers to only find whole words (you
  324. can use "g*" and "g#" to match partial words).
  325. HIGHLIGHTING MATCHES
  326. While editing a program you see a variable called "nr". You want to check
  327. where it's used. You could move the cursor to "nr" and use the "*" command
  328. and press "n" to go along all the matches.
  329. There is another way. Type this command: >
  330. :set hlsearch
  331. If you now search for "nr", Vim will highlight all matches. That is a very
  332. good way to see where the variable is used, without the need to type commands.
  333. To switch this off: >
  334. :set nohlsearch
  335. Then you need to switch it on again if you want to use it for the next search
  336. command. If you only want to remove the highlighting, use this command: >
  337. :nohlsearch
  338. This doesn't reset the option. Instead, it disables the highlighting. As
  339. soon as you execute a search command, the highlighting will be used again.
  340. Also for the "n" and "N" commands.
  341. TUNING SEARCHES
  342. There are a few options that change how searching works. These are the
  343. essential ones:
  344. >
  345. :set incsearch
  346. This makes Vim display the match for the string while you are still typing it.
  347. Use this to check if the right match will be found. Then press <Enter> to
  348. really jump to that location. Or type more to change the search string.
  349. >
  350. :set nowrapscan
  351. This stops the search at the end of the file. Or, when you are searching
  352. backwards, it stops the search at the start of the file. The 'wrapscan'
  353. option is on by default, thus searching wraps around the end of the file.
  354. INTERMEZZO
  355. If you like one of the options mentioned before, and set it each time you use
  356. Vim, you can put the command in your Vim startup file.
  357. Edit the file, as mentioned at |not-compatible|. Or use this command to
  358. find out where it is: >
  359. :scriptnames
  360. Edit the file, for example with: >
  361. :edit ~/.vimrc
  362. Then add a line with the command to set the option, just like you typed it in
  363. Vim. Example: >
  364. Go:set hlsearch<Esc>
  365. "G" moves to the end of the file. "o" starts a new line, where you type the
  366. ":set" command. You end insert mode with <Esc>. Then write and close the
  367. file: >
  368. ZZ
  369. If you now start Vim again, the 'hlsearch' option will already be set.
  370. ==============================================================================
  371. *03.9* Simple search patterns
  372. The Vim editor uses regular expressions to specify what to search for.
  373. Regular expressions are an extremely powerful and compact way to specify a
  374. search pattern. Unfortunately, this power comes at a price, because regular
  375. expressions are a bit tricky to specify.
  376. In this section we mention only a few essential ones. More about search
  377. patterns and commands can be found in chapter 27 |usr_27.txt|. You can find
  378. the full explanation here: |pattern|.
  379. BEGINNING AND END OF A LINE
  380. The ^ character matches the beginning of a line. On an English-US keyboard
  381. you find it above the 6. The pattern "include" matches the word include
  382. anywhere on the line. But the pattern "^include" matches the word include
  383. only if it is at the beginning of a line.
  384. The $ character matches the end of a line. Therefore, "was$" matches the
  385. word was only if it is at the end of a line.
  386. Let's mark the places where "/the" matches in this example line with "x"s:
  387. the solder holding one of the chips melted and the ~
  388. xxx xxx xxx
  389. Using "/the$" we find this match:
  390. the solder holding one of the chips melted and the ~
  391. xxx
  392. And with "/^the" we find this one:
  393. the solder holding one of the chips melted and the ~
  394. xxx
  395. You can try searching with "/^the$"; it will only match a single line
  396. consisting entirely of "the". White space does matter here, thus if a line
  397. contains a space after the word, like "the ", the pattern will not match.
  398. MATCHING ANY SINGLE CHARACTER
  399. The . (dot) character matches any existing character. For example, the
  400. pattern "c.m" matches a string whose first character is a c, whose second
  401. character is anything, and whose third character is m. Example:
  402. We use a computer that became the cummin winter. ~
  403. xxx xxx xxx
  404. MATCHING SPECIAL CHARACTERS
  405. If you really want to match a dot, you must avoid its special meaning by
  406. putting a backslash before it.
  407. If you search for "ter.", you will find these matches:
  408. We use a computer that became the cummin winter. ~
  409. xxxx xxxx
  410. Searching for "ter\." only finds the second match.
  411. ==============================================================================
  412. *03.10* Using marks
  413. When you make a jump to a position with the "G" command, Vim remembers the
  414. position from before this jump. This position is called a mark. To go back
  415. where you came from, use this command: >
  416. ``
  417. This ` is a backtick or open single-quote character.
  418. If you use the same command a second time you will jump back again. That's
  419. because the "`" command is a jump itself, and the position from before this
  420. jump is remembered.
  421. Generally, every time you do a command that can move the cursor further than
  422. within the same line, this is called a jump. This includes the search
  423. commands "/" and "n" (it doesn't matter how far away the match is). But not
  424. the character searches with "fx" and "tx" or the word movements "w" and "e".
  425. Also, "j" and "k" are not considered to be a jump, even when you use a
  426. count to make them move the cursor quite a long way away.
  427. The "``" command jumps back and forth, between two points. The CTRL-O command
  428. jumps to older positions (Hint: O for older). CTRL-I then jumps back to newer
  429. positions (Hint: for many common keyboard layouts, I is just next to O).
  430. Consider this sequence of commands: >
  431. 33G
  432. /^The
  433. CTRL-O
  434. You first jump to line 33, then search for a line that starts with "The".
  435. Then with CTRL-O you jump back to line 33. Another CTRL-O takes you back to
  436. where you started. If you now use CTRL-I you jump to line 33 again. And
  437. to the match for "The" with another CTRL-I.
  438. | example text ^ |
  439. 33G | example text | CTRL-O | CTRL-I
  440. | example text | |
  441. V line 33 text ^ V
  442. | example text | |
  443. /^The | example text | CTRL-O | CTRL-I
  444. V There you are | V
  445. example text
  446. Note:
  447. CTRL-I is the same as <Tab>.
  448. The ":jumps" command gives a list of positions you jumped to. The entry which
  449. you used last is marked with a ">".
  450. NAMED MARKS *bookmark*
  451. Vim enables you to place your own marks in the text. The command "ma" marks
  452. the place under the cursor as mark a. You can place 26 marks (a through z) in
  453. your text. You can't see them, it's just a position that Vim remembers.
  454. To go to a mark, use the command `{mark}, where {mark} is the mark letter.
  455. Thus to move to the a mark:
  456. >
  457. `a
  458. The command "'mark" (single quotation mark, or apostrophe) moves you to the
  459. beginning of the line containing the mark. This differs from the "`mark"
  460. command, which also moves you to the marked column.
  461. The marks can be very useful when working on two related parts in a file.
  462. Suppose you have some text near the start of the file you need to look at,
  463. while working on some text near the end of the file.
  464. Move to the text at the start and place the s (start) mark there: >
  465. ms
  466. Then move to the text you want to work on and put the e (end) mark there: >
  467. me
  468. Now you can move around, and when you want to look at the start of the file,
  469. you use this to jump there: >
  470. 's
  471. Then you can use '' to jump back to where you were, or 'e to jump to the text
  472. you were working on at the end.
  473. There is nothing special about using s for start and e for end, they are
  474. just easy to remember.
  475. You can use this command to get a list of marks: >
  476. :marks
  477. You will notice a few special marks. These include:
  478. ' The cursor position before doing a jump
  479. " The cursor position when last editing the file
  480. [ Start of the last change
  481. ] End of the last change
  482. ==============================================================================
  483. Next chapter: |usr_04.txt| Making small changes
  484. Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: