Movatterモバイル変換


[0]ホーム

URL:


Visual

Nvim:help pages,generated fromsource using thetree-sitter-vimdoc parser.


Visual modeVisual-modevisual-mode
Visual mode is a flexible and easy way to select a piece of text for anoperator. It is the only way to select a block of text.
This is introduced in section04.4 of the user manual.

1. Using Visual modevisual-use

Using Visual mode consists of three parts:1. Mark the start of the text with "v", "V" orCTRL-V. The character under the cursor will be used as the start.2. Move to the end of the text. The text from the start of the Visual mode up to and including the character under the cursor is highlighted.3. Type an operator command. The highlighted characters will be operated upon.
Thehl-Visual group determines the highlighting of the visual selection.The'virtualedit' option can be used to allow positioning the cursor topositions where there is no actual character.
The highlighted text normally includes the character under the cursor.However, when the'selection' option is set to "exclusive" and the cursor isafter the Visual area, the character under the cursor is not included.
With "v" the text before the start position and after the end position willnot be highlighted. However, all uppercase and non-alpha operators, except"~" and "U", will work on whole lines anyway. See the list of operatorsbelow.
visual-block
WithCTRL-V (blockwise Visual mode) the highlighted text will be a rectanglebetween start position and the cursor. However, some operators work on wholelines anyway (see the list below). The change and substitute operators willdelete the highlighted text and then start insertion at the top leftposition.

2. Starting and stopping Visual modevisual-start

vcharwise-visual[count]vStart Visual mode per character.With [count] select the same number of characters orlines as used for the last Visual operation, but atthe current cursor position, multiplied by [count].When the previous Visual operation was on a block boththe width and height of the block are multiplied by[count].When there was no previous Visual operation [count]characters are selected. This is like moving thecursor right N * [count] characters. One less when'selection' is not "exclusive".
Vlinewise-visual[count]VStart Visual mode linewise.With [count] select the same number of lines as usedfor the last Visual operation, but at the currentcursor position, multiplied by [count]. When therewas no previous Visual operation [count] lines areselected.
CTRL-Vblockwise-visual[count]CTRL-VStart Visual mode blockwise.
If you use<Esc>, click the left mouse button or use any command thatdoes a jump to another buffer while in Visual mode, the highlighting stopsand no text is affected. Also when you hit "v" in charwise Visual mode,"CTRL-V" in blockwise Visual mode or "V" in linewise Visual mode. If you hitCTRL-Z the highlighting stops and the editor is suspended or a new shell isstartedCTRL-Z.
new mode after typing:v_vv_CTRL-Vv_V
old mode "v" "CTRL-V" "V"
Normal Visual blockwise Visual linewise VisualVisual Normal blockwise Visual linewise Visualblockwise Visual Visual Normal linewise Visuallinewise Visual Visual blockwise Visual Normal
gvv_gvreselect-VisualgvStart Visual mode with the same area as the previousarea and the same mode.In Visual mode the current and the previous Visualarea are exchanged.After using "p" or "P" in Visual mode the text thatwas put will be selected.
gnv_gngnSearch forward for the last used search pattern, likewithn, and start Visual mode to select the match.If the cursor is on the match, visually selects it.If an operator is pending, operates on the match.E.g., "dgn" deletes the text of the next match.If Visual mode is active, extends the selectionuntil the end of the next match.'wrapscan' applies.Note: Unliken the search direction does not dependon the previous search command.
gNv_gNgNLikegn but searches backward, like withN.
<LeftMouse>
<LeftMouse>Set the current cursor position. If Visual mode isactive it is stopped. Only when'mouse' optioncontains 'n' or 'a'. If the position is within'so'lines from the last line on the screen the text isscrolled up. If the position is within'so' linesfrom the first line on the screen the text is scrolleddown.
<RightMouse>
<RightMouse>Start Visual mode if it is not active. The text fromthe cursor position to the position of the click ishighlighted. If Visual mode was already active movethe start or end of the highlighted text, whicheveris closest, to the position of the click. Only when'mouse' option contains 'n' or 'a'.
Note: when'mousemodel' is set to "popup",<S-LeftMouse> has to be used instead of<RightMouse>.
<LeftRelease>
<LeftRelease>This works like a<LeftMouse>, if it is not atthe same position as<LeftMouse>. In an older versionof xterm you won't see the selected area until thebutton is released, unless there is access to thedisplay where the xterm is running (via the DISPLAYenvironment variable or the -display argument). Onlywhen'mouse' option contains 'n' or 'a'.
If Visual mode is not active and the "v", "V" orCTRL-V is preceded with acount, the size of the previously highlighted area is used for a start. Youcan then move the end of the highlighted area and give an operator. The typeof the old area is used (character, line or blockwise).
Linewise Visual mode: The number of lines is multiplied with the count.
Blockwise Visual mode: The number of lines and columns is multiplied with the count.
Normal Visual mode within one line: The number of characters is multiplied with the count.
Normal Visual mode with several lines: The number of lines is multiplied with the count, in the last line the same number of characters is used as in the last line in the previously highlighted area.The start of the text is the Cursor position. If the "$" command was used asone of the last commands to extend the highlighted text, the area will beextended to the rightmost column of the longest line.
If you want to highlight exactly the same area as the last time, you can use"gv"gvv_gv.
v_<Esc>
<Esc>In Visual mode: Stop Visual mode.v_METAv_ALTALT (META) may act like<Esc> if the chord is not mapped.For example<A-x> acts like<Esc>x if<A-x> does not have avisual-mode mapping.
v_CTRL-C
CTRL-CIn Visual mode: Stop Visual mode. When insert mode ispending (the mode message shows"-- (insert) VISUAL --"), it is also stopped.On MS-Windows, you may need to pressCTRL-Break.

