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

How do I wrap a word in quotes or brackets using vim-surround?

Answer

ysiw"

Explanation

The ysiw" command wraps the word under the cursor in double quotes using the vim-surround plugin. It combines ys ("you surround"), the iw text object (inner word), and the desired surrounding character. This is one of the most frequently used vim-surround operations for quickly quoting identifiers, function arguments, or configuration values.

How it works

  • ys — the "you surround" operator (adds a surrounding)
  • iw — the inner word text object (selects the word under the cursor)
  • " — the character to use as the surrounding delimiter

The plugin automatically adds the opening and closing delimiter. For paired characters like (, [, or {, vim-surround adds the matching closing character. Using the opening character (e.g., () adds spaces around the content; using the closing character (e.g., )) does not.

Example

Given the text with cursor on hello:

console.log(hello)

Pressing ysiw" produces:

console.log("hello")

To wrap in parentheses instead:

ysiw)

Result:

console.log((hello))

Tips

  • yss" wraps the entire current line (ignoring leading whitespace)
  • ys$" wraps from cursor to end of line
  • ysi" wraps inside an existing pair of " — useful for nested quoting
  • In visual mode, press S" to surround the selection
  • Works with any single character: ', `, (, [, {, <, and HTML/XML tags
  • To change an existing surrounding, use cs'" to replace ' with "
  • To delete a surrounding, use ds" to remove "

Next

How do I use capture groups in Vim substitutions to rearrange or swap matched text?