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