@@ -17,8 +17,10 @@ script. There are a lot of them, therefore this is a long chapter.
1717| 41.6 | Using functions
1818| 41.7 | Defining a function
1919| 41.8 | Lists and Dictionaries
20- | 41.9 | Exceptions
21- | 41.10 | Various remarks
20+ | 41.9 | White space
21+ | 41.10 | Line continuation
22+ | 41.11 | Comments
23+ | 41.12 | Fileformat
2224
2325 Next chapter: | usr_42.txt | Add new menus
2426 Previous chapter: | usr_40.txt | Make new commands
@@ -112,7 +114,7 @@ the links if you are impatient.
112114TRYING OUT EXAMPLES
113115
114116You can easily try out most examples in these help files without saving the
115- commands in a file. For example, to try out the "for" loop above do this:
117+ commands to a file. For example, to try out the "for" loop above do this:
1161181. position the cursor on the "for"
1171192. start Visual mode with "v"
1181203. move down to the "endfor"
@@ -195,7 +197,7 @@ cannot start with a digit. Valid variable names are:
195197 CamelCaseName
196198 LENGTH
197199
198- Invalid names are "foo+ bar" and "6var".
200+ Invalid names are "foo. bar" and "6var".
199201
200202Some variables are global. To see a list of currently defined global
201203variables type this command: >
@@ -684,8 +686,8 @@ search() function uses its first argument as a search pattern and the second
684686one as flags. The "W" flag means the search doesn't wrap around the end of
685687the file.
686688
687- Using the `call ` command is optional in | Vim9 | script. This works the same
688- way and also works in legacy script and on the command line: >
689+ Using the `call ` command is optional in | Vim9 | script. It is required in
690+ legacy script and on the command line: >
689691
690692 call search("Date: ", "W")
691693
@@ -711,7 +713,7 @@ statements is equal to: >
711713
712714 :substitute/\a/*/g
713715
714- Using the functions becomes more interesting when you do more work before and
716+ Using the functions becomes interesting when you do more work before and
715717after the substitute() call.
716718
717719
@@ -1402,8 +1404,9 @@ Let's assign the variable "smaller" the value of the smallest number: >
14021404 smaller = num2
14031405 endif
14041406
1405- The variable "smaller" is a local variable. Variables used inside a function
1406- are local unless prefixed by something like "g:", "w:", or "s:".
1407+ The variable "smaller" is a local variable. It is declared to be a number,
1408+ that way Vim can warn you for any mistakes. Variables used inside a function
1409+ are local unless prefixed by something like "g:", "w:", or "b:".
14071410
14081411 Note:
14091412 To access a global variable from inside a function you must prepend
@@ -1469,6 +1472,10 @@ For a function that does not return anything simply leave out the return type: >
14691472 echo text
14701473 enddef
14711474
1475+ If you want to return any kind of value, you can use the "any" return type: >
1476+ def GetValue(): any
1477+ This disables type checking for the return value, use only when needed.
1478+
14721479It is also possible to define a legacy function with `function ` and
14731480`endfunction ` . These do not have types and are not compiled. Therefore they
14741481execute much slower.
@@ -1485,47 +1492,12 @@ once for every line in the range, with the cursor in that line. Example: >
14851492
14861493 If you call this function with: >
14871494
1488- :10,15call Number ()
1495+ :10,15Number ()
14891496
14901497 The function will be called six times, starting on line 10 and ending on line
1491149815.
14921499
14931500
1494- VARIABLE NUMBER OF ARGUMENTS
1495-
1496- Vim enables you to define functions that have a variable number of arguments.
1497- The following command, for instance, defines a function that must have 1
1498- argument (start) and can have up to 20 additional arguments: >
1499-
1500- def Show(start: string, ...items: list<string>)
1501-
1502- The variable "items" will be a list in the function containing the extra
1503- arguments. You can use it like any list, for example: >
1504-
1505- def Show(start: string, ...items: list<string>)
1506- echohl Title
1507- echo "start is " .. start
1508- echohl None
1509- for index in range(len(items))
1510- echon $" Arg {index} is {items[index]}"
1511- endfor
1512- echo
1513- enddef
1514-
1515- You can call it like this: >
1516-
1517- Show('Title', 'one', 'two', 'three')
1518- < start is Title Arg 0 is one Arg 1 is two Arg 2 is three ~
1519-
1520- This uses the `echohl ` command to specify the highlighting used for the
1521- following `echo ` command. `echohl None` stops it again. The `echon ` command
1522- works like `echo ` , but doesn't output a line break.
1523-
1524- If you call it with one argument the "items" list will be empty.
1525- `range (len (items ))` returns a list with the indexes, what `for ` loops over,
1526- we'll explain that further down.
1527-
1528-
15291501LISTING FUNCTIONS
15301502
15311503The `function ` command lists the names and arguments of all user-defined
@@ -1606,6 +1578,11 @@ Note that the name of a variable that holds a function reference must start
16061578with a capital. Otherwise it could be confused with the name of a builtin
16071579function.
16081580
1581+
1582+ FURTHER READING
1583+
1584+ Using a variable number of arguments is introduced in section | 50.2 | .
1585+
16091586More information about defining your own functions here: | user-functions | .
16101587
16111588==============================================================================
@@ -1766,95 +1743,16 @@ need to use a List, it stores items in an ordered sequence.
17661743For further reading see | Dictionaries | .
17671744
17681745==============================================================================
1769- *41.9* Exceptions
1770-
1771- Let's start with an example: >
1772-
1773- try
1774- read ~/templates/pascal.tmpl
1775- catch /E484:/
1776- echo "Sorry, the Pascal template file cannot be found."
1777- endtry
1778-
1779- The `read ` command will fail if the file does not exist. Instead of
1780- generating an error message, this code catches the error and gives the user a
1781- message with more information.
1782-
1783- For the commands in between `try ` and `endtry ` errors are turned into
1784- exceptions. An exception is a string. In the case of an error the string
1785- contains the error message. And every error message has a number. In this
1786- case, the error we catch contains "E484:". This number is guaranteed to stay
1787- the same (the text may change, e.g., it may be translated).
1788-
1789- Besides being able to give a nice error message, Vim will also continue
1790- executing commands after the `:endtry ` . Otherwise, once an uncaught error is
1791- encountered, execution of the script/function/mapping will be aborted.
1792-
1793- When the `read ` command causes another error, the pattern "E484:" will not
1794- match in it. Thus this exception will not be caught and result in the usual
1795- error message and execution is aborted.
1796-
1797- You might be tempted to do this: >
1798-
1799- try
1800- read ~/templates/pascal.tmpl
1801- catch
1802- echo "Sorry, the Pascal template file cannot be found."
1803- endtry
1804-
1805- This means all errors are caught. But then you will not see an error that
1806- would indicate a completely different problem, such as "E21: Cannot make
1807- changes, 'modifiable' is off". Think twice before you catch any error!
1808-
1809- Another useful mechanism is the `finally ` command: >
1810-
1811- var tmp = tempname()
1812- try
1813- exe ":.,$write " .. tmp
1814- exe "!filter " .. tmp
1815- :.,$delete
1816- exe ":$read " .. tmp
1817- finally
1818- delete(tmp)
1819- endtry
1820-
1821- This filters the lines from the cursor until the end of the file through the
1822- "filter" command, which takes a file name argument. No matter if the
1823- filtering works, if something goes wrong in between `try ` and `finally ` or the
1824- user cancels the filtering by pressing CTRL-C , the `delete (tmp)` call is
1825- always executed. This makes sure you don't leave the temporary file behind.
1826-
1827- The `finally ` does not catch the exception, the error will still abort
1828- further execution.
1829-
1830- More information about exception handling can be found in the reference
1831- manual: | exception-handling | .
1832-
1833- ==============================================================================
1834- *41.10* Various remarks
1835-
1836- Here are a few items that are useful to know when writing Vim scripts.
1837-
1838-
1839- FILEFORMAT
1840-
1841- The end-of-line character depends on the system. For Vim scripts it is
1842- recommended to always use the Unix fileformat. This also works on any other
1843- system. That way you can copy your Vim scripts from MS-Windows to Unix and
1844- they still work. See | :source_crnl | . To be sure it is set right, do this
1845- before writing the file: >
1846-
1847- :setlocal fileformat=unix
1848-
1849-
1850- WHITE SPACE
1746+ *41.9* White space
18511747
18521748Blank lines are allowed and ignored.
18531749
18541750Leading whitespace characters (blanks and TABs) are always ignored.
18551751
18561752Trailing whitespace is often ignored, but not always. One command that
1857- includes it is `map ` .
1753+ includes it is `map ` . You have to watch out for that, it can cause hard to
1754+ understand mistakes. A generic solution is to never use trailing white space,
1755+ unless you really need it.
18581756
18591757To include a whitespace character in the value of an option, it must be
18601758escaped by a "\" (backslash) as in the following example: >
@@ -1876,8 +1774,39 @@ intentionally to make sure scripts are easy to read and to avoid mistakes.
18761774If you use white space sensibly it will just work. When not you will get an
18771775error message telling you where white space is missing or should be removed.
18781776
1777+ ==============================================================================
1778+ *41.10* Line continuation
1779+
1780+ In legacy Vim script line continuation is done by preceding a continuation
1781+ line with a backslash: >
1782+ let mylist = [
1783+ \ 'one',
1784+ \ 'two',
1785+ \ ]
1786+
1787+ This requires the 'cpo' option to exclude the "C" flag. Normally this is done
1788+ by putting this at the start of the script: >
1789+ let s:save_cpo = &cpo
1790+ set cpo&vim
1791+
1792+ And restore the option at the end of the script: >
1793+ let &cpo = s:save_cpo
1794+ unlet s:save_cpo
1795+
1796+ A few more details can be found here: | line-continuation | .
1797+
1798+ In | Vim9 | script the backslash can still be used, but in most places it is not
1799+ needed: >
1800+ var mylist = [
1801+ 'one',
1802+ 'two',
1803+ ]
1804+
1805+ Also, the 'cpo' option does not need to be changed. See
1806+ | vim9-line-continuation | for details.
18791807
1880- COMMENTS
1808+ ==============================================================================
1809+ *41.11* Comments
18811810
18821811In | Vim9 | script the character # starts a comment. That character and
18831812everything after it until the end-of-line is considered a comment and
@@ -1933,11 +1862,21 @@ script executable, and it also works in legacy script: >
19331862 echo "this is a Vim script"
19341863 quit
19351864
1865+ ==============================================================================
1866+ *41.12* Fileformat
19361867
1937- Advance information about writing Vim script is in | usr_50.txt | .
1868+ The end-of-line character depends on the system. For Vim scripts it is
1869+ recommended to always use the Unix fileformat. This also works on any other
1870+ system. That way you can copy your Vim scripts from MS-Windows to Unix and
1871+ they still work. See | :source_crnl | . To be sure it is set right, do this
1872+ before writing the file: >
1873+
1874+ :setlocal fileformat=unix
19381875
19391876==============================================================================
19401877
1878+ Advance information about writing Vim script is in | usr_50.txt | .
1879+
19411880Next chapter: | usr_42.txt | Add new menus
19421881
19431882Copyright: see | manual-copyright | vim:tw=78:ts=8:noet:ft=help:norl:
0 commit comments