1

I use :%s/'/"/g to replace all single quotes ' with double quotes " in a plain-text file

but when I want to put this command in a vim macro in .vimrc

let @r=':%s/'/"/g'

opening vim will give me this error

E114: Missing quote: "/g'

E15: Invalid expression: ':%s/'/"/g'

Press ENTER or type command to continue

I've tried not including the surrounding single-quotes let @r=:%s/'/"/g

it still gives me a similar error.

how do I include commands with single-quotes in a vim macro?

3

you add another single quote to escape your single quote inside your vim macro

let @r=':%s/''/"/g'

from https://vim.fandom.com/wiki/Macros

if your register must itself contain single quote characters, you use a second single-quote character to escape it.

For example, if you want your register to contain "Vim's quote handling is a little tricky" you would type :let @a='"Vim''s quote handling is a little tricky"'

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.