Surrounding Pairs

Deleting and Selecting Content between Pairs

As shown in Deleting and Selecting you can use Ctrl-f and Ctrl-g to both delete and select the contents between a pair of surrounding characters ({}, [], (), <>, "", '') or between a pair of HTML/XML tags:

Ctrl-f:

change the content between the closest enclosing pair of symbols/tags

Ctrl-g:

select the content between enclosing pair of symbols/tags (repeated presses select progressively outer pairs)

Adding Surrounding Pairs

You can also enclose a piece of selected text between a surrounding pair. To do so, use:

s <char>:

(in visual mode) surround current text selection with a pair of enclosing symbols defined by <char>, which can be one of {[("'>)]}, or also < and t for HTML/XML tags.

For example, s " surrounds the current selection with double quotes and s ) surrounds it with parenthesis.

Note that to surround text between angle brackets you need to use s > (as s < will enclose the text between HTML/XML tags):

# Selected text

A fair field full of folk

# Brackets added with: s >

A fair field <full> of folk

Adding extra space

For brackets and parenthesis, if you use the opening character (ie. {, [, (), a blank space is added between the surrounding symbols and the selected text. If you use the closing character, no space is added:

# Selected text

A fair field full of folk

# Parenthesis added with: s (

A fair field ( full ) of folk

# Parenthesis added with: s )

A fair field (full) of folk

The extra space is not added for any of the rest of the symbols or when using tags.

Position of surrounding pairs according to the type of selection

If you add a surrounding pair to a characterwise selection, the symbols will be added inline:

# Selected text (characterwise)

This line

# After s }

{This line}

However, if the selection is linewise, the symbols will be placed in their own lines (and the content indented):

# Selected text (linewise)

This line

# After s }

{
    This line
}

Finally, if the selection is blockwise, each line of the block is surrounded:

# Selected text (blockwise)

aaa bbb ccc
aaa bbb ccc
aaa bbb ccc

# After s }

aaa {bbb} ccc
aaa {bbb} ccc
aaa {bbb} ccc

Adding HTML/XML tags

To surround the selected text with an HTML/XML tag use either s < or s t. You'll be prompted in the command line for the complete tag and, as soon as you close the tag with > or press Enter, the selected text will be enclosed:

# Selected text

A fair field full of folk

# Tag added with: s <span class="foo">

A fair field <span class="foo">full</span> of folk

As you can see, you can add attributes inside the tag if you need to.

As an special case, if you press s Ctrl-t the operation will work in the same way but tags will be placed each one in their own lines.

Deleting Surrounding Pairs

To delete a pair of surrounding symbols, in normal mode, use:

Space d <char>:

delete the surrounding pair defined by <char>, which can be one of {[(<"'>)]} or t to delete a HTML/XML pair of tags.

If <char> is one of {, [, (, any white space between the text and the symbols is also removed when the surrounding pair is deleted. When using }, ], ) or any other symbol, the content within the pair is left untouched.

Changing Surrounding Pairs

To change a pair of surrounding symbols (eg. " ") by another (eg. ' '), use:

Space c <old> <new>:

change the surrounding pair defined by <old> by a new surrounding pair defined by <new>. Both <old> and <new> can be one of {[(<"'>)]} or t for HTML/XML pair of tags.

If <new> is one of {, [, (, an extra blank space is added between the text and the surrounding symbols.

In the case of tags, when you change a tag by another, the attributes are preserved if you press Enter to submit your new tag, and they are removed if you close your new tag with >:

# Selected text

A fair field <span class="foo">full</span> of folk

# Tag changed with: Space t <div Enter

A fair field <div class="foo">full</div> of folk

# Tag changed with: Space t <div>

A fair field <div>full</div> of folk