Could anyone please provide an explanation of the syntax in the following example, or post me a link where there is a more general explanation of the individual symbols used in this expression? I found Vim help to be incomplete in this regard.
:set foldexpr=getline(v:lnum)=~'^\\s*$'&&getline(v:lnum+1)=~'\\S'?'<1':1
What is unclear to me is the following.
- Why are strings enclosed in single quotes instead of double quotes? Is it a matter of choice?
- What does the explanation mark mean in
=~'\\S'?'<1':1? - What does the expression
'string1'string2'string3mean? - What does
:1mean?
The
foldexproption supposed to contain an expression that evaluatesinto an integer or a string of particular format that specifies the folding
level of the line which number is stored in the
v:lnumglobal variableat the moment of evaluation.
Let us follow the logic of this
foldexprexample from top to bottom.At the top level, the whole expression is an instance of the ternary
operator
A ? B : C. The result of the operator is the value of theBexpression ifAevaluates to non-zero, and the value of theCexpression otherwise (see:help expr1). In this case,Bisthe string literal
'<1', andCis the number1(for meaningof
'<1'and1as fold level specifiers see:help fold-expr).The
Aexpression consists of two conditions joined by the&&operator:Both conditions have the same form:
The
getlinefunction returns contents of the line (in the currentbuffer) that is referenced by the line number passed as an argument
(see
:help getline). When thefoldexpris evaluated, thev:lnumvariable contains number of the line for which folding level should
be calculated.
The
=~operator tests whether its left operand matches a regularexpression given by its right string operand, and returns boolean value
(see
:help expr4, in particular, near the end of theexpr4section).Thus, the
Acondition is intended to check that thev:lnum-th linematches the
'^\\s*$'pattern, and the line following thatv:lnum-thline matches the
'\\S'pattern.The regular expression patterns are specified in the expression as
string literals. String literals have two syntactic forms and can be
quoted using double or single quotes. The difference between these
forms is that double quoted string could contain various control
sequences which start with backslash. That sequences allow to specify
special characters that cannot be easily typed otherwise (double
quote, for example—it writes
\"). Single quoted strings, at theother hand, do not allow such backslash-sequences. (For complete
description of single and double quoted strings see
:help expr-stringand
:help literal-string.)The notable consequence of the double quoted strings syntax is that
backslash symbol itself must be escaped (
\\). That is why singlequoted strings are often used to specify regular expressions: there
is no need to escape constantly demanded backslash symbol. One can
notice, though, that backslashes are nevertheless escaped in those
patterns above. This is due to that some symbols (including backslash)
have special meaning when in Ex commands (including
:set, ofcourse). When you hit Enter to start the command
Vim interprets some character sequences first (see
:help cmdline-special).In particular, the
\\sequence is treated as a single backslash.Putting it all together, the expression tests whether the line number
v:lnumcontains only blank characters and whether the following line(number
v:lnum+1) has any non-blank character (see:help patternto grasp the meaning of the patterns). If so, the expression evaluates
to the string
'<1', otherwise it evaluates to the number1.