blob: b6407d101ceb569892c4d64d4bd4b1baa729f53f [file] [log] [blame]
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001*eval.txt* For Vim version 7.0aa. Last change: 2005 Jan 11
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7Expression evaluation *expression* *expr* *E15* *eval*
8
9Using expressions is introduced in chapter 41 of the user manual |usr_41.txt|.
10
11Note: Expression evaluation can be disabled at compile time. If this has been
12done, the features in this document are not available. See |+eval| and the
13last chapter below.
14
Bram Moolenaar13065c42005-01-08 16:08:21 +0000151. Variables |variables|
16 1.1 Variable types
Bram Moolenaar9588a0f2005-01-08 21:45:39 +000017 1.2 Function references |Funcref|
18 1.3 Lists |List|
Bram Moolenaar13065c42005-01-08 16:08:21 +000019 1.4 More about variables |more-variables|
202. Expression syntax |expression-syntax|
213. Internal variable |internal-variables|
224. Builtin Functions |functions|
235. Defining functions |user-functions|
246. Curly braces names |curly-braces-names|
257. Commands |expression-commands|
268. Exception handling |exception-handling|
279. Examples |eval-examples|
2810. No +eval feature |no-eval-feature|
2911. The sandbox |eval-sandbox|
Bram Moolenaar071d4272004-06-13 20:20:40 +000030
31{Vi does not have any of these commands}
32
33==============================================================================
341. Variables *variables*
35
Bram Moolenaar13065c42005-01-08 16:08:21 +0000361.1 Variable types ~
37
38There are four types of variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +000040Number a 32 bit signed number
41String a NUL terminated string of 8-bit unsigned characters (bytes)
42Funcref a reference to a function |Funcref|
43List an ordered sequence of items |List|
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +000045The Number and String types are converted automatically, depending on how they
46are used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000047
48Conversion from a Number to a String is by making the ASCII representation of
49the Number. Examples: >
50 Number 123 --> String "123"
51 Number 0 --> String "0"
52 Number -1 --> String "-1"
53
54Conversion from a String to a Number is done by converting the first digits
55to a number. Hexadecimal "0xf9" and Octal "017" numbers are recognized. If
56the String doesn't start with digits, the result is zero. Examples: >
57 String "456" --> Number 456
58 String "6bar" --> Number 6
59 String "foo" --> Number 0
60 String "0xf1" --> Number 241
61 String "0100" --> Number 64
62 String "-8" --> Number -8
63 String "+8" --> Number 0
64
65To force conversion from String to Number, add zero to it: >
66 :echo "0100" + 0
67
68For boolean operators Numbers are used. Zero is FALSE, non-zero is TRUE.
69
70Note that in the command >
71 :if "foo"
72"foo" is converted to 0, which means FALSE. To test for a non-empty string,
73use strlen(): >
74 :if strlen("foo")
75
Bram Moolenaar13065c42005-01-08 16:08:21 +000076List and Funcref types are not automatically converted.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +000077
Bram Moolenaar13065c42005-01-08 16:08:21 +000078 *E706*
79You will get an error if you try to change the type of a variable. You need
80to |:unlet| it first to avoid this error. String and Number are considered
81equivalent though. >
82 :let l = "string"
Bram Moolenaar9588a0f2005-01-08 21:45:39 +000083 :let l = 44 " changes type from String to Number
Bram Moolenaar13065c42005-01-08 16:08:21 +000084 :let l = [1, 2, 3] " error!
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +000085
Bram Moolenaar13065c42005-01-08 16:08:21 +000086
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000871.2 Function references ~
Bram Moolenaar13065c42005-01-08 16:08:21 +000088 *Funcref* *E695* *E703*
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +000089A Funcref variable is obtained with the |function()| function. It can be used
90in an expression to invoke the function it refers to by using it in the place
91of a function name, before the parenthesis around the arguments. Example: >
92
93 :let Fn = function("MyFunc")
94 :echo Fn()
Bram Moolenaar13065c42005-01-08 16:08:21 +000095<
96 *E704* *E705* *E707*
97A Funcref variable must start with a capital, "s:", "w:" or "b:". You cannot
98have both a Funcref variable and a function with the same name.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +000099
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000100Note that a Funcref cannot be used with the |:call| command, because its
101argument is not an expression.
Bram Moolenaar13065c42005-01-08 16:08:21 +0000102
103The name of the referenced function can be obtained with |string()|. >
104 :echo "The function is " . string(Myfunc)
105
106You can use |call()| to invoke a Funcref and use a list variable for the
107arguments: >
108 :let r = call(Myfunc, mylist)
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000109
110
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00001111.3 Lists ~
Bram Moolenaar13065c42005-01-08 16:08:21 +0000112 *List* *E686*
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000113A List is an ordered sequence of items. An item can be of any type. Items
114can be accessed by their index number. Items can be added and removed at any
115position in the sequence.
116
Bram Moolenaar13065c42005-01-08 16:08:21 +0000117
118List creation ~
119 *E696* *E697*
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000120A List is created with a comma separated list of items in square brackets.
Bram Moolenaar13065c42005-01-08 16:08:21 +0000121Examples: >
122 :let mylist = [1, two, 3, "four"]
123 :let emptylist = []
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000124
125An item can be any expression. Using a List for an item creates a
Bram Moolenaar13065c42005-01-08 16:08:21 +0000126nested List: >
127 :let nestlist = [[11, 12], [21, 22], [31, 32]]
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000128
129An extra comma after the last item is ignored.
130
Bram Moolenaar13065c42005-01-08 16:08:21 +0000131
132List index ~
133 *list-index* *E684*
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000134An item in the List can be accessed by putting the index in square brackets
Bram Moolenaar13065c42005-01-08 16:08:21 +0000135after the List. Indexes are zero-based, thus the first item has index zero. >
136 :let item = mylist[0] " get the first item: 1
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000137 :let item = mylist[2] " get the third item: 3
Bram Moolenaar13065c42005-01-08 16:08:21 +0000138
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000139When the resulting item is a list this can be repeated: >
Bram Moolenaar13065c42005-01-08 16:08:21 +0000140 :let item = nestlist[0][1] " get the first list, second item: 12
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000141<
Bram Moolenaar13065c42005-01-08 16:08:21 +0000142A negative index is counted from the end. Index -1 refers to the last item in
143the List, -2 to the last but one item, etc. >
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000144 :let last = mylist[-1] " get the last item: "four"
145
Bram Moolenaar13065c42005-01-08 16:08:21 +0000146To avoid an error for an invalid index use the |get()| function. When an item
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000147is not available it returns zero or the default value you specify: >
Bram Moolenaar13065c42005-01-08 16:08:21 +0000148 :echo get(mylist, idx)
149 :echo get(mylist, idx, "NONE")
150
151
152List concatenation ~
153
154Two lists can be concatenated with the "+" operator: >
155 :let longlist = mylist + [5, 6]
156
157To prepend or append an item turn the item into a list by putting [] around
158it. To change a list in-place see |list-modification| below.
159
160
161Sublist ~
162
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000163A part of the List can be obtained by specifying the first and last index,
164separated by a colon in square brackets: >
Bram Moolenaar13065c42005-01-08 16:08:21 +0000165 :let shortlist = mylist[2:-1] " get List [3, "four"]
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000166
167Omitting the first index is similar to zero. Omitting the last index is
168similar to -1. The difference is that there is no error if the items are not
169available. >
Bram Moolenaar540d6e32005-01-09 21:20:18 +0000170 :let endlist = mylist[2:] " from item 2 to the end: [3, "four"]
171 :let shortlist = mylist[2:2] " List with one item: [3]
172 :let otherlist = mylist[:] " make a copy of the List
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000173
174
Bram Moolenaar13065c42005-01-08 16:08:21 +0000175List identity ~
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000176
Bram Moolenaar13065c42005-01-08 16:08:21 +0000177When variable "aa" is a list and you assign it to another variable "bb", both
178variables refer to the same list. Thus changing the list "aa" will also
179change "bb": >
180 :let aa = [1, 2, 3]
181 :let bb = aa
182 :call add(aa, 4)
183 :echo bb
184 [1, 2, 3, 4]
185
186Making a copy of a list is done with the |copy()| function. Using [:] also
187works, as explained above. This creates a shallow copy of the list: Changing
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000188a list item in the list will also change the item in the copied list: >
Bram Moolenaar13065c42005-01-08 16:08:21 +0000189 :let aa = [[1, 'a'], 2, 3]
190 :let bb = copy(aa)
191 :let aa = aa + [4]
192 :let aa[0][1] = 'aaa'
193 :echo aa
194 [[1, aaa], 2, 3, 4]
195 :echo bb
196 [[1, aaa], 2, 3]
197
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000198To make a completely independent list use |deepcopy()|. This also makes a
199copy of the values in the list, recursively.
Bram Moolenaar13065c42005-01-08 16:08:21 +0000200
201The operator "is" can be used to check if two variables refer to the same
202list. "isnot" does the opposite. In contrast "==" compares if two lists have
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000203the same value. >
204 :let alist = [1, 2, 3]
205 :let blist = [1, 2, 3]
206 :echo alist is blist
207 0
208 :echo alist == blist
209 1
Bram Moolenaar13065c42005-01-08 16:08:21 +0000210
211
212List unpack ~
213
214To unpack the items in a list to individual variables, put the variables in
215square brackets, like list items: >
216 :let [var1, var2] = mylist
217
218When the number of variables does not match the number of items in the list
219this produces an error. To handle any extra items from the list append ";"
220and a variable name: >
221 :let [var1, var2; rest] = mylist
222
223This works like: >
224 :let var1 = mylist[0]
225 :let var2 = mylist[1]
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000226 :let rest = mylist[2:]
Bram Moolenaar13065c42005-01-08 16:08:21 +0000227
228Except that there is no error if there are only two items. "rest" will be an
229empty list then.
230
231
232List modification ~
233 *list-modification*
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000234To change a specific item of a list use |:let| this way: >
Bram Moolenaar13065c42005-01-08 16:08:21 +0000235 :let list[4] = "four"
236 :let listlist[0][3] = item
237
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000238To change part of a list you can specify the first and last item to be
Bram Moolenaar540d6e32005-01-09 21:20:18 +0000239modified. The value must match the range of replaced items: >
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000240 :let list[3:5] = [3, 4, 5]
241
Bram Moolenaar13065c42005-01-08 16:08:21 +0000242Adding and removing items from a list is done with functions. Here are a few
243examples: >
244 :call insert(list, 'a') " prepend item 'a'
245 :call insert(list, 'a', 3) " insert item 'a' before list[3]
246 :call add(list, "new") " append String item
247 :call add(list, [1, 2]) " append List as one new item
248 :call extend(list, [1, 2]) " extend the list with two more items
249 :let i = remove(list, 3) " remove item 3
250 :let l = remove(list, 3, -1) " remove items 3 to last item
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000251 :call filter(list, #& =~ 'x'#) " remove items with an 'x'
Bram Moolenaar13065c42005-01-08 16:08:21 +0000252
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000253Changing the oder of items in a list: >
254 :call sort(list) " sort a list alphabetically
255 :call reverse(list) " reverse the order of items
256
Bram Moolenaar13065c42005-01-08 16:08:21 +0000257
258For loop ~
259
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000260The |:for| loop executes commands for each item in a list. A variable is set
261to each item in the list in sequence. Example: >
Bram Moolenaar13065c42005-01-08 16:08:21 +0000262 :for i in mylist
263 : call Doit(i)
264 :endfor
265
266This works like: >
267 :let index = 0
268 :while index < len(mylist)
269 : let i = mylist[index]
270 : :call Doit(i)
271 : let index = index + 1
272 :endwhile
273
274Note that all items in the list should be of the same type, otherwise this
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000275results in an error |E706|. To avoid this |:unlet| the variable at the end of
276the loop.
Bram Moolenaar13065c42005-01-08 16:08:21 +0000277
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000278If all you want to do is modify each item in the list then the |map()|
279function might be a simpler method than a for loop.
280
Bram Moolenaar13065c42005-01-08 16:08:21 +0000281Just like the |:let| command, |:for| also accepts a list of variables. This
282requires the argument to be a list of lists. >
283 :for [lnum, col] in [[1, 3], [2, 8], [3, 0]]
284 : call Doit(lnum, col)
285 :endfor
286
287This works like a |:let| command is done for each list item. Again, the types
288must remain the same to avoid an error.
289
290It is also possible to put remaining items in a list: >
291 :for [i, j; rest] in listlist
292 : call Doit(i, j)
293 : if !empty(rest)
294 : echo "remainder: " . string(rest)
295 : endif
296 :endfor
297
298
299List functions ~
300
301Functions that are useful with a List: >
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000302 :let r = call(funcname, list) " call a function with an argument list
Bram Moolenaar13065c42005-01-08 16:08:21 +0000303 :if empty(list) " check if list is empty
304 :let l = len(list) " number of items in a list
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000305 :let big = max(list) " maximum value in a list
306 :let small = min(list) " minumum value in a list
307 :let xs = count(list, 'x') " count nr of times 'x' appears in list
308 :let i = index(list, 'x') " index of first 'x' in list
Bram Moolenaar13065c42005-01-08 16:08:21 +0000309 :let lines = getline(1, 10) " get ten text lines from buffer
310 :call append('$', lines) " append text lines in buffer
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000311 :let list = split("a b c") " create list from items in a string
312 :let string = join(list, ', ') " create string from list items
Bram Moolenaar13065c42005-01-08 16:08:21 +0000313 :let s = string() " String representation of a list
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000314 :call map(list, #'>> ' . &#) " prepend ">> " to each item
Bram Moolenaar13065c42005-01-08 16:08:21 +0000315
316
3171.4 More about variables ~
318 *more-variables*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319If you need to know the type of a variable or expression, use the |type()|
320function.
321
322When the '!' flag is included in the 'viminfo' option, global variables that
323start with an uppercase letter, and don't contain a lowercase letter, are
324stored in the viminfo file |viminfo-file|.
325
326When the 'sessionoptions' option contains "global", global variables that
327start with an uppercase letter and contain at least one lowercase letter are
328stored in the session file |session-file|.
329
330variable name can be stored where ~
331my_var_6 not
332My_Var_6 session file
333MY_VAR_6 viminfo file
334
335
336It's possible to form a variable name with curly braces, see
337|curly-braces-names|.
338
339==============================================================================
3402. Expression syntax *expression-syntax*
341
342Expression syntax summary, from least to most significant:
343
344|expr1| expr2 ? expr1 : expr1 if-then-else
345
346|expr2| expr3 || expr3 .. logical OR
347
348|expr3| expr4 && expr4 .. logical AND
349
350|expr4| expr5 == expr5 equal
351 expr5 != expr5 not equal
352 expr5 > expr5 greater than
353 expr5 >= expr5 greater than or equal
354 expr5 < expr5 smaller than
355 expr5 <= expr5 smaller than or equal
356 expr5 =~ expr5 regexp matches
357 expr5 !~ expr5 regexp doesn't match
358
359 expr5 ==? expr5 equal, ignoring case
360 expr5 ==# expr5 equal, match case
361 etc. As above, append ? for ignoring case, # for
362 matching case
363
Bram Moolenaarde8866b2005-01-06 23:24:37 +0000364 expr5 is expr5 same List instance
365 expr5 isnot expr5 different List instance
366
367|expr5| expr6 + expr6 .. number addition or list concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 expr6 - expr6 .. number subtraction
369 expr6 . expr6 .. string concatenation
370
371|expr6| expr7 * expr7 .. number multiplication
372 expr7 / expr7 .. number division
373 expr7 % expr7 .. number modulo
374
375|expr7| ! expr7 logical NOT
376 - expr7 unary minus
377 + expr7 unary plus
378 expr8
379
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000380|expr8| expr9[expr1] byte of a String or item of a List
381 expr9[expr1 : expr2] substring of a String or sublist of a List
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382
383|expr9| number number constant
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000384 "string" string constant, backslash is special
385 'string' string constant
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000386 #string# string constant
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000387 [expr1, ...] List
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 &option option value
389 (expr1) nested expression
390 variable internal variable
391 va{ria}ble internal variable with curly braces
392 $VAR environment variable
393 @r contents of register 'r'
394 function(expr1, ...) function call
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000395 Funcref(expr1, ...) function call with Funcref variable
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 func{ti}on(expr1, ...) function call with curly braces
397
398
399".." indicates that the operations in this level can be concatenated.
400Example: >
401 &nu || &list && &shell == "csh"
402
403All expressions within one level are parsed from left to right.
404
405
406expr1 *expr1* *E109*
407-----
408
409expr2 ? expr1 : expr1
410
411The expression before the '?' is evaluated to a number. If it evaluates to
412non-zero, the result is the value of the expression between the '?' and ':',
413otherwise the result is the value of the expression after the ':'.
414Example: >
415 :echo lnum == 1 ? "top" : lnum
416
417Since the first expression is an "expr2", it cannot contain another ?:. The
418other two expressions can, thus allow for recursive use of ?:.
419Example: >
420 :echo lnum == 1 ? "top" : lnum == 1000 ? "last" : lnum
421
422To keep this readable, using |line-continuation| is suggested: >
423 :echo lnum == 1
424 :\ ? "top"
425 :\ : lnum == 1000
426 :\ ? "last"
427 :\ : lnum
428
429
430expr2 and expr3 *expr2* *expr3*
431---------------
432
433 *expr-barbar* *expr-&&*
434The "||" and "&&" operators take one argument on each side. The arguments
435are (converted to) Numbers. The result is:
436
437 input output ~
438n1 n2 n1 || n2 n1 && n2 ~
439zero zero zero zero
440zero non-zero non-zero zero
441non-zero zero non-zero zero
442non-zero non-zero non-zero non-zero
443
444The operators can be concatenated, for example: >
445
446 &nu || &list && &shell == "csh"
447
448Note that "&&" takes precedence over "||", so this has the meaning of: >
449
450 &nu || (&list && &shell == "csh")
451
452Once the result is known, the expression "short-circuits", that is, further
453arguments are not evaluated. This is like what happens in C. For example: >
454
455 let a = 1
456 echo a || b
457
458This is valid even if there is no variable called "b" because "a" is non-zero,
459so the result must be non-zero. Similarly below: >
460
461 echo exists("b") && b == "yes"
462
463This is valid whether "b" has been defined or not. The second clause will
464only be evaluated if "b" has been defined.
465
466
467expr4 *expr4*
468-----
469
470expr5 {cmp} expr5
471
472Compare two expr5 expressions, resulting in a 0 if it evaluates to false, or 1
473if it evaluates to true.
474
475 *expr-==* *expr-!=* *expr->* *expr->=*
476 *expr-<* *expr-<=* *expr-=~* *expr-!~*
477 *expr-==#* *expr-!=#* *expr->#* *expr->=#*
478 *expr-<#* *expr-<=#* *expr-=~#* *expr-!~#*
479 *expr-==?* *expr-!=?* *expr->?* *expr->=?*
480 *expr-<?* *expr-<=?* *expr-=~?* *expr-!~?*
Bram Moolenaarde8866b2005-01-06 23:24:37 +0000481 *expr-is*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 use 'ignorecase' match case ignore case ~
483equal == ==# ==?
484not equal != !=# !=?
485greater than > ># >?
486greater than or equal >= >=# >=?
487smaller than < <# <?
488smaller than or equal <= <=# <=?
489regexp matches =~ =~# =~?
490regexp doesn't match !~ !~# !~?
Bram Moolenaarde8866b2005-01-06 23:24:37 +0000491same instance is
492different instance isnot
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493
494Examples:
495"abc" ==# "Abc" evaluates to 0
496"abc" ==? "Abc" evaluates to 1
497"abc" == "Abc" evaluates to 1 if 'ignorecase' is set, 0 otherwise
498
Bram Moolenaar13065c42005-01-08 16:08:21 +0000499 *E691* *E692*
Bram Moolenaarde8866b2005-01-06 23:24:37 +0000500A List can only be compared with a List and only "equal", "not equal" and "is"
501can be used. This compares the values of the list, recursively. Ignoring
502case means case is ignored when comparing item values.
503
Bram Moolenaar13065c42005-01-08 16:08:21 +0000504 *E693* *E694*
Bram Moolenaarde8866b2005-01-06 23:24:37 +0000505A Funcref can only be compared with a Funcref and only "equal" and "not equal"
506can be used. Case is never ignored.
507
508When using "is" or "isnot" with a List this checks if the expressions are
509referring to the same List instance. A copy of a List is different from the
510original List. When using "is" without a List it is equivalent to using
511"equal", using "isnot" equivalent to using "not equal". Except that a
512different type means the values are different. "4 == '4'" is true, "4 is '4'"
513is false.
514
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515When comparing a String with a Number, the String is converted to a Number,
516and the comparison is done on Numbers. This means that "0 == 'x'" is TRUE,
517because 'x' converted to a Number is zero.
518
519When comparing two Strings, this is done with strcmp() or stricmp(). This
520results in the mathematical difference (comparing byte values), not
521necessarily the alphabetical difference in the local language.
522
523When using the operators with a trailing '#", or the short version and
524'ignorecase' is off, the comparing is done with strcmp().
525
526When using the operators with a trailing '?', or the short version and
527'ignorecase' is set, the comparing is done with stricmp().
528
529The "=~" and "!~" operators match the lefthand argument with the righthand
530argument, which is used as a pattern. See |pattern| for what a pattern is.
531This matching is always done like 'magic' was set and 'cpoptions' is empty, no
532matter what the actual value of 'magic' or 'cpoptions' is. This makes scripts
533portable. To avoid backslashes in the regexp pattern to be doubled, use a
534single-quote string, see |literal-string|.
535Since a string is considered to be a single line, a multi-line pattern
536(containing \n, backslash-n) will not match. However, a literal NL character
537can be matched like an ordinary character. Examples:
538 "foo\nbar" =~ "\n" evaluates to 1
539 "foo\nbar" =~ "\\n" evaluates to 0
540
541
542expr5 and expr6 *expr5* *expr6*
543---------------
Bram Moolenaarde8866b2005-01-06 23:24:37 +0000544expr6 + expr6 .. Number addition or List concatenation *expr-+*
545expr6 - expr6 .. Number subtraction *expr--*
546expr6 . expr6 .. String concatenation *expr-.*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
Bram Moolenaarde8866b2005-01-06 23:24:37 +0000548For Lists only "+" is possible and then both expr6 must be a list. The result
549is a new list with the two lists Concatenated.
550
551expr7 * expr7 .. number multiplication *expr-star*
552expr7 / expr7 .. number division *expr-/*
553expr7 % expr7 .. number modulo *expr-%*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
555For all, except ".", Strings are converted to Numbers.
556
557Note the difference between "+" and ".":
558 "123" + "456" = 579
559 "123" . "456" = "123456"
560
561When the righthand side of '/' is zero, the result is 0x7fffffff.
562When the righthand side of '%' is zero, the result is 0.
563
Bram Moolenaarde8866b2005-01-06 23:24:37 +0000564None of these work for Funcrefs.
565
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566
567expr7 *expr7*
568-----
569! expr7 logical NOT *expr-!*
570- expr7 unary minus *expr-unary--*
571+ expr7 unary plus *expr-unary-+*
572
573For '!' non-zero becomes zero, zero becomes one.
574For '-' the sign of the number is changed.
575For '+' the number is unchanged.
576
577A String will be converted to a Number first.
578
579These three can be repeated and mixed. Examples:
580 !-1 == 0
581 !!8 == 1
582 --9 == 9
583
584
585expr8 *expr8*
586-----
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000587expr9[expr1] item of String or List *expr-[]* *E111*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000589If expr9 is a Number or String this results in a String that contains the
590expr1'th single byte from expr9. expr9 is used as a String, expr1 as a
591Number. Note that this doesn't recognize multi-byte encodings.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000593Index zero gives the first character. This is like it works in C. Careful:
594text column numbers start with one! Example, to get the character under the
595cursor: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 :let c = getline(line("."))[col(".") - 1]
597
598If the length of the String is less than the index, the result is an empty
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000599String. A negative index always results in an empty string (reason: backwards
600compatibility). Use [-1:] to get the last byte.
601
602If expr9 is a List then it results the item at index expr1. See |list-index|
603for possible index values. If the index is out of range this results in an
604error. Example: >
605 :let item = mylist[-1] " get last item
606
607Generally, if a List index is equal to or higher than the length of the List,
608or more negative than the length of the List, this results in an error.
609
610expr9[expr1a : expr1b] substring or sublist *expr-[:]*
611
612If expr9 is a Number or String this results in the substring with the bytes
613from expr1a to and including expr1b. expr9 is used as a String, expr1a and
614expr1b are used as a Number. Note that this doesn't recognize multi-byte
615encodings.
616
617If expr1a is omitted zero is used. If expr1b is omitted the length of the
618string minus one is used.
619
620A negative number can be used to measure from the end of the string. -1 is
621the last character, -2 the last but one, etc.
622
623If an index goes out of range for the string characters are omitted. If
624expr1b is smaller than expr1a the result is an empty string.
625
626Examples: >
627 :let c = name[-1:] " last byte of a string
628 :let c = name[-2:-2] " last but one byte of a string
629 :let s = line(".")[4:] " from the fifth byte to the end
630 :let s = s[:-3] " remove last two bytes
631
632If expr9 is a List this results in a new List with the items indicated by the
633indexes expr1a and expr1b. This works like with a String, as explained just
634above, except that indexes out of range cause an error. Examples: >
635 :let l = mylist[:3] " first four items
636 :let l = mylist[4:4] " List with one item
637 :let l = mylist[:] " shallow copy of a List
638
639Using expr9[expr1] or expr9[expr1a : expr1b] on a Funcref results in an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
641 *expr9*
642number
643------
644number number constant *expr-number*
645
646Decimal, Hexadecimal (starting with 0x or 0X), or Octal (starting with 0).
647
648
649string *expr-string* *E114*
650------
651"string" string constant *expr-quote*
652
653Note that double quotes are used.
654
655A string constant accepts these special characters:
656\... three-digit octal number (e.g., "\316")
657\.. two-digit octal number (must be followed by non-digit)
658\. one-digit octal number (must be followed by non-digit)
659\x.. byte specified with two hex numbers (e.g., "\x1f")
660\x. byte specified with one hex number (must be followed by non-hex char)
661\X.. same as \x..
662\X. same as \x.
663\u.... character specified with up to 4 hex numbers, stored according to the
664 current value of 'encoding' (e.g., "\u02a4")
665\U.... same as \u....
666\b backspace <BS>
667\e escape <Esc>
668\f formfeed <FF>
669\n newline <NL>
670\r return <CR>
671\t tab <Tab>
672\\ backslash
673\" double quote
674\<xxx> Special key named "xxx". e.g. "\<C-W>" for CTRL-W.
675
676Note that "\000" and "\x00" force the end of the string.
677
678
679literal-string *literal-string* *E115*
680---------------
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000681'string' string constant *expr-'*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682
683Note that single quotes are used.
684
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000685This string is taken as it is. No backslashes are removed or have a special
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000686meaning. A literal-string cannot contain a single quote. Use a double-quoted
687string or sharp-string for that.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000688
689Single quoted strings are useful for patterns, so that backslashes do not need
690to be doubled. These two commands are equivalent: >
691 if a =~ "\\s*"
692 if a =~ '\s*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
694
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000695sharp-string *sharp-string*
696---------------
697#string# string constant *expr-#*
698
699Most characters in the string are taken as-is. Only the '#' character is
700special: It needs to be double to get one.
701
702Sharp-strings are useful when a string may contain single quotes, double
703quotes and/or backslashes.
704
705
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706option *expr-option* *E112* *E113*
707------
708&option option value, local value if possible
709&g:option global option value
710&l:option local option value
711
712Examples: >
713 echo "tabstop is " . &tabstop
714 if &insertmode
715
716Any option name can be used here. See |options|. When using the local value
717and there is no buffer-local or window-local value, the global value is used
718anyway.
719
720
721register *expr-register*
722--------
723@r contents of register 'r'
724
725The result is the contents of the named register, as a single string.
726Newlines are inserted where required. To get the contents of the unnamed
727register use @" or @@. The '=' register can not be used here. See
728|registers| for an explanation of the available registers.
729
730
731nesting *expr-nesting* *E110*
732-------
733(expr1) nested expression
734
735
736environment variable *expr-env*
737--------------------
738$VAR environment variable
739
740The String value of any environment variable. When it is not defined, the
741result is an empty string.
742 *expr-env-expand*
743Note that there is a difference between using $VAR directly and using
744expand("$VAR"). Using it directly will only expand environment variables that
745are known inside the current Vim session. Using expand() will first try using
746the environment variables known inside the current Vim session. If that
747fails, a shell will be used to expand the variable. This can be slow, but it
748does expand all variables that the shell knows about. Example: >
749 :echo $version
750 :echo expand("$version")
751The first one probably doesn't echo anything, the second echoes the $version
752variable (if your shell supports it).
753
754
755internal variable *expr-variable*
756-----------------
757variable internal variable
758See below |internal-variables|.
759
760
761function call *expr-function* *E116* *E117* *E118* *E119* *E120*
762-------------
763function(expr1, ...) function call
764See below |functions|.
765
766
767==============================================================================
7683. Internal variable *internal-variables* *E121*
769 *E461*
770An internal variable name can be made up of letters, digits and '_'. But it
771cannot start with a digit. It's also possible to use curly braces, see
772|curly-braces-names|.
773
774An internal variable is created with the ":let" command |:let|.
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000775An internal variable is explicitly destroyed with the ":unlet" command
776|:unlet|.
777Using a name that is not an internal variable or refers to a variable that has
778been destroyed results in an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
780There are several name spaces for variables. Which one is to be used is
781specified by what is prepended:
782
783 (nothing) In a function: local to a function; otherwise: global
784|buffer-variable| b: Local to the current buffer.
785|window-variable| w: Local to the current window.
786|global-variable| g: Global.
787|local-variable| l: Local to a function.
788|script-variable| s: Local to a |:source|'ed Vim script.
789|function-argument| a: Function argument (only inside a function).
790|vim-variable| v: Global, predefined by Vim.
791
792 *buffer-variable* *b:var*
793A variable name that is preceded with "b:" is local to the current buffer.
794Thus you can have several "b:foo" variables, one for each buffer.
795This kind of variable is deleted when the buffer is wiped out or deleted with
796|:bdelete|.
797
798One local buffer variable is predefined:
799 *b:changedtick-variable* *changetick*
800b:changedtick The total number of changes to the current buffer. It is
801 incremented for each change. An undo command is also a change
802 in this case. This can be used to perform an action only when
803 the buffer has changed. Example: >
804 :if my_changedtick != b:changedtick
805 : let my_changedtick = b:changedtick
806 : call My_Update()
807 :endif
808<
809 *window-variable* *w:var*
810A variable name that is preceded with "w:" is local to the current window. It
811is deleted when the window is closed.
812
813 *global-variable* *g:var*
814Inside functions global variables are accessed with "g:". Omitting this will
815access a variable local to a function. But "g:" can also be used in any other
816place if you like.
817
818 *local-variable* *l:var*
819Inside functions local variables are accessed without prepending anything.
820But you can also prepend "l:" if you like.
821
822 *script-variable* *s:var*
823In a Vim script variables starting with "s:" can be used. They cannot be
824accessed from outside of the scripts, thus are local to the script.
825
826They can be used in:
827- commands executed while the script is sourced
828- functions defined in the script
829- autocommands defined in the script
830- functions and autocommands defined in functions and autocommands which were
831 defined in the script (recursively)
832- user defined commands defined in the script
833Thus not in:
834- other scripts sourced from this one
835- mappings
836- etc.
837
838script variables can be used to avoid conflicts with global variable names.
839Take this example:
840
841 let s:counter = 0
842 function MyCounter()
843 let s:counter = s:counter + 1
844 echo s:counter
845 endfunction
846 command Tick call MyCounter()
847
848You can now invoke "Tick" from any script, and the "s:counter" variable in
849that script will not be changed, only the "s:counter" in the script where
850"Tick" was defined is used.
851
852Another example that does the same: >
853
854 let s:counter = 0
855 command Tick let s:counter = s:counter + 1 | echo s:counter
856
857When calling a function and invoking a user-defined command, the context for
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000858script variables is set to the script where the function or command was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859defined.
860
861The script variables are also available when a function is defined inside a
862function that is defined in a script. Example: >
863
864 let s:counter = 0
865 function StartCounting(incr)
866 if a:incr
867 function MyCounter()
868 let s:counter = s:counter + 1
869 endfunction
870 else
871 function MyCounter()
872 let s:counter = s:counter - 1
873 endfunction
874 endif
875 endfunction
876
877This defines the MyCounter() function either for counting up or counting down
878when calling StartCounting(). It doesn't matter from where StartCounting() is
879called, the s:counter variable will be accessible in MyCounter().
880
881When the same script is sourced again it will use the same script variables.
882They will remain valid as long as Vim is running. This can be used to
883maintain a counter: >
884
885 if !exists("s:counter")
886 let s:counter = 1
887 echo "script executed for the first time"
888 else
889 let s:counter = s:counter + 1
890 echo "script executed " . s:counter . " times now"
891 endif
892
893Note that this means that filetype plugins don't get a different set of script
894variables for each buffer. Use local buffer variables instead |b:var|.
895
896
897Predefined Vim variables: *vim-variable* *v:var*
898
899 *v:charconvert_from* *charconvert_from-variable*
900v:charconvert_from
901 The name of the character encoding of a file to be converted.
902 Only valid while evaluating the 'charconvert' option.
903
904 *v:charconvert_to* *charconvert_to-variable*
905v:charconvert_to
906 The name of the character encoding of a file after conversion.
907 Only valid while evaluating the 'charconvert' option.
908
909 *v:cmdarg* *cmdarg-variable*
910v:cmdarg This variable is used for two purposes:
911 1. The extra arguments given to a file read/write command.
912 Currently these are "++enc=" and "++ff=". This variable is
913 set before an autocommand event for a file read/write
914 command is triggered. There is a leading space to make it
915 possible to append this variable directly after the
916 read/write command. Note: The "+cmd" argument isn't
917 included here, because it will be executed anyway.
918 2. When printing a PostScript file with ":hardcopy" this is
919 the argument for the ":hardcopy" command. This can be used
920 in 'printexpr'.
921
922 *v:cmdbang* *cmdbang-variable*
923v:cmdbang Set like v:cmdarg for a file read/write command. When a "!"
924 was used the value is 1, otherwise it is 0. Note that this
925 can only be used in autocommands. For user commands |<bang>|
926 can be used.
927
928 *v:count* *count-variable*
929v:count The count given for the last Normal mode command. Can be used
930 to get the count before a mapping. Read-only. Example: >
931 :map _x :<C-U>echo "the count is " . v:count<CR>
932< Note: The <C-U> is required to remove the line range that you
933 get when typing ':' after a count.
934 "count" also works, for backwards compatibility.
935
936 *v:count1* *count1-variable*
937v:count1 Just like "v:count", but defaults to one when no count is
938 used.
939
940 *v:ctype* *ctype-variable*
941v:ctype The current locale setting for characters of the runtime
942 environment. This allows Vim scripts to be aware of the
943 current locale encoding. Technical: it's the value of
944 LC_CTYPE. When not using a locale the value is "C".
945 This variable can not be set directly, use the |:language|
946 command.
947 See |multi-lang|.
948
949 *v:dying* *dying-variable*
950v:dying Normally zero. When a deadly signal is caught it's set to
951 one. When multiple signals are caught the number increases.
952 Can be used in an autocommand to check if Vim didn't
953 terminate normally. {only works on Unix}
954 Example: >
955 :au VimLeave * if v:dying | echo "\nAAAAaaaarrrggghhhh!!!\n" | endif
956<
957 *v:errmsg* *errmsg-variable*
958v:errmsg Last given error message. It's allowed to set this variable.
959 Example: >
960 :let v:errmsg = ""
961 :silent! next
962 :if v:errmsg != ""
963 : ... handle error
964< "errmsg" also works, for backwards compatibility.
965
966 *v:exception* *exception-variable*
967v:exception The value of the exception most recently caught and not
968 finished. See also |v:throwpoint| and |throw-variables|.
969 Example: >
970 :try
971 : throw "oops"
972 :catch /.*/
973 : echo "caught" v:exception
974 :endtry
975< Output: "caught oops".
976
977 *v:fname_in* *fname_in-variable*
978v:fname_in The name of the input file. Only valid while evaluating:
979 option used for ~
980 'charconvert' file to be converted
981 'diffexpr' original file
982 'patchexpr' original file
983 'printexpr' file to be printed
984
985 *v:fname_out* *fname_out-variable*
986v:fname_out The name of the output file. Only valid while
987 evaluating:
988 option used for ~
989 'charconvert' resulting converted file (*)
990 'diffexpr' output of diff
991 'patchexpr' resulting patched file
992 (*) When doing conversion for a write command (e.g., ":w
993 file") it will be equal to v:fname_in. When doing conversion
994 for a read command (e.g., ":e file") it will be a temporary
995 file and different from v:fname_in.
996
997 *v:fname_new* *fname_new-variable*
998v:fname_new The name of the new version of the file. Only valid while
999 evaluating 'diffexpr'.
1000
1001 *v:fname_diff* *fname_diff-variable*
1002v:fname_diff The name of the diff (patch) file. Only valid while
1003 evaluating 'patchexpr'.
1004
1005 *v:folddashes* *folddashes-variable*
1006v:folddashes Used for 'foldtext': dashes representing foldlevel of a closed
1007 fold.
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001008 Read-only in the |sandbox|. |fold-foldtext|
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009
1010 *v:foldlevel* *foldlevel-variable*
1011v:foldlevel Used for 'foldtext': foldlevel of closed fold.
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001012 Read-only in the |sandbox|. |fold-foldtext|
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
1014 *v:foldend* *foldend-variable*
1015v:foldend Used for 'foldtext': last line of closed fold.
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001016 Read-only in the |sandbox|. |fold-foldtext|
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
1018 *v:foldstart* *foldstart-variable*
1019v:foldstart Used for 'foldtext': first line of closed fold.
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001020 Read-only in the |sandbox|. |fold-foldtext|
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021
Bram Moolenaar843ee412004-06-30 16:16:41 +00001022 *v:insertmode* *insertmode-variable*
1023v:insertmode Used for the |InsertEnter| and |InsertChange| autocommand
1024 events. Values:
1025 i Insert mode
1026 r Replace mode
1027 v Virtual Replace mode
1028
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 *v:lang* *lang-variable*
1030v:lang The current locale setting for messages of the runtime
1031 environment. This allows Vim scripts to be aware of the
1032 current language. Technical: it's the value of LC_MESSAGES.
1033 The value is system dependent.
1034 This variable can not be set directly, use the |:language|
1035 command.
1036 It can be different from |v:ctype| when messages are desired
1037 in a different language than what is used for character
1038 encoding. See |multi-lang|.
1039
1040 *v:lc_time* *lc_time-variable*
1041v:lc_time The current locale setting for time messages of the runtime
1042 environment. This allows Vim scripts to be aware of the
1043 current language. Technical: it's the value of LC_TIME.
1044 This variable can not be set directly, use the |:language|
1045 command. See |multi-lang|.
1046
1047 *v:lnum* *lnum-variable*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001048v:lnum Line number for the 'foldexpr' |fold-expr| and 'indentexpr'
1049 expressions. Only valid while one of these expressions is
1050 being evaluated. Read-only when in the |sandbox|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
1052 *v:prevcount* *prevcount-variable*
1053v:prevcount The count given for the last but one Normal mode command.
1054 This is the v:count value of the previous command. Useful if
1055 you want to cancel Visual mode and then use the count. >
1056 :vmap % <Esc>:call MyFilter(v:prevcount)<CR>
1057< Read-only.
1058
1059 *v:progname* *progname-variable*
1060v:progname Contains the name (with path removed) with which Vim was
1061 invoked. Allows you to do special initialisations for "view",
1062 "evim" etc., or any other name you might symlink to Vim.
1063 Read-only.
1064
1065 *v:register* *register-variable*
1066v:register The name of the register supplied to the last normal mode
1067 command. Empty if none were supplied. |getreg()| |setreg()|
1068
1069 *v:servername* *servername-variable*
1070v:servername The resulting registered |x11-clientserver| name if any.
1071 Read-only.
1072
1073 *v:shell_error* *shell_error-variable*
1074v:shell_error Result of the last shell command. When non-zero, the last
1075 shell command had an error. When zero, there was no problem.
1076 This only works when the shell returns the error code to Vim.
1077 The value -1 is often used when the command could not be
1078 executed. Read-only.
1079 Example: >
1080 :!mv foo bar
1081 :if v:shell_error
1082 : echo 'could not rename "foo" to "bar"!'
1083 :endif
1084< "shell_error" also works, for backwards compatibility.
1085
1086 *v:statusmsg* *statusmsg-variable*
1087v:statusmsg Last given status message. It's allowed to set this variable.
1088
1089 *v:termresponse* *termresponse-variable*
1090v:termresponse The escape sequence returned by the terminal for the |t_RV|
1091 termcap entry. It is set when Vim receives an escape sequence
1092 that starts with ESC [ or CSI and ends in a 'c', with only
1093 digits, ';' and '.' in between.
1094 When this option is set, the TermResponse autocommand event is
1095 fired, so that you can react to the response from the
1096 terminal.
1097 The response from a new xterm is: "<Esc>[ Pp ; Pv ; Pc c". Pp
1098 is the terminal type: 0 for vt100 and 1 for vt220. Pv is the
1099 patch level (since this was introduced in patch 95, it's
1100 always 95 or bigger). Pc is always zero.
1101 {only when compiled with |+termresponse| feature}
1102
1103 *v:this_session* *this_session-variable*
1104v:this_session Full filename of the last loaded or saved session file. See
1105 |:mksession|. It is allowed to set this variable. When no
1106 session file has been saved, this variable is empty.
1107 "this_session" also works, for backwards compatibility.
1108
1109 *v:throwpoint* *throwpoint-variable*
1110v:throwpoint The point where the exception most recently caught and not
1111 finished was thrown. Not set when commands are typed. See
1112 also |v:exception| and |throw-variables|.
1113 Example: >
1114 :try
1115 : throw "oops"
1116 :catch /.*/
1117 : echo "Exception from" v:throwpoint
1118 :endtry
1119< Output: "Exception from test.vim, line 2"
1120
1121 *v:version* *version-variable*
1122v:version Version number of Vim: Major version number times 100 plus
1123 minor version number. Version 5.0 is 500. Version 5.1 (5.01)
1124 is 501. Read-only. "version" also works, for backwards
1125 compatibility.
1126 Use |has()| to check if a certain patch was included, e.g.: >
1127 if has("patch123")
1128< Note that patch numbers are specific to the version, thus both
1129 version 5.0 and 5.1 may have a patch 123, but these are
1130 completely different.
1131
1132 *v:warningmsg* *warningmsg-variable*
1133v:warningmsg Last given warning message. It's allowed to set this variable.
1134
1135==============================================================================
11364. Builtin Functions *functions*
1137
1138See |function-list| for a list grouped by what the function is used for.
1139
1140(Use CTRL-] on the function name to jump to the full explanation)
1141
1142USAGE RESULT DESCRIPTION ~
1143
Bram Moolenaara14de3d2005-01-07 21:48:26 +00001144add( {list}, {item}) List append {item} to List {list}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001145append( {lnum}, {string}) Number append {string} below line {lnum}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146argc() Number number of files in the argument list
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001147argidx() Number current index in the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148argv( {nr}) String {nr} entry of the argument list
1149browse( {save}, {title}, {initdir}, {default})
1150 String put up a file requester
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001151browsedir( {title}, {initdir}) String put up a directory requester
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152bufexists( {expr}) Number TRUE if buffer {expr} exists
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001153buflisted( {expr}) Number TRUE if buffer {expr} is listed
1154bufloaded( {expr}) Number TRUE if buffer {expr} is loaded
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155bufname( {expr}) String Name of the buffer {expr}
1156bufnr( {expr}) Number Number of the buffer {expr}
1157bufwinnr( {expr}) Number window number of buffer {expr}
1158byte2line( {byte}) Number line number at byte count {byte}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001159byteidx( {expr}, {nr}) Number byte index of {nr}'th char in {expr}
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001160call( {func}, {arglist}) any call {func} with arguments {arglist}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161char2nr( {expr}) Number ASCII value of first char in {expr}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001162cindent( {lnum}) Number C indent for line {lnum}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163col( {expr}) Number column nr of cursor or mark
1164confirm( {msg} [, {choices} [, {default} [, {type}]]])
1165 Number number of choice picked by user
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001166copy( {expr}) any make a shallow copy of {expr}
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001167count( {list}, {expr} [, {start} [, {ic}]])
1168 Number count how many {expr} are in {list}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169cscope_connection( [{num} , {dbpath} [, {prepend}]])
1170 Number checks existence of cscope connection
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001171cursor( {lnum}, {col}) Number position cursor at {lnum}, {col}
1172deepcopy( {expr}) any make a full copy of {expr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173delete( {fname}) Number delete file {fname}
1174did_filetype() Number TRUE if FileType autocommand event used
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001175diff_filler( {lnum}) Number diff filler lines about {lnum}
1176diff_hlID( {lnum}, {col}) Number diff highlighting at {lnum}/{col}
Bram Moolenaar13065c42005-01-08 16:08:21 +00001177empty( {expr}) Number TRUE if {expr} is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178escape( {string}, {chars}) String escape {chars} in {string} with '\'
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001179eval( {string}) any evaluate {string} into its value
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001180eventhandler( ) Number TRUE if inside an event handler
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181executable( {expr}) Number 1 if executable {expr} exists
1182exists( {expr}) Number TRUE if {expr} exists
1183expand( {expr}) String expand special keywords in {expr}
1184filereadable( {file}) Number TRUE if {file} is a readable file
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001185filter( {list}, {expr}) List remove from {list} where {expr} is 0
1186finddir( {name}[, {path}[, {count}]])
1187 String Find directory {name} in {path}
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00001188findfile( {name}[, {path}[, {count}]])
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001189 String Find file {name} in {path}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190filewritable( {file}) Number TRUE if {file} is a writable file
1191fnamemodify( {fname}, {mods}) String modify file name
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001192foldclosed( {lnum}) Number first line of fold at {lnum} if closed
1193foldclosedend( {lnum}) Number last line of fold at {lnum} if closed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194foldlevel( {lnum}) Number fold level at {lnum}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001195foldtext( ) String line displayed for closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196foreground( ) Number bring the Vim window to the foreground
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001197function( {name}) Funcref reference to function {name}
Bram Moolenaara14de3d2005-01-07 21:48:26 +00001198get( {list}, {idx} [, {def}]) any get item {idx} from {list} or {def}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001199getchar( [expr]) Number get one character from the user
1200getcharmod( ) Number modifiers for the last typed character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201getbufvar( {expr}, {varname}) variable {varname} in buffer {expr}
1202getcmdline() String return the current command-line
1203getcmdpos() Number return cursor position in command-line
1204getcwd() String the current working directory
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001205getfperm( {fname}) String file permissions of file {fname}
1206getfsize( {fname}) Number size in bytes of file {fname}
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001207getfontname( [{name}]) String name of font being used
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208getftime( {fname}) Number last modification time of file
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001209getftype( {fname}) String description of type of file {fname}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210getline( {lnum}) String line {lnum} from current buffer
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001211getreg( [{regname}]) String contents of register
1212getregtype( [{regname}]) String type of register
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213getwinposx() Number X coord in pixels of GUI Vim window
1214getwinposy() Number Y coord in pixels of GUI Vim window
1215getwinvar( {nr}, {varname}) variable {varname} in window {nr}
1216glob( {expr}) String expand file wildcards in {expr}
1217globpath( {path}, {expr}) String do glob({expr}) for all dirs in {path}
1218has( {feature}) Number TRUE if feature {feature} supported
1219hasmapto( {what} [, {mode}]) Number TRUE if mapping to {what} exists
1220histadd( {history},{item}) String add an item to a history
1221histdel( {history} [, {item}]) String remove an item from a history
1222histget( {history} [, {index}]) String get the item {index} from a history
1223histnr( {history}) Number highest index of a history
1224hlexists( {name}) Number TRUE if highlight group {name} exists
1225hlID( {name}) Number syntax ID of highlight group {name}
1226hostname() String name of the machine Vim is running on
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001227iconv( {expr}, {from}, {to}) String convert encoding of {expr}
1228indent( {lnum}) Number indent of line {lnum}
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001229index( {list}, {expr} [, {start} [, {ic}]])
1230 Number index in {list} where {expr} appears
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231input( {prompt} [, {text}]) String get input from the user
1232inputdialog( {p} [, {t} [, {c}]]) String like input() but in a GUI dialog
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001233inputrestore() Number restore typeahead
1234inputsave() Number save and clear typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235inputsecret( {prompt} [, {text}]) String like input() but hiding the text
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001236insert( {list}, {item} [, {idx}]) List insert {item} in {list} [before {idx}]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237isdirectory( {directory}) Number TRUE if {directory} is a directory
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001238join( {list} [, {sep}]) String join {list} items into one String
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001239len( {expr}) Number the length of {expr}
1240libcall( {lib}, {func}, {arg}) String call {func} in library {lib} with {arg}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241libcallnr( {lib}, {func}, {arg}) Number idem, but return a Number
1242line( {expr}) Number line nr of cursor, last line or mark
1243line2byte( {lnum}) Number byte count of line {lnum}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001244lispindent( {lnum}) Number Lisp indent for line {lnum}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245localtime() Number current time
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001246map( {list}, {expr}) List change each item in {list} to {expr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247maparg( {name}[, {mode}]) String rhs of mapping {name} in mode {mode}
1248mapcheck( {name}[, {mode}]) String check for mappings matching {name}
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00001249match( {expr}, {pat}[, {start}[, {count}]])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 Number position where {pat} matches in {expr}
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00001251matchend( {expr}, {pat}[, {start}[, {count}]])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 Number position where {pat} ends in {expr}
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00001253matchstr( {expr}, {pat}[, {start}[, {count}]])
1254 String {count}'th match of {pat} in {expr}
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00001255max({list}) Number maximum value of items in {list}
1256min({list}) Number minumum value of items in {list}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001257mode() String current editing mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258nextnonblank( {lnum}) Number line nr of non-blank line >= {lnum}
1259nr2char( {expr}) String single char with ASCII value {expr}
1260prevnonblank( {lnum}) Number line nr of non-blank line <= {lnum}
1261remote_expr( {server}, {string} [, {idvar}])
1262 String send expression
1263remote_foreground( {server}) Number bring Vim server to the foreground
1264remote_peek( {serverid} [, {retvar}])
1265 Number check for reply string
1266remote_read( {serverid}) String read reply string
1267remote_send( {server}, {string} [, {idvar}])
1268 String send key sequence
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001269remove( {list}, {idx} [, {end}]) any remove items {idx}-{end} from {list}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001270rename( {from}, {to}) Number rename (move) file from {from} to {to}
1271repeat( {expr}, {count}) String repeat {expr} {count} times
1272resolve( {filename}) String get filename a shortcut points to
Bram Moolenaara14de3d2005-01-07 21:48:26 +00001273reverse( {list}) List reverse {list} in-place
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001274search( {pattern} [, {flags}]) Number search for {pattern}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275searchpair( {start}, {middle}, {end} [, {flags} [, {skip}]])
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001276 Number search for other end of start/end pair
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277server2client( {clientid}, {string})
1278 Number send reply string
1279serverlist() String get a list of available servers
1280setbufvar( {expr}, {varname}, {val}) set {varname} in buffer {expr} to {val}
1281setcmdpos( {pos}) Number set cursor position in command-line
1282setline( {lnum}, {line}) Number set line {lnum} to {line}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001283setreg( {n}, {v}[, {opt}]) Number set register to value and type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284setwinvar( {nr}, {varname}, {val}) set {varname} in window {nr} to {val}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001285simplify( {filename}) String simplify filename as much as possible
Bram Moolenaara14de3d2005-01-07 21:48:26 +00001286sort( {list} [, {func}]) List sort {list}, using {func} to compare
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001287split( {expr} [, {pat}]) List make List from {pat} separated {expr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288strftime( {format}[, {time}]) String time in specified format
1289stridx( {haystack}, {needle}) Number first index of {needle} in {haystack}
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001290string( {expr}) String String representation of {expr} value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291strlen( {expr}) Number length of the String {expr}
1292strpart( {src}, {start}[, {len}])
1293 String {len} characters of {src} at {start}
1294strridx( {haystack}, {needle}) Number last index of {needle} in {haystack}
1295strtrans( {expr}) String translate string to make it printable
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001296submatch( {nr}) String specific match in ":substitute"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297substitute( {expr}, {pat}, {sub}, {flags})
1298 String all {pat} in {expr} replaced with {sub}
Bram Moolenaar47136d72004-10-12 20:02:24 +00001299synID( {lnum}, {col}, {trans}) Number syntax ID at {lnum} and {col}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300synIDattr( {synID}, {what} [, {mode}])
1301 String attribute {what} of syntax ID {synID}
1302synIDtrans( {synID}) Number translated syntax ID of {synID}
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001303system( {expr} [, {input}]) String output of shell command/filter {expr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304tempname() String name for a temporary file
1305tolower( {expr}) String the String {expr} switched to lowercase
1306toupper( {expr}) String the String {expr} switched to uppercase
Bram Moolenaar8299df92004-07-10 09:47:34 +00001307tr( {src}, {fromstr}, {tostr}) String translate chars of {src} in {fromstr}
1308 to chars in {tostr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309type( {name}) Number type of variable {name}
1310virtcol( {expr}) Number screen column of cursor or mark
1311visualmode( [expr]) String last visual mode used
1312winbufnr( {nr}) Number buffer number of window {nr}
1313wincol() Number window column of the cursor
1314winheight( {nr}) Number height of window {nr}
1315winline() Number window line of the cursor
1316winnr() Number number of current window
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001317winrestcmd() String returns command to restore window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318winwidth( {nr}) Number width of window {nr}
1319
Bram Moolenaara14de3d2005-01-07 21:48:26 +00001320add({list}, {expr}) *add()*
1321 Append the item {expr} to List {list}. Returns the resulting
1322 List. Examples: >
1323 :let alist = add([1, 2, 3], item)
1324 :call add(mylist, "woodstock")
1325< Note that when {expr} is a List it is appended as a single
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001326 item. Use |extend()| to concatenate Lists.
Bram Moolenaar13065c42005-01-08 16:08:21 +00001327 Use |insert()| to add an item at another position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328
Bram Moolenaara14de3d2005-01-07 21:48:26 +00001329
1330append({lnum}, {expr}) *append()*
1331 When {expr} is a List: Append each item of the list as a text
1332 line below line {lnum} in the current buffer.
1333 Otherwise append the text line {expr} below line {lnum} in the
1334 current buffer.
1335 {lnum} can be zero, to insert a line before the first one.
1336 Returns 1 for failure ({lnum} out of range or out of memory),
1337 0 for success. Example: >
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001338 :let failed = append(line('$'), "# THE END")
Bram Moolenaara14de3d2005-01-07 21:48:26 +00001339 :let failed = append(0, ["Chapter 1", "the beginning"])
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001340<
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 *argc()*
1342argc() The result is the number of files in the argument list of the
1343 current window. See |arglist|.
1344
1345 *argidx()*
1346argidx() The result is the current index in the argument list. 0 is
1347 the first file. argc() - 1 is the last one. See |arglist|.
1348
1349 *argv()*
1350argv({nr}) The result is the {nr}th file in the argument list of the
1351 current window. See |arglist|. "argv(0)" is the first one.
1352 Example: >
1353 :let i = 0
1354 :while i < argc()
1355 : let f = escape(argv(i), '. ')
1356 : exe 'amenu Arg.' . f . ' :e ' . f . '<CR>'
1357 : let i = i + 1
1358 :endwhile
1359<
1360 *browse()*
1361browse({save}, {title}, {initdir}, {default})
1362 Put up a file requester. This only works when "has("browse")"
1363 returns non-zero (only in some GUI versions).
1364 The input fields are:
1365 {save} when non-zero, select file to write
1366 {title} title for the requester
1367 {initdir} directory to start browsing in
1368 {default} default file name
1369 When the "Cancel" button is hit, something went wrong, or
1370 browsing is not possible, an empty string is returned.
1371
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001372 *browsedir()*
1373browsedir({title}, {initdir})
1374 Put up a directory requester. This only works when
1375 "has("browse")" returns non-zero (only in some GUI versions).
1376 On systems where a directory browser is not supported a file
1377 browser is used. In that case: select a file in the directory
1378 to be used.
1379 The input fields are:
1380 {title} title for the requester
1381 {initdir} directory to start browsing in
1382 When the "Cancel" button is hit, something went wrong, or
1383 browsing is not possible, an empty string is returned.
1384
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385bufexists({expr}) *bufexists()*
1386 The result is a Number, which is non-zero if a buffer called
1387 {expr} exists.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001388 If the {expr} argument is a number, buffer numbers are used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 If the {expr} argument is a string it must match a buffer name
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001390 exactly. The name can be:
1391 - Relative to the current directory.
1392 - A full path.
1393 - The name of a buffer with 'filetype' set to "nofile".
1394 - A URL name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 Unlisted buffers will be found.
1396 Note that help files are listed by their short name in the
1397 output of |:buffers|, but bufexists() requires using their
1398 long name to be able to find them.
1399 Use "bufexists(0)" to test for the existence of an alternate
1400 file name.
1401 *buffer_exists()*
1402 Obsolete name: buffer_exists().
1403
1404buflisted({expr}) *buflisted()*
1405 The result is a Number, which is non-zero if a buffer called
1406 {expr} exists and is listed (has the 'buflisted' option set).
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001407 The {expr} argument is used like with |bufexists()|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
1409bufloaded({expr}) *bufloaded()*
1410 The result is a Number, which is non-zero if a buffer called
1411 {expr} exists and is loaded (shown in a window or hidden).
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001412 The {expr} argument is used like with |bufexists()|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413
1414bufname({expr}) *bufname()*
1415 The result is the name of a buffer, as it is displayed by the
1416 ":ls" command.
1417 If {expr} is a Number, that buffer number's name is given.
1418 Number zero is the alternate buffer for the current window.
1419 If {expr} is a String, it is used as a |file-pattern| to match
1420 with the buffer names. This is always done like 'magic' is
1421 set and 'cpoptions' is empty. When there is more than one
1422 match an empty string is returned.
1423 "" or "%" can be used for the current buffer, "#" for the
1424 alternate buffer.
1425 A full match is preferred, otherwise a match at the start, end
1426 or middle of the buffer name is accepted.
1427 Listed buffers are found first. If there is a single match
1428 with a listed buffer, that one is returned. Next unlisted
1429 buffers are searched for.
1430 If the {expr} is a String, but you want to use it as a buffer
1431 number, force it to be a Number by adding zero to it: >
1432 :echo bufname("3" + 0)
1433< If the buffer doesn't exist, or doesn't have a name, an empty
1434 string is returned. >
1435 bufname("#") alternate buffer name
1436 bufname(3) name of buffer 3
1437 bufname("%") name of current buffer
1438 bufname("file2") name of buffer where "file2" matches.
1439< *buffer_name()*
1440 Obsolete name: buffer_name().
1441
1442 *bufnr()*
1443bufnr({expr}) The result is the number of a buffer, as it is displayed by
1444 the ":ls" command. For the use of {expr}, see |bufname()|
1445 above. If the buffer doesn't exist, -1 is returned.
1446 bufnr("$") is the last buffer: >
1447 :let last_buffer = bufnr("$")
1448< The result is a Number, which is the highest buffer number
1449 of existing buffers. Note that not all buffers with a smaller
1450 number necessarily exist, because ":bwipeout" may have removed
1451 them. Use bufexists() to test for the existence of a buffer.
1452 *buffer_number()*
1453 Obsolete name: buffer_number().
1454 *last_buffer_nr()*
1455 Obsolete name for bufnr("$"): last_buffer_nr().
1456
1457bufwinnr({expr}) *bufwinnr()*
1458 The result is a Number, which is the number of the first
1459 window associated with buffer {expr}. For the use of {expr},
1460 see |bufname()| above. If buffer {expr} doesn't exist or
1461 there is no such window, -1 is returned. Example: >
1462
1463 echo "A window containing buffer 1 is " . (bufwinnr(1))
1464
1465< The number can be used with |CTRL-W_w| and ":wincmd w"
1466 |:wincmd|.
1467
1468
1469byte2line({byte}) *byte2line()*
1470 Return the line number that contains the character at byte
1471 count {byte} in the current buffer. This includes the
1472 end-of-line character, depending on the 'fileformat' option
1473 for the current buffer. The first character has byte count
1474 one.
1475 Also see |line2byte()|, |go| and |:goto|.
1476 {not available when compiled without the |+byte_offset|
1477 feature}
1478
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001479byteidx({expr}, {nr}) *byteidx()*
1480 Return byte index of the {nr}'th character in the string
1481 {expr}. Use zero for the first character, it returns zero.
1482 This function is only useful when there are multibyte
1483 characters, otherwise the returned value is equal to {nr}.
1484 Composing characters are counted as a separate character.
1485 Example : >
1486 echo matchstr(str, ".", byteidx(str, 3))
1487< will display the fourth character. Another way to do the
1488 same: >
1489 let s = strpart(str, byteidx(str, 3))
1490 echo strpart(s, 0, byteidx(s, 1))
1491< If there are less than {nr} characters -1 is returned.
1492 If there are exactly {nr} characters the length of the string
1493 is returned.
1494
Bram Moolenaar13065c42005-01-08 16:08:21 +00001495call({func}, {arglist}) *call()* *E699*
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001496 Call function {func} with the items in List {arglist} as
1497 arguments.
1498 {func} can either be a Funcref or the name of a function.
1499 a:firstline and a:lastline are set to the cursor line.
1500 Returns the return value of the called function.
1501
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502char2nr({expr}) *char2nr()*
1503 Return number value of the first char in {expr}. Examples: >
1504 char2nr(" ") returns 32
1505 char2nr("ABC") returns 65
1506< The current 'encoding' is used. Example for "utf-8": >
1507 char2nr("á") returns 225
1508 char2nr("á"[0]) returns 195
1509
1510cindent({lnum}) *cindent()*
1511 Get the amount of indent for line {lnum} according the C
1512 indenting rules, as with 'cindent'.
1513 The indent is counted in spaces, the value of 'tabstop' is
1514 relevant. {lnum} is used just like in |getline()|.
1515 When {lnum} is invalid or Vim was not compiled the |+cindent|
1516 feature, -1 is returned.
1517
1518 *col()*
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001519col({expr}) The result is a Number, which is the byte index of the column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 position given with {expr}. The accepted positions are:
1521 . the cursor position
1522 $ the end of the cursor line (the result is the
1523 number of characters in the cursor line plus one)
1524 'x position of mark x (if the mark is not set, 0 is
1525 returned)
1526 For the screen column position use |virtcol()|.
1527 Note that only marks in the current file can be used.
1528 Examples: >
1529 col(".") column of cursor
1530 col("$") length of cursor line plus one
1531 col("'t") column of mark t
1532 col("'" . markname) column of mark markname
1533< The first column is 1. 0 is returned for an error.
1534 For the cursor position, when 'virtualedit' is active, the
1535 column is one higher if the cursor is after the end of the
1536 line. This can be used to obtain the column in Insert mode: >
1537 :imap <F2> <C-O>:let save_ve = &ve<CR>
1538 \<C-O>:set ve=all<CR>
1539 \<C-O>:echo col(".") . "\n" <Bar>
1540 \let &ve = save_ve<CR>
1541<
1542 *confirm()*
1543confirm({msg} [, {choices} [, {default} [, {type}]]])
1544 Confirm() offers the user a dialog, from which a choice can be
1545 made. It returns the number of the choice. For the first
1546 choice this is 1.
1547 Note: confirm() is only supported when compiled with dialog
1548 support, see |+dialog_con| and |+dialog_gui|.
1549 {msg} is displayed in a |dialog| with {choices} as the
1550 alternatives. When {choices} is missing or empty, "&OK" is
1551 used (and translated).
1552 {msg} is a String, use '\n' to include a newline. Only on
1553 some systems the string is wrapped when it doesn't fit.
1554 {choices} is a String, with the individual choices separated
1555 by '\n', e.g. >
1556 confirm("Save changes?", "&Yes\n&No\n&Cancel")
1557< The letter after the '&' is the shortcut key for that choice.
1558 Thus you can type 'c' to select "Cancel". The shortcut does
1559 not need to be the first letter: >
1560 confirm("file has been modified", "&Save\nSave &All")
1561< For the console, the first letter of each choice is used as
1562 the default shortcut key.
1563 The optional {default} argument is the number of the choice
1564 that is made if the user hits <CR>. Use 1 to make the first
1565 choice the default one. Use 0 to not set a default. If
1566 {default} is omitted, 1 is used.
1567 The optional {type} argument gives the type of dialog. This
1568 is only used for the icon of the Win32 GUI. It can be one of
1569 these values: "Error", "Question", "Info", "Warning" or
1570 "Generic". Only the first character is relevant. When {type}
1571 is omitted, "Generic" is used.
1572 If the user aborts the dialog by pressing <Esc>, CTRL-C,
1573 or another valid interrupt key, confirm() returns 0.
1574
1575 An example: >
1576 :let choice = confirm("What do you want?", "&Apples\n&Oranges\n&Bananas", 2)
1577 :if choice == 0
1578 : echo "make up your mind!"
1579 :elseif choice == 3
1580 : echo "tasteful"
1581 :else
1582 : echo "I prefer bananas myself."
1583 :endif
1584< In a GUI dialog, buttons are used. The layout of the buttons
1585 depends on the 'v' flag in 'guioptions'. If it is included,
1586 the buttons are always put vertically. Otherwise, confirm()
1587 tries to put the buttons in one horizontal line. If they
1588 don't fit, a vertical layout is used anyway. For some systems
1589 the horizontal layout is always used.
1590
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001591 *copy()*
1592copy({expr}) Make a copy of {expr}. For Numbers and Strings this isn't
1593 different from using {expr} directly.
1594 When {expr} is a List a shallow copy is created. This means
1595 that the original List can be changed without changing the
1596 copy, and vise versa. But the items are identical, thus
1597 changing an item changes the contents of both Lists. Also see
1598 |deepcopy()|.
1599
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001600count({list}, {expr} [, {start} [, {ic}]]) *count()*
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001601 Return the number of times an item with value {expr} appears
1602 in List {list}.
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001603 If {start} is given then don't count items with a lower index.
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001604 When {ic} is given and it's non-zero then case is ignored.
1605
1606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 *cscope_connection()*
1608cscope_connection([{num} , {dbpath} [, {prepend}]])
1609 Checks for the existence of a |cscope| connection. If no
1610 parameters are specified, then the function returns:
1611 0, if cscope was not available (not compiled in), or
1612 if there are no cscope connections;
1613 1, if there is at least one cscope connection.
1614
1615 If parameters are specified, then the value of {num}
1616 determines how existence of a cscope connection is checked:
1617
1618 {num} Description of existence check
1619 ----- ------------------------------
1620 0 Same as no parameters (e.g., "cscope_connection()").
1621 1 Ignore {prepend}, and use partial string matches for
1622 {dbpath}.
1623 2 Ignore {prepend}, and use exact string matches for
1624 {dbpath}.
1625 3 Use {prepend}, use partial string matches for both
1626 {dbpath} and {prepend}.
1627 4 Use {prepend}, use exact string matches for both
1628 {dbpath} and {prepend}.
1629
1630 Note: All string comparisons are case sensitive!
1631
1632 Examples. Suppose we had the following (from ":cs show"): >
1633
1634 # pid database name prepend path
1635 0 27664 cscope.out /usr/local
1636<
1637 Invocation Return Val ~
1638 ---------- ---------- >
1639 cscope_connection() 1
1640 cscope_connection(1, "out") 1
1641 cscope_connection(2, "out") 0
1642 cscope_connection(3, "out") 0
1643 cscope_connection(3, "out", "local") 1
1644 cscope_connection(4, "out") 0
1645 cscope_connection(4, "out", "local") 0
1646 cscope_connection(4, "cscope.out", "/usr/local") 1
1647<
1648cursor({lnum}, {col}) *cursor()*
1649 Positions the cursor at the column {col} in the line {lnum}.
1650 Does not change the jumplist.
1651 If {lnum} is greater than the number of lines in the buffer,
1652 the cursor will be positioned at the last line in the buffer.
1653 If {lnum} is zero, the cursor will stay in the current line.
1654 If {col} is greater than the number of characters in the line,
1655 the cursor will be positioned at the last character in the
1656 line.
1657 If {col} is zero, the cursor will stay in the current column.
1658
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001659
Bram Moolenaar13065c42005-01-08 16:08:21 +00001660deepcopy({expr}) *deepcopy()* *E698*
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001661 Make a copy of {expr}. For Numbers and Strings this isn't
1662 different from using {expr} directly.
1663 When {expr} is a List a full copy is created. This means
1664 that the original List can be changed without changing the
1665 copy, and vise versa. When an item is a List, a copy for it
1666 is made, recursively. Thus changing an item in the copy does
1667 not change the contents of the original List.
1668 Also see |copy()|.
1669
1670delete({fname}) *delete()*
1671 Deletes the file by the name {fname}. The result is a Number,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 which is 0 if the file was deleted successfully, and non-zero
1673 when the deletion failed.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001674 Use |remove()| to delete an item from a List.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
1676 *did_filetype()*
1677did_filetype() Returns non-zero when autocommands are being executed and the
1678 FileType event has been triggered at least once. Can be used
1679 to avoid triggering the FileType event again in the scripts
1680 that detect the file type. |FileType|
1681 When editing another file, the counter is reset, thus this
1682 really checks if the FileType event has been triggered for the
1683 current buffer. This allows an autocommand that starts
1684 editing another buffer to set 'filetype' and load a syntax
1685 file.
1686
Bram Moolenaar47136d72004-10-12 20:02:24 +00001687diff_filler({lnum}) *diff_filler()*
1688 Returns the number of filler lines above line {lnum}.
1689 These are the lines that were inserted at this point in
1690 another diff'ed window. These filler lines are shown in the
1691 display but don't exist in the buffer.
1692 {lnum} is used like with |getline()|. Thus "." is the current
1693 line, "'m" mark m, etc.
1694 Returns 0 if the current window is not in diff mode.
1695
1696diff_hlID({lnum}, {col}) *diff_hlID()*
1697 Returns the highlight ID for diff mode at line {lnum} column
1698 {col} (byte index). When the current line does not have a
1699 diff change zero is returned.
1700 {lnum} is used like with |getline()|. Thus "." is the current
1701 line, "'m" mark m, etc.
1702 {col} is 1 for the leftmost column, {lnum} is 1 for the first
1703 line.
1704 The highlight ID can be used with |synIDattr()| to obtain
1705 syntax information about the highlighting.
1706
Bram Moolenaar13065c42005-01-08 16:08:21 +00001707empty({expr}) *empty()*
1708 Return the Number 1 if {expr} is empty, zero otherwise.
1709 A List is empty when it does not have any items.
1710 A Number is empty when its value is zero.
1711 For a long List this is much faster then comparing the length
1712 with zero.
1713
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714escape({string}, {chars}) *escape()*
1715 Escape the characters in {chars} that occur in {string} with a
1716 backslash. Example: >
1717 :echo escape('c:\program files\vim', ' \')
1718< results in: >
1719 c:\\program\ files\\vim
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001720
1721< *eval()*
1722eval({string}) Evaluate {string} and return the result. Especially useful to
1723 turn the result of |string()| back into the original value.
1724 This works for Numbers, Strings and composites of them.
1725 Also works for Funcrefs that refer to existing functions.
1726
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727eventhandler() *eventhandler()*
1728 Returns 1 when inside an event handler. That is that Vim got
1729 interrupted while waiting for the user to type a character,
1730 e.g., when dropping a file on Vim. This means interactive
1731 commands cannot be used. Otherwise zero is returned.
1732
1733executable({expr}) *executable()*
1734 This function checks if an executable with the name {expr}
1735 exists. {expr} must be the name of the program without any
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001736 arguments.
1737 executable() uses the value of $PATH and/or the normal
1738 searchpath for programs. *PATHEXT*
1739 On MS-DOS and MS-Windows the ".exe", ".bat", etc. can
1740 optionally be included. Then the extensions in $PATHEXT are
1741 tried. Thus if "foo.exe" does not exist, "foo.exe.bat" can be
1742 found. If $PATHEXT is not set then ".exe;.com;.bat;.cmd" is
1743 used. A dot by itself can be used in $PATHEXT to try using
1744 the name without an extension. When 'shell' looks like a
1745 Unix shell, then the name is also tried without adding an
1746 extension.
1747 On MS-DOS and MS-Windows it only checks if the file exists and
1748 is not a directory, not if it's really executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 The result is a Number:
1750 1 exists
1751 0 does not exist
1752 -1 not implemented on this system
1753
1754 *exists()*
1755exists({expr}) The result is a Number, which is non-zero if {expr} is
1756 defined, zero otherwise. The {expr} argument is a string,
1757 which contains one of these:
1758 &option-name Vim option (only checks if it exists,
1759 not if it really works)
1760 +option-name Vim option that works.
1761 $ENVNAME environment variable (could also be
1762 done by comparing with an empty
1763 string)
1764 *funcname built-in function (see |functions|)
1765 or user defined function (see
1766 |user-functions|).
1767 varname internal variable (see
1768 |internal-variables|). Does not work
1769 for |curly-braces-names|.
1770 :cmdname Ex command: built-in command, user
1771 command or command modifier |:command|.
1772 Returns:
1773 1 for match with start of a command
1774 2 full match with a command
1775 3 matches several user commands
1776 To check for a supported command
1777 always check the return value to be 2.
1778 #event autocommand defined for this event
1779 #event#pattern autocommand defined for this event and
1780 pattern (the pattern is taken
1781 literally and compared to the
1782 autocommand patterns character by
1783 character)
1784 For checking for a supported feature use |has()|.
1785
1786 Examples: >
1787 exists("&shortname")
1788 exists("$HOSTNAME")
1789 exists("*strftime")
1790 exists("*s:MyFunc")
1791 exists("bufcount")
1792 exists(":Make")
1793 exists("#CursorHold");
1794 exists("#BufReadPre#*.gz")
1795< There must be no space between the symbol (&/$/*/#) and the
1796 name.
1797 Note that the argument must be a string, not the name of the
1798 variable itself! For example: >
1799 exists(bufcount)
1800< This doesn't check for existence of the "bufcount" variable,
1801 but gets the contents of "bufcount", and checks if that
1802 exists.
1803
1804expand({expr} [, {flag}]) *expand()*
1805 Expand wildcards and the following special keywords in {expr}.
1806 The result is a String.
1807
1808 When there are several matches, they are separated by <NL>
1809 characters. [Note: in version 5.0 a space was used, which
1810 caused problems when a file name contains a space]
1811
1812 If the expansion fails, the result is an empty string. A name
1813 for a non-existing file is not included.
1814
1815 When {expr} starts with '%', '#' or '<', the expansion is done
1816 like for the |cmdline-special| variables with their associated
1817 modifiers. Here is a short overview:
1818
1819 % current file name
1820 # alternate file name
1821 #n alternate file name n
1822 <cfile> file name under the cursor
1823 <afile> autocmd file name
1824 <abuf> autocmd buffer number (as a String!)
1825 <amatch> autocmd matched name
1826 <sfile> sourced script file name
1827 <cword> word under the cursor
1828 <cWORD> WORD under the cursor
1829 <client> the {clientid} of the last received
1830 message |server2client()|
1831 Modifiers:
1832 :p expand to full path
1833 :h head (last path component removed)
1834 :t tail (last path component only)
1835 :r root (one extension removed)
1836 :e extension only
1837
1838 Example: >
1839 :let &tags = expand("%:p:h") . "/tags"
1840< Note that when expanding a string that starts with '%', '#' or
1841 '<', any following text is ignored. This does NOT work: >
1842 :let doesntwork = expand("%:h.bak")
1843< Use this: >
1844 :let doeswork = expand("%:h") . ".bak"
1845< Also note that expanding "<cfile>" and others only returns the
1846 referenced file name without further expansion. If "<cfile>"
1847 is "~/.cshrc", you need to do another expand() to have the
1848 "~/" expanded into the path of the home directory: >
1849 :echo expand(expand("<cfile>"))
1850<
1851 There cannot be white space between the variables and the
1852 following modifier. The |fnamemodify()| function can be used
1853 to modify normal file names.
1854
1855 When using '%' or '#', and the current or alternate file name
1856 is not defined, an empty string is used. Using "%:p" in a
1857 buffer with no name, results in the current directory, with a
1858 '/' added.
1859
1860 When {expr} does not start with '%', '#' or '<', it is
1861 expanded like a file name is expanded on the command line.
1862 'suffixes' and 'wildignore' are used, unless the optional
1863 {flag} argument is given and it is non-zero. Names for
1864 non-existing files are included.
1865
1866 Expand() can also be used to expand variables and environment
1867 variables that are only known in a shell. But this can be
1868 slow, because a shell must be started. See |expr-env-expand|.
1869 The expanded variable is still handled like a list of file
1870 names. When an environment variable cannot be expanded, it is
1871 left unchanged. Thus ":echo expand('$FOOBAR')" results in
1872 "$FOOBAR".
1873
1874 See |glob()| for finding existing files. See |system()| for
1875 getting the raw output of an external command.
1876
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001877extend({list1}, {list2} [, {idx}]) *extend()*
1878 Append {list2} to {list1}.
1879 If {idx} is given insert the items of {list2} before item
1880 {idx} in {list1}. When {idx} is zero insert before the first
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001881 item. When {idx} is equal to len({list1}) then {list2} is
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001882 appended.
1883 {list1} is changed when {list2} is not empty.
1884 {list2} remains unchanged.
1885 {list1} and {list2} must be Lists.
1886 Returns {list1}.
1887 Examples: >
1888 :echo sort(extend(mylist, [7, 5]))
1889 :call extend(mylist, [2, 3], 1)
Bram Moolenaara14de3d2005-01-07 21:48:26 +00001890< Use |add()| to concatenate one item to a list. To concatenate
1891 two lists into a new list use the + operator: >
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001892 :let newlist = [1, 2, 3] + [4, 5]
1893
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894filereadable({file}) *filereadable()*
1895 The result is a Number, which is TRUE when a file with the
1896 name {file} exists, and can be read. If {file} doesn't exist,
1897 or is a directory, the result is FALSE. {file} is any
1898 expression, which is used as a String.
1899 *file_readable()*
1900 Obsolete name: file_readable().
1901
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001902
1903filter({list}, {expr}) *filter()* *E712*
1904 For each item in {list} evaluate {expr} and when the result is
1905 zero remove the item from the List.
1906 Inside {expr} the symbol "&" stands for the existing
1907 item. Example: >
1908 :call filter(mylist, #& !~ "OLD"#)
1909< Removes the items where "OLD" appears.
1910 Note that {expr} is an expression that evaluates to an
1911 expression. Often it is good to use a |sharp-string| to avoid
1912 having to double backslashes.
1913 The operation is done in-place. If you want a list to remain
1914 unmodified make a copy first: >
1915 :let l = filter(copy(mylist), #& =~ "KEEP"#)
1916< Returns {list}.
1917
1918
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00001919finddir({name}[, {path}[, {count}]]) *finddir()*
1920 Find directory {name} in {path}.
1921 If {path} is omitted or empty then 'path' is used.
1922 If the optional {count} is given, find {count}'s occurrence of
1923 {name} in {path}.
1924 This is quite similar to the ex-command |:find|.
1925 When the found directory is below the current directory a
1926 relative path is returned. Otherwise a full path is returned.
1927 Example: >
1928 :echo findfile("tags.vim", ".;")
1929< Searches from the current directory upwards until it finds
1930 the file "tags.vim".
1931 {only available when compiled with the +file_in_path feature}
1932
1933findfile({name}[, {path}[, {count}]]) *findfile()*
1934 Just like |finddir()|, but find a file instead of a directory.
1935
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936filewritable({file}) *filewritable()*
1937 The result is a Number, which is 1 when a file with the
1938 name {file} exists, and can be written. If {file} doesn't
1939 exist, or is not writable, the result is 0. If (file) is a
1940 directory, and we can write to it, the result is 2.
1941
1942fnamemodify({fname}, {mods}) *fnamemodify()*
1943 Modify file name {fname} according to {mods}. {mods} is a
1944 string of characters like it is used for file names on the
1945 command line. See |filename-modifiers|.
1946 Example: >
1947 :echo fnamemodify("main.c", ":p:h")
1948< results in: >
1949 /home/mool/vim/vim/src
1950< Note: Environment variables and "~" don't work in {fname}, use
1951 |expand()| first then.
1952
1953foldclosed({lnum}) *foldclosed()*
1954 The result is a Number. If the line {lnum} is in a closed
1955 fold, the result is the number of the first line in that fold.
1956 If the line {lnum} is not in a closed fold, -1 is returned.
1957
1958foldclosedend({lnum}) *foldclosedend()*
1959 The result is a Number. If the line {lnum} is in a closed
1960 fold, the result is the number of the last line in that fold.
1961 If the line {lnum} is not in a closed fold, -1 is returned.
1962
1963foldlevel({lnum}) *foldlevel()*
1964 The result is a Number, which is the foldlevel of line {lnum}
1965 in the current buffer. For nested folds the deepest level is
1966 returned. If there is no fold at line {lnum}, zero is
1967 returned. It doesn't matter if the folds are open or closed.
1968 When used while updating folds (from 'foldexpr') -1 is
1969 returned for lines where folds are still to be updated and the
1970 foldlevel is unknown. As a special case the level of the
1971 previous line is usually available.
1972
1973 *foldtext()*
1974foldtext() Returns a String, to be displayed for a closed fold. This is
1975 the default function used for the 'foldtext' option and should
1976 only be called from evaluating 'foldtext'. It uses the
1977 |v:foldstart|, |v:foldend| and |v:folddashes| variables.
1978 The returned string looks like this: >
1979 +-- 45 lines: abcdef
1980< The number of dashes depends on the foldlevel. The "45" is
1981 the number of lines in the fold. "abcdef" is the text in the
1982 first non-blank line of the fold. Leading white space, "//"
1983 or "/*" and the text from the 'foldmarker' and 'commentstring'
1984 options is removed.
1985 {not available when compiled without the |+folding| feature}
1986
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001987foldtextresult({lnum}) *foldtextresult()*
1988 Returns the text that is displayed for the closed fold at line
1989 {lnum}. Evaluates 'foldtext' in the appropriate context.
1990 When there is no closed fold at {lnum} an empty string is
1991 returned.
1992 {lnum} is used like with |getline()|. Thus "." is the current
1993 line, "'m" mark m, etc.
1994 Useful when exporting folded text, e.g., to HTML.
1995 {not available when compiled without the |+folding| feature}
1996
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 *foreground()*
1998foreground() Move the Vim window to the foreground. Useful when sent from
1999 a client to a Vim server. |remote_send()|
2000 On Win32 systems this might not work, the OS does not always
2001 allow a window to bring itself to the foreground. Use
2002 |remote_foreground()| instead.
2003 {only in the Win32, Athena, Motif and GTK GUI versions and the
2004 Win32 console version}
2005
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002006
Bram Moolenaar13065c42005-01-08 16:08:21 +00002007function({name}) *function()* *E700*
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00002008 Return a Funcref variable that refers to function {name}.
2009 {name} can be a user defined function or an internal function.
2010
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002011
2012get({list}, {idx} [, {default}]) *get*
2013 Get item {idx} from List {list}. When this item is not
2014 available return {default}. Return zero when {default} is
2015 omitted.
2016
2017getbufvar({expr}, {varname}) *getbufvar()*
2018 The result is the value of option or local buffer variable
2019 {varname} in buffer {expr}. Note that the name without "b:"
2020 must be used.
2021 This also works for a global or local window option, but it
2022 doesn't work for a global or local window variable.
2023 For the use of {expr}, see |bufname()| above.
2024 When the buffer or variable doesn't exist an empty string is
2025 returned, there is no error message.
2026 Examples: >
2027 :let bufmodified = getbufvar(1, "&mod")
2028 :echo "todo myvar = " . getbufvar("todo", "myvar")
2029<
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030getchar([expr]) *getchar()*
2031 Get a single character from the user. If it is an 8-bit
2032 character, the result is a number. Otherwise a String is
2033 returned with the encoded character. For a special key it's a
2034 sequence of bytes starting with 0x80 (decimal: 128).
2035 If [expr] is omitted, wait until a character is available.
2036 If [expr] is 0, only get a character when one is available.
2037 If [expr] is 1, only check if a character is available, it is
2038 not consumed. If a normal character is
2039 available, it is returned, otherwise a
2040 non-zero value is returned.
2041 If a normal character available, it is returned as a Number.
2042 Use nr2char() to convert it to a String.
2043 The returned value is zero if no character is available.
2044 The returned value is a string of characters for special keys
2045 and when a modifier (shift, control, alt) was used.
2046 There is no prompt, you will somehow have to make clear to the
2047 user that a character has to be typed.
2048 There is no mapping for the character.
2049 Key codes are replaced, thus when the user presses the <Del>
2050 key you get the code for the <Del> key, not the raw character
2051 sequence. Examples: >
2052 getchar() == "\<Del>"
2053 getchar() == "\<S-Left>"
2054< This example redefines "f" to ignore case: >
2055 :nmap f :call FindChar()<CR>
2056 :function FindChar()
2057 : let c = nr2char(getchar())
2058 : while col('.') < col('$') - 1
2059 : normal l
2060 : if getline('.')[col('.') - 1] ==? c
2061 : break
2062 : endif
2063 : endwhile
2064 :endfunction
2065
2066getcharmod() *getcharmod()*
2067 The result is a Number which is the state of the modifiers for
2068 the last obtained character with getchar() or in another way.
2069 These values are added together:
2070 2 shift
2071 4 control
2072 8 alt (meta)
2073 16 mouse double click
2074 32 mouse triple click
2075 64 mouse quadruple click
2076 128 Macintosh only: command
2077 Only the modifiers that have not been included in the
2078 character itself are obtained. Thus Shift-a results in "A"
2079 with no modifier.
2080
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081getcmdline() *getcmdline()*
2082 Return the current command-line. Only works when the command
2083 line is being edited, thus requires use of |c_CTRL-\_e| or
2084 |c_CTRL-R_=|.
2085 Example: >
2086 :cmap <F7> <C-\>eescape(getcmdline(), ' \')<CR>
2087< Also see |getcmdpos()| and |setcmdpos()|.
2088
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002089getcmdpos() *getcmdpos()*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 Return the position of the cursor in the command line as a
2091 byte count. The first column is 1.
2092 Only works when editing the command line, thus requires use of
2093 |c_CTRL-\_e| or |c_CTRL-R_=|. Returns 0 otherwise.
2094 Also see |setcmdpos()| and |getcmdline()|.
2095
2096 *getcwd()*
2097getcwd() The result is a String, which is the name of the current
2098 working directory.
2099
2100getfsize({fname}) *getfsize()*
2101 The result is a Number, which is the size in bytes of the
2102 given file {fname}.
2103 If {fname} is a directory, 0 is returned.
2104 If the file {fname} can't be found, -1 is returned.
2105
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002106getfontname([{name}]) *getfontname()*
2107 Without an argument returns the name of the normal font being
2108 used. Like what is used for the Normal highlight group
2109 |hl-Normal|.
2110 With an argument a check is done whether {name} is a valid
2111 font name. If not then an empty string is returned.
2112 Otherwise the actual font name is returned, or {name} if the
2113 GUI does not support obtaining the real name.
2114 Only works when the GUI is running, thus not you your vimrc or
2115 Note that the GTK 2 GUI accepts any font name, thus checking
2116 for a valid name does not work.
2117 gvimrc file. Use the |GUIEnter| autocommand to use this
2118 function just after the GUI has started.
2119
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002120getfperm({fname}) *getfperm()*
2121 The result is a String, which is the read, write, and execute
2122 permissions of the given file {fname}.
2123 If {fname} does not exist or its directory cannot be read, an
2124 empty string is returned.
2125 The result is of the form "rwxrwxrwx", where each group of
2126 "rwx" flags represent, in turn, the permissions of the owner
2127 of the file, the group the file belongs to, and other users.
2128 If a user does not have a given permission the flag for this
2129 is replaced with the string "-". Example: >
2130 :echo getfperm("/etc/passwd")
2131< This will hopefully (from a security point of view) display
2132 the string "rw-r--r--" or even "rw-------".
2133
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134getftime({fname}) *getftime()*
2135 The result is a Number, which is the last modification time of
2136 the given file {fname}. The value is measured as seconds
2137 since 1st Jan 1970, and may be passed to strftime(). See also
2138 |localtime()| and |strftime()|.
2139 If the file {fname} can't be found -1 is returned.
2140
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002141getftype({fname}) *getftype()*
2142 The result is a String, which is a description of the kind of
2143 file of the given file {fname}.
2144 If {fname} does not exist an empty string is returned.
2145 Here is a table over different kinds of files and their
2146 results:
2147 Normal file "file"
2148 Directory "dir"
2149 Symbolic link "link"
2150 Block device "bdev"
2151 Character device "cdev"
2152 Socket "socket"
2153 FIFO "fifo"
2154 All other "other"
2155 Example: >
2156 getftype("/home")
2157< Note that a type such as "link" will only be returned on
2158 systems that support it. On some systems only "dir" and
2159 "file" are returned.
2160
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 *getline()*
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002162getline({lnum} [, {end}])
2163 Without {end} the result is a String, which is line {lnum}
2164 from the current buffer. Example: >
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 getline(1)
2166< When {lnum} is a String that doesn't start with a
2167 digit, line() is called to translate the String into a Number.
2168 To get the line under the cursor: >
2169 getline(".")
2170< When {lnum} is smaller than 1 or bigger than the number of
2171 lines in the buffer, an empty string is returned.
2172
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002173 When {end} is given the result is a List where each item is a
2174 line from the current buffer in the range {lnum} to {end},
2175 including line {end}.
2176 {end} is used in the same way as {lnum}.
2177 Non-existing lines are silently omitted.
2178 When {end} is before {lnum} an error is given.
2179 Example: >
2180 :let start = line('.')
2181 :let end = search("^$") - 1
2182 :let lines = getline(start, end)
2183
2184
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185getreg([{regname}]) *getreg()*
2186 The result is a String, which is the contents of register
2187 {regname}. Example: >
2188 :let cliptext = getreg('*')
2189< getreg('=') returns the last evaluated value of the expression
2190 register. (For use in maps).
2191 If {regname} is not specified, |v:register| is used.
2192
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002193
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194getregtype([{regname}]) *getregtype()*
2195 The result is a String, which is type of register {regname}.
2196 The value will be one of:
2197 "v" for |characterwise| text
2198 "V" for |linewise| text
2199 "<CTRL-V>{width}" for |blockwise-visual| text
2200 0 for an empty or unknown register
2201 <CTRL-V> is one character with value 0x16.
2202 If {regname} is not specified, |v:register| is used.
2203
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002204
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 *getwinposx()*
2206getwinposx() The result is a Number, which is the X coordinate in pixels of
2207 the left hand side of the GUI Vim window. The result will be
2208 -1 if the information is not available.
2209
2210 *getwinposy()*
2211getwinposy() The result is a Number, which is the Y coordinate in pixels of
2212 the top of the GUI Vim window. The result will be -1 if the
2213 information is not available.
2214
2215getwinvar({nr}, {varname}) *getwinvar()*
2216 The result is the value of option or local window variable
2217 {varname} in window {nr}.
2218 This also works for a global or local buffer option, but it
2219 doesn't work for a global or local buffer variable.
2220 Note that the name without "w:" must be used.
2221 Examples: >
2222 :let list_is_on = getwinvar(2, '&list')
2223 :echo "myvar = " . getwinvar(1, 'myvar')
2224<
2225 *glob()*
2226glob({expr}) Expand the file wildcards in {expr}. The result is a String.
2227 When there are several matches, they are separated by <NL>
2228 characters.
2229 If the expansion fails, the result is an empty string.
2230 A name for a non-existing file is not included.
2231
2232 For most systems backticks can be used to get files names from
2233 any external command. Example: >
2234 :let tagfiles = glob("`find . -name tags -print`")
2235 :let &tags = substitute(tagfiles, "\n", ",", "g")
2236< The result of the program inside the backticks should be one
2237 item per line. Spaces inside an item are allowed.
2238
2239 See |expand()| for expanding special Vim variables. See
2240 |system()| for getting the raw output of an external command.
2241
2242globpath({path}, {expr}) *globpath()*
2243 Perform glob() on all directories in {path} and concatenate
2244 the results. Example: >
2245 :echo globpath(&rtp, "syntax/c.vim")
2246< {path} is a comma-separated list of directory names. Each
2247 directory name is prepended to {expr} and expanded like with
2248 glob(). A path separator is inserted when needed.
2249 To add a comma inside a directory name escape it with a
2250 backslash. Note that on MS-Windows a directory may have a
2251 trailing backslash, remove it if you put a comma after it.
2252 If the expansion fails for one of the directories, there is no
2253 error message.
2254 The 'wildignore' option applies: Names matching one of the
2255 patterns in 'wildignore' will be skipped.
2256
2257 *has()*
2258has({feature}) The result is a Number, which is 1 if the feature {feature} is
2259 supported, zero otherwise. The {feature} argument is a
2260 string. See |feature-list| below.
2261 Also see |exists()|.
2262
2263hasmapto({what} [, {mode}]) *hasmapto()*
2264 The result is a Number, which is 1 if there is a mapping that
2265 contains {what} in somewhere in the rhs (what it is mapped to)
2266 and this mapping exists in one of the modes indicated by
2267 {mode}.
2268 Both the global mappings and the mappings local to the current
2269 buffer are checked for a match.
2270 If no matching mapping is found 0 is returned.
2271 The following characters are recognized in {mode}:
2272 n Normal mode
2273 v Visual mode
2274 o Operator-pending mode
2275 i Insert mode
2276 l Language-Argument ("r", "f", "t", etc.)
2277 c Command-line mode
2278 When {mode} is omitted, "nvo" is used.
2279
2280 This function is useful to check if a mapping already exists
2281 to a function in a Vim script. Example: >
2282 :if !hasmapto('\ABCdoit')
2283 : map <Leader>d \ABCdoit
2284 :endif
2285< This installs the mapping to "\ABCdoit" only if there isn't
2286 already a mapping to "\ABCdoit".
2287
2288histadd({history}, {item}) *histadd()*
2289 Add the String {item} to the history {history} which can be
2290 one of: *hist-names*
2291 "cmd" or ":" command line history
2292 "search" or "/" search pattern history
2293 "expr" or "=" typed expression history
2294 "input" or "@" input line history
2295 If {item} does already exist in the history, it will be
2296 shifted to become the newest entry.
2297 The result is a Number: 1 if the operation was successful,
2298 otherwise 0 is returned.
2299
2300 Example: >
2301 :call histadd("input", strftime("%Y %b %d"))
2302 :let date=input("Enter date: ")
2303< This function is not available in the |sandbox|.
2304
2305histdel({history} [, {item}]) *histdel()*
2306 Clear {history}, ie. delete all its entries. See |hist-names|
2307 for the possible values of {history}.
2308
2309 If the parameter {item} is given as String, this is seen
2310 as regular expression. All entries matching that expression
2311 will be removed from the history (if there are any).
2312 Upper/lowercase must match, unless "\c" is used |/\c|.
2313 If {item} is a Number, it will be interpreted as index, see
2314 |:history-indexing|. The respective entry will be removed
2315 if it exists.
2316
2317 The result is a Number: 1 for a successful operation,
2318 otherwise 0 is returned.
2319
2320 Examples:
2321 Clear expression register history: >
2322 :call histdel("expr")
2323<
2324 Remove all entries starting with "*" from the search history: >
2325 :call histdel("/", '^\*')
2326<
2327 The following three are equivalent: >
2328 :call histdel("search", histnr("search"))
2329 :call histdel("search", -1)
2330 :call histdel("search", '^'.histget("search", -1).'$')
2331<
2332 To delete the last search pattern and use the last-but-one for
2333 the "n" command and 'hlsearch': >
2334 :call histdel("search", -1)
2335 :let @/ = histget("search", -1)
2336
2337histget({history} [, {index}]) *histget()*
2338 The result is a String, the entry with Number {index} from
2339 {history}. See |hist-names| for the possible values of
2340 {history}, and |:history-indexing| for {index}. If there is
2341 no such entry, an empty String is returned. When {index} is
2342 omitted, the most recent item from the history is used.
2343
2344 Examples:
2345 Redo the second last search from history. >
2346 :execute '/' . histget("search", -2)
2347
2348< Define an Ex command ":H {num}" that supports re-execution of
2349 the {num}th entry from the output of |:history|. >
2350 :command -nargs=1 H execute histget("cmd", 0+<args>)
2351<
2352histnr({history}) *histnr()*
2353 The result is the Number of the current entry in {history}.
2354 See |hist-names| for the possible values of {history}.
2355 If an error occurred, -1 is returned.
2356
2357 Example: >
2358 :let inp_index = histnr("expr")
2359<
2360hlexists({name}) *hlexists()*
2361 The result is a Number, which is non-zero if a highlight group
2362 called {name} exists. This is when the group has been
2363 defined in some way. Not necessarily when highlighting has
2364 been defined for it, it may also have been used for a syntax
2365 item.
2366 *highlight_exists()*
2367 Obsolete name: highlight_exists().
2368
2369 *hlID()*
2370hlID({name}) The result is a Number, which is the ID of the highlight group
2371 with name {name}. When the highlight group doesn't exist,
2372 zero is returned.
2373 This can be used to retrieve information about the highlight
2374 group. For example, to get the background color of the
2375 "Comment" group: >
2376 :echo synIDattr(synIDtrans(hlID("Comment")), "bg")
2377< *highlightID()*
2378 Obsolete name: highlightID().
2379
2380hostname() *hostname()*
2381 The result is a String, which is the name of the machine on
2382 which Vim is currently running. Machine names greater than
2383 256 characters long are truncated.
2384
2385iconv({expr}, {from}, {to}) *iconv()*
2386 The result is a String, which is the text {expr} converted
2387 from encoding {from} to encoding {to}.
2388 When the conversion fails an empty string is returned.
2389 The encoding names are whatever the iconv() library function
2390 can accept, see ":!man 3 iconv".
2391 Most conversions require Vim to be compiled with the |+iconv|
2392 feature. Otherwise only UTF-8 to latin1 conversion and back
2393 can be done.
2394 This can be used to display messages with special characters,
2395 no matter what 'encoding' is set to. Write the message in
2396 UTF-8 and use: >
2397 echo iconv(utf8_str, "utf-8", &enc)
2398< Note that Vim uses UTF-8 for all Unicode encodings, conversion
2399 from/to UCS-2 is automatically changed to use UTF-8. You
2400 cannot use UCS-2 in a string anyway, because of the NUL bytes.
2401 {only available when compiled with the +multi_byte feature}
2402
2403 *indent()*
2404indent({lnum}) The result is a Number, which is indent of line {lnum} in the
2405 current buffer. The indent is counted in spaces, the value
2406 of 'tabstop' is relevant. {lnum} is used just like in
2407 |getline()|.
2408 When {lnum} is invalid -1 is returned.
2409
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002410
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002411index({list}, {expr} [, {start} [, {ic}]]) *index()*
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002412 Return the lowest index in List {list} where the item has a
2413 value equal to {expr}.
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002414 If {start} is given then skip items with a lower index.
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002415 When {ic} is given and it is non-zero, ignore case. Otherwise
2416 case must match.
2417 -1 is returned when {expr} is not found in {list}.
2418 Example: >
2419 :let idx = index(words, "the")
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002420 :if index(numbers, 123) >= 0
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002421
2422
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423input({prompt} [, {text}]) *input()*
2424 The result is a String, which is whatever the user typed on
2425 the command-line. The parameter is either a prompt string, or
2426 a blank string (for no prompt). A '\n' can be used in the
2427 prompt to start a new line. The highlighting set with
2428 |:echohl| is used for the prompt. The input is entered just
2429 like a command-line, with the same editing commands and
2430 mappings. There is a separate history for lines typed for
2431 input().
2432 If the optional {text} is present, this is used for the
2433 default reply, as if the user typed this.
2434 NOTE: This must not be used in a startup file, for the
2435 versions that only run in GUI mode (e.g., the Win32 GUI).
2436 Note: When input() is called from within a mapping it will
2437 consume remaining characters from that mapping, because a
2438 mapping is handled like the characters were typed.
2439 Use |inputsave()| before input() and |inputrestore()|
2440 after input() to avoid that. Another solution is to avoid
2441 that further characters follow in the mapping, e.g., by using
2442 |:execute| or |:normal|.
2443
2444 Example: >
2445 :if input("Coffee or beer? ") == "beer"
2446 : echo "Cheers!"
2447 :endif
2448< Example with default text: >
2449 :let color = input("Color? ", "white")
2450< Example with a mapping: >
2451 :nmap \x :call GetFoo()<CR>:exe "/" . Foo<CR>
2452 :function GetFoo()
2453 : call inputsave()
2454 : let g:Foo = input("enter search pattern: ")
2455 : call inputrestore()
2456 :endfunction
2457
2458inputdialog({prompt} [, {text} [, {cancelreturn}]]) *inputdialog()*
2459 Like input(), but when the GUI is running and text dialogs are
2460 supported, a dialog window pops up to input the text.
2461 Example: >
2462 :let n = inputdialog("value for shiftwidth", &sw)
2463 :if n != ""
2464 : let &sw = n
2465 :endif
2466< When the dialog is cancelled {cancelreturn} is returned. When
2467 omitted an empty string is returned.
2468 Hitting <Enter> works like pressing the OK button. Hitting
2469 <Esc> works like pressing the Cancel button.
2470
2471inputrestore() *inputrestore()*
2472 Restore typeahead that was saved with a previous inputsave().
2473 Should be called the same number of times inputsave() is
2474 called. Calling it more often is harmless though.
2475 Returns 1 when there is nothing to restore, 0 otherwise.
2476
2477inputsave() *inputsave()*
2478 Preserve typeahead (also from mappings) and clear it, so that
2479 a following prompt gets input from the user. Should be
2480 followed by a matching inputrestore() after the prompt. Can
2481 be used several times, in which case there must be just as
2482 many inputrestore() calls.
2483 Returns 1 when out of memory, 0 otherwise.
2484
2485inputsecret({prompt} [, {text}]) *inputsecret()*
2486 This function acts much like the |input()| function with but
2487 two exceptions:
2488 a) the user's response will be displayed as a sequence of
2489 asterisks ("*") thereby keeping the entry secret, and
2490 b) the user's response will not be recorded on the input
2491 |history| stack.
2492 The result is a String, which is whatever the user actually
2493 typed on the command-line in response to the issued prompt.
2494
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00002495insert({list}, {item} [, {idx}]) *insert()*
2496 Insert {item} at the start of List {list}.
2497 If {idx} is specified insert {item} before the item with index
2498 {idx}. If {idx} is zero it goes before the first item, just
2499 like omitting {idx}. A negative {idx} is also possible, see
2500 |list-index|. -1 inserts just before the last item.
2501 Returns the resulting List. Examples: >
2502 :let mylist = insert([2, 3, 5], 1)
2503 :call insert(mylist, 4, -1)
2504 :call insert(mylist, 6, len(mylist))
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002505< The last example can be done simpler with |add()|.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00002506 Note that when {item} is a List it is inserted as a single
2507 item. Use |extend()| to concatenate Lists.
2508
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509isdirectory({directory}) *isdirectory()*
2510 The result is a Number, which is non-zero when a directory
2511 with the name {directory} exists. If {directory} doesn't
2512 exist, or isn't a directory, the result is FALSE. {directory}
2513 is any expression, which is used as a String.
2514
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002515
2516join({list} [, {sep}]) *join()*
2517 Join the items in {list} together into one String.
2518 When {sep} is specified it is put in between the items. If
2519 {sep} is omitted a single space is used.
2520 Note that {sep} is not added at the end. You might want to
2521 add it there too: >
2522 let lines = join(mylist, "\n") . "\n"
2523< String items are used as-is. Lists and Dictionaries are
2524 converted into a string like with |string()|.
2525 The opposite function is |split()|.
2526
Bram Moolenaar13065c42005-01-08 16:08:21 +00002527 *len()* *E701*
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00002528len({expr}) The result is a Number, which is the length of the argument.
2529 When {expr} is a String or a Number the length in bytes is
2530 used, as with |strlen()|.
2531 When {expr} is a List the number of items in the List is
2532 returned.
2533 Otherwise an error is given.
2534
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 *libcall()* *E364* *E368*
2536libcall({libname}, {funcname}, {argument})
2537 Call function {funcname} in the run-time library {libname}
2538 with single argument {argument}.
2539 This is useful to call functions in a library that you
2540 especially made to be used with Vim. Since only one argument
2541 is possible, calling standard library functions is rather
2542 limited.
2543 The result is the String returned by the function. If the
2544 function returns NULL, this will appear as an empty string ""
2545 to Vim.
2546 If the function returns a number, use libcallnr()!
2547 If {argument} is a number, it is passed to the function as an
2548 int; if {argument} is a string, it is passed as a
2549 null-terminated string.
2550 This function will fail in |restricted-mode|.
2551
2552 libcall() allows you to write your own 'plug-in' extensions to
2553 Vim without having to recompile the program. It is NOT a
2554 means to call system functions! If you try to do so Vim will
2555 very probably crash.
2556
2557 For Win32, the functions you write must be placed in a DLL
2558 and use the normal C calling convention (NOT Pascal which is
2559 used in Windows System DLLs). The function must take exactly
2560 one parameter, either a character pointer or a long integer,
2561 and must return a character pointer or NULL. The character
2562 pointer returned must point to memory that will remain valid
2563 after the function has returned (e.g. in static data in the
2564 DLL). If it points to allocated memory, that memory will
2565 leak away. Using a static buffer in the function should work,
2566 it's then freed when the DLL is unloaded.
2567
2568 WARNING: If the function returns a non-valid pointer, Vim may
2569 crash! This also happens if the function returns a number,
2570 because Vim thinks it's a pointer.
2571 For Win32 systems, {libname} should be the filename of the DLL
2572 without the ".DLL" suffix. A full path is only required if
2573 the DLL is not in the usual places.
2574 For Unix: When compiling your own plugins, remember that the
2575 object code must be compiled as position-independent ('PIC').
2576 {only in Win32 on some Unix versions, when the |+libcall|
2577 feature is present}
2578 Examples: >
2579 :echo libcall("libc.so", "getenv", "HOME")
2580 :echo libcallnr("/usr/lib/libc.so", "getpid", "")
2581<
2582 *libcallnr()*
2583libcallnr({libname}, {funcname}, {argument})
2584 Just like libcall(), but used for a function that returns an
2585 int instead of a string.
2586 {only in Win32 on some Unix versions, when the |+libcall|
2587 feature is present}
2588 Example (not very useful...): >
2589 :call libcallnr("libc.so", "printf", "Hello World!\n")
2590 :call libcallnr("libc.so", "sleep", 10)
2591<
2592 *line()*
2593line({expr}) The result is a Number, which is the line number of the file
2594 position given with {expr}. The accepted positions are:
2595 . the cursor position
2596 $ the last line in the current buffer
2597 'x position of mark x (if the mark is not set, 0 is
2598 returned)
2599 Note that only marks in the current file can be used.
2600 Examples: >
2601 line(".") line number of the cursor
2602 line("'t") line number of mark t
2603 line("'" . marker) line number of mark marker
2604< *last-position-jump*
2605 This autocommand jumps to the last known position in a file
2606 just after opening it, if the '" mark is set: >
2607 :au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal g'\"" | endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002608
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609line2byte({lnum}) *line2byte()*
2610 Return the byte count from the start of the buffer for line
2611 {lnum}. This includes the end-of-line character, depending on
2612 the 'fileformat' option for the current buffer. The first
2613 line returns 1.
2614 This can also be used to get the byte count for the line just
2615 below the last line: >
2616 line2byte(line("$") + 1)
2617< This is the file size plus one.
2618 When {lnum} is invalid, or the |+byte_offset| feature has been
2619 disabled at compile time, -1 is returned.
2620 Also see |byte2line()|, |go| and |:goto|.
2621
2622lispindent({lnum}) *lispindent()*
2623 Get the amount of indent for line {lnum} according the lisp
2624 indenting rules, as with 'lisp'.
2625 The indent is counted in spaces, the value of 'tabstop' is
2626 relevant. {lnum} is used just like in |getline()|.
2627 When {lnum} is invalid or Vim was not compiled the
2628 |+lispindent| feature, -1 is returned.
2629
2630localtime() *localtime()*
2631 Return the current time, measured as seconds since 1st Jan
2632 1970. See also |strftime()| and |getftime()|.
2633
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002634
2635map({list}, {expr}) *map()*
2636 Replace each item in {list} with the result of evaluating
2637 {expr}.
2638 Inside {expr} the symbol "&" stands for the existing
2639 item. Example: >
2640 :call map(mylist, #"> " . & . " <"#)
2641< This puts "> " before and " <" after each item in "mylist".
2642 Note that {expr} is an expression that evaluates to an
2643 expression. Often it is good to use a |sharp-string| to avoid
2644 having to double backslashes.
2645 The operation is done in-place. If you want a list to remain
2646 unmodified make a copy first: >
2647 :let tlist = map(copy(mylist), # & . "\t"#)
2648< Returns {list}.
2649
2650
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651maparg({name}[, {mode}]) *maparg()*
2652 Return the rhs of mapping {name} in mode {mode}. When there
2653 is no mapping for {name}, an empty String is returned.
2654 These characters can be used for {mode}:
2655 "n" Normal
2656 "v" Visual
2657 "o" Operator-pending
2658 "i" Insert
2659 "c" Cmd-line
2660 "l" langmap |language-mapping|
2661 "" Normal, Visual and Operator-pending
2662 When {mode} is omitted, the modes from "" are used.
2663 The {name} can have special key names, like in the ":map"
2664 command. The returned String has special characters
2665 translated like in the output of the ":map" command listing.
2666 The mappings local to the current buffer are checked first,
2667 then the global mappings.
2668
2669mapcheck({name}[, {mode}]) *mapcheck()*
2670 Check if there is a mapping that matches with {name} in mode
2671 {mode}. See |maparg()| for {mode} and special names in
2672 {name}.
2673 A match happens with a mapping that starts with {name} and
2674 with a mapping which is equal to the start of {name}.
2675
2676 matches mapping "a" "ab" "abc" ~
2677 mapcheck("a") yes yes yes
2678 mapcheck("abc") yes yes yes
2679 mapcheck("ax") yes no no
2680 mapcheck("b") no no no
2681
2682 The difference with maparg() is that mapcheck() finds a
2683 mapping that matches with {name}, while maparg() only finds a
2684 mapping for {name} exactly.
2685 When there is no mapping that starts with {name}, an empty
2686 String is returned. If there is one, the rhs of that mapping
2687 is returned. If there are several mappings that start with
2688 {name}, the rhs of one of them is returned.
2689 The mappings local to the current buffer are checked first,
2690 then the global mappings.
2691 This function can be used to check if a mapping can be added
2692 without being ambiguous. Example: >
2693 :if mapcheck("_vv") == ""
2694 : map _vv :set guifont=7x13<CR>
2695 :endif
2696< This avoids adding the "_vv" mapping when there already is a
2697 mapping for "_v" or for "_vvv".
2698
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00002699match({expr}, {pat}[, {start}[, {count}]]) *match()*
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002700 When {expr} is a List then this returns the index of the first
2701 item where {pat} matches. Each item is used as a String,
2702 Lists and Dictionaries are used as echoed.
2703 Otherwise, {expr} is used as a String. The result is a
2704 Number, which gives the index (byte offset) in {expr} where
2705 {pat} matches.
2706 A match at the first character or List item returns zero.
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00002707 If there is no match -1 is returned.
2708 Example: >
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002709 :echo match("testing", "ing") " results in 4
2710 :echo match([1, 'x'], '\a') " results in 2
2711< See |string-match| for how {pat} is used.
2712
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00002713 When {count} is given use the {count}'th match. When a match
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002714 is found in a String the search for the next one starts on
2715 character further. Thus this example results in 1: >
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00002716 echo match("testing", "..", 0, 2)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002717< In a List the search continues in the next item.
2718
2719 If {start} is given, the search starts from byte index
2720 {start} in a String or item {start} in a List.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 The result, however, is still the index counted from the
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002722 first character/item. Example: >
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 :echo match("testing", "ing", 2)
2724< result is again "4". >
2725 :echo match("testing", "ing", 4)
2726< result is again "4". >
2727 :echo match("testing", "t", 2)
2728< result is "3".
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002729 For a String, if {start} < 0, it will be set to 0. For a list
2730 the index is counted from the end.
2731 If {start} is out of range (> strlen({expr} for a String or
2732 > len({expr} for a List) -1 is returned.
2733
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 See |pattern| for the patterns that are accepted.
2735 The 'ignorecase' option is used to set the ignore-caseness of
2736 the pattern. 'smartcase' is NOT used. The matching is always
2737 done like 'magic' is set and 'cpoptions' is empty.
2738
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00002739matchend({expr}, {pat}[, {start}[, {count}]]) *matchend()*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 Same as match(), but return the index of first character after
2741 the match. Example: >
2742 :echo matchend("testing", "ing")
2743< results in "7".
2744 The {start}, if given, has the same meaning as for match(). >
2745 :echo matchend("testing", "ing", 2)
2746< results in "7". >
2747 :echo matchend("testing", "ing", 5)
2748< result is "-1".
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002749 When {expr} is a List the result is equal to match().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00002751matchstr({expr}, {pat}[, {start}[, {count}]]) *matchstr()*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 Same as match(), but return the matched string. Example: >
2753 :echo matchstr("testing", "ing")
2754< results in "ing".
2755 When there is no match "" is returned.
2756 The {start}, if given, has the same meaning as for match(). >
2757 :echo matchstr("testing", "ing", 2)
2758< results in "ing". >
2759 :echo matchstr("testing", "ing", 5)
2760< result is "".
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002761 When {expr} is a List then the matching item is returned.
2762 The type isn't changed, it's not necessarily a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002764 *max()*
2765max({list}) Return the maximum value of all items in {list}.
2766 If {list} is not a list or one of the items in {list} cannot
2767 be used as a Number this results in an error.
2768 An empty List results in zero.
2769
2770 *min()*
2771min({list}) Return the minumum value of all items in {list}.
2772 If {list} is not a list or one of the items in {list} cannot
2773 be used as a Number this results in an error.
2774 An empty List results in zero.
2775
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 *mode()*
2777mode() Return a string that indicates the current mode:
2778 n Normal
2779 v Visual by character
2780 V Visual by line
2781 CTRL-V Visual blockwise
2782 s Select by character
2783 S Select by line
2784 CTRL-S Select blockwise
2785 i Insert
2786 R Replace
2787 c Command-line
2788 r Hit-enter prompt
2789 This is useful in the 'statusline' option. In most other
2790 places it always returns "c" or "n".
2791
2792nextnonblank({lnum}) *nextnonblank()*
2793 Return the line number of the first line at or below {lnum}
2794 that is not blank. Example: >
2795 if getline(nextnonblank(1)) =~ "Java"
2796< When {lnum} is invalid or there is no non-blank line at or
2797 below it, zero is returned.
2798 See also |prevnonblank()|.
2799
2800nr2char({expr}) *nr2char()*
2801 Return a string with a single character, which has the number
2802 value {expr}. Examples: >
2803 nr2char(64) returns "@"
2804 nr2char(32) returns " "
2805< The current 'encoding' is used. Example for "utf-8": >
2806 nr2char(300) returns I with bow character
2807< Note that a NUL character in the file is specified with
2808 nr2char(10), because NULs are represented with newline
2809 characters. nr2char(0) is a real NUL and terminates the
2810 string, thus isn't very useful.
2811
2812prevnonblank({lnum}) *prevnonblank()*
2813 Return the line number of the first line at or above {lnum}
2814 that is not blank. Example: >
2815 let ind = indent(prevnonblank(v:lnum - 1))
2816< When {lnum} is invalid or there is no non-blank line at or
2817 above it, zero is returned.
2818 Also see |nextnonblank()|.
2819
2820 *remote_expr()* *E449*
2821remote_expr({server}, {string} [, {idvar}])
2822 Send the {string} to {server}. The string is sent as an
2823 expression and the result is returned after evaluation.
2824 If {idvar} is present, it is taken as the name of a
2825 variable and a {serverid} for later use with
2826 remote_read() is stored there.
2827 See also |clientserver| |RemoteReply|.
2828 This function is not available in the |sandbox|.
2829 {only available when compiled with the |+clientserver| feature}
2830 Note: Any errors will cause a local error message to be issued
2831 and the result will be the empty string.
2832 Examples: >
2833 :echo remote_expr("gvim", "2+2")
2834 :echo remote_expr("gvim1", "b:current_syntax")
2835<
2836
2837remote_foreground({server}) *remote_foreground()*
2838 Move the Vim server with the name {server} to the foreground.
2839 This works like: >
2840 remote_expr({server}, "foreground()")
2841< Except that on Win32 systems the client does the work, to work
2842 around the problem that the OS doesn't always allow the server
2843 to bring itself to the foreground.
2844 This function is not available in the |sandbox|.
2845 {only in the Win32, Athena, Motif and GTK GUI versions and the
2846 Win32 console version}
2847
2848
2849remote_peek({serverid} [, {retvar}]) *remote_peek()*
2850 Returns a positive number if there are available strings
2851 from {serverid}. Copies any reply string into the variable
2852 {retvar} if specified. {retvar} must be a string with the
2853 name of a variable.
2854 Returns zero if none are available.
2855 Returns -1 if something is wrong.
2856 See also |clientserver|.
2857 This function is not available in the |sandbox|.
2858 {only available when compiled with the |+clientserver| feature}
2859 Examples: >
2860 :let repl = ""
2861 :echo "PEEK: ".remote_peek(id, "repl").": ".repl
2862
2863remote_read({serverid}) *remote_read()*
2864 Return the oldest available reply from {serverid} and consume
2865 it. It blocks until a reply is available.
2866 See also |clientserver|.
2867 This function is not available in the |sandbox|.
2868 {only available when compiled with the |+clientserver| feature}
2869 Example: >
2870 :echo remote_read(id)
2871<
2872 *remote_send()* *E241*
2873remote_send({server}, {string} [, {idvar}])
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002874 Send the {string} to {server}. The string is sent as input
2875 keys and the function returns immediately. At the Vim server
2876 the keys are not mapped |:map|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 If {idvar} is present, it is taken as the name of a
2878 variable and a {serverid} for later use with
2879 remote_read() is stored there.
2880 See also |clientserver| |RemoteReply|.
2881 This function is not available in the |sandbox|.
2882 {only available when compiled with the |+clientserver| feature}
2883 Note: Any errors will be reported in the server and may mess
2884 up the display.
2885 Examples: >
2886 :echo remote_send("gvim", ":DropAndReply ".file, "serverid").
2887 \ remote_read(serverid)
2888
2889 :autocmd NONE RemoteReply *
2890 \ echo remote_read(expand("<amatch>"))
2891 :echo remote_send("gvim", ":sleep 10 | echo ".
2892 \ 'server2client(expand("<client>"), "HELLO")<CR>')
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002893<
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002894remove({list}, {idx} [, {end}]) *remove()*
2895 Without {end}: Remove the item at {idx} from List {list} and
2896 return it.
2897 With {end}: Remove items from {idx} to {end} (inclusive) and
2898 return a list with these items. When {idx} points to the same
2899 item as {end} a list with one item is returned. When {end}
2900 points to an item before {idx} this is an error.
2901 See |list-index| for possible values of {idx} and {end}.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00002902 Example: >
2903 :echo "last item: " . remove(mylist, -1)
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002904 :call remove(mylist, 0, 9)
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00002905< Use |delete()| to remove a file.
2906
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907rename({from}, {to}) *rename()*
2908 Rename the file by the name {from} to the name {to}. This
2909 should also work to move files across file systems. The
2910 result is a Number, which is 0 if the file was renamed
2911 successfully, and non-zero when the renaming failed.
2912 This function is not available in the |sandbox|.
2913
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00002914repeat({expr}, {count}) *repeat()*
2915 Repeat {expr} {count} times and return the concatenated
2916 result. Example: >
2917 :let seperator = repeat('-', 80)
2918< When {count} is zero or negative the result is empty.
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002919 When {expr} is a List the result is {expr} concatenated
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002920 {count} times. Example: >
2921 :let longlist = repeat(['a', 'b'], 3)
2922< Results in ['a', 'b', 'a', 'b', 'a', 'b'].
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00002923
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002924
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925resolve({filename}) *resolve()* *E655*
2926 On MS-Windows, when {filename} is a shortcut (a .lnk file),
2927 returns the path the shortcut points to in a simplified form.
2928 On Unix, repeat resolving symbolic links in all path
2929 components of {filename} and return the simplified result.
2930 To cope with link cycles, resolving of symbolic links is
2931 stopped after 100 iterations.
2932 On other systems, return the simplified {filename}.
2933 The simplification step is done as by |simplify()|.
2934 resolve() keeps a leading path component specifying the
2935 current directory (provided the result is still a relative
2936 path name) and also keeps a trailing path separator.
2937
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002938 *reverse()*
2939reverse({list}) Reverse the order of items in {list} in-place. Returns
2940 {list}.
2941 If you want a list to remain unmodified make a copy first: >
2942 :let revlist = reverse(copy(mylist))
2943
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944search({pattern} [, {flags}]) *search()*
2945 Search for regexp pattern {pattern}. The search starts at the
2946 cursor position.
2947 {flags} is a String, which can contain these character flags:
2948 'b' search backward instead of forward
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002949 'n' do Not move the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 'w' wrap around the end of the file
2951 'W' don't wrap around the end of the file
2952 If neither 'w' or 'W' is given, the 'wrapscan' option applies.
2953
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002954 When a match has been found its line number is returned.
2955 The cursor will be positioned at the match, unless the 'n'
2956 flag is used).
2957 If there is no match a 0 is returned and the cursor doesn't
2958 move. No error message is given.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959
2960 Example (goes over all files in the argument list): >
2961 :let n = 1
2962 :while n <= argc() " loop over all files in arglist
2963 : exe "argument " . n
2964 : " start at the last char in the file and wrap for the
2965 : " first search to find match at start of file
2966 : normal G$
2967 : let flags = "w"
2968 : while search("foo", flags) > 0
2969 : s/foo/bar/g
2970 : let flags = "W"
2971 : endwhile
2972 : update " write the file if modified
2973 : let n = n + 1
2974 :endwhile
2975<
2976 *searchpair()*
2977searchpair({start}, {middle}, {end} [, {flags} [, {skip}]])
2978 Search for the match of a nested start-end pair. This can be
2979 used to find the "endif" that matches an "if", while other
2980 if/endif pairs in between are ignored.
2981 The search starts at the cursor. If a match is found, the
2982 cursor is positioned at it and the line number is returned.
2983 If no match is found 0 or -1 is returned and the cursor
2984 doesn't move. No error message is given.
2985
2986 {start}, {middle} and {end} are patterns, see |pattern|. They
2987 must not contain \( \) pairs. Use of \%( \) is allowed. When
2988 {middle} is not empty, it is found when searching from either
2989 direction, but only when not in a nested start-end pair. A
2990 typical use is: >
2991 searchpair('\<if\>', '\<else\>', '\<endif\>')
2992< By leaving {middle} empty the "else" is skipped.
2993
2994 {flags} are used like with |search()|. Additionally:
2995 'n' do Not move the cursor
2996 'r' Repeat until no more matches found; will find the
2997 outer pair
2998 'm' return number of Matches instead of line number with
2999 the match; will only be > 1 when 'r' is used.
3000
3001 When a match for {start}, {middle} or {end} is found, the
3002 {skip} expression is evaluated with the cursor positioned on
3003 the start of the match. It should return non-zero if this
3004 match is to be skipped. E.g., because it is inside a comment
3005 or a string.
3006 When {skip} is omitted or empty, every match is accepted.
3007 When evaluating {skip} causes an error the search is aborted
3008 and -1 returned.
3009
3010 The value of 'ignorecase' is used. 'magic' is ignored, the
3011 patterns are used like it's on.
3012
3013 The search starts exactly at the cursor. A match with
3014 {start}, {middle} or {end} at the next character, in the
3015 direction of searching, is the first one found. Example: >
3016 if 1
3017 if 2
3018 endif 2
3019 endif 1
3020< When starting at the "if 2", with the cursor on the "i", and
3021 searching forwards, the "endif 2" is found. When starting on
3022 the character just before the "if 2", the "endif 1" will be
3023 found. That's because the "if 2" will be found first, and
3024 then this is considered to be a nested if/endif from "if 2" to
3025 "endif 2".
3026 When searching backwards and {end} is more than one character,
3027 it may be useful to put "\zs" at the end of the pattern, so
3028 that when the cursor is inside a match with the end it finds
3029 the matching start.
3030
3031 Example, to find the "endif" command in a Vim script: >
3032
3033 :echo searchpair('\<if\>', '\<el\%[seif]\>', '\<en\%[dif]\>', 'W',
3034 \ 'getline(".") =~ "^\\s*\""')
3035
3036< The cursor must be at or after the "if" for which a match is
3037 to be found. Note that single-quote strings are used to avoid
3038 having to double the backslashes. The skip expression only
3039 catches comments at the start of a line, not after a command.
3040 Also, a word "en" or "if" halfway a line is considered a
3041 match.
3042 Another example, to search for the matching "{" of a "}": >
3043
3044 :echo searchpair('{', '', '}', 'bW')
3045
3046< This works when the cursor is at or before the "}" for which a
3047 match is to be found. To reject matches that syntax
3048 highlighting recognized as strings: >
3049
3050 :echo searchpair('{', '', '}', 'bW',
3051 \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"')
3052<
3053server2client( {clientid}, {string}) *server2client()*
3054 Send a reply string to {clientid}. The most recent {clientid}
3055 that sent a string can be retrieved with expand("<client>").
3056 {only available when compiled with the |+clientserver| feature}
3057 Note:
3058 This id has to be stored before the next command can be
3059 received. Ie. before returning from the received command and
3060 before calling any commands that waits for input.
3061 See also |clientserver|.
3062 Example: >
3063 :echo server2client(expand("<client>"), "HELLO")
3064<
3065serverlist() *serverlist()*
3066 Return a list of available server names, one per line.
3067 When there are no servers or the information is not available
3068 an empty string is returned. See also |clientserver|.
3069 {only available when compiled with the |+clientserver| feature}
3070 Example: >
3071 :echo serverlist()
3072<
3073setbufvar({expr}, {varname}, {val}) *setbufvar()*
3074 Set option or local variable {varname} in buffer {expr} to
3075 {val}.
3076 This also works for a global or local window option, but it
3077 doesn't work for a global or local window variable.
3078 For a local window option the global value is unchanged.
3079 For the use of {expr}, see |bufname()| above.
3080 Note that the variable name without "b:" must be used.
3081 Examples: >
3082 :call setbufvar(1, "&mod", 1)
3083 :call setbufvar("todo", "myvar", "foobar")
3084< This function is not available in the |sandbox|.
3085
3086setcmdpos({pos}) *setcmdpos()*
3087 Set the cursor position in the command line to byte position
3088 {pos}. The first position is 1.
3089 Use |getcmdpos()| to obtain the current position.
3090 Only works while editing the command line, thus you must use
3091 |c_CTRL-\_e| or |c_CTRL-R_=|. The position is set after the
3092 command line is set to the expression.
3093 When the number is too big the cursor is put at the end of the
3094 line. A number smaller than one has undefined results.
3095 Returns 0 when successful, 1 when not editing the command
3096 line.
3097
3098setline({lnum}, {line}) *setline()*
3099 Set line {lnum} of the current buffer to {line}. If this
3100 succeeds, 0 is returned. If this fails (most likely because
3101 {lnum} is invalid) 1 is returned. Example: >
3102 :call setline(5, strftime("%c"))
3103< Note: The '[ and '] marks are not set.
3104
3105 *setreg()*
3106setreg({regname}, {value} [,{options}])
3107 Set the register {regname} to {value}.
3108 If {options} contains "a" or {regname} is upper case,
3109 then the value is appended.
3110 {options} can also contains a register type specification:
3111 "c" or "v" |characterwise| mode
3112 "l" or "V" |linewise| mode
3113 "b" or "<CTRL-V>" |blockwise-visual| mode
3114 If a number immediately follows "b" or "<CTRL-V>" then this is
3115 used as the width of the selection - if it is not specified
3116 then the width of the block is set to the number of characters
3117 in the longest line (counting a <TAB> as 1 character).
3118
3119 If {options} contains no register settings, then the default
3120 is to use character mode unless {value} ends in a <NL>.
3121 Setting the '=' register is not possible.
3122 Returns zero for success, non-zero for failure.
3123
3124 Examples: >
3125 :call setreg(v:register, @*)
3126 :call setreg('*', @%, 'ac')
3127 :call setreg('a', "1\n2\n3", 'b5')
3128
3129< This example shows using the functions to save and restore a
3130 register. >
3131 :let var_a = getreg('a')
3132 :let var_amode = getregtype('a')
3133 ....
3134 :call setreg('a', var_a, var_amode)
3135
3136< You can also change the type of a register by appending
3137 nothing: >
3138 :call setreg('a', '', 'al')
3139
3140setwinvar({nr}, {varname}, {val}) *setwinvar()*
3141 Set option or local variable {varname} in window {nr} to
3142 {val}.
3143 This also works for a global or local buffer option, but it
3144 doesn't work for a global or local buffer variable.
3145 For a local buffer option the global value is unchanged.
3146 Note that the variable name without "w:" must be used.
3147 Examples: >
3148 :call setwinvar(1, "&list", 0)
3149 :call setwinvar(2, "myvar", "foobar")
3150< This function is not available in the |sandbox|.
3151
3152simplify({filename}) *simplify()*
3153 Simplify the file name as much as possible without changing
3154 the meaning. Shortcuts (on MS-Windows) or symbolic links (on
3155 Unix) are not resolved. If the first path component in
3156 {filename} designates the current directory, this will be
3157 valid for the result as well. A trailing path separator is
3158 not removed either.
3159 Example: >
3160 simplify("./dir/.././/file/") == "./file/"
3161< Note: The combination "dir/.." is only removed if "dir" is
3162 a searchable directory or does not exist. On Unix, it is also
3163 removed when "dir" is a symbolic link within the same
3164 directory. In order to resolve all the involved symbolic
3165 links before simplifying the path name, use |resolve()|.
3166
Bram Moolenaara14de3d2005-01-07 21:48:26 +00003167
Bram Moolenaar13065c42005-01-08 16:08:21 +00003168sort({list} [, {func}]) *sort()* *E702*
Bram Moolenaara14de3d2005-01-07 21:48:26 +00003169 Sort the items in {list} in-place. Returns {list}. If you
3170 want a list to remain unmodified make a copy first: >
3171 :let sortedlist = sort(copy(mylist))
3172< Uses the string representation of each item to sort on.
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003173 Numbers sort after Strings, Lists after Numbers.
Bram Moolenaara14de3d2005-01-07 21:48:26 +00003174 When {func} is given and it is one then case is ignored.
3175 When {func} is a Funcref or a function name, this function is
3176 called to compare items. The function is invoked with two
3177 items as argument and must return zero if they are equal, 1 if
3178 the first one sorts after the second one, -1 if the first one
3179 sorts before the second one. Example: >
3180 func MyCompare(i1, i2)
3181 return a:i1 == a:i2 ? 0 : a:i1 > a:i2 ? 1 : -1
3182 endfunc
3183 let sortedlist = sort(mylist, "MyCompare")
3184
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003185split({expr} [, {pattern}]) *split()*
Bram Moolenaara14de3d2005-01-07 21:48:26 +00003186 Make a List out of {expr}. When {pattern} is omitted each
3187 white-separated sequence of characters becomes an item.
3188 Otherwise the string is split where {pattern} matches,
3189 removing the matched characters. Empty strings are omitted.
3190 Example: >
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003191 :let words = split(getline('.'), '\W\+')
Bram Moolenaara14de3d2005-01-07 21:48:26 +00003192< Since empty strings are not added the "\+" isn't required but
3193 it makes the function work a bit faster.
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003194 The opposite function is |join()|.
Bram Moolenaara14de3d2005-01-07 21:48:26 +00003195
3196
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197strftime({format} [, {time}]) *strftime()*
3198 The result is a String, which is a formatted date and time, as
3199 specified by the {format} string. The given {time} is used,
3200 or the current time if no time is given. The accepted
3201 {format} depends on your system, thus this is not portable!
3202 See the manual page of the C function strftime() for the
3203 format. The maximum length of the result is 80 characters.
3204 See also |localtime()| and |getftime()|.
3205 The language can be changed with the |:language| command.
3206 Examples: >
3207 :echo strftime("%c") Sun Apr 27 11:49:23 1997
3208 :echo strftime("%Y %b %d %X") 1997 Apr 27 11:53:25
3209 :echo strftime("%y%m%d %T") 970427 11:53:55
3210 :echo strftime("%H:%M") 11:55
3211 :echo strftime("%c", getftime("file.c"))
3212 Show mod time of file.c.
Bram Moolenaara14de3d2005-01-07 21:48:26 +00003213< Not available on all systems. To check use: >
3214 :if exists("*strftime")
3215
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216stridx({haystack}, {needle}) *stridx()*
3217 The result is a Number, which gives the index in {haystack} of
3218 the first occurrence of the String {needle} in the String
3219 {haystack}. The search is done case-sensitive. For advanced
3220 searches use |match()|.
3221 If the {needle} does not occur in {haystack} it returns -1.
3222 See also |strridx()|. Examples: >
3223 :echo stridx("An Example", "Example") 3
3224 :echo stridx("Starting point", "Start") 0
3225 :echo stridx("Starting point", "start") -1
3226<
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00003227 *string()*
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003228string({expr}) Return {expr} converted to a String. If {expr} is a Number,
3229 String or a composition of them, then the result can be parsed
3230 back with |eval()|.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00003231 {expr} type result ~
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003232 String #string#
3233 Number 123
3234 Funcref function(#name#)
3235 List [item, item]
3236 Note that in String values the # character is doubled.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00003237
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 *strlen()*
3239strlen({expr}) The result is a Number, which is the length of the String
3240 {expr} in bytes. If you want to count the number of
3241 multi-byte characters use something like this: >
3242
3243 :let len = strlen(substitute(str, ".", "x", "g"))
3244
3245< Composing characters are not counted.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00003246 If the argument is a Number it is first converted to a String.
3247 For other types an error is given.
3248 Also see |len()|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249
3250strpart({src}, {start}[, {len}]) *strpart()*
3251 The result is a String, which is part of {src}, starting from
3252 byte {start}, with the length {len}.
3253 When non-existing bytes are included, this doesn't result in
3254 an error, the bytes are simply omitted.
3255 If {len} is missing, the copy continues from {start} till the
3256 end of the {src}. >
3257 strpart("abcdefg", 3, 2) == "de"
3258 strpart("abcdefg", -2, 4) == "ab"
3259 strpart("abcdefg", 5, 4) == "fg"
3260 strpart("abcdefg", 3) == "defg"
3261< Note: To get the first character, {start} must be 0. For
3262 example, to get three bytes under and after the cursor: >
3263 strpart(getline(line(".")), col(".") - 1, 3)
3264<
3265strridx({haystack}, {needle}) *strridx()*
3266 The result is a Number, which gives the index in {haystack} of
3267 the last occurrence of the String {needle} in the String
3268 {haystack}. The search is done case-sensitive. For advanced
3269 searches use |match()|.
3270 If the {needle} does not occur in {haystack} it returns -1.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003271 If the {needle} is empty the length of {haystack} is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 See also |stridx()|. Examples: >
3273 :echo strridx("an angry armadillo", "an") 3
3274<
3275strtrans({expr}) *strtrans()*
3276 The result is a String, which is {expr} with all unprintable
3277 characters translated into printable characters |'isprint'|.
3278 Like they are shown in a window. Example: >
3279 echo strtrans(@a)
3280< This displays a newline in register a as "^@" instead of
3281 starting a new line.
3282
3283submatch({nr}) *submatch()*
3284 Only for an expression in a |:substitute| command. Returns
3285 the {nr}'th submatch of the matched text. When {nr} is 0
3286 the whole matched text is returned.
3287 Example: >
3288 :s/\d\+/\=submatch(0) + 1/
3289< This finds the first number in the line and adds one to it.
3290 A line break is included as a newline character.
3291
3292substitute({expr}, {pat}, {sub}, {flags}) *substitute()*
3293 The result is a String, which is a copy of {expr}, in which
3294 the first match of {pat} is replaced with {sub}. This works
3295 like the ":substitute" command (without any flags). But the
3296 matching with {pat} is always done like the 'magic' option is
3297 set and 'cpoptions' is empty (to make scripts portable).
3298 See |string-match| for how {pat} is used.
3299 And a "~" in {sub} is not replaced with the previous {sub}.
3300 Note that some codes in {sub} have a special meaning
3301 |sub-replace-special|. For example, to replace something with
3302 "\n" (two characters), use "\\\\n" or '\\n'.
3303 When {pat} does not match in {expr}, {expr} is returned
3304 unmodified.
3305 When {flags} is "g", all matches of {pat} in {expr} are
3306 replaced. Otherwise {flags} should be "".
3307 Example: >
3308 :let &path = substitute(&path, ",\\=[^,]*$", "", "")
3309< This removes the last component of the 'path' option. >
3310 :echo substitute("testing", ".*", "\\U\\0", "")
3311< results in "TESTING".
3312
Bram Moolenaar47136d72004-10-12 20:02:24 +00003313synID({lnum}, {col}, {trans}) *synID()*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 The result is a Number, which is the syntax ID at the position
Bram Moolenaar47136d72004-10-12 20:02:24 +00003315 {lnum} and {col} in the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 The syntax ID can be used with |synIDattr()| and
3317 |synIDtrans()| to obtain syntax information about text.
Bram Moolenaar47136d72004-10-12 20:02:24 +00003318 {col} is 1 for the leftmost column, {lnum} is 1 for the first
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 line.
3320 When {trans} is non-zero, transparent items are reduced to the
3321 item that they reveal. This is useful when wanting to know
3322 the effective color. When {trans} is zero, the transparent
3323 item is returned. This is useful when wanting to know which
3324 syntax item is effective (e.g. inside parens).
3325 Warning: This function can be very slow. Best speed is
3326 obtained by going through the file in forward direction.
3327
3328 Example (echoes the name of the syntax item under the cursor): >
3329 :echo synIDattr(synID(line("."), col("."), 1), "name")
3330<
3331synIDattr({synID}, {what} [, {mode}]) *synIDattr()*
3332 The result is a String, which is the {what} attribute of
3333 syntax ID {synID}. This can be used to obtain information
3334 about a syntax item.
3335 {mode} can be "gui", "cterm" or "term", to get the attributes
3336 for that mode. When {mode} is omitted, or an invalid value is
3337 used, the attributes for the currently active highlighting are
3338 used (GUI, cterm or term).
3339 Use synIDtrans() to follow linked highlight groups.
3340 {what} result
3341 "name" the name of the syntax item
3342 "fg" foreground color (GUI: color name used to set
3343 the color, cterm: color number as a string,
3344 term: empty string)
3345 "bg" background color (like "fg")
3346 "fg#" like "fg", but for the GUI and the GUI is
3347 running the name in "#RRGGBB" form
3348 "bg#" like "fg#" for "bg"
3349 "bold" "1" if bold
3350 "italic" "1" if italic
3351 "reverse" "1" if reverse
3352 "inverse" "1" if inverse (= reverse)
3353 "underline" "1" if underlined
3354
3355 Example (echoes the color of the syntax item under the
3356 cursor): >
3357 :echo synIDattr(synIDtrans(synID(line("."), col("."), 1)), "fg")
3358<
3359synIDtrans({synID}) *synIDtrans()*
3360 The result is a Number, which is the translated syntax ID of
3361 {synID}. This is the syntax group ID of what is being used to
3362 highlight the character. Highlight links given with
3363 ":highlight link" are followed.
3364
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003365system({expr} [, {input}]) *system()* *E677*
3366 Get the output of the shell command {expr}.
3367 When {input} is given, this string is written to a file and
3368 passed as stdin to the command. The string is written as-is,
3369 you need to take care of using the correct line separators
3370 yourself.
3371 Note: newlines in {expr} may cause the command to fail. The
3372 characters in 'shellquote' and 'shellxquote' may also cause
3373 trouble.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 This is not to be used for interactive commands.
3375 The result is a String. Example: >
3376
3377 :let files = system("ls")
3378
3379< To make the result more system-independent, the shell output
3380 is filtered to replace <CR> with <NL> for Macintosh, and
3381 <CR><NL> with <NL> for DOS-like systems.
3382 The command executed is constructed using several options:
3383 'shell' 'shellcmdflag' 'shellxquote' {expr} 'shellredir' {tmp} 'shellxquote'
3384 ({tmp} is an automatically generated file name).
3385 For Unix and OS/2 braces are put around {expr} to allow for
3386 concatenated commands.
3387
3388 The resulting error code can be found in |v:shell_error|.
3389 This function will fail in |restricted-mode|.
3390 Unlike ":!cmd" there is no automatic check for changed files.
3391 Use |:checktime| to force a check.
3392
3393tempname() *tempname()* *temp-file-name*
3394 The result is a String, which is the name of a file that
3395 doesn't exist. It can be used for a temporary file. The name
3396 is different for at least 26 consecutive calls. Example: >
3397 :let tmpfile = tempname()
3398 :exe "redir > " . tmpfile
3399< For Unix, the file will be in a private directory (only
3400 accessible by the current user) to avoid security problems
3401 (e.g., a symlink attack or other people reading your file).
3402 When Vim exits the directory and all files in it are deleted.
3403 For MS-Windows forward slashes are used when the 'shellslash'
3404 option is set or when 'shellcmdflag' starts with '-'.
3405
3406tolower({expr}) *tolower()*
3407 The result is a copy of the String given, with all uppercase
3408 characters turned into lowercase (just like applying |gu| to
3409 the string).
3410
3411toupper({expr}) *toupper()*
3412 The result is a copy of the String given, with all lowercase
3413 characters turned into uppercase (just like applying |gU| to
3414 the string).
3415
Bram Moolenaar8299df92004-07-10 09:47:34 +00003416tr({src}, {fromstr}, {tostr}) *tr()*
3417 The result is a copy of the {src} string with all characters
3418 which appear in {fromstr} replaced by the character in that
3419 position in the {tostr} string. Thus the first character in
3420 {fromstr} is translated into the first character in {tostr}
3421 and so on. Exactly like the unix "tr" command.
3422 This code also deals with multibyte characters properly.
3423
3424 Examples: >
3425 echo tr("hello there", "ht", "HT")
3426< returns "Hello THere" >
3427 echo tr("<blob>", "<>", "{}")
3428< returns "{blob}"
3429
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003430 *type()*
3431type({expr}) The result is a Number, depending on the type of {expr}:
3432 Number: 0
3433 String: 1
3434 Funcref: 2
3435 List: 3
3436 To avoid the magic numbers it can be used this way: >
3437 :if type(myvar) == type(0)
3438 :if type(myvar) == type("")
3439 :if type(myvar) == type(function("tr"))
3440 :if type(myvar) == type([])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441
3442virtcol({expr}) *virtcol()*
3443 The result is a Number, which is the screen column of the file
3444 position given with {expr}. That is, the last screen position
3445 occupied by the character at that position, when the screen
3446 would be of unlimited width. When there is a <Tab> at the
3447 position, the returned Number will be the column at the end of
3448 the <Tab>. For example, for a <Tab> in column 1, with 'ts'
3449 set to 8, it returns 8.
3450 For the byte position use |col()|.
3451 When Virtual editing is active in the current mode, a position
3452 beyond the end of the line can be returned. |'virtualedit'|
3453 The accepted positions are:
3454 . the cursor position
3455 $ the end of the cursor line (the result is the
3456 number of displayed characters in the cursor line
3457 plus one)
3458 'x position of mark x (if the mark is not set, 0 is
3459 returned)
3460 Note that only marks in the current file can be used.
3461 Examples: >
3462 virtcol(".") with text "foo^Lbar", with cursor on the "^L", returns 5
3463 virtcol("$") with text "foo^Lbar", returns 9
3464 virtcol("'t") with text " there", with 't at 'h', returns 6
3465< The first column is 1. 0 is returned for an error.
3466
3467visualmode([expr]) *visualmode()*
3468 The result is a String, which describes the last Visual mode
3469 used. Initially it returns an empty string, but once Visual
3470 mode has been used, it returns "v", "V", or "<CTRL-V>" (a
3471 single CTRL-V character) for character-wise, line-wise, or
3472 block-wise Visual mode respectively.
3473 Example: >
3474 :exe "normal " . visualmode()
3475< This enters the same Visual mode as before. It is also useful
3476 in scripts if you wish to act differently depending on the
3477 Visual mode that was used.
3478
3479 If an expression is supplied that results in a non-zero number
3480 or a non-empty string, then the Visual mode will be cleared
3481 and the old value is returned. Note that " " and "0" are also
3482 non-empty strings, thus cause the mode to be cleared.
3483
3484 *winbufnr()*
3485winbufnr({nr}) The result is a Number, which is the number of the buffer
3486 associated with window {nr}. When {nr} is zero, the number of
3487 the buffer in the current window is returned. When window
3488 {nr} doesn't exist, -1 is returned.
3489 Example: >
3490 :echo "The file in the current window is " . bufname(winbufnr(0))
3491<
3492 *wincol()*
3493wincol() The result is a Number, which is the virtual column of the
3494 cursor in the window. This is counting screen cells from the
3495 left side of the window. The leftmost column is one.
3496
3497winheight({nr}) *winheight()*
3498 The result is a Number, which is the height of window {nr}.
3499 When {nr} is zero, the height of the current window is
3500 returned. When window {nr} doesn't exist, -1 is returned.
3501 An existing window always has a height of zero or more.
3502 Examples: >
3503 :echo "The current window has " . winheight(0) . " lines."
3504<
3505 *winline()*
3506winline() The result is a Number, which is the screen line of the cursor
3507 in the window. This is counting screen lines from the top of
3508 the window. The first line is one.
3509
3510 *winnr()*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00003511winnr([{arg}]) The result is a Number, which is the number of the current
3512 window. The top window has number 1.
3513 When the optional argument is "$", the number of the
3514 last window is returnd (the window count).
3515 When the optional argument is "#", the number of the last
3516 accessed window is returned (where |CTRL-W_p| goes to).
3517 If there is no previous window 0 is returned.
3518 The number can be used with |CTRL-W_w| and ":wincmd w"
3519 |:wincmd|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
3521 *winrestcmd()*
3522winrestcmd() Returns a sequence of |:resize| commands that should restore
3523 the current window sizes. Only works properly when no windows
3524 are opened or closed and the current window is unchanged.
3525 Example: >
3526 :let cmd = winrestcmd()
3527 :call MessWithWindowSizes()
3528 :exe cmd
3529
3530winwidth({nr}) *winwidth()*
3531 The result is a Number, which is the width of window {nr}.
3532 When {nr} is zero, the width of the current window is
3533 returned. When window {nr} doesn't exist, -1 is returned.
3534 An existing window always has a width of zero or more.
3535 Examples: >
3536 :echo "The current window has " . winwidth(0) . " columns."
3537 :if winwidth(0) <= 50
3538 : exe "normal 50\<C-W>|"
3539 :endif
3540<
3541
3542 *feature-list*
3543There are three types of features:
35441. Features that are only supported when they have been enabled when Vim
3545 was compiled |+feature-list|. Example: >
3546 :if has("cindent")
35472. Features that are only supported when certain conditions have been met.
3548 Example: >
3549 :if has("gui_running")
3550< *has-patch*
35513. Included patches. First check |v:version| for the version of Vim.
3552 Then the "patch123" feature means that patch 123 has been included for
3553 this version. Example (checking version 6.2.148 or later): >
3554 :if v:version > 602 || v:version == 602 && has("patch148")
3555
3556all_builtin_terms Compiled with all builtin terminals enabled.
3557amiga Amiga version of Vim.
3558arabic Compiled with Arabic support |Arabic|.
3559arp Compiled with ARP support (Amiga).
3560autocmd Compiled with autocommands support.
3561balloon_eval Compiled with |balloon-eval| support.
3562beos BeOS version of Vim.
3563browse Compiled with |:browse| support, and browse() will
3564 work.
3565builtin_terms Compiled with some builtin terminals.
3566byte_offset Compiled with support for 'o' in 'statusline'
3567cindent Compiled with 'cindent' support.
3568clientserver Compiled with remote invocation support |clientserver|.
3569clipboard Compiled with 'clipboard' support.
3570cmdline_compl Compiled with |cmdline-completion| support.
3571cmdline_hist Compiled with |cmdline-history| support.
3572cmdline_info Compiled with 'showcmd' and 'ruler' support.
3573comments Compiled with |'comments'| support.
3574cryptv Compiled with encryption support |encryption|.
3575cscope Compiled with |cscope| support.
3576compatible Compiled to be very Vi compatible.
3577debug Compiled with "DEBUG" defined.
3578dialog_con Compiled with console dialog support.
3579dialog_gui Compiled with GUI dialog support.
3580diff Compiled with |vimdiff| and 'diff' support.
3581digraphs Compiled with support for digraphs.
3582dnd Compiled with support for the "~ register |quote_~|.
3583dos32 32 bits DOS (DJGPP) version of Vim.
3584dos16 16 bits DOS version of Vim.
3585ebcdic Compiled on a machine with ebcdic character set.
3586emacs_tags Compiled with support for Emacs tags.
3587eval Compiled with expression evaluation support. Always
3588 true, of course!
3589ex_extra Compiled with extra Ex commands |+ex_extra|.
3590extra_search Compiled with support for |'incsearch'| and
3591 |'hlsearch'|
3592farsi Compiled with Farsi support |farsi|.
3593file_in_path Compiled with support for |gf| and |<cfile>|
3594find_in_path Compiled with support for include file searches
3595 |+find_in_path|.
3596fname_case Case in file names matters (for Amiga, MS-DOS, and
3597 Windows this is not present).
3598folding Compiled with |folding| support.
3599footer Compiled with GUI footer support. |gui-footer|
3600fork Compiled to use fork()/exec() instead of system().
3601gettext Compiled with message translation |multi-lang|
3602gui Compiled with GUI enabled.
3603gui_athena Compiled with Athena GUI.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003604gui_beos Compiled with BeOS GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605gui_gtk Compiled with GTK+ GUI (any version).
3606gui_gtk2 Compiled with GTK+ 2 GUI (gui_gtk is also defined).
Bram Moolenaar843ee412004-06-30 16:16:41 +00003607gui_kde Compiled with KDE GUI |KVim|
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608gui_mac Compiled with Macintosh GUI.
3609gui_motif Compiled with Motif GUI.
3610gui_photon Compiled with Photon GUI.
3611gui_win32 Compiled with MS Windows Win32 GUI.
3612gui_win32s idem, and Win32s system being used (Windows 3.1)
3613gui_running Vim is running in the GUI, or it will start soon.
3614hangul_input Compiled with Hangul input support. |hangul|
3615iconv Can use iconv() for conversion.
3616insert_expand Compiled with support for CTRL-X expansion commands in
3617 Insert mode.
3618jumplist Compiled with |jumplist| support.
3619keymap Compiled with 'keymap' support.
3620langmap Compiled with 'langmap' support.
3621libcall Compiled with |libcall()| support.
3622linebreak Compiled with 'linebreak', 'breakat' and 'showbreak'
3623 support.
3624lispindent Compiled with support for lisp indenting.
3625listcmds Compiled with commands for the buffer list |:files|
3626 and the argument list |arglist|.
3627localmap Compiled with local mappings and abbr. |:map-local|
3628mac Macintosh version of Vim.
3629macunix Macintosh version of Vim, using Unix files (OS-X).
3630menu Compiled with support for |:menu|.
3631mksession Compiled with support for |:mksession|.
3632modify_fname Compiled with file name modifiers. |filename-modifiers|
3633mouse Compiled with support mouse.
3634mouseshape Compiled with support for 'mouseshape'.
3635mouse_dec Compiled with support for Dec terminal mouse.
3636mouse_gpm Compiled with support for gpm (Linux console mouse)
3637mouse_netterm Compiled with support for netterm mouse.
3638mouse_pterm Compiled with support for qnx pterm mouse.
3639mouse_xterm Compiled with support for xterm mouse.
3640multi_byte Compiled with support for editing Korean et al.
3641multi_byte_ime Compiled with support for IME input method.
3642multi_lang Compiled with support for multiple languages.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003643mzscheme Compiled with MzScheme interface |mzscheme|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644netbeans_intg Compiled with support for |netbeans|.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003645netbeans_enabled Compiled with support for |netbeans| and it's used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646ole Compiled with OLE automation support for Win32.
3647os2 OS/2 version of Vim.
3648osfiletype Compiled with support for osfiletypes |+osfiletype|
3649path_extra Compiled with up/downwards search in 'path' and 'tags'
3650perl Compiled with Perl interface.
3651postscript Compiled with PostScript file printing.
3652printer Compiled with |:hardcopy| support.
3653python Compiled with Python interface.
3654qnx QNX version of Vim.
3655quickfix Compiled with |quickfix| support.
3656rightleft Compiled with 'rightleft' support.
3657ruby Compiled with Ruby interface |ruby|.
3658scrollbind Compiled with 'scrollbind' support.
3659showcmd Compiled with 'showcmd' support.
3660signs Compiled with |:sign| support.
3661smartindent Compiled with 'smartindent' support.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003662sniff Compiled with SNiFF interface support.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663statusline Compiled with support for 'statusline', 'rulerformat'
3664 and special formats of 'titlestring' and 'iconstring'.
3665sun_workshop Compiled with support for Sun |workshop|.
3666syntax Compiled with syntax highlighting support.
3667syntax_items There are active syntax highlighting items for the
3668 current buffer.
3669system Compiled to use system() instead of fork()/exec().
3670tag_binary Compiled with binary searching in tags files
3671 |tag-binary-search|.
3672tag_old_static Compiled with support for old static tags
3673 |tag-old-static|.
3674tag_any_white Compiled with support for any white characters in tags
3675 files |tag-any-white|.
3676tcl Compiled with Tcl interface.
3677terminfo Compiled with terminfo instead of termcap.
3678termresponse Compiled with support for |t_RV| and |v:termresponse|.
3679textobjects Compiled with support for |text-objects|.
3680tgetent Compiled with tgetent support, able to use a termcap
3681 or terminfo file.
3682title Compiled with window title support |'title'|.
3683toolbar Compiled with support for |gui-toolbar|.
3684unix Unix version of Vim.
3685user_commands User-defined commands.
3686viminfo Compiled with viminfo support.
3687vim_starting True while initial source'ing takes place.
3688vertsplit Compiled with vertically split windows |:vsplit|.
3689virtualedit Compiled with 'virtualedit' option.
3690visual Compiled with Visual mode.
3691visualextra Compiled with extra Visual mode commands.
3692 |blockwise-operators|.
3693vms VMS version of Vim.
3694vreplace Compiled with |gR| and |gr| commands.
3695wildignore Compiled with 'wildignore' option.
3696wildmenu Compiled with 'wildmenu' option.
3697windows Compiled with support for more than one window.
3698winaltkeys Compiled with 'winaltkeys' option.
3699win16 Win16 version of Vim (MS-Windows 3.1).
3700win32 Win32 version of Vim (MS-Windows 95/98/ME/NT/2000/XP).
3701win64 Win64 version of Vim (MS-Windows 64 bit).
3702win32unix Win32 version of Vim, using Unix files (Cygwin)
3703win95 Win32 version for MS-Windows 95/98/ME.
3704writebackup Compiled with 'writebackup' default on.
3705xfontset Compiled with X fontset support |xfontset|.
3706xim Compiled with X input method support |xim|.
3707xsmp Compiled with X session management support.
3708xsmp_interact Compiled with interactive X session management support.
3709xterm_clipboard Compiled with support for xterm clipboard.
3710xterm_save Compiled with support for saving and restoring the
3711 xterm screen.
3712x11 Compiled with X11 support.
3713
3714 *string-match*
3715Matching a pattern in a String
3716
3717A regexp pattern as explained at |pattern| is normally used to find a match in
3718the buffer lines. When a pattern is used to find a match in a String, almost
3719everything works in the same way. The difference is that a String is handled
3720like it is one line. When it contains a "\n" character, this is not seen as a
3721line break for the pattern. It can be matched with a "\n" in the pattern, or
3722with ".". Example: >
3723 :let a = "aaaa\nxxxx"
3724 :echo matchstr(a, "..\n..")
3725 aa
3726 xx
3727 :echo matchstr(a, "a.x")
3728 a
3729 x
3730
3731Don't forget that "^" will only match at the first character of the String and
3732"$" at the last character of the string. They don't match after or before a
3733"\n".
3734
3735==============================================================================
37365. Defining functions *user-functions*
3737
3738New functions can be defined. These can be called just like builtin
3739functions. The function executes a sequence of Ex commands. Normal mode
3740commands can be executed with the |:normal| command.
3741
3742The function name must start with an uppercase letter, to avoid confusion with
3743builtin functions. To prevent from using the same name in different scripts
3744avoid obvious, short names. A good habit is to start the function name with
3745the name of the script, e.g., "HTMLcolor()".
3746
3747It's also possible to use curly braces, see |curly-braces-names|.
3748
3749 *local-function*
3750A function local to a script must start with "s:". A local script function
3751can only be called from within the script and from functions, user commands
3752and autocommands defined in the script. It is also possible to call the
3753function from a mappings defined in the script, but then |<SID>| must be used
3754instead of "s:" when the mapping is expanded outside of the script.
3755
3756 *:fu* *:function* *E128* *E129* *E123*
3757:fu[nction] List all functions and their arguments.
3758
3759:fu[nction] {name} List function {name}.
3760 *E124* *E125*
3761:fu[nction][!] {name}([arguments]) [range] [abort]
3762 Define a new function by the name {name}. The name
3763 must be made of alphanumeric characters and '_', and
3764 must start with a capital or "s:" (see above).
3765 *function-argument* *a:var*
3766 An argument can be defined by giving its name. In the
3767 function this can then be used as "a:name" ("a:" for
3768 argument).
3769 Up to 20 arguments can be given, separated by commas.
3770 Finally, an argument "..." can be specified, which
3771 means that more arguments may be following. In the
3772 function they can be used as "a:1", "a:2", etc. "a:0"
3773 is set to the number of extra arguments (which can be
3774 0).
3775 When not using "...", the number of arguments in a
3776 function call must be equal to the number of named
3777 arguments. When using "...", the number of arguments
3778 may be larger.
3779 It is also possible to define a function without any
3780 arguments. You must still supply the () then.
3781 The body of the function follows in the next lines,
3782 until the matching |:endfunction|. It is allowed to
3783 define another function inside a function body.
3784 *E127* *E122*
3785 When a function by this name already exists and [!] is
3786 not used an error message is given. When [!] is used,
3787 an existing function is silently replaced. Unless it
3788 is currently being executed, that is an error.
3789 *a:firstline* *a:lastline*
3790 When the [range] argument is added, the function is
3791 expected to take care of a range itself. The range is
3792 passed as "a:firstline" and "a:lastline". If [range]
3793 is excluded, ":{range}call" will call the function for
3794 each line in the range, with the cursor on the start
3795 of each line. See |function-range-example|.
3796 When the [abort] argument is added, the function will
3797 abort as soon as an error is detected.
3798 The last used search pattern and the redo command "."
3799 will not be changed by the function.
3800
3801 *:endf* *:endfunction* *E126* *E193*
3802:endf[unction] The end of a function definition. Must be on a line
3803 by its own, without other commands.
3804
3805 *:delf* *:delfunction* *E130* *E131*
3806:delf[unction] {name} Delete function {name}.
3807
3808 *:retu* *:return* *E133*
3809:retu[rn] [expr] Return from a function. When "[expr]" is given, it is
3810 evaluated and returned as the result of the function.
3811 If "[expr]" is not given, the number 0 is returned.
3812 When a function ends without an explicit ":return",
3813 the number 0 is returned.
3814 Note that there is no check for unreachable lines,
3815 thus there is no warning if commands follow ":return".
3816
3817 If the ":return" is used after a |:try| but before the
3818 matching |:finally| (if present), the commands
3819 following the ":finally" up to the matching |:endtry|
3820 are executed first. This process applies to all
3821 nested ":try"s inside the function. The function
3822 returns at the outermost ":endtry".
3823
3824
3825Inside a function variables can be used. These are local variables, which
3826will disappear when the function returns. Global variables need to be
3827accessed with "g:".
3828
3829Example: >
3830 :function Table(title, ...)
3831 : echohl Title
3832 : echo a:title
3833 : echohl None
3834 : let idx = 1
3835 : while idx <= a:0
3836 : echo a:{idx} . ' '
3837 : let idx = idx + 1
3838 : endwhile
3839 : return idx
3840 :endfunction
3841
3842This function can then be called with: >
3843 let lines = Table("Table", "line1", "line2")
3844 let lines = Table("Empty Table")
3845
3846To return more than one value, pass the name of a global variable: >
3847 :function Compute(n1, n2, divname)
3848 : if a:n2 == 0
3849 : return "fail"
3850 : endif
3851 : let g:{a:divname} = a:n1 / a:n2
3852 : return "ok"
3853 :endfunction
3854
3855This function can then be called with: >
3856 :let success = Compute(13, 1324, "div")
3857 :if success == "ok"
3858 : echo div
3859 :endif
3860
3861An alternative is to return a command that can be executed. This also works
3862with local variables in a calling function. Example: >
3863 :function Foo()
3864 : execute Bar()
3865 : echo "line " . lnum . " column " . col
3866 :endfunction
3867
3868 :function Bar()
3869 : return "let lnum = " . line(".") . " | let col = " . col(".")
3870 :endfunction
3871
3872The names "lnum" and "col" could also be passed as argument to Bar(), to allow
3873the caller to set the names.
3874
3875 *:cal* *:call* *E107*
3876:[range]cal[l] {name}([arguments])
3877 Call a function. The name of the function and its arguments
3878 are as specified with |:function|. Up to 20 arguments can be
3879 used.
3880 Without a range and for functions that accept a range, the
3881 function is called once. When a range is given the cursor is
3882 positioned at the start of the first line before executing the
3883 function.
3884 When a range is given and the function doesn't handle it
3885 itself, the function is executed for each line in the range,
3886 with the cursor in the first column of that line. The cursor
3887 is left at the last line (possibly moved by the last function
3888 call). The arguments are re-evaluated for each line. Thus
3889 this works:
3890 *function-range-example* >
3891 :function Mynumber(arg)
3892 : echo line(".") . " " . a:arg
3893 :endfunction
3894 :1,5call Mynumber(getline("."))
3895<
3896 The "a:firstline" and "a:lastline" are defined anyway, they
3897 can be used to do something different at the start or end of
3898 the range.
3899
3900 Example of a function that handles the range itself: >
3901
3902 :function Cont() range
3903 : execute (a:firstline + 1) . "," . a:lastline . 's/^/\t\\ '
3904 :endfunction
3905 :4,8call Cont()
3906<
3907 This function inserts the continuation character "\" in front
3908 of all the lines in the range, except the first one.
3909
3910 *E132*
3911The recursiveness of user functions is restricted with the |'maxfuncdepth'|
3912option.
3913
3914 *autoload-functions*
3915When using many or large functions, it's possible to automatically define them
3916only when they are used. Use the FuncUndefined autocommand event with a
3917pattern that matches the function(s) to be defined. Example: >
3918
3919 :au FuncUndefined BufNet* source ~/vim/bufnetfuncs.vim
3920
3921The file "~/vim/bufnetfuncs.vim" should then define functions that start with
3922"BufNet". Also see |FuncUndefined|.
3923
3924==============================================================================
39256. Curly braces names *curly-braces-names*
3926
3927Wherever you can use a variable, you can use a "curly braces name" variable.
3928This is a regular variable name with one or more expressions wrapped in braces
3929{} like this: >
3930 my_{adjective}_variable
3931
3932When Vim encounters this, it evaluates the expression inside the braces, puts
3933that in place of the expression, and re-interprets the whole as a variable
3934name. So in the above example, if the variable "adjective" was set to
3935"noisy", then the reference would be to "my_noisy_variable", whereas if
3936"adjective" was set to "quiet", then it would be to "my_quiet_variable".
3937
3938One application for this is to create a set of variables governed by an option
3939value. For example, the statement >
3940 echo my_{&background}_message
3941
3942would output the contents of "my_dark_message" or "my_light_message" depending
3943on the current value of 'background'.
3944
3945You can use multiple brace pairs: >
3946 echo my_{adverb}_{adjective}_message
3947..or even nest them: >
3948 echo my_{ad{end_of_word}}_message
3949where "end_of_word" is either "verb" or "jective".
3950
3951However, the expression inside the braces must evaluate to a valid single
3952variable name. e.g. this is invalid: >
3953 :let foo='a + b'
3954 :echo c{foo}d
3955.. since the result of expansion is "ca + bd", which is not a variable name.
3956
3957 *curly-braces-function-names*
3958You can call and define functions by an evaluated name in a similar way.
3959Example: >
3960 :let func_end='whizz'
3961 :call my_func_{func_end}(parameter)
3962
3963This would call the function "my_func_whizz(parameter)".
3964
3965==============================================================================
39667. Commands *expression-commands*
3967
3968:let {var-name} = {expr1} *:let* *E18*
3969 Set internal variable {var-name} to the result of the
3970 expression {expr1}. The variable will get the type
3971 from the {expr}. If {var-name} didn't exist yet, it
3972 is created.
3973
Bram Moolenaar13065c42005-01-08 16:08:21 +00003974:let {var-name}[{idx}] = {expr1} *E689*
3975 Set a list item to the result of the expression
3976 {expr1}. {var-name} must refer to a list and {idx}
3977 must be a valid index in that list. For nested list
3978 the index can be repeated.
3979 This cannot be used to add an item to a list.
3980
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003981:let {var-name}[{idx1}:{idx2}] = {expr1} *E708* *E709* *E710* *E711*
3982 Set a sequence of items in a List to the result of the
3983 expression {expr1}, which must be a list with the
3984 correct number of items.
3985 {idx1} can be omitted, zero is used instead.
3986 {idx2} can be omitted, meaning the end of the list.
3987 When the selected range of items is partly past the
3988 end of the list, items will be added.
3989
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990:let ${env-name} = {expr1} *:let-environment* *:let-$*
3991 Set environment variable {env-name} to the result of
3992 the expression {expr1}. The type is always String.
3993
3994:let @{reg-name} = {expr1} *:let-register* *:let-@*
3995 Write the result of the expression {expr1} in register
3996 {reg-name}. {reg-name} must be a single letter, and
3997 must be the name of a writable register (see
3998 |registers|). "@@" can be used for the unnamed
3999 register, "@/" for the search pattern.
4000 If the result of {expr1} ends in a <CR> or <NL>, the
4001 register will be linewise, otherwise it will be set to
4002 characterwise.
4003 This can be used to clear the last search pattern: >
4004 :let @/ = ""
4005< This is different from searching for an empty string,
4006 that would match everywhere.
4007
4008:let &{option-name} = {expr1} *:let-option* *:let-star*
4009 Set option {option-name} to the result of the
Bram Moolenaarfca34d62005-01-04 21:38:36 +00004010 expression {expr1}. A String or Number value is
4011 always converted to the type of the option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 For an option local to a window or buffer the effect
4013 is just like using the |:set| command: both the local
4014 value and the global value is changed.
Bram Moolenaarfca34d62005-01-04 21:38:36 +00004015 Example: >
4016 :let &path = &path . ',/usr/local/include'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017
4018:let &l:{option-name} = {expr1}
4019 Like above, but only set the local value of an option
4020 (if there is one). Works like |:setlocal|.
4021
4022:let &g:{option-name} = {expr1}
4023 Like above, but only set the global value of an option
4024 (if there is one). Works like |:setglobal|.
4025
Bram Moolenaar13065c42005-01-08 16:08:21 +00004026:let [{name1}, {name2}, ...] = {expr1} *:let-unpack* *E687* *E688*
Bram Moolenaarfca34d62005-01-04 21:38:36 +00004027 {expr1} must evaluate to a List. The first item in
4028 the list is assigned to {name1}, the second item to
4029 {name2}, etc.
4030 The number of names must match the number of items in
4031 the List.
4032 Each name can be one of the items of the ":let"
4033 command as mentioned above.
4034 Example: >
4035 :let [s, item] = GetItem(s)
4036
4037:let [{name}, ..., ; {lastname}] = {expr1}
4038 Like above, but the List may have more items than
4039 there are names. A list of the remaining items is
4040 assigned to {lastname}. If there are no remaining
4041 items {lastname} is set to an empty list.
4042 Example: >
4043 :let [a, b; rest] = ["aval", "bval", 3, 4]
4044<
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 *E106*
4046:let {var-name} .. List the value of variable {var-name}. Several
4047 variable names may be given.
4048
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00004049:let List the values of all variables. The type of the
4050 variable is indicated before the value:
4051 <nothing> String
4052 # Number
4053 * Funcref
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054
4055 *:unlet* *:unl* *E108*
4056:unl[et][!] {var-name} ...
4057 Remove the internal variable {var-name}. Several
4058 variable names can be given, they are all removed.
4059 With [!] no error message is given for non-existing
4060 variables.
4061
4062:if {expr1} *:if* *:endif* *:en* *E171* *E579* *E580*
4063:en[dif] Execute the commands until the next matching ":else"
4064 or ":endif" if {expr1} evaluates to non-zero.
4065
4066 From Vim version 4.5 until 5.0, every Ex command in
4067 between the ":if" and ":endif" is ignored. These two
4068 commands were just to allow for future expansions in a
4069 backwards compatible way. Nesting was allowed. Note
4070 that any ":else" or ":elseif" was ignored, the "else"
4071 part was not executed either.
4072
4073 You can use this to remain compatible with older
4074 versions: >
4075 :if version >= 500
4076 : version-5-specific-commands
4077 :endif
4078< The commands still need to be parsed to find the
4079 "endif". Sometimes an older Vim has a problem with a
4080 new command. For example, ":silent" is recognized as
4081 a ":substitute" command. In that case ":execute" can
4082 avoid problems: >
4083 :if version >= 600
4084 : execute "silent 1,$delete"
4085 :endif
4086<
4087 NOTE: The ":append" and ":insert" commands don't work
4088 properly in between ":if" and ":endif".
4089
4090 *:else* *:el* *E581* *E583*
4091:el[se] Execute the commands until the next matching ":else"
4092 or ":endif" if they previously were not being
4093 executed.
4094
4095 *:elseif* *:elsei* *E582* *E584*
4096:elsei[f] {expr1} Short for ":else" ":if", with the addition that there
4097 is no extra ":endif".
4098
4099:wh[ile] {expr1} *:while* *:endwhile* *:wh* *:endw*
4100 *E170* *E585* *E588*
4101:endw[hile] Repeat the commands between ":while" and ":endwhile",
4102 as long as {expr1} evaluates to non-zero.
4103 When an error is detected from a command inside the
4104 loop, execution continues after the "endwhile".
Bram Moolenaar12805862005-01-05 22:16:17 +00004105 Example: >
4106 :let lnum = 1
4107 :while lnum <= line("$")
4108 :call FixLine(lnum)
4109 :let lnum = lnum + 1
4110 :endwhile
4111<
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 NOTE: The ":append" and ":insert" commands don't work
Bram Moolenaar12805862005-01-05 22:16:17 +00004113 properly inside a :while" and ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
Bram Moolenaar13065c42005-01-08 16:08:21 +00004115:for {var} in {list} *:for* *E690*
Bram Moolenaar12805862005-01-05 22:16:17 +00004116:endfo[r] *:endfo* *:endfor*
4117 Repeat the commands between ":for" and ":endfor" for
Bram Moolenaarde8866b2005-01-06 23:24:37 +00004118 each item in {list}. variable {var} is set to the
4119 value of each item.
4120 When an error is detected for a command inside the
Bram Moolenaar12805862005-01-05 22:16:17 +00004121 loop, execution continues after the "endfor".
Bram Moolenaarde8866b2005-01-06 23:24:37 +00004122 Changing {list} affects what items are used. Make a
4123 copy if this is unwanted: >
4124 :for item in copy(mylist)
4125< When not making a copy, Vim stores a reference to the
4126 next item in the list, before executing the commands
4127 with the current item. Thus the current item can be
4128 removed without effect. Removing any later item means
4129 it will not be found. Thus the following example
4130 works (an inefficient way to make a list empty): >
4131 :for item in mylist
Bram Moolenaar12805862005-01-05 22:16:17 +00004132 :call remove(mylist, 0)
4133 :endfor
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004134< Note that reordering the list (e.g., with sort() or
4135 reverse()) may have unexpected effects.
4136 Note that the type of each list item should be
Bram Moolenaar12805862005-01-05 22:16:17 +00004137 identical to avoid errors for the type of {var}
4138 changing. Unlet the variable at the end of the loop
4139 to allow multiple item types.
4140
4141:for {var} in {string}
4142:endfo[r] Like ":for" above, but use each character in {string}
4143 as a list item.
4144 Composing characters are used as separate characters.
4145 A Number is first converted to a String.
4146
4147:for [{var1}, {var2}, ...] in {listlist}
4148:endfo[r]
4149 Like ":for" above, but each item in {listlist} must be
4150 a list, of which each item is assigned to {var1},
4151 {var2}, etc. Example: >
4152 :for [lnum, col] in [[1, 3], [2, 5], [3, 8]]
4153 :echo getline(lnum)[col]
4154 :endfor
4155<
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 *:continue* *:con* *E586*
Bram Moolenaar12805862005-01-05 22:16:17 +00004157:con[tinue] When used inside a ":while" or ":for" loop, jumps back
4158 to the start of the loop.
4159 If it is used after a |:try| inside the loop but
4160 before the matching |:finally| (if present), the
4161 commands following the ":finally" up to the matching
4162 |:endtry| are executed first. This process applies to
4163 all nested ":try"s inside the loop. The outermost
4164 ":endtry" then jumps back to the start of the loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
4166 *:break* *:brea* *E587*
Bram Moolenaar12805862005-01-05 22:16:17 +00004167:brea[k] When used inside a ":while" or ":for" loop, skips to
4168 the command after the matching ":endwhile" or
4169 ":endfor".
4170 If it is used after a |:try| inside the loop but
4171 before the matching |:finally| (if present), the
4172 commands following the ":finally" up to the matching
4173 |:endtry| are executed first. This process applies to
4174 all nested ":try"s inside the loop. The outermost
4175 ":endtry" then jumps to the command after the loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176
4177:try *:try* *:endt* *:endtry* *E600* *E601* *E602*
4178:endt[ry] Change the error handling for the commands between
4179 ":try" and ":endtry" including everything being
4180 executed across ":source" commands, function calls,
4181 or autocommand invocations.
4182
4183 When an error or interrupt is detected and there is
4184 a |:finally| command following, execution continues
4185 after the ":finally". Otherwise, or when the
4186 ":endtry" is reached thereafter, the next
4187 (dynamically) surrounding ":try" is checked for
4188 a corresponding ":finally" etc. Then the script
4189 processing is terminated. (Whether a function
4190 definition has an "abort" argument does not matter.)
4191 Example: >
4192 :try | edit too much | finally | echo "cleanup" | endtry
4193 :echo "impossible" " not reached, script terminated above
4194<
4195 Moreover, an error or interrupt (dynamically) inside
4196 ":try" and ":endtry" is converted to an exception. It
4197 can be caught as if it were thrown by a |:throw|
4198 command (see |:catch|). In this case, the script
4199 processing is not terminated.
4200
4201 The value "Vim:Interrupt" is used for an interrupt
4202 exception. An error in a Vim command is converted
4203 to a value of the form "Vim({command}):{errmsg}",
4204 other errors are converted to a value of the form
4205 "Vim:{errmsg}". {command} is the full command name,
4206 and {errmsg} is the message that is displayed if the
4207 error exception is not caught, always beginning with
4208 the error number.
4209 Examples: >
4210 :try | sleep 100 | catch /^Vim:Interrupt$/ | endtry
4211 :try | edit | catch /^Vim(edit):E\d\+/ | echo "error" | endtry
4212<
4213 *:cat* *:catch* *E603* *E604* *E605*
4214:cat[ch] /{pattern}/ The following commands until the next ":catch",
4215 |:finally|, or |:endtry| that belongs to the same
4216 |:try| as the ":catch" are executed when an exception
4217 matching {pattern} is being thrown and has not yet
4218 been caught by a previous ":catch". Otherwise, these
4219 commands are skipped.
4220 When {pattern} is omitted all errors are caught.
4221 Examples: >
4222 :catch /^Vim:Interrupt$/ " catch interrupts (CTRL-C)
4223 :catch /^Vim\%((\a\+)\)\=:E/ " catch all Vim errors
4224 :catch /^Vim\%((\a\+)\)\=:/ " catch errors and interrupts
4225 :catch /^Vim(write):/ " catch all errors in :write
4226 :catch /^Vim\%((\a\+)\)\=:E123/ " catch error E123
4227 :catch /my-exception/ " catch user exception
4228 :catch /.*/ " catch everything
4229 :catch " same as /.*/
4230<
4231 Another character can be used instead of / around the
4232 {pattern}, so long as it does not have a special
4233 meaning (e.g., '|' or '"') and doesn't occur inside
4234 {pattern}.
4235 NOTE: It is not reliable to ":catch" the TEXT of
4236 an error message because it may vary in different
4237 locales.
4238
4239 *:fina* *:finally* *E606* *E607*
4240:fina[lly] The following commands until the matching |:endtry|
4241 are executed whenever the part between the matching
4242 |:try| and the ":finally" is left: either by falling
4243 through to the ":finally" or by a |:continue|,
4244 |:break|, |:finish|, or |:return|, or by an error or
4245 interrupt or exception (see |:throw|).
4246
4247 *:th* *:throw* *E608*
4248:th[row] {expr1} The {expr1} is evaluated and thrown as an exception.
4249 If the ":throw" is used after a |:try| but before the
4250 first corresponding |:catch|, commands are skipped
4251 until the first ":catch" matching {expr1} is reached.
4252 If there is no such ":catch" or if the ":throw" is
4253 used after a ":catch" but before the |:finally|, the
4254 commands following the ":finally" (if present) up to
4255 the matching |:endtry| are executed. If the ":throw"
4256 is after the ":finally", commands up to the ":endtry"
4257 are skipped. At the ":endtry", this process applies
4258 again for the next dynamically surrounding ":try"
4259 (which may be found in a calling function or sourcing
4260 script), until a matching ":catch" has been found.
4261 If the exception is not caught, the command processing
4262 is terminated.
4263 Example: >
4264 :try | throw "oops" | catch /^oo/ | echo "caught" | endtry
4265<
4266
4267 *:ec* *:echo*
4268:ec[ho] {expr1} .. Echoes each {expr1}, with a space in between. The
4269 first {expr1} starts on a new line.
4270 Also see |:comment|.
4271 Use "\n" to start a new line. Use "\r" to move the
4272 cursor to the first column.
4273 Uses the highlighting set by the |:echohl| command.
4274 Cannot be followed by a comment.
4275 Example: >
4276 :echo "the value of 'shell' is" &shell
4277< A later redraw may make the message disappear again.
4278 To avoid that a command from before the ":echo" causes
4279 a redraw afterwards (redraws are often postponed until
4280 you type something), force a redraw with the |:redraw|
4281 command. Example: >
4282 :new | redraw | echo "there is a new window"
4283<
4284 *:echon*
4285:echon {expr1} .. Echoes each {expr1}, without anything added. Also see
4286 |:comment|.
4287 Uses the highlighting set by the |:echohl| command.
4288 Cannot be followed by a comment.
4289 Example: >
4290 :echon "the value of 'shell' is " &shell
4291<
4292 Note the difference between using ":echo", which is a
4293 Vim command, and ":!echo", which is an external shell
4294 command: >
4295 :!echo % --> filename
4296< The arguments of ":!" are expanded, see |:_%|. >
4297 :!echo "%" --> filename or "filename"
4298< Like the previous example. Whether you see the double
4299 quotes or not depends on your 'shell'. >
4300 :echo % --> nothing
4301< The '%' is an illegal character in an expression. >
4302 :echo "%" --> %
4303< This just echoes the '%' character. >
4304 :echo expand("%") --> filename
4305< This calls the expand() function to expand the '%'.
4306
4307 *:echoh* *:echohl*
4308:echoh[l] {name} Use the highlight group {name} for the following
4309 |:echo|, |:echon| and |:echomsg| commands. Also used
4310 for the |input()| prompt. Example: >
4311 :echohl WarningMsg | echo "Don't panic!" | echohl None
4312< Don't forget to set the group back to "None",
4313 otherwise all following echo's will be highlighted.
4314
4315 *:echom* *:echomsg*
4316:echom[sg] {expr1} .. Echo the expression(s) as a true message, saving the
4317 message in the |message-history|.
4318 Spaces are placed between the arguments as with the
4319 |:echo| command. But unprintable characters are
4320 displayed, not interpreted.
4321 Uses the highlighting set by the |:echohl| command.
4322 Example: >
4323 :echomsg "It's a Zizzer Zazzer Zuzz, as you can plainly see."
4324<
4325 *:echoe* *:echoerr*
4326:echoe[rr] {expr1} .. Echo the expression(s) as an error message, saving the
4327 message in the |message-history|. When used in a
4328 script or function the line number will be added.
4329 Spaces are placed between the arguments as with the
4330 :echo command. When used inside a try conditional,
4331 the message is raised as an error exception instead
4332 (see |try-echoerr|).
4333 Example: >
4334 :echoerr "This script just failed!"
4335< If you just want a highlighted message use |:echohl|.
4336 And to get a beep: >
4337 :exe "normal \<Esc>"
4338<
4339 *:exe* *:execute*
4340:exe[cute] {expr1} .. Executes the string that results from the evaluation
4341 of {expr1} as an Ex command. Multiple arguments are
4342 concatenated, with a space in between. {expr1} is
4343 used as the processed command, command line editing
4344 keys are not recognized.
4345 Cannot be followed by a comment.
4346 Examples: >
4347 :execute "buffer " nextbuf
4348 :execute "normal " count . "w"
4349<
4350 ":execute" can be used to append a command to commands
4351 that don't accept a '|'. Example: >
4352 :execute '!ls' | echo "theend"
4353
4354< ":execute" is also a nice way to avoid having to type
4355 control characters in a Vim script for a ":normal"
4356 command: >
4357 :execute "normal ixxx\<Esc>"
4358< This has an <Esc> character, see |expr-string|.
4359
4360 Note: The executed string may be any command-line, but
4361 you cannot start or end a "while" or "if" command.
4362 Thus this is illegal: >
4363 :execute 'while i > 5'
4364 :execute 'echo "test" | break'
4365<
4366 It is allowed to have a "while" or "if" command
4367 completely in the executed string: >
4368 :execute 'while i < 5 | echo i | let i = i + 1 | endwhile'
4369<
4370
4371 *:comment*
4372 ":execute", ":echo" and ":echon" cannot be followed by
4373 a comment directly, because they see the '"' as the
4374 start of a string. But, you can use '|' followed by a
4375 comment. Example: >
4376 :echo "foo" | "this is a comment
4377
4378==============================================================================
43798. Exception handling *exception-handling*
4380
4381The Vim script language comprises an exception handling feature. This section
4382explains how it can be used in a Vim script.
4383
4384Exceptions may be raised by Vim on an error or on interrupt, see
4385|catch-errors| and |catch-interrupt|. You can also explicitly throw an
4386exception by using the ":throw" command, see |throw-catch|.
4387
4388
4389TRY CONDITIONALS *try-conditionals*
4390
4391Exceptions can be caught or can cause cleanup code to be executed. You can
4392use a try conditional to specify catch clauses (that catch exceptions) and/or
4393a finally clause (to be executed for cleanup).
4394 A try conditional begins with a |:try| command and ends at the matching
4395|:endtry| command. In between, you can use a |:catch| command to start
4396a catch clause, or a |:finally| command to start a finally clause. There may
4397be none or multiple catch clauses, but there is at most one finally clause,
4398which must not be followed by any catch clauses. The lines before the catch
4399clauses and the finally clause is called a try block. >
4400
4401 :try
4402 : ...
4403 : ... TRY BLOCK
4404 : ...
4405 :catch /{pattern}/
4406 : ...
4407 : ... CATCH CLAUSE
4408 : ...
4409 :catch /{pattern}/
4410 : ...
4411 : ... CATCH CLAUSE
4412 : ...
4413 :finally
4414 : ...
4415 : ... FINALLY CLAUSE
4416 : ...
4417 :endtry
4418
4419The try conditional allows to watch code for exceptions and to take the
4420appropriate actions. Exceptions from the try block may be caught. Exceptions
4421from the try block and also the catch clauses may cause cleanup actions.
4422 When no exception is thrown during execution of the try block, the control
4423is transferred to the finally clause, if present. After its execution, the
4424script continues with the line following the ":endtry".
4425 When an exception occurs during execution of the try block, the remaining
4426lines in the try block are skipped. The exception is matched against the
4427patterns specified as arguments to the ":catch" commands. The catch clause
4428after the first matching ":catch" is taken, other catch clauses are not
4429executed. The catch clause ends when the next ":catch", ":finally", or
4430":endtry" command is reached - whatever is first. Then, the finally clause
4431(if present) is executed. When the ":endtry" is reached, the script execution
4432continues in the following line as usual.
4433 When an exception that does not match any of the patterns specified by the
4434":catch" commands is thrown in the try block, the exception is not caught by
4435that try conditional and none of the catch clauses is executed. Only the
4436finally clause, if present, is taken. The exception pends during execution of
4437the finally clause. It is resumed at the ":endtry", so that commands after
4438the ":endtry" are not executed and the exception might be caught elsewhere,
4439see |try-nesting|.
4440 When during execution of a catch clause another exception is thrown, the
4441remaining lines in that catch clause are not executed. The new exception is
4442not matched against the patterns in any of the ":catch" commands of the same
4443try conditional and none of its catch clauses is taken. If there is, however,
4444a finally clause, it is executed, and the exception pends during its
4445execution. The commands following the ":endtry" are not executed. The new
4446exception might, however, be caught elsewhere, see |try-nesting|.
4447 When during execution of the finally clause (if present) an exception is
4448thrown, the remaining lines in the finally clause are skipped. If the finally
4449clause has been taken because of an exception from the try block or one of the
4450catch clauses, the original (pending) exception is discarded. The commands
4451following the ":endtry" are not executed, and the exception from the finally
4452clause is propagated and can be caught elsewhere, see |try-nesting|.
4453
4454The finally clause is also executed, when a ":break" or ":continue" for
4455a ":while" loop enclosing the complete try conditional is executed from the
4456try block or a catch clause. Or when a ":return" or ":finish" is executed
4457from the try block or a catch clause of a try conditional in a function or
4458sourced script, respectively. The ":break", ":continue", ":return", or
4459":finish" pends during execution of the finally clause and is resumed when the
4460":endtry" is reached. It is, however, discarded when an exception is thrown
4461from the finally clause.
4462 When a ":break" or ":continue" for a ":while" loop enclosing the complete
4463try conditional or when a ":return" or ":finish" is encountered in the finally
4464clause, the rest of the finally clause is skipped, and the ":break",
4465":continue", ":return" or ":finish" is executed as usual. If the finally
4466clause has been taken because of an exception or an earlier ":break",
4467":continue", ":return", or ":finish" from the try block or a catch clause,
4468this pending exception or command is discarded.
4469
4470For examples see |throw-catch| and |try-finally|.
4471
4472
4473NESTING OF TRY CONDITIONALS *try-nesting*
4474
4475Try conditionals can be nested arbitrarily. That is, a complete try
4476conditional can be put into the try block, a catch clause, or the finally
4477clause of another try conditional. If the inner try conditional does not
4478catch an exception thrown in its try block or throws a new exception from one
4479of its catch clauses or its finally clause, the outer try conditional is
4480checked according to the rules above. If the inner try conditional is in the
4481try block of the outer try conditional, its catch clauses are checked, but
4482otherwise only the finally clause is executed. It does not matter for
4483nesting, whether the inner try conditional is directly contained in the outer
4484one, or whether the outer one sources a script or calls a function containing
4485the inner try conditional.
4486
4487When none of the active try conditionals catches an exception, just their
4488finally clauses are executed. Thereafter, the script processing terminates.
4489An error message is displayed in case of an uncaught exception explicitly
4490thrown by a ":throw" command. For uncaught error and interrupt exceptions
4491implicitly raised by Vim, the error message(s) or interrupt message are shown
4492as usual.
4493
4494For examples see |throw-catch|.
4495
4496
4497EXAMINING EXCEPTION HANDLING CODE *except-examine*
4498
4499Exception handling code can get tricky. If you are in doubt what happens, set
4500'verbose' to 13 or use the ":13verbose" command modifier when sourcing your
4501script file. Then you see when an exception is thrown, discarded, caught, or
4502finished. When using a verbosity level of at least 14, things pending in
4503a finally clause are also shown. This information is also given in debug mode
4504(see |debug-scripts|).
4505
4506
4507THROWING AND CATCHING EXCEPTIONS *throw-catch*
4508
4509You can throw any number or string as an exception. Use the |:throw| command
4510and pass the value to be thrown as argument: >
4511 :throw 4711
4512 :throw "string"
4513< *throw-expression*
4514You can also specify an expression argument. The expression is then evaluated
4515first, and the result is thrown: >
4516 :throw 4705 + strlen("string")
4517 :throw strpart("strings", 0, 6)
4518
4519An exception might be thrown during evaluation of the argument of the ":throw"
4520command. Unless it is caught there, the expression evaluation is abandoned.
4521The ":throw" command then does not throw a new exception.
4522 Example: >
4523
4524 :function! Foo(arg)
4525 : try
4526 : throw a:arg
4527 : catch /foo/
4528 : endtry
4529 : return 1
4530 :endfunction
4531 :
4532 :function! Bar()
4533 : echo "in Bar"
4534 : return 4710
4535 :endfunction
4536 :
4537 :throw Foo("arrgh") + Bar()
4538
4539This throws "arrgh", and "in Bar" is not displayed since Bar() is not
4540executed. >
4541 :throw Foo("foo") + Bar()
4542however displays "in Bar" and throws 4711.
4543
4544Any other command that takes an expression as argument might also be
4545abandoned by an (uncaught) exception during the expression evaluation. The
4546exception is then propagated to the caller of the command.
4547 Example: >
4548
4549 :if Foo("arrgh")
4550 : echo "then"
4551 :else
4552 : echo "else"
4553 :endif
4554
4555Here neither of "then" or "else" is displayed.
4556
4557 *catch-order*
4558Exceptions can be caught by a try conditional with one or more |:catch|
4559commands, see |try-conditionals|. The values to be caught by each ":catch"
4560command can be specified as a pattern argument. The subsequent catch clause
4561gets executed when a matching exception is caught.
4562 Example: >
4563
4564 :function! Foo(value)
4565 : try
4566 : throw a:value
4567 : catch /^\d\+$/
4568 : echo "Number thrown"
4569 : catch /.*/
4570 : echo "String thrown"
4571 : endtry
4572 :endfunction
4573 :
4574 :call Foo(0x1267)
4575 :call Foo('string')
4576
4577The first call to Foo() displays "Number thrown", the second "String thrown".
4578An exception is matched against the ":catch" commands in the order they are
4579specified. Only the first match counts. So you should place the more
4580specific ":catch" first. The following order does not make sense: >
4581
4582 : catch /.*/
4583 : echo "String thrown"
4584 : catch /^\d\+$/
4585 : echo "Number thrown"
4586
4587The first ":catch" here matches always, so that the second catch clause is
4588never taken.
4589
4590 *throw-variables*
4591If you catch an exception by a general pattern, you may access the exact value
4592in the variable |v:exception|: >
4593
4594 : catch /^\d\+$/
4595 : echo "Number thrown. Value is" v:exception
4596
4597You may also be interested where an exception was thrown. This is stored in
4598|v:throwpoint|. Note that "v:exception" and "v:throwpoint" are valid for the
4599exception most recently caught as long it is not finished.
4600 Example: >
4601
4602 :function! Caught()
4603 : if v:exception != ""
4604 : echo 'Caught "' . v:exception . '" in ' . v:throwpoint
4605 : else
4606 : echo 'Nothing caught'
4607 : endif
4608 :endfunction
4609 :
4610 :function! Foo()
4611 : try
4612 : try
4613 : try
4614 : throw 4711
4615 : finally
4616 : call Caught()
4617 : endtry
4618 : catch /.*/
4619 : call Caught()
4620 : throw "oops"
4621 : endtry
4622 : catch /.*/
4623 : call Caught()
4624 : finally
4625 : call Caught()
4626 : endtry
4627 :endfunction
4628 :
4629 :call Foo()
4630
4631This displays >
4632
4633 Nothing caught
4634 Caught "4711" in function Foo, line 4
4635 Caught "oops" in function Foo, line 10
4636 Nothing caught
4637
4638A practical example: The following command ":LineNumber" displays the line
4639number in the script or function where it has been used: >
4640
4641 :function! LineNumber()
4642 : return substitute(v:throwpoint, '.*\D\(\d\+\).*', '\1', "")
4643 :endfunction
4644 :command! LineNumber try | throw "" | catch | echo LineNumber() | endtry
4645<
4646 *try-nested*
4647An exception that is not caught by a try conditional can be caught by
4648a surrounding try conditional: >
4649
4650 :try
4651 : try
4652 : throw "foo"
4653 : catch /foobar/
4654 : echo "foobar"
4655 : finally
4656 : echo "inner finally"
4657 : endtry
4658 :catch /foo/
4659 : echo "foo"
4660 :endtry
4661
4662The inner try conditional does not catch the exception, just its finally
4663clause is executed. The exception is then caught by the outer try
4664conditional. The example displays "inner finally" and then "foo".
4665
4666 *throw-from-catch*
4667You can catch an exception and throw a new one to be caught elsewhere from the
4668catch clause: >
4669
4670 :function! Foo()
4671 : throw "foo"
4672 :endfunction
4673 :
4674 :function! Bar()
4675 : try
4676 : call Foo()
4677 : catch /foo/
4678 : echo "Caught foo, throw bar"
4679 : throw "bar"
4680 : endtry
4681 :endfunction
4682 :
4683 :try
4684 : call Bar()
4685 :catch /.*/
4686 : echo "Caught" v:exception
4687 :endtry
4688
4689This displays "Caught foo, throw bar" and then "Caught bar".
4690
4691 *rethrow*
4692There is no real rethrow in the Vim script language, but you may throw
4693"v:exception" instead: >
4694
4695 :function! Bar()
4696 : try
4697 : call Foo()
4698 : catch /.*/
4699 : echo "Rethrow" v:exception
4700 : throw v:exception
4701 : endtry
4702 :endfunction
4703< *try-echoerr*
4704Note that this method cannot be used to "rethrow" Vim error or interrupt
4705exceptions, because it is not possible to fake Vim internal exceptions.
4706Trying so causes an error exception. You should throw your own exception
4707denoting the situation. If you want to cause a Vim error exception containing
4708the original error exception value, you can use the |:echoerr| command: >
4709
4710 :try
4711 : try
4712 : asdf
4713 : catch /.*/
4714 : echoerr v:exception
4715 : endtry
4716 :catch /.*/
4717 : echo v:exception
4718 :endtry
4719
4720This code displays
4721
4722 Vim(echoerr):Vim:E492: Not an editor command: asdf ~
4723
4724
4725CLEANUP CODE *try-finally*
4726
4727Scripts often change global settings and restore them at their end. If the
4728user however interrupts the script by pressing CTRL-C, the settings remain in
4729an inconsistent state. The same may happen to you in the development phase of
4730a script when an error occurs or you explicitly throw an exception without
4731catching it. You can solve these problems by using a try conditional with
4732a finally clause for restoring the settings. Its execution is guaranteed on
4733normal control flow, on error, on an explicit ":throw", and on interrupt.
4734(Note that errors and interrupts from inside the try conditional are converted
4735to exceptions. When not caught, they terminate the script after the finally
4736clause has been executed.)
4737Example: >
4738
4739 :try
4740 : let s:saved_ts = &ts
4741 : set ts=17
4742 :
4743 : " Do the hard work here.
4744 :
4745 :finally
4746 : let &ts = s:saved_ts
4747 : unlet s:saved_ts
4748 :endtry
4749
4750This method should be used locally whenever a function or part of a script
4751changes global settings which need to be restored on failure or normal exit of
4752that function or script part.
4753
4754 *break-finally*
4755Cleanup code works also when the try block or a catch clause is left by
4756a ":continue", ":break", ":return", or ":finish".
4757 Example: >
4758
4759 :let first = 1
4760 :while 1
4761 : try
4762 : if first
4763 : echo "first"
4764 : let first = 0
4765 : continue
4766 : else
4767 : throw "second"
4768 : endif
4769 : catch /.*/
4770 : echo v:exception
4771 : break
4772 : finally
4773 : echo "cleanup"
4774 : endtry
4775 : echo "still in while"
4776 :endwhile
4777 :echo "end"
4778
4779This displays "first", "cleanup", "second", "cleanup", and "end". >
4780
4781 :function! Foo()
4782 : try
4783 : return 4711
4784 : finally
4785 : echo "cleanup\n"
4786 : endtry
4787 : echo "Foo still active"
4788 :endfunction
4789 :
4790 :echo Foo() "returned by Foo"
4791
4792This displays "cleanup" and "4711 returned by Foo". You don't need to add an
4793extra ":return" in the finally clause. (Above all, this would override the
4794return value.)
4795
4796 *except-from-finally*
4797Using either of ":continue", ":break", ":return", ":finish", or ":throw" in
4798a finally clause is possible, but not recommended since it abandons the
4799cleanup actions for the try conditional. But, of course, interrupt and error
4800exceptions might get raised from a finally clause.
4801 Example where an error in the finally clause stops an interrupt from
4802working correctly: >
4803
4804 :try
4805 : try
4806 : echo "Press CTRL-C for interrupt"
4807 : while 1
4808 : endwhile
4809 : finally
4810 : unlet novar
4811 : endtry
4812 :catch /novar/
4813 :endtry
4814 :echo "Script still running"
4815 :sleep 1
4816
4817If you need to put commands that could fail into a finally clause, you should
4818think about catching or ignoring the errors in these commands, see
4819|catch-errors| and |ignore-errors|.
4820
4821
4822CATCHING ERRORS *catch-errors*
4823
4824If you want to catch specific errors, you just have to put the code to be
4825watched in a try block and add a catch clause for the error message. The
4826presence of the try conditional causes all errors to be converted to an
4827exception. No message is displayed and |v:errmsg| is not set then. To find
4828the right pattern for the ":catch" command, you have to know how the format of
4829the error exception is.
4830 Error exceptions have the following format: >
4831
4832 Vim({cmdname}):{errmsg}
4833or >
4834 Vim:{errmsg}
4835
4836{cmdname} is the name of the command that failed; the second form is used when
4837the command name is not known. {errmsg} is the error message usually produced
4838when the error occurs outside try conditionals. It always begins with
4839a capital "E", followed by a two or three-digit error number, a colon, and
4840a space.
4841
4842Examples:
4843
4844The command >
4845 :unlet novar
4846normally produces the error message >
4847 E108: No such variable: "novar"
4848which is converted inside try conditionals to an exception >
4849 Vim(unlet):E108: No such variable: "novar"
4850
4851The command >
4852 :dwim
4853normally produces the error message >
4854 E492: Not an editor command: dwim
4855which is converted inside try conditionals to an exception >
4856 Vim:E492: Not an editor command: dwim
4857
4858You can catch all ":unlet" errors by a >
4859 :catch /^Vim(unlet):/
4860or all errors for misspelled command names by a >
4861 :catch /^Vim:E492:/
4862
4863Some error messages may be produced by different commands: >
4864 :function nofunc
4865and >
4866 :delfunction nofunc
4867both produce the error message >
4868 E128: Function name must start with a capital: nofunc
4869which is converted inside try conditionals to an exception >
4870 Vim(function):E128: Function name must start with a capital: nofunc
4871or >
4872 Vim(delfunction):E128: Function name must start with a capital: nofunc
4873respectively. You can catch the error by its number independently on the
4874command that caused it if you use the following pattern: >
4875 :catch /^Vim(\a\+):E128:/
4876
4877Some commands like >
4878 :let x = novar
4879produce multiple error messages, here: >
4880 E121: Undefined variable: novar
4881 E15: Invalid expression: novar
4882Only the first is used for the exception value, since it is the most specific
4883one (see |except-several-errors|). So you can catch it by >
4884 :catch /^Vim(\a\+):E121:/
4885
4886You can catch all errors related to the name "nofunc" by >
4887 :catch /\<nofunc\>/
4888
4889You can catch all Vim errors in the ":write" and ":read" commands by >
4890 :catch /^Vim(\(write\|read\)):E\d\+:/
4891
4892You can catch all Vim errors by the pattern >
4893 :catch /^Vim\((\a\+)\)\=:E\d\+:/
4894<
4895 *catch-text*
4896NOTE: You should never catch the error message text itself: >
4897 :catch /No such variable/
4898only works in the english locale, but not when the user has selected
4899a different language by the |:language| command. It is however helpful to
4900cite the message text in a comment: >
4901 :catch /^Vim(\a\+):E108:/ " No such variable
4902
4903
4904IGNORING ERRORS *ignore-errors*
4905
4906You can ignore errors in a specific Vim command by catching them locally: >
4907
4908 :try
4909 : write
4910 :catch
4911 :endtry
4912
4913But you are strongly recommended NOT to use this simple form, since it could
4914catch more than you want. With the ":write" command, some autocommands could
4915be executed and cause errors not related to writing, for instance: >
4916
4917 :au BufWritePre * unlet novar
4918
4919There could even be such errors you are not responsible for as a script
4920writer: a user of your script might have defined such autocommands. You would
4921then hide the error from the user.
4922 It is much better to use >
4923
4924 :try
4925 : write
4926 :catch /^Vim(write):/
4927 :endtry
4928
4929which only catches real write errors. So catch only what you'd like to ignore
4930intentionally.
4931
4932For a single command that does not cause execution of autocommands, you could
4933even suppress the conversion of errors to exceptions by the ":silent!"
4934command: >
4935 :silent! nunmap k
4936This works also when a try conditional is active.
4937
4938
4939CATCHING INTERRUPTS *catch-interrupt*
4940
4941When there are active try conditionals, an interrupt (CTRL-C) is converted to
4942the exception "Vim:Interrupt". You can catch it like every exception. The
4943script is not terminated, then.
4944 Example: >
4945
4946 :function! TASK1()
4947 : sleep 10
4948 :endfunction
4949
4950 :function! TASK2()
4951 : sleep 20
4952 :endfunction
4953
4954 :while 1
4955 : let command = input("Type a command: ")
4956 : try
4957 : if command == ""
4958 : continue
4959 : elseif command == "END"
4960 : break
4961 : elseif command == "TASK1"
4962 : call TASK1()
4963 : elseif command == "TASK2"
4964 : call TASK2()
4965 : else
4966 : echo "\nIllegal command:" command
4967 : continue
4968 : endif
4969 : catch /^Vim:Interrupt$/
4970 : echo "\nCommand interrupted"
4971 : " Caught the interrupt. Continue with next prompt.
4972 : endtry
4973 :endwhile
4974
4975You can interrupt a task here by pressing CTRL-C; the script then asks for
4976a new command. If you press CTRL-C at the prompt, the script is terminated.
4977
4978For testing what happens when CTRL-C would be pressed on a specific line in
4979your script, use the debug mode and execute the |>quit| or |>interrupt|
4980command on that line. See |debug-scripts|.
4981
4982
4983CATCHING ALL *catch-all*
4984
4985The commands >
4986
4987 :catch /.*/
4988 :catch //
4989 :catch
4990
4991catch everything, error exceptions, interrupt exceptions and exceptions
4992explicitly thrown by the |:throw| command. This is useful at the top level of
4993a script in order to catch unexpected things.
4994 Example: >
4995
4996 :try
4997 :
4998 : " do the hard work here
4999 :
5000 :catch /MyException/
5001 :
5002 : " handle known problem
5003 :
5004 :catch /^Vim:Interrupt$/
5005 : echo "Script interrupted"
5006 :catch /.*/
5007 : echo "Internal error (" . v:exception . ")"
5008 : echo " - occurred at " . v:throwpoint
5009 :endtry
5010 :" end of script
5011
5012Note: Catching all might catch more things than you want. Thus, you are
5013strongly encouraged to catch only for problems that you can really handle by
5014specifying a pattern argument to the ":catch".
5015 Example: Catching all could make it nearly impossible to interrupt a script
5016by pressing CTRL-C: >
5017
5018 :while 1
5019 : try
5020 : sleep 1
5021 : catch
5022 : endtry
5023 :endwhile
5024
5025
5026EXCEPTIONS AND AUTOCOMMANDS *except-autocmd*
5027
5028Exceptions may be used during execution of autocommands. Example: >
5029
5030 :autocmd User x try
5031 :autocmd User x throw "Oops!"
5032 :autocmd User x catch
5033 :autocmd User x echo v:exception
5034 :autocmd User x endtry
5035 :autocmd User x throw "Arrgh!"
5036 :autocmd User x echo "Should not be displayed"
5037 :
5038 :try
5039 : doautocmd User x
5040 :catch
5041 : echo v:exception
5042 :endtry
5043
5044This displays "Oops!" and "Arrgh!".
5045
5046 *except-autocmd-Pre*
5047For some commands, autocommands get executed before the main action of the
5048command takes place. If an exception is thrown and not caught in the sequence
5049of autocommands, the sequence and the command that caused its execution are
5050abandoned and the exception is propagated to the caller of the command.
5051 Example: >
5052
5053 :autocmd BufWritePre * throw "FAIL"
5054 :autocmd BufWritePre * echo "Should not be displayed"
5055 :
5056 :try
5057 : write
5058 :catch
5059 : echo "Caught:" v:exception "from" v:throwpoint
5060 :endtry
5061
5062Here, the ":write" command does not write the file currently being edited (as
5063you can see by checking 'modified'), since the exception from the BufWritePre
5064autocommand abandons the ":write". The exception is then caught and the
5065script displays: >
5066
5067 Caught: FAIL from BufWrite Auto commands for "*"
5068<
5069 *except-autocmd-Post*
5070For some commands, autocommands get executed after the main action of the
5071command has taken place. If this main action fails and the command is inside
5072an active try conditional, the autocommands are skipped and an error exception
5073is thrown that can be caught by the caller of the command.
5074 Example: >
5075
5076 :autocmd BufWritePost * echo "File successfully written!"
5077 :
5078 :try
5079 : write /i/m/p/o/s/s/i/b/l/e
5080 :catch
5081 : echo v:exception
5082 :endtry
5083
5084This just displays: >
5085
5086 Vim(write):E212: Can't open file for writing (/i/m/p/o/s/s/i/b/l/e)
5087
5088If you really need to execute the autocommands even when the main action
5089fails, trigger the event from the catch clause.
5090 Example: >
5091
5092 :autocmd BufWritePre * set noreadonly
5093 :autocmd BufWritePost * set readonly
5094 :
5095 :try
5096 : write /i/m/p/o/s/s/i/b/l/e
5097 :catch
5098 : doautocmd BufWritePost /i/m/p/o/s/s/i/b/l/e
5099 :endtry
5100<
5101You can also use ":silent!": >
5102
5103 :let x = "ok"
5104 :let v:errmsg = ""
5105 :autocmd BufWritePost * if v:errmsg != ""
5106 :autocmd BufWritePost * let x = "after fail"
5107 :autocmd BufWritePost * endif
5108 :try
5109 : silent! write /i/m/p/o/s/s/i/b/l/e
5110 :catch
5111 :endtry
5112 :echo x
5113
5114This displays "after fail".
5115
5116If the main action of the command does not fail, exceptions from the
5117autocommands will be catchable by the caller of the command: >
5118
5119 :autocmd BufWritePost * throw ":-("
5120 :autocmd BufWritePost * echo "Should not be displayed"
5121 :
5122 :try
5123 : write
5124 :catch
5125 : echo v:exception
5126 :endtry
5127<
5128 *except-autocmd-Cmd*
5129For some commands, the normal action can be replaced by a sequence of
5130autocommands. Exceptions from that sequence will be catchable by the caller
5131of the command.
5132 Example: For the ":write" command, the caller cannot know whether the file
5133had actually been written when the exception occurred. You need to tell it in
5134some way. >
5135
5136 :if !exists("cnt")
5137 : let cnt = 0
5138 :
5139 : autocmd BufWriteCmd * if &modified
5140 : autocmd BufWriteCmd * let cnt = cnt + 1
5141 : autocmd BufWriteCmd * if cnt % 3 == 2
5142 : autocmd BufWriteCmd * throw "BufWriteCmdError"
5143 : autocmd BufWriteCmd * endif
5144 : autocmd BufWriteCmd * write | set nomodified
5145 : autocmd BufWriteCmd * if cnt % 3 == 0
5146 : autocmd BufWriteCmd * throw "BufWriteCmdError"
5147 : autocmd BufWriteCmd * endif
5148 : autocmd BufWriteCmd * echo "File successfully written!"
5149 : autocmd BufWriteCmd * endif
5150 :endif
5151 :
5152 :try
5153 : write
5154 :catch /^BufWriteCmdError$/
5155 : if &modified
5156 : echo "Error on writing (file contents not changed)"
5157 : else
5158 : echo "Error after writing"
5159 : endif
5160 :catch /^Vim(write):/
5161 : echo "Error on writing"
5162 :endtry
5163
5164When this script is sourced several times after making changes, it displays
5165first >
5166 File successfully written!
5167then >
5168 Error on writing (file contents not changed)
5169then >
5170 Error after writing
5171etc.
5172
5173 *except-autocmd-ill*
5174You cannot spread a try conditional over autocommands for different events.
5175The following code is ill-formed: >
5176
5177 :autocmd BufWritePre * try
5178 :
5179 :autocmd BufWritePost * catch
5180 :autocmd BufWritePost * echo v:exception
5181 :autocmd BufWritePost * endtry
5182 :
5183 :write
5184
5185
5186EXCEPTION HIERARCHIES AND PARAMETERIZED EXCEPTIONS *except-hier-param*
5187
5188Some programming languages allow to use hierarchies of exception classes or to
5189pass additional information with the object of an exception class. You can do
5190similar things in Vim.
5191 In order to throw an exception from a hierarchy, just throw the complete
5192class name with the components separated by a colon, for instance throw the
5193string "EXCEPT:MATHERR:OVERFLOW" for an overflow in a mathematical library.
5194 When you want to pass additional information with your exception class, add
5195it in parentheses, for instance throw the string "EXCEPT:IO:WRITEERR(myfile)"
5196for an error when writing "myfile".
5197 With the appropriate patterns in the ":catch" command, you can catch for
5198base classes or derived classes of your hierarchy. Additional information in
5199parentheses can be cut out from |v:exception| with the ":substitute" command.
5200 Example: >
5201
5202 :function! CheckRange(a, func)
5203 : if a:a < 0
5204 : throw "EXCEPT:MATHERR:RANGE(" . a:func . ")"
5205 : endif
5206 :endfunction
5207 :
5208 :function! Add(a, b)
5209 : call CheckRange(a:a, "Add")
5210 : call CheckRange(a:b, "Add")
5211 : let c = a:a + a:b
5212 : if c < 0
5213 : throw "EXCEPT:MATHERR:OVERFLOW"
5214 : endif
5215 : return c
5216 :endfunction
5217 :
5218 :function! Div(a, b)
5219 : call CheckRange(a:a, "Div")
5220 : call CheckRange(a:b, "Div")
5221 : if (a:b == 0)
5222 : throw "EXCEPT:MATHERR:ZERODIV"
5223 : endif
5224 : return a:a / a:b
5225 :endfunction
5226 :
5227 :function! Write(file)
5228 : try
5229 : execute "write" a:file
5230 : catch /^Vim(write):/
5231 : throw "EXCEPT:IO(" . getcwd() . ", " . a:file . "):WRITEERR"
5232 : endtry
5233 :endfunction
5234 :
5235 :try
5236 :
5237 : " something with arithmetics and I/O
5238 :
5239 :catch /^EXCEPT:MATHERR:RANGE/
5240 : let function = substitute(v:exception, '.*(\(\a\+\)).*', '\1', "")
5241 : echo "Range error in" function
5242 :
5243 :catch /^EXCEPT:MATHERR/ " catches OVERFLOW and ZERODIV
5244 : echo "Math error"
5245 :
5246 :catch /^EXCEPT:IO/
5247 : let dir = substitute(v:exception, '.*(\(.\+\),\s*.\+).*', '\1', "")
5248 : let file = substitute(v:exception, '.*(.\+,\s*\(.\+\)).*', '\1', "")
5249 : if file !~ '^/'
5250 : let file = dir . "/" . file
5251 : endif
5252 : echo 'I/O error for "' . file . '"'
5253 :
5254 :catch /^EXCEPT/
5255 : echo "Unspecified error"
5256 :
5257 :endtry
5258
5259The exceptions raised by Vim itself (on error or when pressing CTRL-C) use
5260a flat hierarchy: they are all in the "Vim" class. You cannot throw yourself
5261exceptions with the "Vim" prefix; they are reserved for Vim.
5262 Vim error exceptions are parameterized with the name of the command that
5263failed, if known. See |catch-errors|.
5264
5265
5266PECULIARITIES
5267 *except-compat*
5268The exception handling concept requires that the command sequence causing the
5269exception is aborted immediately and control is transferred to finally clauses
5270and/or a catch clause.
5271
5272In the Vim script language there are cases where scripts and functions
5273continue after an error: in functions without the "abort" flag or in a command
5274after ":silent!", control flow goes to the following line, and outside
5275functions, control flow goes to the line following the outermost ":endwhile"
5276or ":endif". On the other hand, errors should be catchable as exceptions
5277(thus, requiring the immediate abortion).
5278
5279This problem has been solved by converting errors to exceptions and using
5280immediate abortion (if not suppressed by ":silent!") only when a try
5281conditional is active. This is no restriction since an (error) exception can
5282be caught only from an active try conditional. If you want an immediate
5283termination without catching the error, just use a try conditional without
5284catch clause. (You can cause cleanup code being executed before termination
5285by specifying a finally clause.)
5286
5287When no try conditional is active, the usual abortion and continuation
5288behavior is used instead of immediate abortion. This ensures compatibility of
5289scripts written for Vim 6.1 and earlier.
5290
5291However, when sourcing an existing script that does not use exception handling
5292commands (or when calling one of its functions) from inside an active try
5293conditional of a new script, you might change the control flow of the existing
5294script on error. You get the immediate abortion on error and can catch the
5295error in the new script. If however the sourced script suppresses error
5296messages by using the ":silent!" command (checking for errors by testing
5297|v:errmsg| if appropriate), its execution path is not changed. The error is
5298not converted to an exception. (See |:silent|.) So the only remaining cause
5299where this happens is for scripts that don't care about errors and produce
5300error messages. You probably won't want to use such code from your new
5301scripts.
5302
5303 *except-syntax-err*
5304Syntax errors in the exception handling commands are never caught by any of
5305the ":catch" commands of the try conditional they belong to. Its finally
5306clauses, however, is executed.
5307 Example: >
5308
5309 :try
5310 : try
5311 : throw 4711
5312 : catch /\(/
5313 : echo "in catch with syntax error"
5314 : catch
5315 : echo "inner catch-all"
5316 : finally
5317 : echo "inner finally"
5318 : endtry
5319 :catch
5320 : echo 'outer catch-all caught "' . v:exception . '"'
5321 : finally
5322 : echo "outer finally"
5323 :endtry
5324
5325This displays: >
5326 inner finally
5327 outer catch-all caught "Vim(catch):E54: Unmatched \("
5328 outer finally
5329The original exception is discarded and an error exception is raised, instead.
5330
5331 *except-single-line*
5332The ":try", ":catch", ":finally", and ":endtry" commands can be put on
5333a single line, but then syntax errors may make it difficult to recognize the
5334"catch" line, thus you better avoid this.
5335 Example: >
5336 :try | unlet! foo # | catch | endtry
5337raises an error exception for the trailing characters after the ":unlet!"
5338argument, but does not see the ":catch" and ":endtry" commands, so that the
5339error exception is discarded and the "E488: Trailing characters" message gets
5340displayed.
5341
5342 *except-several-errors*
5343When several errors appear in a single command, the first error message is
5344usually the most specific one and therefor converted to the error exception.
5345 Example: >
5346 echo novar
5347causes >
5348 E121: Undefined variable: novar
5349 E15: Invalid expression: novar
5350The value of the error exception inside try conditionals is: >
5351 Vim(echo):E121: Undefined variable: novar
5352< *except-syntax-error*
5353But when a syntax error is detected after a normal error in the same command,
5354the syntax error is used for the exception being thrown.
5355 Example: >
5356 unlet novar #
5357causes >
5358 E108: No such variable: "novar"
5359 E488: Trailing characters
5360The value of the error exception inside try conditionals is: >
5361 Vim(unlet):E488: Trailing characters
5362This is done because the syntax error might change the execution path in a way
5363not intended by the user. Example: >
5364 try
5365 try | unlet novar # | catch | echo v:exception | endtry
5366 catch /.*/
5367 echo "outer catch:" v:exception
5368 endtry
5369This displays "outer catch: Vim(unlet):E488: Trailing characters", and then
5370a "E600: Missing :endtry" error message is given, see |except-single-line|.
5371
5372==============================================================================
53739. Examples *eval-examples*
5374
5375Printing in Hex ~
5376>
5377 :" The function Nr2Hex() returns the Hex string of a number.
5378 :func Nr2Hex(nr)
5379 : let n = a:nr
5380 : let r = ""
5381 : while n
5382 : let r = '0123456789ABCDEF'[n % 16] . r
5383 : let n = n / 16
5384 : endwhile
5385 : return r
5386 :endfunc
5387
5388 :" The function String2Hex() converts each character in a string to a two
5389 :" character Hex string.
5390 :func String2Hex(str)
5391 : let out = ''
5392 : let ix = 0
5393 : while ix < strlen(a:str)
5394 : let out = out . Nr2Hex(char2nr(a:str[ix]))
5395 : let ix = ix + 1
5396 : endwhile
5397 : return out
5398 :endfunc
5399
5400Example of its use: >
5401 :echo Nr2Hex(32)
5402result: "20" >
5403 :echo String2Hex("32")
5404result: "3332"
5405
5406
5407Sorting lines (by Robert Webb) ~
5408
5409Here is a Vim script to sort lines. Highlight the lines in Vim and type
5410":Sort". This doesn't call any external programs so it'll work on any
5411platform. The function Sort() actually takes the name of a comparison
5412function as its argument, like qsort() does in C. So you could supply it
5413with different comparison functions in order to sort according to date etc.
5414>
5415 :" Function for use with Sort(), to compare two strings.
5416 :func! Strcmp(str1, str2)
5417 : if (a:str1 < a:str2)
5418 : return -1
5419 : elseif (a:str1 > a:str2)
5420 : return 1
5421 : else
5422 : return 0
5423 : endif
5424 :endfunction
5425
5426 :" Sort lines. SortR() is called recursively.
5427 :func! SortR(start, end, cmp)
5428 : if (a:start >= a:end)
5429 : return
5430 : endif
5431 : let partition = a:start - 1
5432 : let middle = partition
5433 : let partStr = getline((a:start + a:end) / 2)
5434 : let i = a:start
5435 : while (i <= a:end)
5436 : let str = getline(i)
5437 : exec "let result = " . a:cmp . "(str, partStr)"
5438 : if (result <= 0)
5439 : " Need to put it before the partition. Swap lines i and partition.
5440 : let partition = partition + 1
5441 : if (result == 0)
5442 : let middle = partition
5443 : endif
5444 : if (i != partition)
5445 : let str2 = getline(partition)
5446 : call setline(i, str2)
5447 : call setline(partition, str)
5448 : endif
5449 : endif
5450 : let i = i + 1
5451 : endwhile
5452
5453 : " Now we have a pointer to the "middle" element, as far as partitioning
5454 : " goes, which could be anywhere before the partition. Make sure it is at
5455 : " the end of the partition.
5456 : if (middle != partition)
5457 : let str = getline(middle)
5458 : let str2 = getline(partition)
5459 : call setline(middle, str2)
5460 : call setline(partition, str)
5461 : endif
5462 : call SortR(a:start, partition - 1, a:cmp)
5463 : call SortR(partition + 1, a:end, a:cmp)
5464 :endfunc
5465
5466 :" To Sort a range of lines, pass the range to Sort() along with the name of a
5467 :" function that will compare two lines.
5468 :func! Sort(cmp) range
5469 : call SortR(a:firstline, a:lastline, a:cmp)
5470 :endfunc
5471
5472 :" :Sort takes a range of lines and sorts them.
5473 :command! -nargs=0 -range Sort <line1>,<line2>call Sort("Strcmp")
5474<
5475 *sscanf*
5476There is no sscanf() function in Vim. If you need to extract parts from a
5477line, you can use matchstr() and substitute() to do it. This example shows
5478how to get the file name, line number and column number out of a line like
5479"foobar.txt, 123, 45". >
5480 :" Set up the match bit
5481 :let mx='\(\f\+\),\s*\(\d\+\),\s*\(\d\+\)'
5482 :"get the part matching the whole expression
5483 :let l = matchstr(line, mx)
5484 :"get each item out of the match
5485 :let file = substitute(l, mx, '\1', '')
5486 :let lnum = substitute(l, mx, '\2', '')
5487 :let col = substitute(l, mx, '\3', '')
5488
5489The input is in the variable "line", the results in the variables "file",
5490"lnum" and "col". (idea from Michael Geddes)
5491
5492==============================================================================
549310. No +eval feature *no-eval-feature*
5494
5495When the |+eval| feature was disabled at compile time, none of the expression
5496evaluation commands are available. To prevent this from causing Vim scripts
5497to generate all kinds of errors, the ":if" and ":endif" commands are still
5498recognized, though the argument of the ":if" and everything between the ":if"
5499and the matching ":endif" is ignored. Nesting of ":if" blocks is allowed, but
5500only if the commands are at the start of the line. The ":else" command is not
5501recognized.
5502
5503Example of how to avoid executing commands when the |+eval| feature is
5504missing: >
5505
5506 :if 1
5507 : echo "Expression evaluation is compiled in"
5508 :else
5509 : echo "You will _never_ see this message"
5510 :endif
5511
5512==============================================================================
551311. The sandbox *eval-sandbox* *sandbox* *E48*
5514
5515The 'foldexpr', 'includeexpr', 'indentexpr', 'statusline' and 'foldtext'
5516options are evaluated in a sandbox. This means that you are protected from
5517these expressions having nasty side effects. This gives some safety for when
5518these options are set from a modeline. It is also used when the command from
5519a tags file is executed.
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005520The sandbox is also used for the |:sandbox| command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521
5522These items are not allowed in the sandbox:
5523 - changing the buffer text
5524 - defining or changing mapping, autocommands, functions, user commands
5525 - setting certain options (see |option-summary|)
5526 - executing a shell command
5527 - reading or writing a file
5528 - jumping to another buffer or editing a file
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005529This is not guaranteed 100% secure, but it should block most attacks.
5530
5531 *:san* *:sandbox*
5532:sandbox {cmd} Execute {cmd} in the sandbox. Useful to evaluate an
5533 option that may have been set from a modeline, e.g.
5534 'foldexpr'.
5535
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536
5537 vim:tw=78:ts=8:ft=help:norl: