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
ciwchanges 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 modePpastes 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>Pgives you one-key word quoting - The vim-surround plugin (
tpope/vim-surround) adds a dedicatedysoperator that makes this trivial:ysiw"surrounds the inner word with quotes,ysiw)with parentheses,ysiw>with angle brackets, and evenysiw<div>with HTML tags - To surround a visual selection instead of a word: select the text, then type
c""<Esc>Por use vim-surround'sS"in visual mode - To remove surrounding quotes, use
di"hPl2x— or with vim-surround, simplyds" - To change surrounding characters (e.g.,
"to'), vim-surround offerscs"'— 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