visual.txt 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. *visual.txt* Nvim
  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. Type |gO| to see the table of contents.
  8. ==============================================================================
  9. 1. Using Visual mode *visual-use*
  10. Using Visual mode consists of three parts:
  11. 1. Mark the start of the text with "v", "V" or CTRL-V.
  12. The character under the cursor will be used as the start.
  13. 2. Move to the end of the text.
  14. The text from the start of the Visual mode up to and including the
  15. character under the cursor is highlighted.
  16. 3. Type an operator command.
  17. The highlighted characters will be operated upon.
  18. The |hl-Visual| group determines the highlighting of the visual selection.
  19. The 'virtualedit' option can be used to allow positioning the cursor to
  20. positions where there is no actual character.
  21. The highlighted text normally includes the character under the cursor.
  22. However, when the 'selection' option is set to "exclusive" and the cursor is
  23. after the Visual area, the character under the cursor is not included.
  24. With "v" the text before the start position and after the end position will
  25. not be highlighted. However, all uppercase and non-alpha operators, except
  26. "~" and "U", will work on whole lines anyway. See the list of operators
  27. below.
  28. *visual-block*
  29. With CTRL-V (blockwise Visual mode) the highlighted text will be a rectangle
  30. between start position and the cursor. However, some operators work on whole
  31. lines anyway (see the list below). The change and substitute operators will
  32. delete the highlighted text and then start insertion at the top left
  33. position.
  34. ==============================================================================
  35. 2. Starting and stopping Visual mode *visual-start*
  36. *v* *charwise-visual*
  37. [count]v Start Visual mode per character.
  38. With [count] select the same number of characters or
  39. lines as used for the last Visual operation, but at
  40. the current cursor position, multiplied by [count].
  41. When the previous Visual operation was on a block both
  42. the width and height of the block are multiplied by
  43. [count].
  44. When there was no previous Visual operation [count]
  45. characters are selected. This is like moving the
  46. cursor right N * [count] characters. One less when
  47. 'selection' is not "exclusive".
  48. *V* *linewise-visual*
  49. [count]V Start Visual mode linewise.
  50. With [count] select the same number of lines as used
  51. for the last Visual operation, but at the current
  52. cursor position, multiplied by [count]. When there
  53. was no previous Visual operation [count] lines are
  54. selected.
  55. *CTRL-V* *blockwise-visual*
  56. [count]CTRL-V Start Visual mode blockwise.
  57. If you use <Esc>, click the left mouse button or use any command that
  58. does a jump to another buffer while in Visual mode, the highlighting stops
  59. and no text is affected. Also when you hit "v" in charwise Visual mode,
  60. "CTRL-V" in blockwise Visual mode or "V" in linewise Visual mode. If you hit
  61. CTRL-Z the highlighting stops and the editor is suspended or a new shell is
  62. started |CTRL-Z|.
  63. new mode after typing: *v_v* *v_CTRL-V* *v_V*
  64. old mode "v" "CTRL-V" "V" ~
  65. Normal Visual blockwise Visual linewise Visual
  66. Visual Normal blockwise Visual linewise Visual
  67. blockwise Visual Visual Normal linewise Visual
  68. linewise Visual Visual blockwise Visual Normal
  69. *gv* *v_gv* *reselect-Visual*
  70. gv Start Visual mode with the same area as the previous
  71. area and the same mode.
  72. In Visual mode the current and the previous Visual
  73. area are exchanged.
  74. After using "p" or "P" in Visual mode the text that
  75. was put will be selected.
  76. *gn* *v_gn*
  77. gn Search forward for the last used search pattern, like
  78. with `n`, and start Visual mode to select the match.
  79. If the cursor is on the match, visually selects it.
  80. If an operator is pending, operates on the match.
  81. E.g., "dgn" deletes the text of the next match.
  82. If Visual mode is active, extends the selection
  83. until the end of the next match.
  84. 'wrapscan' applies.
  85. Note: Unlike `n` the search direction does not depend
  86. on the previous search command.
  87. *gN* *v_gN*
  88. gN Like |gn| but searches backward, like with `N`.
  89. *<LeftMouse>*
  90. <LeftMouse> Set the current cursor position. If Visual mode is
  91. active it is stopped. Only when 'mouse' option
  92. contains 'n' or 'a'. If the position is within 'so'
  93. lines from the last line on the screen the text is
  94. scrolled up. If the position is within 'so' lines from
  95. the first line on the screen the text is scrolled
  96. down.
  97. *<RightMouse>*
  98. <RightMouse> Start Visual mode if it is not active. The text from
  99. the cursor position to the position of the click is
  100. highlighted. If Visual mode was already active move
  101. the start or end of the highlighted text, whichever
  102. is closest, to the position of the click. Only when
  103. 'mouse' option contains 'n' or 'a'.
  104. Note: when 'mousemodel' is set to "popup",
  105. <S-LeftMouse> has to be used instead of <RightMouse>.
  106. *<LeftRelease>*
  107. <LeftRelease> This works like a <LeftMouse>, if it is not at
  108. the same position as <LeftMouse>. In an older version
  109. of xterm you won't see the selected area until the
  110. button is released, unless there is access to the
  111. display where the xterm is running (via the DISPLAY
  112. environment variable or the -display argument). Only
  113. when 'mouse' option contains 'n' or 'a'.
  114. If Visual mode is not active and the "v", "V" or CTRL-V is preceded with a
  115. count, the size of the previously highlighted area is used for a start. You
  116. can then move the end of the highlighted area and give an operator. The type
  117. of the old area is used (character, line or blockwise).
  118. - Linewise Visual mode: The number of lines is multiplied with the count.
  119. - Blockwise Visual mode: The number of lines and columns is multiplied with
  120. the count.
  121. - Normal Visual mode within one line: The number of characters is multiplied
  122. with the count.
  123. - Normal Visual mode with several lines: The number of lines is multiplied
  124. with the count, in the last line the same number of characters is used as
  125. in the last line in the previously highlighted area.
  126. The start of the text is the Cursor position. If the "$" command was used as
  127. one of the last commands to extend the highlighted text, the area will be
  128. extended to the rightmost column of the longest line.
  129. If you want to highlight exactly the same area as the last time, you can use
  130. "gv" |gv| |v_gv|.
  131. *v_<Esc>*
  132. <Esc> In Visual mode: Stop Visual mode.
  133. *v_META* *v_ALT*
  134. ALT (|META|) may act like <Esc> if the chord is not mapped.
  135. For example <A-x> acts like <Esc>x if <A-x> does not have a
  136. visual-mode mapping.
  137. *v_CTRL-C*
  138. CTRL-C In Visual mode: Stop Visual mode. When insert mode is
  139. pending (the mode message shows
  140. "-- (insert) VISUAL --"), it is also stopped.
  141. On MS-Windows, you may need to press CTRL-Break.
  142. ==============================================================================
  143. 3. Changing the Visual area *visual-change*
  144. *v_o*
  145. o Go to Other end of highlighted text: The current
  146. cursor position becomes the start of the highlighted
  147. text and the cursor is moved to the other end of the
  148. highlighted text. The highlighted area remains the
  149. same.
  150. *v_O*
  151. O Go to Other end of highlighted text. This is like
  152. "o", but in Visual block mode the cursor moves to the
  153. other corner in the same line. When the corner is at
  154. a character that occupies more than one position on
  155. the screen (e.g., a <Tab>), the highlighted text may
  156. change.
  157. *v_$*
  158. When the "$" command is used with blockwise Visual mode, the right end of the
  159. highlighted text will be determined by the longest highlighted line. This
  160. stops when a motion command is used that does not move straight up or down.
  161. For moving the end of the block many commands can be used, but you cannot
  162. use Ex commands, commands that make changes or abandon the file. Commands
  163. (starting with) ".", "&", CTRL-^, "Z", CTRL-], CTRL-T, CTRL-R, CTRL-I
  164. and CTRL-O cause a beep and Visual mode continues.
  165. When switching to another window on the same buffer, the cursor position in
  166. that window is adjusted, so that the same Visual area is still selected. This
  167. is especially useful to view the start of the Visual area in one window, and
  168. the end in another. You can then use <RightMouse> (or <S-LeftMouse> when
  169. 'mousemodel' is "popup") to drag either end of the Visual area.
  170. ==============================================================================
  171. 4. Operating on the Visual area *visual-operators*
  172. The operators that can be used are:
  173. ~ switch case |v_~|
  174. d delete |v_d|
  175. c change (4) |v_c|
  176. y yank |v_y|
  177. > shift right (4) |v_>|
  178. < shift left (4) |v_<|
  179. ! filter through external command (1) |v_!|
  180. = filter through 'equalprg' option command (1) |v_=|
  181. gq format lines to 'textwidth' length (1) |v_gq|
  182. The objects that can be used are:
  183. aw a word (with white space) |v_aw|
  184. iw inner word |v_iw|
  185. aW a WORD (with white space) |v_aW|
  186. iW inner WORD |v_iW|
  187. as a sentence (with white space) |v_as|
  188. is inner sentence |v_is|
  189. ap a paragraph (with white space) |v_ap|
  190. ip inner paragraph |v_ip|
  191. ab a () block (with parentheses) |v_ab|
  192. ib inner () block |v_ib|
  193. aB a {} block (with braces) |v_aB|
  194. iB inner {} block |v_iB|
  195. at a <tag> </tag> block (with tags) |v_at|
  196. it inner <tag> </tag> block |v_it|
  197. a< a <> block (with <>) |v_a<|
  198. i< inner <> block |v_i<|
  199. a[ a [] block (with []) |v_a[|
  200. i[ inner [] block |v_i[|
  201. a" a double quoted string (with quotes) |v_aquote|
  202. i" inner double quoted string |v_iquote|
  203. a' a single quoted string (with quotes) |v_a'|
  204. i' inner simple quoted string |v_i'|
  205. a` a string in backticks (with backticks) |v_a`|
  206. i` inner string in backticks |v_i`|
  207. Additionally the following commands can be used:
  208. : start Ex command for highlighted lines (1) |v_:|
  209. r change (4) |v_r|
  210. s change |v_s|
  211. C change (2)(4) |v_C|
  212. S change (2) |v_S|
  213. R change (2) |v_R|
  214. x delete |v_x|
  215. D delete (3) |v_D|
  216. X delete (2) |v_X|
  217. Y yank (2) |v_Y|
  218. p put |v_p|
  219. P put without overwriting registers |v_P|
  220. J join (1) |v_J|
  221. U make uppercase |v_U|
  222. u make lowercase |v_u|
  223. ^] find tag |v_CTRL-]|
  224. I block insert |v_b_I|
  225. A block append |v_b_A|
  226. (1): Always whole lines, see |:visual_example|.
  227. (2): Whole lines when not using CTRL-V.
  228. (3): Whole lines when not using CTRL-V, delete until the end of the line when
  229. using CTRL-V.
  230. (4): When using CTRL-V operates on the block only.
  231. Note that the ":vmap" command can be used to specifically map keys in Visual
  232. mode. For example, if you would like the "/" command not to extend the Visual
  233. area, but instead take the highlighted text and search for that: >
  234. :vmap / y/<C-R>"<CR>
  235. (In the <> notation |<>|, when typing it you should type it literally; you
  236. need to remove the 'B' flag from 'cpoptions'.)
  237. If you want to give a register name using the """ command, do this just before
  238. typing the operator character: "v{move-around}"xd".
  239. If you want to give a count to the command, do this just before typing the
  240. operator character: "v{move-around}3>" (move lines 3 indents to the right).
  241. *{move-around}*
  242. The {move-around} is any sequence of movement commands. Note the difference
  243. with {motion}, which is only ONE movement command.
  244. Another way to operate on the Visual area is using the |/\%V| item in a
  245. pattern. For example, to replace all '(' in the Visual area with '#': >
  246. :'<,'>s/\%V(/#/g
  247. Note that the "'<,'>" will appear automatically when you press ":" in Visual
  248. mode.
  249. ==============================================================================
  250. 5. Blockwise operators *blockwise-operators*
  251. Reminder: Use 'virtualedit' to be able to select blocks that start or end
  252. after the end of a line or halfway through a tab.
  253. Visual-block Insert *v_b_I*
  254. With a blockwise selection, I{string}<ESC> will insert {string} at the start
  255. of block on every line of the block, provided that the line extends into the
  256. block. Thus lines that are short will remain unmodified. TABs are split to
  257. retain visual columns. Works only for adding text to a line, not for
  258. deletions. See |v_b_I_example|.
  259. Visual-block Append *v_b_A*
  260. With a blockwise selection, A{string}<ESC> will append {string} to the end of
  261. block on every line of the block. There is some differing behavior where the
  262. block RHS is not straight, due to different line lengths:
  263. 1. Block was created with <C-v>$
  264. In this case the string is appended to the end of each line.
  265. 2. Block was created with <C-v>{move-around}
  266. In this case the string is appended to the end of the block on each line,
  267. and whitespace is inserted to pad to the end-of-block column.
  268. See |v_b_A_example|.
  269. Note: "I" and "A" behave differently for lines that don't extend into the
  270. selected block. This was done intentionally, so that you can do it the way
  271. you want.
  272. Works only for adding text to a line, not for deletions.
  273. Visual-block change *v_b_c*
  274. All selected text in the block will be replaced by the same text string. When
  275. using "c" the selected text is deleted and Insert mode started. You can then
  276. enter text (without a line break). When you hit <Esc>, the same string is
  277. inserted in all previously selected lines.
  278. Visual-block Change *v_b_C*
  279. Like using "c", but the selection is extended until the end of the line for
  280. all lines.
  281. *v_b_<*
  282. Visual-block Shift *v_b_>*
  283. The block is shifted by 'shiftwidth'. The RHS of the block is irrelevant. The
  284. LHS of the block determines the point from which to apply a right shift, and
  285. padding includes TABs optimally according to 'ts' and 'et'. The LHS of the
  286. block determines the point up to which to shift left.
  287. See |v_b_>_example|.
  288. See |v_b_<_example|.
  289. Visual-block Replace *v_b_r*
  290. Every screen char in the highlighted region is replaced with the same char, ie
  291. TABs are split and the virtual whitespace is replaced, maintaining screen
  292. layout.
  293. See |v_b_r_example|.
  294. ==============================================================================
  295. 6. Repeating *visual-repeat*
  296. When repeating a Visual mode operator, the operator will be applied to the
  297. same amount of text as the last time:
  298. - Linewise Visual mode: The same number of lines.
  299. - Blockwise Visual mode: The same number of lines and columns.
  300. - Normal Visual mode within one line: The same number of characters.
  301. - Normal Visual mode with several lines: The same number of lines, in the
  302. last line the same number of characters as in the last line the last time.
  303. The start of the text is the Cursor position. If the "$" command was used as
  304. one of the last commands to extend the highlighted text, the repeating will
  305. be applied up to the rightmost column of the longest line. Any count passed
  306. to the `.` command is not used.
  307. Visual mode |default-mappings| "@" and "Q" repeat a register for all selected
  308. lines if the selection is linewise. See |v_@-default| and |v_Q-default| for
  309. details. For example, given the text:
  310. 123(hello)321
  311. 456(world)654
  312. 456(NOT THIS)654
  313. With register "x" containing the commands `yi(VP`, visually selecting the
  314. first two lines and typing `@x` produces:
  315. hello
  316. world
  317. 456(NOT THIS)654
  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' flag 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' flag 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 charwise selection. For
  414. example, when a whole line is deleted, it can later be pasted in the middle of
  415. a line.
  416. Mappings and menus in Select mode. *Select-mode-mapping*
  417. When mappings and menus are defined with the |:vmap| or |:vmenu| command they
  418. work both in Visual mode and in Select mode. When these are used in Select
  419. mode Vim automatically switches to Visual mode, so that the same behavior as
  420. in Visual mode is effective. If you don't want this use |:xmap| or |:smap|.
  421. One particular edge case: >
  422. :vnoremap <C-K> <Esc>
  423. This ends Visual mode when in Visual mode, but in Select mode it does not
  424. work, because Select mode is restored after executing the mapped keys. You
  425. need to use: >
  426. :snoremap <C-K> <Esc>
  427. <
  428. Users will expect printable characters to replace the selected area.
  429. Therefore avoid mapping printable characters in Select mode. Or use
  430. |:sunmap| after |:map| and |:vmap| to remove it for Select mode.
  431. After the mapping or menu finishes, the selection is enabled again and Select
  432. mode entered, unless the selected area was deleted, another buffer became
  433. the current one or the window layout was changed.
  434. When a character was typed that causes the selection to be deleted and Insert
  435. mode started, Insert mode mappings are applied to this character. This may
  436. cause some confusion, because it means Insert mode mappings apply to a
  437. character typed in Select mode. Language mappings apply as well.
  438. *gV* *v_gV*
  439. gV Avoid the automatic reselection of the Visual area
  440. after a Select mode mapping or menu has finished.
  441. Put this just before the end of the mapping or menu.
  442. At least it should be after any operations on the
  443. selection.
  444. *gh*
  445. gh Start Select mode, charwise. This is like "v",
  446. but starts Select mode instead of Visual mode.
  447. Mnemonic: "get highlighted".
  448. *gH*
  449. gH Start Select mode, linewise. This is like "V",
  450. but starts Select mode instead of Visual mode.
  451. Mnemonic: "get Highlighted".
  452. *g_CTRL-H*
  453. g CTRL-H Start Select mode, blockwise. This is like CTRL-V,
  454. but starts Select mode instead of Visual mode.
  455. Mnemonic: "get Highlighted".
  456. vim:tw=78:ts=8:noet:ft=help:norl: