I would like to wrap a word in Vim with enclosing tags, such as from word
to 'word'
or from word
to (word)
.
Use Tim Pope's vim-surround, you can use: ysw'
to enclose a word with "`", ysw(
to surround the word with "(". If you want to surround the word with an actual tag such as the HTML tag <code>
, you can use yswt
, then type in your tag in the prompt. If you want to change existing surround elements, use csw
, type in the surround element you want to replace and hit <CR> (enter)
, and type the new surrounding element.
Note that for the commands above to work, you have to put the cursor in the beginning of the word you want to surround.
From its official page:
Press
cs"'
(that's c, s, double quote, single quote) inside"Hello world!"
to change it to
'Hello world!'
Now press
cs'<q>
to change it toHello world!
To go full circle, press
cat"
to get"Hello world!"
To remove the delimiters entirely, press
ds"
.Hello world!
Now with the cursor on "Hello", press
ysiw]
(iw
is a text object).[Hello] world!
Let's make that braces and add some space (use "}" instead of "{" for no space):
cs]{
{ Hello } world!
Now wrap the entire line in parentheses with
yssb
oryes)
.({ Hello } world!)
Revert to the original text:
ds{ds)
Hello world!
Emphasize hello:
ysiw<em>
Hello world!
Finally, let's try out visual mode. Press a capital
Hello world!V
(for linewise visual mode) followed byS<p class="important">
.
For more detailed usage info, install vim-surround
, and then use :help surround
to invoke the docs.