Manipulating Selected Text
Advanced techniques for working with visual selections in Neovim.
Visual Mode Basics
Entering Visual Mode
v " Character-wise
V " Line-wise
Ctrl-v " Block-wise (column selection)
gv " Reselect last selection
Expanding Selection
o " Jump to other end of selection
O " Jump to other corner (block mode)
Text Objects
Text objects let you select semantic units of text. Use with v, d, c, y, etc.
Inner vs Around
| Prefix | Meaning |
|---|---|
i | Inner (excludes delimiters) |
a | Around (includes delimiters) |
Word Objects
iw " Inner word
aw " A word (includes surrounding space)
iW " Inner WORD (space-delimited)
aW " A WORD
Example: With cursor on “hello” in say hello world:
viwselectshellovawselectshello(with trailing space)
Delimiter Objects
i" " Inside double quotes
a" " Around double quotes (includes quotes)
i' " Inside single quotes
a' " Around single quotes
i` " Inside backticks
a` " Around backticks
Bracket Objects
i( or ib " Inside parentheses
a( or ab " Around parentheses
i{ or iB " Inside braces
a{ or aB " Around braces
i[ " Inside square brackets
a[ " Around square brackets
i< " Inside angle brackets
a< " Around angle brackets
Block Objects
it " Inside tag (HTML/XML)
at " Around tag
ip " Inner paragraph
ap " A paragraph
is " Inner sentence
as " A sentence
Combining with Operators
di" " Delete inside quotes
ci( " Change inside parentheses
ya{ " Yank around braces
>ip " Indent paragraph
Block Visual Mode
Block mode (Ctrl-v) enables column editing.
Insert on Multiple Lines
1. Ctrl-v " Enter block mode
2. Move to select column " j/k to extend selection
3. I " Insert mode (capital I)
4. Type text
5. Esc " Text appears on all lines
Append to Multiple Lines
1. Ctrl-v " Enter block mode
2. Select lines " j/k to extend
3. $ " Extend to end of each line
4. A " Append mode (capital A)
5. Type text
6. Esc " Text appears on all lines
Replace Column
1. Ctrl-v " Enter block mode
2. Select rectangle
3. c " Change
4. Type replacement
5. Esc
Delete Column
1. Ctrl-v " Enter block mode
2. Select rectangle
3. d " Delete
Case Changing
In Visual Mode
u " Lowercase selection
U " Uppercase selection
~ " Toggle case of selection
In Normal Mode
~ " Toggle case of character under cursor
g~w " Toggle case of word
g~iw " Toggle case of inner word
guw " Lowercase word
gUw " Uppercase word
gU$ " Uppercase to end of line
gu$ " Lowercase to end of line
gUU " Uppercase entire line
guu " Lowercase entire line
Joining Lines
Basic Join
J " Join current line with next (adds space)
gJ " Join without adding space
3J " Join next 3 lines
Visual Mode Join
1. V " Line-wise visual
2. Select lines
3. J " Join all selected lines
Indentation
Visual Mode
> " Indent selection
< " Unindent selection
= " Auto-indent selection
Repeat Indentation
. " Repeat last indent
2> " Indent twice
Normal Mode
>> " Indent current line
<< " Unindent current line
== " Auto-indent current line
>ip " Indent paragraph
=i{ " Auto-indent inside braces
Sorting and Filtering
Sort Selection
1. V " Select lines
2. : " Enter command (shows :'<,'>)
3. !sort " Sort alphabetically
Sort Options
:'<,'>!sort " Alphabetical
:'<,'>!sort -n " Numeric
:'<,'>!sort -r " Reverse
:'<,'>!sort -u " Unique
:'<,'>!sort -t',' -k2 " Sort by 2nd CSV column
Other Filters
:'<,'>!uniq " Remove adjacent duplicates
:'<,'>!column -t " Align as table
:'<,'>!tac " Reverse line order
:'<,'>!shuf " Shuffle lines
Surround Operations
These require the vim-surround plugin (or similar in Neovim).
Add Surroundings
ysiw" " Surround word with "
ysiw) " Surround word with ()
yss" " Surround entire line with "
Change Surroundings
cs"' " Change " to '
cs)] " Change () to []
cst<div> " Change tag to <div>
Delete Surroundings
ds" " Delete surrounding "
ds( " Delete surrounding ()
dst " Delete surrounding tag
Visual Mode Surround
1. Select text
2. S" " Surround selection with "
Formatting
Text Wrapping
gq " Format selection (wrap to textwidth)
gqq " Format current line
gqip " Format paragraph
gw " Format and keep cursor position
Set wrap width:
:set textwidth=80
Align Text
:'<,'>center " Center lines
:'<,'>right " Right-align
:'<,'>left " Left-align
Practical Examples
Comment Multiple Lines
1. Ctrl-v " Block mode
2. Select lines (j/k)
3. I " Insert
4. // " Comment syntax
5. Esc
Add Semicolons to Line Ends
1. Ctrl-v " Block mode
2. Select lines
3. $ " Go to end
4. A " Append
5. ;
6. Esc
Extract and Sort Unique Values
1. V " Select lines
2. :'<,'>!sort -u " Sort and dedupe
Change Variable Name in Block
1. Ctrl-v " Block mode
2. Select the variable column
3. c " Change
4. newName
5. Esc
Wrap Selection in Function
1. v " Select text
2. S) " Surround with parens (vim-surround)
3. i " Insert before
4. functionName
Tips
- Use
.to repeat - Visual operations can be repeated on new selections - Use
gvto reselect - Handy after an operation to do more - Block mode +
$- Handles uneven line lengths - Text objects are powerful - Learn
i",i{,itfor common editing - Filters are composable - Chain with
|like!sort | uniq
Tags: #neovim #cli