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

How do I quickly surround a word with quotes, brackets, or parentheses?

Answer

ciw""<Esc>P

Explanation

Vim doesn't have a built-in "surround" operator, but you can wrap any word in quotes or brackets with a short sequence: ciw""<Esc>P. This deletes the word into the unnamed register, types the surrounding characters, and pastes the word back between them. Once you internalize this pattern, you can surround anything with any delimiter in seconds.

How it works

  • ciw changes the inner word — deletes it into the unnamed register and enters insert mode
  • "" types two double quotes (the cursor is between them)
  • <Esc> returns to normal mode
  • P pastes the deleted word before the cursor, placing it between the quotes

The result is the original word wrapped in double quotes.

Example

Given the text with the cursor on hello:

return hello;

Press ciw""<Esc>P:

return "hello";

For parentheses, use ciw()<Esc>P:

return (hello);

For square brackets: ciw[]<Esc>P:

return [hello];

Alternative approaches

You can also use a two-step yank-based approach:

ysiw"    " Requires vim-surround plugin

Or a manual approach without clobbering the register:

bi"<Esc>ea"<Esc>

This inserts " before the word (bi"), then goes to the end and inserts " after (ea").

Tips

  • The bi"<Esc>ea"<Esc> approach is register-safe — it doesn't overwrite the unnamed register, making it preferable when your register contents matter
  • Map this for frequent use: nnoremap <leader>q ciw""<Esc>P gives you one-key word quoting
  • The vim-surround plugin (tpope/vim-surround) adds a dedicated ys operator that makes this trivial: ysiw" surrounds the inner word with quotes, ysiw) with parentheses, ysiw> with angle brackets, and even ysiw<div> with HTML tags
  • To surround a visual selection instead of a word: select the text, then type c""<Esc>P or use vim-surround's S" in visual mode
  • To remove surrounding quotes, use di"hPl2x — or with vim-surround, simply ds"
  • To change surrounding characters (e.g., " to '), vim-surround offers cs"' — changing double quotes to single quotes in one command
  • If you find yourself surrounding text frequently, installing vim-surround is highly recommended — it's one of the most universally loved Vim plugins

Next

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