errors that are
useful, such as "E21: Cannot make changes, 'modifiable' is off".
Another useful mechanism is the ":finally" command: >
:let tmp = tempname()
:try
: exe ".,$write " .. tmp
: exe "!filter " .. tmp
: .,$delete
: exe "$read " .. tmp
:finally
: call delete(tmp)
:endtry
This filters the lines from the cursor until the end of the file through the
"filter" command, which takes a file name argument. No matter if the
filtering works, something goes wrong in between ":try" and ":finally" or the
user cancels the filtering by pressing CTRL-C, the "call delete(tmp)" is
always executed. This makes sure you don't leave the temporary file behind.
More information about exception handling can be found in the reference
manual: |exception-handling|.
==============================================================================
*41.10* Various remarks
Here is a summary of items that apply to Vim scripts. They are also mentioned
elsewhere, but form a nice checklist.
The end-of-line character depends on the system. For Vim scripts it is
recommended to always use the Unix fileformat. Lines are then separated with
the Newline character. This also works on any other system. That way you can
copy your Vim scripts from MS-Windows to Unix and they still work. See
|:source_crnl|. To be sure it is set right, do this before writing the file:
>
:setlocal fileformat=unix
When using "dos" fileformat, lines are separated with CR-NL, two characters.
The CR character causes various problems, better avoid this.
WHITE SPACE
Blank lines are allowed in a script and ignored.
Leading whitespace characters (blanks and TABs) are ignored, except when using
|:let-heredoc| without "trim".
Trailing whitespace is often ignored, but not always. One command that
includes it is `map`. You have to watch out for that, it can cause hard to
understand mistakes. A generic solution is to never use trailing white space,
unless you really need it.
To include a whitespace character in the value of an option, it must be
escaped by a "\" (backslash) as in the following example: >
:set tags=my\ nice\ file
The same example written as: >
:set tags=my nice file
will issue an error, because it is interpreted as: >
:set tags=my
:set nice
:set file
COMMENTS
The character " (the double quote mark) starts a comment. Everything after
and including this character until the end-of-line is considered a comment and
is ignored, except for commands that don't consider comments, as shown in
examples below. A comment can start on any character position on the line.
There is a little "catch" with comments for some commands. Examples: >
:abbrev dev development " shorthand
:map <F3> o#include " insert include
:execute cmd " do it
:!ls *.c " list C files
The abbreviation "dev" will be expanded to 'development " shorthand'. The
mapping of <F3> will actually be the whole line after the 'o# ....' including
the '" insert include'. The "execute" command will give an error. The "!"
command will send everything after it to the shell, causing an error for an
unmatched '"' character.
There can be no comment after ":map", ":abbreviate", ":execute" and "!"
commands (there are a few more commands with this restriction). For the
":map", ":abbreviate" and ":execute" commands there is a trick: >
:abbrev dev development|" shorthand
:map <F3> o#include|" insert include
:execute cmd |" do it
With the '|' character the command is separated from the next one. And that
next command is only a comment. For the last command you need to do two
things: |:execute| and use '|': >
:exe '!ls *.c' |" list C files
Notice that there is no white space before the '|' in the abbreviation and
mapping. For these commands, any character until the end-of-line or '|' is
included. As a consequence of this behavior, you don't always see that
trailing whitespace is included: >
:map <F4> o#include
To spot these problems, you can set the