Vim field guide · dot-files

vim-surround

Add, change, or delete surrounding characters — quotes, brackets, tags — with single keypresses.

Add a surrounding (ys)

ys{motion}{char}
KeypressBeforeAfter
ysiw"hello"hello"
ysiw'hello'hello'
ysiw(hello( hello )
ysiw)hello(hello)
ysiw[hello[ hello ]
ysiw]hello[hello]
ysiw{hello{ hello }
ysiw}hello{hello}
ysiwthelloprompts for a tag, e.g. <em>hello</em>

Opening bracket adds spaces; closing bracket does not.

Surround a whole line

yss"    →   surrounds the entire line (ignoring indent)

Change a surrounding (cs)

cs{old}{new}
KeypressBeforeAfter
cs"'"hello"'hello'
cs'('hello'( hello )
cs[{[hello]{hello}
cst"<em>hello</em>"hello"
cs"t"hello"prompts for a tag

Delete a surrounding (ds)

ds{char}
KeypressBeforeAfter
ds""hello"hello
ds(( hello )hello
dst<em>hello</em>hello

Visual mode (S)

Select text visually, then press S{char}:

v + select + S"    →   wraps selection in double quotes
v + select + St    →   wraps selection in an HTML tag

Common patterns

TaskKeys
Wrap word in double quotesysiw"
Wrap word in backticksysiw followed by a backtick
Remove quotes from stringds"
Switch single to double quotescs'"
Wrap line in parenthesesyss)
Wrap selection in a tagVS<div>