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

How do I put selected text inside quotes or brackets manually?

Answer

c""<Esc>P

Explanation

Without a surround plugin, you can manually wrap selected text by changing it, typing the delimiters, and pasting the original text back. This is a basic technique that works in vanilla Vim.

How it works

  1. Select the text in visual mode
  2. c changes (deletes and enters insert mode)
  3. Type the opening and closing delimiters: ""
  4. <Esc> returns to normal mode
  5. P pastes the deleted text before the cursor (between the delimiters)

Example

hello

Select hello with viw, then type c""<Esc>P:

"hello"

Tips

  • For parentheses: c()<Esc>P
  • For brackets: c[]<Esc>P
  • The vim-surround plugin makes this much easier
  • S" with vim-surround wraps the selection in quotes
  • This technique uses the default register, so be mindful of what you have yanked

Next

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