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

How do I quickly change surrounding quotes, brackets, or tags in Vim?

Answer

cs"'

Explanation

The vim-surround plugin by Tim Pope lets you change any surrounding delimiter pair with a single cs command. Instead of manually finding and replacing both the opening and closing characters, cs targets both at once.

How it works

  • c stands for change
  • s stands for surrounding
  • The first character is the existing surrounding
  • The second character is the replacement surrounding

So cs"' means: change surrounding " to '.

Example

Given the text with your cursor anywhere inside the quotes:

const name = "Hello world";

Pressing cs"' results in:

const name = 'Hello world';

Common combinations

cs"'    " double quotes to single quotes
cs'"    " single quotes to double quotes
cs)]    " parentheses to square brackets
cs}]    " curly braces to square brackets
cs]>    " square brackets to angle brackets
cst"    " any HTML/XML tag to double quotes
cs"<em> " double quotes to <em> tag

Spacing rules

Opening brackets (, {, [ add a space inside the new delimiters, while closing brackets ), }, ] do not:

" cs]) on (hello) gives:
[hello]

" cs]( on [hello] gives:
( hello )

Tips

  • Install vim-repeat alongside vim-surround so you can use . to repeat surround changes
  • Use cs with t to target HTML/XML tags: cst<div> replaces the nearest enclosing tag with <div>
  • The b alias works for () and B for {}, matching Vim's built-in text objects

Next

How do I edit multiple lines at once using multiple cursors in Vim?