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
qa— start recordingciw— change inner word (deletes word, stores in register, enters insert mode)"— type opening quote<C-r>"— paste the deleted word from the default register"— type closing quote<Esc>— return to normal modew— move to next wordq— stop recording
Example
foo bar baz
After running on each word:
"foo" "bar" "baz"
Tips
- Use
@ato repeat on the next word - Use
binstead ofwto move backward - The vim-surround plugin does this more elegantly with
ysiw" - Change
"to'for single quotes