Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim(script) - quotes in strings when used as expressions

Tags:

vim

I'm trying to do the following:

:put = 'a string with "quotes"'

But I get:

Missing quote:  'a string
Invalid expression:  'a string

I can get around this:

:let s:var = 'a "var"'
:put = s:var

By reading :h :put, I found out that put, when followed by =, expects an expression. I don't know if this is the problem though, I thought strings were expressions (being values) like in many other languages.

Note this doesn't work either:

:put = "a string \"with quotes\""
like image 513
whatyouhide Avatar asked Jun 11 '14 13:06

whatyouhide


1 Answers

The key to understanding this peculiarity can indeed be found at :help :put:

You need to escape the '|' and '"' characters to prevent them from terminating the command.

Apparently, the unescaped " would terminate the expression (and presumably start a comment). Therefore, escaping works:

:put = 'a string with \"quotes\"'

You can also circumvent this by using (doubled) single quotes:

:put ='a string with ''quotes'''

If you want to use double quotes, they all need escaping, and the inner once twice:

:put = \"a string with \\"quotes\\"\"
like image 91
Ingo Karkat Avatar answered Oct 02 '22 00:10

Ingo Karkat