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

How do I add, change, or delete surrounding characters with vim-surround?

Answer

cs"' (change), ds" (delete), ys iw" (add)

Explanation

The vim-surround plugin by Tim Pope adds commands for working with surrounding characters like quotes, brackets, and tags.

How it works

  • cs"' changes double quotes to single quotes
  • ds" deletes surrounding double quotes
  • ysiw" adds double quotes around a word
  • yss" adds quotes around the entire line

Example

Starting with "Hello world":

  • cs"' changes to 'Hello world'
  • ds' removes quotes: Hello world
  • ysiw" on Hello: "Hello" world

Tips

  • S" in visual mode wraps the selection in quotes
  • cs"<p> changes quotes to <p> tags
  • Works with (, {, [, ", ', `, and HTML tags
  • cst" changes surrounding tags to quotes
  • Install: Plug 'tpope/vim-surround'

Next

How do you yank a single word into a named register?