6

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?

1 Answer 1

8

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"'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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