How do I change surrounding quotes or brackets from one type to another with vim-surround?
Answer
cs'"
Explanation
The vim-surround plugin (by Tim Pope) adds three powerful operators for working with surrounding delimiters — quotes, brackets, parentheses, and HTML tags. The cs command changes an existing surrounding from one delimiter to another in a single keystroke, which is far faster than manually deleting and retyping delimiters.
How it works
cs{from}{to}— change surrounding: replaces the{from}delimiter around the cursor with{to}.ds{char}— delete surrounding: removes the surrounding delimiter entirely.ys{motion}{char}— you surround: wraps the text covered by{motion}with{char}.- For HTML/XML tags, use
tas the target and type the replacement tag name.
Example
Before: 'Hello, world'
Command: cs'"
After: "Hello, world"
Before: "Hello, world"
Command: cs")
After: (Hello, world)
Before: (Hello, world)
Command: ds(
After: Hello, world
Before: Hello world
Command: ysiw[
After: [Hello] world
Tips
- Install with any plugin manager:
Plug 'tpope/vim-surround'(vim-plug) oruse 'tpope/vim-surround'(packer). - Using the opening delimiter (e.g.,
() adds a space inside:cs'(→( Hello ). Using the closing delimiter (e.g.,)) does not:cs')→(Hello). - For tags,
cst<div>changes surrounding tags to<div>. - Works seamlessly with the
.repeat command for applying the same change multiple times.