3. Changing the Visual areavisual-change

v_o
oGo to Other end of highlighted text: The currentcursor position becomes the start of the highlightedtext and the cursor is moved to the other end of thehighlighted text. The highlighted area remains thesame.
v_O
OGo to Other end of highlighted text. This is like"o", but in Visual block mode the cursor moves to theother corner in the same line. When the corner is ata character that occupies more than one position onthe screen (e.g., a<Tab>), the highlighted text maychange.
v_$
When the "$" command is used with blockwise Visual mode, the right end of thehighlighted text will be determined by the longest highlighted line. Thisstops when a motion command is used that does not move straight up or down.
For moving the end of the block many commands can be used, but you cannotuse Ex commands, commands that make changes or abandon the file. Commands(starting with) ".", "&",CTRL-^, "Z",CTRL-],CTRL-T,CTRL-R,CTRL-IandCTRL-O cause a beep and Visual mode continues.
When switching to another window on the same buffer, the cursor position inthat window is adjusted, so that the same Visual area is still selected. Thisis especially useful to view the start of the Visual area in one window, andthe end in another. You can then use<RightMouse> (or<S-LeftMouse> when'mousemodel' is "popup") to drag either end of the Visual area.

4. Operating on the Visual areavisual-operators

The operators that can be used are:~switch casev_~ ddeletev_d cchange (4)v_c yyankv_y >shift right (4)v_> <shift left (4)v_< !filter through external command (1)v_! =filter through'equalprg' option command (1)v_= gqformat lines to'textwidth' length (1)v_gq
The objects that can be used are:awa word (with white space)v_aw iwinner wordv_iw aWa WORD (with white space)v_aW iWinner WORDv_iW asa sentence (with white space)v_as isinner sentencev_is apa paragraph (with white space)v_ap ipinner paragraphv_ip aba () block (with parentheses)v_ab ibinner () blockv_ib aBa {} block (with braces)v_aB iBinner {} blockv_iB ata<tag> </tag> block (with tags)v_at itinner<tag> </tag> blockv_it a<a <> block (with <>)v_a< i<inner <> blockv_i< a[a [] block (with [])v_a[ i[inner [] blockv_i[ a"a double quoted string (with quotes)v_aquote i"inner double quoted stringv_iquote a'a single quoted string (with quotes)v_a' i'inner simple quoted stringv_i' a`a string in backticks (with backticks)v_a` i`inner string in backticksv_i`
Additionally the following commands can be used::start Ex command for highlighted lines (1)v_: rchange (4)v_r schangev_s Cchange (2)(4)v_C Schange (2)v_S Rchange (2)v_R xdeletev_x Ddelete (3)v_D Xdelete (2)v_X Yyank (2)v_Y pputv_p Pput without overwriting registersv_P Jjoin (1)v_J Umake uppercasev_U umake lowercasev_u ^]find tagv_CTRL-] Iblock insertv_b_I Ablock appendv_b_A
(1): Always whole lines, see:visual_example.(2): Whole lines when not usingCTRL-V.(3): Whole lines when not usingCTRL-V, delete until the end of the line when usingCTRL-V.(4): When usingCTRL-V operates on the block only.
Note that the ":vmap" command can be used to specifically map keys in Visualmode. For example, if you would like the "/" command not to extend the Visualarea, but instead take the highlighted text and search for that:
:vmap / y/<C-R>"<CR>
(In the <> notation<>, when typing it you should type it literally; youneed to remove the 'B' flag from'cpoptions'.)
If you want to give a register name using the """ command, do this just beforetyping the operator character: "v{move-around}"xd".
If you want to give a count to the command, do this just before typing theoperator character: "v{move-around}3>" (move lines 3 indents to the right).
{move-around}
The{move-around} is any sequence of movement commands. Note the differencewith{motion}, which is only ONE movement command.
Another way to operate on the Visual area is using the/\%V item in apattern. For example, to replace all '(' in the Visual area with '#':
:'<,'>s/\%V(/#/g
Note that the "'<,'>" will appear automatically when you press ":" in Visualmode.

5. Blockwise operatorsblockwise-operators

Reminder: Use'virtualedit' to be able to select blocks that start or endafter the end of a line or halfway through a tab.
Visual-block Insertv_b_I
With a blockwise selection, I{string}<ESC> will insert{string} at the startof block on every line of the block, provided that the line extends into theblock. Thus lines that are short will remain unmodified. TABs are split toretain visual columns. Works only for adding text to a line, not fordeletions. Seev_b_I_example.
Visual-block Appendv_b_A
With a blockwise selection, A{string}<ESC> will append{string} to the end ofblock on every line of the block. There is some differing behavior where theblock RHS is not straight, due to different line lengths:
1. Block was created with<C-v>$ In this case the string is appended to the end of each line.2. Block was created with<C-v>{move-around} In this case the string is appended to the end of the block on each line, and whitespace is inserted to pad to the end-of-block column.Seev_b_A_example.Note: "I" and "A" behave differently for lines that don't extend into theselected block. This was done intentionally, so that you can do it the wayyou want.Works only for adding text to a line, not for deletions.
Visual-block changev_b_c
All selected text in the block will be replaced by the same text string. Whenusing "c" the selected text is deleted and Insert mode started. You can thenenter text (without a line break). When you hit<Esc>, the same string isinserted in all previously selected lines.
Visual-block Changev_b_C
Like using "c", but the selection is extended until the end of the line forall lines.
v_b_<
Visual-block Shiftv_b_>
The block is shifted by'shiftwidth'. The RHS of the block is irrelevant.The LHS of the block determines the point from which to apply a right shift,and padding includes TABs optimally according to'ts' and'et'. The LHS ofthe block determines the point up to which to shift left.Seev_b_>_example.Seev_b_<_example.
Visual-block Replacev_b_r
Every screen char in the highlighted region is replaced with the same char, ieTABs are split and the virtual whitespace is replaced, maintaining screenlayout.Seev_b_r_example.

6. Repeatingvisual-repeat

When repeating a Visual mode operator, the operator will be applied to thesame amount of text as the last time:
Linewise Visual mode: The same number of lines.
Blockwise Visual mode: The same number of lines and columns.
Normal Visual mode within one line: The same number of characters.
Normal Visual mode with several lines: The same number of lines, in the last line the same number of characters as in the last line the last time.The start of the text is the Cursor position. If the "$" command was used asone of the last commands to extend the highlighted text, the repeating willbe applied up to the rightmost column of the longest line. Any count passedto the. command is not used.
Visual modedefault-mappings "@" and "Q" repeat a register for all selectedlines if the selection is linewise. Seev_@-default andv_Q-default fordetails. For example, given the text:
123(hello)321456(world)654456(NOT THIS)654
With register "x" containing the commandsyi(VP, visually selecting thefirst two lines and typing@x produces:
helloworld456(NOT THIS)654

7. Examplesvisual-examples

:visual_example
Currently the ":" command works on whole lines only. When you select part ofa line, doing something like ":!date" will replace the whole line. If youwant only part of the line to be replaced you will have to make a mapping forit. In a future release ":" may work on partial lines.
Here is an example, to replace the selected text with the output of "date":
:vmap _a <Esc>`>a<CR><Esc>`<i<CR><Esc>!!date<CR>kgJgJ
(In the <> notation<>, when typing it you should type it literally; youneed to remove the 'B' flag from'cpoptions')
What this does is:<Esc>stop Visual mode>go to the end of the Visual areaa<CR><Esc>break the line after the Visual area<jump to the start of the Visual areai<CR><Esc>break the line before the Visual area!!date<CR>filter the Visual text through datekJJJoin the lines back together
visual-search
Here is an idea for a mapping that makes it possible to do a search for theselected text:
:vmap X y/<C-R>"<CR>
(In the <> notation<>, when typing it you should type it literally; youneed to remove the 'B' flag from'cpoptions')
Note that special characters (like '.' and "*") will cause problems.
Visual-block Examplesblockwise-examples
With the following text, I will indicate the commands to produce the block andthe results below. In all cases, the cursor begins on the 'a' in the firstline of the test text.The following modeline settings are assumed ":ts=8:sw=4:".
It will be helpful to:set hls/<TAB>where<TAB> is a real TAB. This helps visualise the operations.
The test text is:
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdef ghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
1. fo<C-v>3jISTRING<ESC>v_b_I_example
abcdefghijklmnSTRINGopqrstuvwxyzabc STRING defghijklmnopqrstuvwxyzabcdef ghi STRING jklmnopqrstuvwxyzabcdefghijklmnSTRINGopqrstuvwxyz
2. fo<C-v>3j$ASTRING<ESC>v_b_A_example
abcdefghijklmnopqrstuvwxyzSTRINGabcdefghijklmnopqrstuvwxyzSTRINGabcdef ghijklmnopqrstuvwxyzSTRINGabcdefghijklmnopqrstuvwxyzSTRING
3. fo<C-v>3j3l<..v_b_<_example
abcdefghijklmnopqrstuvwxyzabc defghijklmnopqrstuvwxyzabcdef ghi jklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
4. fo<C-v>3j>..v_b_>_example
abcdefghijklmn opqrstuvwxyzabc defghijklmnopqrstuvwxyzabcdef ghi jklmnopqrstuvwxyzabcdefghijklmn opqrstuvwxyz
5. fo<C-v>5l3jrXv_b_r_example
abcdefghijklmnXXXXXXuvwxyzabc XXXXXXhijklmnopqrstuvwxyzabcdef ghi XXXXXX jklmnopqrstuvwxyzabcdefghijklmnXXXXXXuvwxyz

8. Select modeSelectSelect-mode

Select mode looks like Visual mode, but the commands accepted are quitedifferent. This resembles the selection mode in Microsoft Windows.When the'showmode' option is set, "-- SELECT --" is shown in the last line.
Entering Select mode:
Using the mouse to select an area, and'selectmode' contains "mouse".'mouse' must also contain a flag for the current mode.
Using a non-printable movement command, with the Shift key pressed, and'selectmode' contains "key". For example:<S-Left> and<S-End>.'keymodel' must also contain "startsel".
Using "v", "V" orCTRL-V command, and'selectmode' contains "cmd".
Using "gh", "gH" or "g_CTRL-H" command in Normal mode.
From Visual mode, pressCTRL-G.v_CTRL-G
Commands in Select mode:
Printable characters,<NL> and<CR> cause the selection to be deleted, and Vim enters Insert mode. The typed character is inserted.
Non-printable movement commands, with the Shift key pressed, extend the selection.'keymodel' must include "startsel".
Non-printable movement commands, with the Shift key NOT pressed, stop Select mode.'keymodel' must include "stopsel".
ESC stops Select mode.
CTRL-O switches to Visual mode for the duration of one command.v_CTRL-O
CTRL-G switches to Visual mode.
CTRL-R{register} selects the register to be used for the text that is deleted when typing text.v_CTRL-R
Unless you specify the "_" (black hole) register, the unnamed register is also overwritten.
Otherwise, typed characters are handled as in Visual mode.
When using an operator in Select mode, and the selection is linewise, theselected lines are operated upon, but like in charwise selection. Forexample, when a whole line is deleted, it can later be pasted in the middle ofa line.
Mappings and menus in Select mode.Select-mode-mapping
When mappings and menus are defined with the:vmap or:vmenu command theywork both in Visual mode and in Select mode. When these are used in Selectmode Vim automatically switches to Visual mode, so that the same behavior asin Visual mode is effective. If you don't want this use:xmap or:smap.
One particular edge case:
:vnoremap <C-K> <Esc>
This ends Visual mode when in Visual mode, but in Select mode it does notwork, because Select mode is restored after executing the mapped keys. Youneed to use:
:snoremap <C-K> <Esc>
Users will expect printable characters to replace the selected area.Therefore avoid mapping printable characters in Select mode. Or use:sunmap after:map and:vmap to remove it for Select mode.
After the mapping or menu finishes, the selection is enabled again and Selectmode entered, unless the selected area was deleted, another buffer becamethe current one or the window layout was changed.
When a character was typed that causes the selection to be deleted and Insertmode started, Insert mode mappings are applied to this character. This maycause some confusion, because it means Insert mode mappings apply to acharacter typed in Select mode. Language mappings apply as well.
gVv_gVgVAvoid the automatic reselection of the Visual areaafter a Select mode mapping or menu has finished.Put this just before the end of the mapping or menu.At least it should be after any operations on theselection.
gh
ghStart Select mode, charwise. This is like "v",but starts Select mode instead of Visual mode.Mnemonic: "get highlighted".
gH
gHStart Select mode, linewise. This is like "V",but starts Select mode instead of Visual mode.Mnemonic: "get Highlighted".
g_CTRL-H
gCTRL-HStart Select mode, blockwise. This is likeCTRL-V,but starts Select mode instead of Visual mode.Mnemonic: "get Highlighted".
Main
Commands index
Quick reference

1. Using Visual mode
2. Starting and stopping Visual mode
3. Changing the Visual area
4. Operating on the Visual area
5. Blockwise operators
6. Repeating
7. Examples
8. Select mode

[8]ページ先頭

©2009-2025 Movatter.jp