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

How do I record a macro to wrap each word in quotes?

Answer

qaciw"<C-r>""<Esc>wq

Explanation

This macro wraps the current word in double quotes by changing the word, inserting quotes around the original content, and moving to the next word.

How it works

  1. qa — start recording
  2. ciw — change inner word (deletes word, stores in register, enters insert mode)
  3. " — type opening quote
  4. <C-r>" — paste the deleted word from the default register
  5. " — type closing quote
  6. <Esc> — return to normal mode
  7. w — move to next word
  8. q — stop recording

Example

foo bar baz

After running on each word:

"foo" "bar" "baz"

Tips

  • Use @a to repeat on the next word
  • Use b instead of w to move backward
  • The vim-surround plugin does this more elegantly with ysiw"
  • Change " to ' for single quotes

Next

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