unlock a built-in variable you
will get an error message "E940: Cannot lock or unlock
variable {name}".
[depth] is relevant when locking a |List| or
|Dictionary|. It specifies how deep the locking goes:
0 Lock the variable {name} but not its
value.
1 Lock the |List| or |Dictionary| itself,
cannot add or remove items, but can
still change their values.
2 Also lock the values, cannot change
the items. If an item is a |List| or
|Dictionary|, cannot add or remove
items, but can still change the
values.
3 Like 2 but for the |List| /
|Dictionary| in the |List| /
|Dictionary|, one level deeper.
The default [depth] is 2, thus when {name} is a |List|
or |Dictionary| the values cannot be changed.
Example with [depth] 0: >
let mylist = [1, 2, 3]
lockvar 0 mylist
let mylist[0] = 77 " OK
call add(mylist, 4) " OK
let mylist = [7, 8, 9] " Error!
< *E743*
For unlimited depth use [!] and omit [depth].
However, there is a maximum depth of 100 to catch
loops.
Note that when two variables refer to the same |List|
and you lock one of them, the |List| will also be
locked when used through the other variable.
Example: >
:let l = [0, 1, 2, 3]
:let cl = l
:lockvar l
:let cl[1] = 99 " won't work!
< You may want to make a copy of a list to avoid this.
See |deepcopy()|.
:unlo[ckvar][!] [depth] {name} ... *:unlockvar* *:unlo*
Unlock the internal variable {name}. Does the
opposite of |:lockvar|.
No error is given if {name} does not exist.
:if {expr1} *:if* *:end* *:endif* *:en* *E171* *E579* *E580*
:en[dif] Execute the commands until the next matching `:else`
or `:endif` if {expr1} evaluates to non-zero.
Although the short forms work, it is recommended to
always use `:endif` to avoid confusion and to make
auto-indenting work properly.
From Vim version 4.5 until 5.0, every Ex command in
between the `:if` and `:endif` is ignored. These two
commands were just to allow for future expansions in a
backward compatible way. Nesting was allowed. Note
that any `:else` or `:elseif` was ignored, the `else`
part was not executed either.
You can use this to remain compatible with older
versions: >
:if version >= 500
: version-5-specific-commands
:endif
< The commands still need to be parsed to find the
`endif`. Sometimes an older Vim has a problem with a
new command. For example, `:silent` is recognized as
a `:substitute` command. In that case `:execute` can
avoid problems: >
:if version >= 600
: execute "silent 1,$delete"
:endif
<
NOTE: The `:append` and `:insert` commands don't work
properly in between `:if` and `:endif`.
*:else* *:el* *E581* *E583*
:el[se] Execute the commands until the next matching `:else`
or `:endif` if they previously were not being
executed.
*:elseif* *:elsei* *E582* *E584*
:elsei[f] {expr1} Short for `:else` `:if`, with the addition that there
is no extra `:endif`.
:wh[ile] {expr1} *:while* *:endwhile* *:wh* *:endw*
*E170* *E585* *E588* *E733*
:endw[hile] Repeat the commands between `:while` and `:endwhile`,
as long as {expr1} evaluates to non-zero.
When an error is detected from a command inside the
loop, execution continues after the `endwhile`.
Example: >
:let lnum = 1
:while lnum <= line("$")
:call FixLine(lnum)
:let lnum = lnum + 1
:endwhile
<
NOTE: The `:append` and `:insert` commands don't work
properly inside a `:while` and `:for` loop.
:for {var} in {object} *:for* *E690* *E732*
:endfo[r] *:endfo* *:endfor*
Repeat the commands between `:for` and `:endfor` for
each item in {object}. {object} can be a |List|,
a |Blob| or a |String|.
Variable {var} is set to the value of each item.
When an error is detected for a command inside the
loop, execution continues after the `endfor`.