visual.txt 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. *visual.txt* For Vim version 9.0. Last change: 2022 Dec 04
  2. VIM REFERENCE MANUAL by Bram Moolenaar
  3. Visual mode *Visual* *Visual-mode* *visual-mode*
  4. Visual mode is a flexible and easy way to select a piece of text for an
  5. operator. It is the only way to select a block of text.
  6. This is introduced in section |04.4| of the user manual.
  7. 1. Using Visual mode |visual-use|
  8. 2. Starting and stopping Visual mode |visual-start|
  9. 3. Changing the Visual area |visual-change|
  10. 4. Operating on the Visual area |visual-operators|
  11. 5. Blockwise operators |blockwise-operators|
  12. 6. Repeating |visual-repeat|
  13. 7. Examples |visual-examples|
  14. 8. Select mode |Select-mode|
  15. {Since Vim 7.4.200 the |+visual| feature is always included}
  16. ==============================================================================
  17. 1. Using Visual mode *visual-use*
  18. Using Visual mode consists of three parts:
  19. 1. Mark the start of the text with "v", "V" or CTRL-V.
  20. The character under the cursor will be used as the start.
  21. 2. Move to the end of the text.
  22. The text from the start of the Visual mode up to and including the
  23. character under the cursor is highlighted.
  24. 3. Type an operator command.
  25. The highlighted characters will be operated upon.
  26. The 'highlight' option can be used to set the display mode to use for
  27. highlighting in Visual mode.
  28. The 'virtualedit' option can be used to allow positioning the cursor to
  29. positions where there is no actual character.
  30. The highlighted text normally includes the character under the cursor.
  31. However, when the 'selection' option is set to "exclusive" and the cursor is
  32. after the Visual area, the character under the cursor is not included.
  33. With "v" the text before the start position and after the end position will
  34. not be highlighted. However, all uppercase and non-alpha operators, except
  35. "~" and "U", will work on whole lines anyway. See the list of operators
  36. below.
  37. *visual-block*
  38. With CTRL-V (blockwise Visual mode) the highlighted text will be a rectangle
  39. between start position and the cursor. However, some operators work on whole
  40. lines anyway (see the list below). The change and substitute operators will
  41. delete the highlighted text and then start insertion at the top left
  42. position.
  43. ==============================================================================
  44. 2. Starting and stopping Visual mode *visual-start*
  45. *v* *characterwise-visual*
  46. [count]v Start Visual mode per character.
  47. With [count] select the same number of characters or
  48. lines as used for the last Visual operation, but at
  49. the current cursor position, multiplied by [count].
  50. When the previous Visual operation was on a block both
  51. the width and height of the block are multiplied by
  52. [count].
  53. When there was no previous Visual operation [count]
  54. characters are selected. This is like moving the
  55. cursor right N * [count] characters. One less when
  56. 'selection' is not "exclusive".
  57. *V* *linewise-visual*
  58. [count]V Start Visual mode linewise.
  59. With [count] select the same number of lines as used
  60. for the last Visual operation, but at the current
  61. cursor position, multiplied by [count]. When there
  62. was no previous Visual operation [count] lines are
  63. selected.
  64. *CTRL-V* *blockwise-visual*
  65. [count]CTRL-V Start Visual mode blockwise. Note: Under Windows
  66. CTRL-V could be mapped to paste text, it doesn't work
  67. to start Visual mode then, see |CTRL-V-alternative|.
  68. [count] is used as with `v` above.
  69. If you use <Esc>, click the left mouse button or use any command that
  70. does a jump to another buffer while in Visual mode, the highlighting stops
  71. and no text is affected. Also when you hit "v" in characterwise Visual mode,
  72. "CTRL-V" in blockwise Visual mode or "V" in linewise Visual mode. If you hit
  73. CTRL-Z the highlighting stops and the editor is suspended or a new shell is
  74. started |CTRL-Z|.
  75. new mode after typing: *v_v* *v_CTRL-V* *v_V*
  76. old mode "v" "CTRL-V" "V" ~
  77. Normal Visual blockwise Visual linewise Visual
  78. Visual Normal blockwise Visual linewise Visual
  79. blockwise Visual Visual Normal linewise Visual
  80. linewise Visual Visual blockwise Visual Normal
  81. *gv* *v_gv* *reselect-Visual*
  82. gv Start Visual mode with the same area as the previous
  83. area and the same mode.
  84. In Visual mode the current and the previous Visual
  85. area are exchanged.
  86. After using "p" or "P" in Visual mode the text that
  87. was put will be selected.
  88. *gn* *v_gn*
  89. gn Search forward for the last used search pattern, like
  90. with `n`, and start Visual mode to select the match.
  91. If the cursor is on the match, visually selects it.
  92. If an operator is pending, operates on the match.
  93. E.g., "dgn" deletes the text of the next match.
  94. If Visual mode is active, extends the selection
  95. until the end of the next match.
  96. 'wrapscan' applies.
  97. Note: Unlike `n` the search direction does not depend
  98. on the previous search command.
  99. *gN* *v_gN*
  100. gN Like |gn| but searches backward, like with `N`.
  101. *<LeftMouse>*
  102. <LeftMouse> Set the current cursor position. If Visual mode is
  103. active it is stopped. Only when 'mouse' option
  104. contains 'n' or 'a'. If the position is within 'so'
  105. lines from the last line on the screen the text is
  106. scrolled up. If the position is within 'so' lines from
  107. the first line on the screen the text is scrolled
  108. down.
  109. *<RightMouse>*
  110. <RightMouse> Start Visual mode if it is not active. The text from
  111. the cursor position to the position of the click is
  112. highlighted. If Visual mode was already active move
  113. the start or end of the highlighted text, whichever
  114. is closest, to the position of the click. Only when
  115. 'mouse' option contains 'n' or 'a'.
  116. Note: when 'mousemodel' is set to "popup",
  117. <S-LeftMouse> has to be used instead of <RightMouse>.
  118. *<LeftRelease>*
  119. <LeftRelease> This works like a <LeftMouse>, if it is not at
  120. the same position as <LeftMouse>. In an older version
  121. of xterm you won't see the selected area until the
  122. button is released, unless there is access to the
  123. display where the xterm is running (via the DISPLAY
  124. environment variable or the -display argument). Only
  125. when 'mouse' option contains 'n' or 'a'.
  126. <LeftMouseNM> Internal mouse code, used for clicking on the status
  127. <LeftReleaseNM> line to focus a window. NM stands for non-mappable.
  128. You cannot use these, but they might show up in some
  129. places.
  130. If Visual mode is not active and the "v", "V" or CTRL-V is preceded with a
  131. count, the size of the previously highlighted area is used for a start. You
  132. can then move the end of the highlighted area and give an operator. The type
  133. of the old area is used (character, line or blockwise).
  134. - Linewise Visual mode: The number of lines is multiplied with the count.
  135. - Blockwise Visual mode: The number of lines and columns is multiplied with
  136. the count.
  137. - Normal Visual mode within one line: The number of characters is multiplied
  138. with the count.
  139. - Normal Visual mode with several lines: The number of lines is multiplied
  140. with the count, in the last line the same number of characters is used as
  141. in the last line in the previously highlighted area.
  142. The start of the text is the Cursor position. If the "$" command was used as
  143. one of the last commands to extend the highlighted text, the area will be
  144. extended to the rightmost column of the longest line.
  145. If you want to highlight exactly the same area as the last time, you can use
  146. "gv" |gv| |v_gv|.
  147. *v_<Esc>*
  148. <Esc> In Visual mode: Stop Visual mode.
  149. *v_CTRL-C*
  150. CTRL-C In Visual mode: Stop Visual mode. When insert mode is
  151. pending (the mode message shows
  152. "-- (insert) VISUAL --"), it is also stopped.
  153. ==============================================================================
  154. 3. Changing the Visual area *visual-change*
  155. *v_o*
  156. o Go to Other end of highlighted text: The current
  157. cursor position becomes the start of the highlighted
  158. text and the cursor is moved to the other end of the
  159. highlighted text. The highlighted area remains the
  160. same.
  161. *v_O*
  162. O Go to Other end of highlighted text. This is like
  163. "o", but in Visual block mode the cursor moves to the
  164. other corner in the same line. When the corner is at
  165. a character that occupies more than one position on
  166. the screen (e.g., a <Tab>), the highlighted text may
  167. change.
  168. *v_$*
  169. When the "$" command is used with blockwise Visual mode, the right end of the
  170. highlighted text will be determined by the longest highlighted line. This
  171. stops when a motion command is used that does not move straight up or down.
  172. For moving the end of the block many commands can be used, but you cannot
  173. use Ex commands, commands that make changes or abandon the file. Commands
  174. (starting with) ".", "&", CTRL-^, "Z", CTRL-], CTRL-T, CTRL-R, CTRL-I
  175. and CTRL-O cause a beep and Visual mode continues.
  176. When switching to another window on the same buffer, the cursor position in
  177. that window is adjusted, so that the same Visual area is still selected. This
  178. is especially useful to view the start of the Visual area in one window, and
  179. the end in another. You can then use <RightMouse> (or <S-LeftMouse> when
  180. 'mousemodel' is "popup") to drag either end of the Visual area.
  181. ==============================================================================
  182. 4. Operating on the Visual area *visual-operators*
  183. The operators that can be used are:
  184. ~ switch case |v_~|
  185. d delete |v_d|
  186. c change (4) |v_c|
  187. y yank |v_y|
  188. > shift right (4) |v_>|
  189. < shift left (4) |v_<|
  190. ! filter through external command (1) |v_!|
  191. = filter through 'equalprg' option command (1) |v_=|
  192. gq format lines to 'textwidth' length (1) |v_gq|
  193. The objects that can be used are:
  194. aw a word (with white space) |v_aw|
  195. iw inner word |v_iw|
  196. aW a WORD (with white space) |v_aW|
  197. iW inner WORD |v_iW|
  198. as a sentence (with white space) |v_as|
  199. is inner sentence |v_is|
  200. ap a paragraph (with white space) |v_ap|
  201. ip inner paragraph |v_ip|
  202. ab a () block (with parentheses) |v_ab|
  203. ib inner () block |v_ib|
  204. aB a {} block (with braces) |v_aB|
  205. iB inner {} block |v_iB|
  206. at a <tag> </tag> block (with tags) |v_at|
  207. it inner <tag> </tag> block |v_it|
  208. a< a <> block (with <>) |v_a<|
  209. i< inner <> block |v_i<|
  210. a[ a [] block (with []) |v_a[|
  211. i[ inner [] block |v_i[|
  212. a" a double quoted string (with quotes) |v_aquote|
  213. i" inner double quoted string |v_iquote|
  214. a' a single quoted string (with quotes) |v_a'|
  215. i' inner simple quoted string |v_i'|
  216. a` a string in backticks (with backticks) |v_a`|
  217. i` inner string in backticks |v_i`|
  218. Additionally the following commands can be used:
  219. : start Ex command for highlighted lines (1) |v_:|
  220. r change (4) |v_r|
  221. s change |v_s|
  222. C change (2)(4) |v_C|
  223. S change (2) |v_S|
  224. R change (2) |v_R|
  225. x delete |v_x|
  226. D delete (3) |v_D|
  227. X delete (2) |v_X|
  228. Y yank (2) |v_Y|
  229. p put |v_p|
  230. P put without overwriting registers |v_P|
  231. J join (1) |v_J|
  232. U make uppercase |v_U|
  233. u make lowercase |v_u|
  234. ^] find tag |v_CTRL-]|
  235. I block insert |v_b_I|
  236. A block append |v_b_A|
  237. (1): Always whole lines, see |:visual_example|.
  238. (2): Whole lines when not using CTRL-V.
  239. (3): Whole lines when not using CTRL-V, delete until the end of the line when
  240. using CTRL-V.
  241. (4): When using CTRL-V operates on the block only.
  242. Note that the ":vmap" command can be used to specifically map keys in Visual
  243. mode. For example, if you would like the "/" command not to extend the Visual
  244. area, but instead take the highlighted text and search for that: >
  245. :vmap / y/<C-R>"<CR>
  246. (In the <> notation |<>|, when typing it you should type it literally; you
  247. need to remove the 'B' and '<' flags from 'cpoptions'.)
  248. If you want to give a register name using the """ command, do this just before
  249. typing the operator character: "v{move-around}"xd".
  250. If you want to give a count to the command, do this just before typing the
  251. operator character: "v{move-around}3>" (move lines 3 indents to the right).
  252. *{move-around}*
  253. The {move-around} is any sequence of movement commands. Note the difference
  254. with {motion}, which is only ONE movement command.
  255. Another way to operate on the Visual area is using the |/\%V| item in a
  256. pattern. For example, to replace all '(' in the Visual area with '#': >
  257. :'<,'>s/\%V(/#/g
  258. Note that the "'<,'>" will appear automatically when you press ":" in Visual
  259. mode.
  260. ==============================================================================
  261. 5. Blockwise operators *blockwise-operators*
  262. Reminder: Use 'virtualedit' to be able to select blocks that start or end
  263. after the end of a line or halfway a tab.
  264. Visual-block Insert *v_b_I*
  265. With a blockwise selection, I{string}<ESC> will insert {string} at the start
  266. of block on every line of the block, provided that the line extends into the
  267. block. Thus lines that are short will remain unmodified. TABs are split to
  268. retain visual columns. Works only for adding text to a line, not for
  269. deletions. See |v_b_I_example|.
  270. Visual-block Append *v_b_A*
  271. With a blockwise selection, A{string}<ESC> will append {string} to the end of
  272. block on every line of the block. There is some differing behavior where the
  273. block RHS is not straight, due to different line lengths:
  274. 1. Block was created with <C-v>$
  275. In this case the string is appended to the end of each line.
  276. 2. Block was created with <C-v>{move-around}
  277. In this case the string is appended to the end of the block on each line,
  278. and whitespace is inserted to pad to the end-of-block column.
  279. See |v_b_A_example|.
  280. Note: "I" and "A" behave differently for lines that don't extend into the
  281. selected block. This was done intentionally, so that you can do it the way
  282. you want.
  283. Works only for adding text to a line, not for deletions.
  284. Visual-block change *v_b_c*
  285. All selected text in the block will be replaced by the same text string. When
  286. using "c" the selected text is deleted and Insert mode started. You can then
  287. enter text (without a line break). When you hit <Esc>, the same string is
  288. inserted in all previously selected lines.
  289. Visual-block Change *v_b_C*
  290. Like using "c", but the selection is extended until the end of the line for
  291. all lines.
  292. *v_b_<*
  293. Visual-block Shift *v_b_>*
  294. The block is shifted by 'shiftwidth'. The RHS of the block is irrelevant. The
  295. LHS of the block determines the point from which to apply a right shift, and
  296. padding includes TABs optimally according to 'ts' and 'et'. The LHS of the
  297. block determines the point up to which to shift left.
  298. See |v_b_>_example|.
  299. See |v_b_<_example|.
  300. Visual-block Replace *v_b_r*
  301. Every screen char in the highlighted region is replaced with the same char, ie
  302. TABs are split and the virtual whitespace is replaced, maintaining screen
  303. layout.
  304. See |v_b_r_example|.
  305. ==============================================================================
  306. 6. Repeating *visual-repeat*
  307. When repeating a Visual mode operator, the operator will be applied to the
  308. same amount of text as the last time:
  309. - Linewise Visual mode: The same number of lines.
  310. - Blockwise Visual mode: The same number of lines and columns.
  311. - Normal Visual mode within one line: The same number of characters.
  312. - Normal Visual mode with several lines: The same number of lines, in the
  313. last line the same number of characters as in the last line the last time.
  314. The start of the text is the Cursor position. If the "$" command was used as
  315. one of the last commands to extend the highlighted text, the repeating will
  316. be applied up to the rightmost column of the longest line. Any count passed
  317. to the `.` command is not used.
  318. ==============================================================================
  319. 7. Examples *visual-examples*
  320. *:visual_example*
  321. Currently the ":" command works on whole lines only. When you select part of
  322. a line, doing something like ":!date" will replace the whole line. If you
  323. want only part of the line to be replaced you will have to make a mapping for
  324. it. In a future release ":" may work on partial lines.
  325. Here is an example, to replace the selected text with the output of "date": >
  326. :vmap _a <Esc>`>a<CR><Esc>`<i<CR><Esc>!!date<CR>kJJ
  327. (In the <> notation |<>|, when typing it you should type it literally; you
  328. need to remove the 'B' and '<' flags from 'cpoptions')
  329. What this does is:
  330. <Esc> stop Visual mode
  331. `> go to the end of the Visual area
  332. a<CR><Esc> break the line after the Visual area
  333. `< jump to the start of the Visual area
  334. i<CR><Esc> break the line before the Visual area
  335. !!date<CR> filter the Visual text through date
  336. kJJ Join the lines back together
  337. *visual-search*
  338. Here is an idea for a mapping that makes it possible to do a search for the
  339. selected text: >
  340. :vmap X y/<C-R>"<CR>
  341. (In the <> notation |<>|, when typing it you should type it literally; you
  342. need to remove the 'B' and '<' flags from 'cpoptions')
  343. Note that special characters (like '.' and '*') will cause problems.
  344. Visual-block Examples *blockwise-examples*
  345. With the following text, I will indicate the commands to produce the block and
  346. the results below. In all cases, the cursor begins on the 'a' in the first
  347. line of the test text.
  348. The following modeline settings are assumed ":ts=8:sw=4:".
  349. It will be helpful to
  350. :set hls
  351. /<TAB>
  352. where <TAB> is a real TAB. This helps visualise the operations.
  353. The test text is:
  354. abcdefghijklmnopqrstuvwxyz
  355. abc defghijklmnopqrstuvwxyz
  356. abcdef ghi jklmnopqrstuvwxyz
  357. abcdefghijklmnopqrstuvwxyz
  358. 1. fo<C-v>3jISTRING<ESC> *v_b_I_example*
  359. abcdefghijklmnSTRINGopqrstuvwxyz
  360. abc STRING defghijklmnopqrstuvwxyz
  361. abcdef ghi STRING jklmnopqrstuvwxyz
  362. abcdefghijklmnSTRINGopqrstuvwxyz
  363. 2. fo<C-v>3j$ASTRING<ESC> *v_b_A_example*
  364. abcdefghijklmnopqrstuvwxyzSTRING
  365. abc defghijklmnopqrstuvwxyzSTRING
  366. abcdef ghi jklmnopqrstuvwxyzSTRING
  367. abcdefghijklmnopqrstuvwxyzSTRING
  368. 3. fo<C-v>3j3l<.. *v_b_<_example*
  369. abcdefghijklmnopqrstuvwxyz
  370. abc defghijklmnopqrstuvwxyz
  371. abcdef ghi jklmnopqrstuvwxyz
  372. abcdefghijklmnopqrstuvwxyz
  373. 4. fo<C-v>3j>.. *v_b_>_example*
  374. abcdefghijklmn opqrstuvwxyz
  375. abc defghijklmnopqrstuvwxyz
  376. abcdef ghi jklmnopqrstuvwxyz
  377. abcdefghijklmn opqrstuvwxyz
  378. 5. fo<C-v>5l3jrX *v_b_r_example*
  379. abcdefghijklmnXXXXXXuvwxyz
  380. abc XXXXXXhijklmnopqrstuvwxyz
  381. abcdef ghi XXXXXX jklmnopqrstuvwxyz
  382. abcdefghijklmnXXXXXXuvwxyz
  383. ==============================================================================
  384. 8. Select mode *Select* *Select-mode*
  385. Select mode looks like Visual mode, but the commands accepted are quite
  386. different. This resembles the selection mode in Microsoft Windows.
  387. When the 'showmode' option is set, "-- SELECT --" is shown in the last line.
  388. Entering Select mode:
  389. - Using the mouse to select an area, and 'selectmode' contains "mouse".
  390. 'mouse' must also contain a flag for the current mode.
  391. - Using a non-printable movement command, with the Shift key pressed, and
  392. 'selectmode' contains "key". For example: <S-Left> and <S-End>. 'keymodel'
  393. must also contain "startsel".
  394. - Using "v", "V" or CTRL-V command, and 'selectmode' contains "cmd".
  395. - Using "gh", "gH" or "g_CTRL-H" command in Normal mode.
  396. - From Visual mode, press CTRL-G. *v_CTRL-G*
  397. Commands in Select mode:
  398. - Printable characters, <NL> and <CR> cause the selection to be deleted, and
  399. Vim enters Insert mode. The typed character is inserted.
  400. - Non-printable movement commands, with the Shift key pressed, extend the
  401. selection. 'keymodel' must include "startsel".
  402. - Non-printable movement commands, with the Shift key NOT pressed, stop Select
  403. mode. 'keymodel' must include "stopsel".
  404. - ESC stops Select mode.
  405. - CTRL-O switches to Visual mode for the duration of one command. *v_CTRL-O*
  406. - CTRL-G switches to Visual mode.
  407. - CTRL-R {register} selects the register to be used for the text that is
  408. deleted when typing text. *v_CTRL-R*
  409. Unless you specify the "_" (black hole) register, the unnamed register is
  410. also overwritten.
  411. Otherwise, typed characters are handled as in Visual mode.
  412. When using an operator in Select mode, and the selection is linewise, the
  413. selected lines are operated upon, but like in characterwise selection. For
  414. example, when a whole line is deleted, it can later be pasted halfway a line.
  415. Mappings and menus in Select mode. *Select-mode-mapping*
  416. When mappings and menus are defined with the |:vmap| or |:vmenu| command they
  417. work both in Visual mode and in Select mode. When these are used in Select
  418. mode Vim automatically switches to Visual mode, so that the same behavior as
  419. in Visual mode is effective. If you don't want this use |:xmap| or |:smap|.
  420. One particular edge case: >
  421. :vnoremap <C-K> <Esc>
  422. This ends Visual mode when in Visual mode, but in Select mode it does not
  423. work, because Select mode is restored after executing the mapped keys. You
  424. need to use: >
  425. :snoremap <C-K> <Esc>
  426. <
  427. Users will expect printable characters to replace the selected area.
  428. Therefore avoid mapping printable characters in Select mode. Or use
  429. |:sunmap| after |:map| and |:vmap| to remove it for Select mode.
  430. After the mapping or menu finishes, the selection is enabled again and Select
  431. mode entered, unless the selected area was deleted, another buffer became
  432. the current one or the window layout was changed.
  433. When a character was typed that causes the selection to be deleted and Insert
  434. mode started, Insert mode mappings are applied to this character. This may
  435. cause some confusion, because it means Insert mode mappings apply to a
  436. character typed in Select mode. Language mappings apply as well.
  437. *gV* *v_gV*
  438. gV Avoid the automatic reselection of the Visual area
  439. after a Select mode mapping or menu has finished.
  440. Put this just before the end of the mapping or menu.
  441. At least it should be after any operations on the
  442. selection.
  443. *gh*
  444. gh Start Select mode, characterwise. This is like "v",
  445. but starts Select mode instead of Visual mode.
  446. Mnemonic: "get highlighted".
  447. *gH*
  448. gH Start Select mode, linewise. This is like "V",
  449. but starts Select mode instead of Visual mode.
  450. Mnemonic: "get Highlighted".
  451. *g_CTRL-H*
  452. g CTRL-H Start Select mode, blockwise. This is like CTRL-V,
  453. but starts Select mode instead of Visual mode.
  454. Mnemonic: "get Highlighted".
  455. vim:tw=78:ts=8:noet:ft=help:norl: