visual.txt 21 KB

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