What is the difference between single and double quoted strings?
Is there a way to get a variable expanded inside a string, or do they always need to be joined like: 'Hello, ' . s:name . '!'
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityWhat is the difference between single and double quoted strings?
Is there a way to get a variable expanded inside a string, or do they always need to be joined like: 'Hello, ' . s:name . '!'
The only difference between single and double quoted string is related to backslash. To display special characters like newline, bells, tabs, etc, you need to use double-quotes -> "\n"
.
Within a single-quoted string, '\'
is itself => '\n'
is a two-characters string (a backslash + n
). Within double quotes, you have to double it -> "\\"
, which makes them un-practical to define regexes.
Regarding string expansion, you have a few choices:
'Hello, ' . s:name . '!'
join(['Hello, ', s:name, '!'], '')
-- which we never useprintf('Hello %s!', s:name)
lh#fmt#printf('Hello %1!', s:name)
-- from lh-vim-lib, when we don't want to know about types, and when we don't need to format fields, but when we want to use formats like: 'Hello %1. How are you doing %1 this %2
'.'foo''bar'
, or use both kind of quotes: 'foo'."'".'bar'
– Luc Hermitte
Sep 28 '16 at 7:40