usr_50.txt ForVim version 9.2. Last change: 2026 Feb 14 VIM USER MANUALbyBramMoolenaar Advanced Vimscriptwriting50.1 Exceptions50.2 Function with variable number of arguments50.3 Restoring theview Next chapter:usr_51.txt Createaplugin Previous chapter:usr_45.txtSelect your language (local)Table of contents:usr_toc.txt==============================================================================50.1 ExceptionsLet's start with an example:try read ~/templates/pascal.tmplcatch /E484:/ echo "Sorry, the Pascal template file cannot be found."endtryTheread command will fail if the file does not exist. Instead ofgenerating an error message, this code catches the error and gives the useramessage with more information.For the commands in betweentry andendtryerrors are turned intoexceptions. An exceptionisa string. In thecase of an error thestringcontains the error message. And every error message hasa number. In thiscase, the error we catch contains "E484:". This numberis guaranteed to staythe same (the text may change, e.g.,it may be translated).Besides being able to givea nice error message, Vim will also continueexecuting commands after the:endtry. Otherwise, once an uncaught errorisencountered, execution of the script/function/mapping will be aborted.When theread command causes another error, thepattern "E484:" will notmatch in it. Thus this exception will not be caught and result in the usualerror message and executionis aborted.You might be tempted todo this:try read ~/templates/pascal.tmplcatch echo "Sorry, the Pascal template file cannot be found."endtryThis means allerrors are caught. But then you will not see an error thatwould indicatea completely different problem, suchas "E21: Cannot makechanges,'modifiable'is off". Think twice before you catch any error!Another useful mechanismis thefinally command:var tmp = tempname()try exe ":.,$write " .. tmp exe "!filter " .. tmp :.,$delete exe ":$read " .. tmpfinally delete(tmp)endtryThis filters the lines from the cursor until theend of the file through the"filter" command, which takesa file name argument. No matter if thefiltering works, if something goes wrong in betweentry andfinally or theuser cancels the filtering by pressingCTRL-C, thedelete(tmp) callisalways executed. This makes sure you don't leave the temporary file behind.Thefinally does not catch the exception, the error will still abortfurther execution.More information about exception handling can be found in thereferencemanual:exception-handling.==============================================================================50.2 Function with variable number of argumentsVim enables you to definefunctions that havea variable number of arguments.The following command, for instance, definesa function thatmust have 1argument (start) and can have up to 20 additional arguments:def Show(start: string, ...items: list<string>)The variable "items" will bealist in the function containing the extraarguments. You can useit like any list, for example:def Show(start: string, ...items: list<string>) echohl Title echo "start is " .. start echohl None for index in range(len(items)) echon $" Arg {index} is {items[index]}" endfor echoenddefYou can callit like this:Show('Title', 'one', 'two', 'three')start is Title Arg 0 is one Arg 1 is two Arg 2 is threeThis uses theechohl command to specify the highlighting used for thefollowingecho command. `echohl None` stopsit again. Theechon commandworks likeecho, but doesn't outputa line break.If you callit with one argument the "items"list will be empty.range(len(items)) returnsalist with the indexes, whatfor loops over,we'll explain that further down.==============================================================================50.3 Restoring theviewSometimes you want to jump around, makea change and thengo back to the sameposition and view. For example to change something in the file header. Thiscan be done with two functions:var view = winsaveview()# Move around, make changeswinrestview(view)==============================================================================Next chapter:usr_51.txt CreateapluginCopyright: seemanual-copyright vim:tw=78:ts=8:noet:ft=help:norl: