vimtricks.wiki Concise Vim tricks, one at a time.

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 t as 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) or use '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.

Next

How do I visually select a double-quoted string including the quotes themselves?