{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`.
Changing {object} inside the loop affects what items
are used. Make a copy if this is unwanted: >
:for item in copy(mylist)
<
When {object} is a |List| and not making a copy, Vim
stores a reference to the next item in the |List|
before executing the commands with the current item.
Thus the current item can be removed without effect.
Removing any later item means it will not be found.
Thus the following example works (an inefficient way
to make a |List| empty): >
for item in mylist
call remove(mylist, 0)
endfor
< Note that reordering the |List| (e.g., with sort() or
reverse()) may have unexpected effects.
When {object} is a |Blob|, Vim always makes a copy to
iterate over. Unlike with |List|, modifying the
|Blob| does not affect the iteration.
When {object} is a |String| each item is a string with
one character, plus any combining characters.
:for [{var1}, {var2}, ...] in {listlist}
:endfo[r]
Like `:for` above, but each item in {listlist} must be
a list, of which each item is assigned to {var1},
{var2}, etc. Example: >
:for [lnum, col] in [[1, 3], [2, 5], [3, 8]]
:echo getline(lnum)[col]
:endfor
<
*:continue* *:con* *E586*
:con[tinue] When used inside a `:while` or `:for` loop, jumps back
to the start of the loop.
If it is used after a `:try` inside the loop but
before the matching `:finally` (if present), the
commands following the `:finally` up to the matching
`:endtry` are executed first. This process applies to
all nested `:try`s inside the loop. The outermost
`:endtry` then jumps back to the start of the loop.
*:break* *:brea* *E587*
:brea[k] When used inside a `:while` or `:for` loop, skips to
the command after the matching `:endwhile` or
`:endfor`.
If it is used after a `:try` inside the loop but
before the matching `:finally` (if present), the
commands following the `:finally` up to the matching
`:endtry` are executed first. This process applies to
all nested `:try`s inside the loop. The outermost
`:endtry` then jumps to the command after the loop.
:try *:try* *:endt* *:endtry* *E600* *E601* *E602*
:endt[ry] Change the error handling for the commands between
`:try` and `:endtry` including everything being
executed across `:source` commands, function calls,
or autocommand invocations.
When an error or interrupt is detected and there is
a `:finally` command following, execution continues
after the `:finally`. Otherwise, or when the
`:endtry` is reached thereafter, the next
(dynamically) surrounding `:try` is checked for
a corresponding `:finally` etc. Then the script
processing is terminated. Whether a function
definition has an "abort" argument does not matter.
Example: >
try | call Unknown() | finally | echomsg "cleanup" | endtry
echomsg "not reached"
<
Moreover, an error or interrupt (dynamically) inside
`:try` and `:endtry`