0

Sometimes I see examples of vimscript code, which uses / instead of " to delimit strings.

Are there any side effects to this, or is it always possible to do that? And what is the advantage of using slashes rather than double quotes?

One example is here: http://vimhelp.appspot.com/syntax.txt.html#E849 (scroll up a few lines)

6
  • 3
    I think it is used only to delimit patterns not strings: if you try to do :echo /foo/ it won't work. But in the substitutions commands for example you can use different characters (see my question here) if you scroll some more lines you'll see that the help also provides an example with + as patterns delimiters. – statox Nov 6 '16 at 17:19
  • @statox I believe I've answer this question "correctly" with a good explanation of the difference of patterns and strings. – Karl Yngve Lervåg Nov 7 '16 at 7:44
  • I think you should update the title of this question to "Usage of slash instead of quotes to delimit strings". Also, even though @grodzik's answer is good, I don't think he really addresses the main point of this question. In particular, reading the question and his answer may falsely invite people to believe that any characters may be used to delimit strings. – Karl Yngve Lervåg Nov 7 '16 at 7:45
  • @Karl Yngve Lervåg: example provided in question clearly shows what is the point of the question. My answer refers to the same document, which shows exact use case of it. Author may eventually add more details to the question to show it's exact intention - if the question generalises patterns to strings or refers to strings in every possible situation in Vim. – grodzik Nov 7 '16 at 9:06
  • @grodzik I agree that the example shows the point of the question, but I still think the combination of the question and your answer could be confusing for less experienced Vim users. It is also worth to note that the example requires that one follows the link. To make it clear, my motivation is only to increase the quality of the Q&A here, and I hope you are not offended by my claim that my answer was more correct in this particular case. – Karl Yngve Lervåg Nov 7 '16 at 21:10
6

The examples you are referring to are not examples of strings, but of patterns. A pattern may be delimited by any character (except alphanumeric characters), although the most usual one is /. This does not seem to be very well documented, but I found it described under :h E146, which reads as follows:

Instead of the '/' which surrounds the pattern and replacement string, you can use any other single-byte character, but not an alphanumeric character, '\', '"' or '|'. This is useful if you want to include a '/' in the search pattern or replacement string. Example:

  :s+/+//+

Strings, on the other hand, are either literal strings, e.g. 'this is a literal string', or "expression" strings, e.g. "this is an expression string". See :h expr-string and :h literal-string. The difference is that the expression string accepts special characters that are parsed, e.g. special keys such as "\<cr>", while literal strings are taken as is, except that double quotes are treated as a single quote.

3
  • 2
    So you are saying one can only use any non-alphanumeric character for patterns but has to use quotes and single quotes for strings, which are not patterns, while one may use quotes as well for patterns? – Zelphir Kaltstahl Nov 7 '16 at 9:36
  • @Zelphir: after clarification of the question I think this should rather be marked as correct anwser to your question as it explains it better and includes all cases, not only patterns. – grodzik Nov 7 '16 at 12:35
  • @Zelphir Exactly. Both single and double quotes count as non-alphanumeric characters. I see I made a minor mistake in the answer (saying "any character") - fixing right away. – Karl Yngve Lervåg Nov 7 '16 at 21:08
2

I don't think there are side effects. Sometimes it can be useful because you don't need to escape same sign that would be used inside. So if you have to search for "foo", using " as container, you would have to do it like this: ... start="\"foo\"", where with / (or any other): ... start=/"foo"/

This is explained in the same document but a bit lower: here

1
  • Wow, this is quote amazing. Almost anything can be used, except I guess alphanumerical characters. Although I guess it is best to stick to a maximum of 2 variations, in order to keep things readable. – Zelphir Kaltstahl Nov 6 '16 at 19:40

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.