blob: b2ca19944725f6dd9401da724a8ece9644517f72 [file] [log] [blame]
Bram Moolenaar65c923a2006-03-03 22:56:30 +00001*eval.txt* For Vim version 7.0aa. Last change: 2006 Mar 03
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
Bram Moolenaare2cc9702005-03-15 22:43:58 +000012done, the features in this document are not available. See |+eval| and
Bram Moolenaard8b02732005-01-14 21:48:43 +000013|no-eval-feature|.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
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|
Bram Moolenaar7c626922005-02-07 22:01:03 +000018 1.3 Lists |Lists|
Bram Moolenaard8b02732005-01-14 21:48:43 +000019 1.4 Dictionaries |Dictionaries|
20 1.5 More about variables |more-variables|
Bram Moolenaar13065c42005-01-08 16:08:21 +0000212. Expression syntax |expression-syntax|
223. Internal variable |internal-variables|
234. Builtin Functions |functions|
245. Defining functions |user-functions|
256. Curly braces names |curly-braces-names|
267. Commands |expression-commands|
278. Exception handling |exception-handling|
289. Examples |eval-examples|
2910. No +eval feature |no-eval-feature|
3011. The sandbox |eval-sandbox|
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003112. Textlock |textlock|
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
33{Vi does not have any of these commands}
34
35==============================================================================
361. Variables *variables*
37
Bram Moolenaar13065c42005-01-08 16:08:21 +0000381.1 Variable types ~
Bram Moolenaar3a3a7232005-01-17 22:16:15 +000039 *E712*
Bram Moolenaar39a58ca2005-06-27 22:42:44 +000040There are five types of variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaard8b02732005-01-14 21:48:43 +000042Number A 32 bit signed number.
43 Examples: -123 0x10 0177
44
45String A NUL terminated string of 8-bit unsigned characters (bytes).
46 Examples: "ab\txx\"--" 'x-z''a,c'
47
48Funcref A reference to a function |Funcref|.
49 Example: function("strlen")
50
51List An ordered sequence of items |List|.
52 Example: [1, 2, ['a', 'b']]
Bram Moolenaar071d4272004-06-13 20:20:40 +000053
Bram Moolenaar39a58ca2005-06-27 22:42:44 +000054Dictionary An associative, unordered array: Each entry has a key and a
55 value. |Dictionary|
56 Example: {'blue': "#0000ff", 'red': "#ff0000"}
57
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +000058The Number and String types are converted automatically, depending on how they
59are used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000060
61Conversion from a Number to a String is by making the ASCII representation of
62the Number. Examples: >
63 Number 123 --> String "123"
64 Number 0 --> String "0"
65 Number -1 --> String "-1"
66
67Conversion from a String to a Number is done by converting the first digits
68to a number. Hexadecimal "0xf9" and Octal "017" numbers are recognized. If
69the String doesn't start with digits, the result is zero. Examples: >
70 String "456" --> Number 456
71 String "6bar" --> Number 6
72 String "foo" --> Number 0
73 String "0xf1" --> Number 241
74 String "0100" --> Number 64
75 String "-8" --> Number -8
76 String "+8" --> Number 0
77
78To force conversion from String to Number, add zero to it: >
79 :echo "0100" + 0
80
81For boolean operators Numbers are used. Zero is FALSE, non-zero is TRUE.
82
83Note that in the command >
84 :if "foo"
85"foo" is converted to 0, which means FALSE. To test for a non-empty string,
86use strlen(): >
87 :if strlen("foo")
Bram Moolenaar748bf032005-02-02 23:04:36 +000088< *E745* *E728* *E703* *E729* *E730* *E731*
89List, Dictionary and Funcref types are not automatically converted.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +000090
Bram Moolenaar13065c42005-01-08 16:08:21 +000091 *E706*
92You will get an error if you try to change the type of a variable. You need
93to |:unlet| it first to avoid this error. String and Number are considered
Bram Moolenaard8b02732005-01-14 21:48:43 +000094equivalent though. Consider this sequence of commands: >
Bram Moolenaar13065c42005-01-08 16:08:21 +000095 :let l = "string"
Bram Moolenaar9588a0f2005-01-08 21:45:39 +000096 :let l = 44 " changes type from String to Number
Bram Moolenaar13065c42005-01-08 16:08:21 +000097 :let l = [1, 2, 3] " error!
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +000098
Bram Moolenaar13065c42005-01-08 16:08:21 +000099
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00001001.2 Function references ~
Bram Moolenaar748bf032005-02-02 23:04:36 +0000101 *Funcref* *E695* *E718*
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000102A Funcref variable is obtained with the |function()| function. It can be used
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000103in an expression in the place of a function name, before the parenthesis
104around the arguments, to invoke the function it refers to. Example: >
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000105
106 :let Fn = function("MyFunc")
107 :echo Fn()
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000108< *E704* *E705* *E707*
Bram Moolenaar13065c42005-01-08 16:08:21 +0000109A Funcref variable must start with a capital, "s:", "w:" or "b:". You cannot
110have both a Funcref variable and a function with the same name.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000111
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000112A special case is defining a function and directly assigning its Funcref to a
113Dictionary entry. Example: >
114 :function dict.init() dict
115 : let self.val = 0
116 :endfunction
117
118The key of the Dictionary can start with a lower case letter. The actual
119function name is not used here. Also see |numbered-function|.
120
121A Funcref can also be used with the |:call| command: >
122 :call Fn()
123 :call dict.init()
Bram Moolenaar13065c42005-01-08 16:08:21 +0000124
125The name of the referenced function can be obtained with |string()|. >
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000126 :let func = string(Fn)
Bram Moolenaar13065c42005-01-08 16:08:21 +0000127
128You can use |call()| to invoke a Funcref and use a list variable for the
129arguments: >
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000130 :let r = call(Fn, mylist)
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000131
132
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00001331.3 Lists ~
Bram Moolenaar7c626922005-02-07 22:01:03 +0000134 *List* *Lists* *E686*
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000135A List is an ordered sequence of items. An item can be of any type. Items
136can be accessed by their index number. Items can be added and removed at any
137position in the sequence.
138
Bram Moolenaar13065c42005-01-08 16:08:21 +0000139
140List creation ~
141 *E696* *E697*
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000142A List is created with a comma separated list of items in square brackets.
Bram Moolenaar13065c42005-01-08 16:08:21 +0000143Examples: >
144 :let mylist = [1, two, 3, "four"]
145 :let emptylist = []
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000146
147An item can be any expression. Using a List for an item creates a
Bram Moolenaar13065c42005-01-08 16:08:21 +0000148nested List: >
149 :let nestlist = [[11, 12], [21, 22], [31, 32]]
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000150
151An extra comma after the last item is ignored.
152
Bram Moolenaar13065c42005-01-08 16:08:21 +0000153
154List index ~
155 *list-index* *E684*
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000156An item in the List can be accessed by putting the index in square brackets
Bram Moolenaar13065c42005-01-08 16:08:21 +0000157after the List. Indexes are zero-based, thus the first item has index zero. >
158 :let item = mylist[0] " get the first item: 1
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000159 :let item = mylist[2] " get the third item: 3
Bram Moolenaar13065c42005-01-08 16:08:21 +0000160
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000161When the resulting item is a list this can be repeated: >
Bram Moolenaar13065c42005-01-08 16:08:21 +0000162 :let item = nestlist[0][1] " get the first list, second item: 12
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000163<
Bram Moolenaar13065c42005-01-08 16:08:21 +0000164A negative index is counted from the end. Index -1 refers to the last item in
165the List, -2 to the last but one item, etc. >
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000166 :let last = mylist[-1] " get the last item: "four"
167
Bram Moolenaar13065c42005-01-08 16:08:21 +0000168To avoid an error for an invalid index use the |get()| function. When an item
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000169is not available it returns zero or the default value you specify: >
Bram Moolenaar13065c42005-01-08 16:08:21 +0000170 :echo get(mylist, idx)
171 :echo get(mylist, idx, "NONE")
172
173
174List concatenation ~
175
176Two lists can be concatenated with the "+" operator: >
177 :let longlist = mylist + [5, 6]
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000178 :let mylist += [7, 8]
Bram Moolenaar13065c42005-01-08 16:08:21 +0000179
180To prepend or append an item turn the item into a list by putting [] around
181it. To change a list in-place see |list-modification| below.
182
183
184Sublist ~
185
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000186A part of the List can be obtained by specifying the first and last index,
187separated by a colon in square brackets: >
Bram Moolenaar13065c42005-01-08 16:08:21 +0000188 :let shortlist = mylist[2:-1] " get List [3, "four"]
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000189
190Omitting the first index is similar to zero. Omitting the last index is
191similar to -1. The difference is that there is no error if the items are not
192available. >
Bram Moolenaar540d6e32005-01-09 21:20:18 +0000193 :let endlist = mylist[2:] " from item 2 to the end: [3, "four"]
194 :let shortlist = mylist[2:2] " List with one item: [3]
195 :let otherlist = mylist[:] " make a copy of the List
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000196
Bram Moolenaard8b02732005-01-14 21:48:43 +0000197The second index can be just before the first index. In that case the result
198is an empty list. If the second index is lower, this results in an error. >
199 :echo mylist[2:1] " result: []
200 :echo mylist[2:0] " error!
201
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000202NOTE: mylist[s:e] means using the variable "s:e" as index. Watch out for
203using a single letter variable before the ":". Insert a space when needed:
204mylist[s : e].
205
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000206
Bram Moolenaar13065c42005-01-08 16:08:21 +0000207List identity ~
Bram Moolenaard8b02732005-01-14 21:48:43 +0000208 *list-identity*
Bram Moolenaar13065c42005-01-08 16:08:21 +0000209When variable "aa" is a list and you assign it to another variable "bb", both
210variables refer to the same list. Thus changing the list "aa" will also
211change "bb": >
212 :let aa = [1, 2, 3]
213 :let bb = aa
214 :call add(aa, 4)
215 :echo bb
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000216< [1, 2, 3, 4]
Bram Moolenaar13065c42005-01-08 16:08:21 +0000217
218Making a copy of a list is done with the |copy()| function. Using [:] also
219works, as explained above. This creates a shallow copy of the list: Changing
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000220a list item in the list will also change the item in the copied list: >
Bram Moolenaar13065c42005-01-08 16:08:21 +0000221 :let aa = [[1, 'a'], 2, 3]
222 :let bb = copy(aa)
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000223 :call add(aa, 4)
Bram Moolenaar13065c42005-01-08 16:08:21 +0000224 :let aa[0][1] = 'aaa'
225 :echo aa
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000226< [[1, aaa], 2, 3, 4] >
Bram Moolenaar13065c42005-01-08 16:08:21 +0000227 :echo bb
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000228< [[1, aaa], 2, 3]
Bram Moolenaar13065c42005-01-08 16:08:21 +0000229
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000230To make a completely independent list use |deepcopy()|. This also makes a
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000231copy of the values in the list, recursively. Up to a hundred levels deep.
Bram Moolenaar13065c42005-01-08 16:08:21 +0000232
233The operator "is" can be used to check if two variables refer to the same
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000234List. "isnot" does the opposite. In contrast "==" compares if two lists have
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000235the same value. >
236 :let alist = [1, 2, 3]
237 :let blist = [1, 2, 3]
238 :echo alist is blist
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000239< 0 >
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000240 :echo alist == blist
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000241< 1
Bram Moolenaar13065c42005-01-08 16:08:21 +0000242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000243Note about comparing lists: Two lists are considered equal if they have the
244same length and all items compare equal, as with using "==". There is one
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000245exception: When comparing a number with a string they are considered
246different. There is no automatic type conversion, as with using "==" on
247variables. Example: >
248 echo 4 == "4"
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000249< 1 >
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000250 echo [4] == ["4"]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000251< 0
252
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000253Thus comparing Lists is more strict than comparing numbers and strings. You
254can compare simple values this way too by putting them in a string: >
255
256 :let a = 5
257 :let b = "5"
258 echo a == b
259< 1 >
260 echo [a] == [b]
261< 0
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000262
Bram Moolenaar13065c42005-01-08 16:08:21 +0000263
264List unpack ~
265
266To unpack the items in a list to individual variables, put the variables in
267square brackets, like list items: >
268 :let [var1, var2] = mylist
269
270When the number of variables does not match the number of items in the list
271this produces an error. To handle any extra items from the list append ";"
272and a variable name: >
273 :let [var1, var2; rest] = mylist
274
275This works like: >
276 :let var1 = mylist[0]
277 :let var2 = mylist[1]
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000278 :let rest = mylist[2:]
Bram Moolenaar13065c42005-01-08 16:08:21 +0000279
280Except that there is no error if there are only two items. "rest" will be an
281empty list then.
282
283
284List modification ~
285 *list-modification*
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000286To change a specific item of a list use |:let| this way: >
Bram Moolenaar13065c42005-01-08 16:08:21 +0000287 :let list[4] = "four"
288 :let listlist[0][3] = item
289
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000290To change part of a list you can specify the first and last item to be
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000291modified. The value must at least have the number of items in the range: >
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000292 :let list[3:5] = [3, 4, 5]
293
Bram Moolenaar13065c42005-01-08 16:08:21 +0000294Adding and removing items from a list is done with functions. Here are a few
295examples: >
296 :call insert(list, 'a') " prepend item 'a'
297 :call insert(list, 'a', 3) " insert item 'a' before list[3]
298 :call add(list, "new") " append String item
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000299 :call add(list, [1, 2]) " append a List as one new item
Bram Moolenaar13065c42005-01-08 16:08:21 +0000300 :call extend(list, [1, 2]) " extend the list with two more items
301 :let i = remove(list, 3) " remove item 3
Bram Moolenaar9cd15162005-01-16 22:02:49 +0000302 :unlet list[3] " idem
Bram Moolenaar13065c42005-01-08 16:08:21 +0000303 :let l = remove(list, 3, -1) " remove items 3 to last item
Bram Moolenaar9cd15162005-01-16 22:02:49 +0000304 :unlet list[3 : ] " idem
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000305 :call filter(list, 'v:val !~ "x"') " remove items with an 'x'
Bram Moolenaar13065c42005-01-08 16:08:21 +0000306
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000307Changing the order of items in a list: >
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000308 :call sort(list) " sort a list alphabetically
309 :call reverse(list) " reverse the order of items
310
Bram Moolenaar13065c42005-01-08 16:08:21 +0000311
312For loop ~
313
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000314The |:for| loop executes commands for each item in a list. A variable is set
315to each item in the list in sequence. Example: >
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000316 :for item in mylist
317 : call Doit(item)
Bram Moolenaar13065c42005-01-08 16:08:21 +0000318 :endfor
319
320This works like: >
321 :let index = 0
322 :while index < len(mylist)
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000323 : let item = mylist[index]
324 : :call Doit(item)
Bram Moolenaar13065c42005-01-08 16:08:21 +0000325 : let index = index + 1
326 :endwhile
327
328Note that all items in the list should be of the same type, otherwise this
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000329results in error |E706|. To avoid this |:unlet| the variable at the end of
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000330the loop.
Bram Moolenaar13065c42005-01-08 16:08:21 +0000331
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000332If all you want to do is modify each item in the list then the |map()|
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000333function will be a simpler method than a for loop.
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000334
Bram Moolenaar13065c42005-01-08 16:08:21 +0000335Just like the |:let| command, |:for| also accepts a list of variables. This
336requires the argument to be a list of lists. >
337 :for [lnum, col] in [[1, 3], [2, 8], [3, 0]]
338 : call Doit(lnum, col)
339 :endfor
340
341This works like a |:let| command is done for each list item. Again, the types
342must remain the same to avoid an error.
343
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000344It is also possible to put remaining items in a List variable: >
Bram Moolenaar13065c42005-01-08 16:08:21 +0000345 :for [i, j; rest] in listlist
346 : call Doit(i, j)
347 : if !empty(rest)
348 : echo "remainder: " . string(rest)
349 : endif
350 :endfor
351
352
353List functions ~
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000354 *E714*
Bram Moolenaar13065c42005-01-08 16:08:21 +0000355Functions that are useful with a List: >
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000356 :let r = call(funcname, list) " call a function with an argument list
Bram Moolenaar13065c42005-01-08 16:08:21 +0000357 :if empty(list) " check if list is empty
Bram Moolenaar2fda12f2005-01-15 22:14:15 +0000358 :let l = len(list) " number of items in list
359 :let big = max(list) " maximum value in list
360 :let small = min(list) " minimum value in list
Bram Moolenaar9588a0f2005-01-08 21:45:39 +0000361 :let xs = count(list, 'x') " count nr of times 'x' appears in list
362 :let i = index(list, 'x') " index of first 'x' in list
Bram Moolenaar13065c42005-01-08 16:08:21 +0000363 :let lines = getline(1, 10) " get ten text lines from buffer
364 :call append('$', lines) " append text lines in buffer
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000365 :let list = split("a b c") " create list from items in a string
366 :let string = join(list, ', ') " create string from list items
Bram Moolenaar2fda12f2005-01-15 22:14:15 +0000367 :let s = string(list) " String representation of list
368 :call map(list, '">> " . v:val') " prepend ">> " to each item
Bram Moolenaar13065c42005-01-08 16:08:21 +0000369
Bram Moolenaar0cb032e2005-04-23 20:52:00 +0000370Don't forget that a combination of features can make things simple. For
371example, to add up all the numbers in a list: >
372 :exe 'let sum = ' . join(nrlist, '+')
373
Bram Moolenaar13065c42005-01-08 16:08:21 +0000374
Bram Moolenaard8b02732005-01-14 21:48:43 +00003751.4 Dictionaries ~
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000376 *Dictionaries* *Dictionary*
Bram Moolenaard8b02732005-01-14 21:48:43 +0000377A Dictionary is an associative array: Each entry has a key and a value. The
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000378entry can be located with the key. The entries are stored without a specific
379ordering.
Bram Moolenaard8b02732005-01-14 21:48:43 +0000380
381
382Dictionary creation ~
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000383 *E720* *E721* *E722* *E723*
Bram Moolenaard8b02732005-01-14 21:48:43 +0000384A Dictionary is created with a comma separated list of entries in curly
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000385braces. Each entry has a key and a value, separated by a colon. Each key can
386only appear once. Examples: >
Bram Moolenaard8b02732005-01-14 21:48:43 +0000387 :let mydict = {1: 'one', 2: 'two', 3: 'three'}
388 :let emptydict = {}
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000389< *E713* *E716* *E717*
Bram Moolenaard8b02732005-01-14 21:48:43 +0000390A key is always a String. You can use a Number, it will be converted to a
391String automatically. Thus the String '4' and the number 4 will find the same
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000392entry. Note that the String '04' and the Number 04 are different, since the
393Number will be converted to the String '4'.
Bram Moolenaard8b02732005-01-14 21:48:43 +0000394
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000395A value can be any expression. Using a Dictionary for a value creates a
Bram Moolenaard8b02732005-01-14 21:48:43 +0000396nested Dictionary: >
397 :let nestdict = {1: {11: 'a', 12: 'b'}, 2: {21: 'c'}}
398
399An extra comma after the last entry is ignored.
400
401
402Accessing entries ~
403
404The normal way to access an entry is by putting the key in square brackets: >
405 :let val = mydict["one"]
406 :let mydict["four"] = 4
407
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000408You can add new entries to an existing Dictionary this way, unlike Lists.
Bram Moolenaard8b02732005-01-14 21:48:43 +0000409
410For keys that consist entirely of letters, digits and underscore the following
411form can be used |expr-entry|: >
412 :let val = mydict.one
413 :let mydict.four = 4
414
415Since an entry can be any type, also a List and a Dictionary, the indexing and
416key lookup can be repeated: >
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000417 :echo dict.key[idx].key
Bram Moolenaard8b02732005-01-14 21:48:43 +0000418
419
420Dictionary to List conversion ~
421
422You may want to loop over the entries in a dictionary. For this you need to
423turn the Dictionary into a List and pass it to |:for|.
424
425Most often you want to loop over the keys, using the |keys()| function: >
426 :for key in keys(mydict)
427 : echo key . ': ' . mydict[key]
428 :endfor
429
430The List of keys is unsorted. You may want to sort them first: >
431 :for key in sort(keys(mydict))
432
433To loop over the values use the |values()| function: >
434 :for v in values(mydict)
435 : echo "value: " . v
436 :endfor
437
438If you want both the key and the value use the |items()| function. It returns
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000439a List in which each item is a List with two items, the key and the value: >
Bram Moolenaard8b02732005-01-14 21:48:43 +0000440 :for entry in items(mydict)
441 : echo entry[0] . ': ' . entry[1]
442 :endfor
443
444
445Dictionary identity ~
Bram Moolenaar7c626922005-02-07 22:01:03 +0000446 *dict-identity*
Bram Moolenaard8b02732005-01-14 21:48:43 +0000447Just like Lists you need to use |copy()| and |deepcopy()| to make a copy of a
448Dictionary. Otherwise, assignment results in referring to the same
449Dictionary: >
450 :let onedict = {'a': 1, 'b': 2}
451 :let adict = onedict
452 :let adict['a'] = 11
453 :echo onedict['a']
454 11
455
Bram Moolenaarf3bd51a2005-06-14 22:11:18 +0000456Two Dictionaries compare equal if all the key-value pairs compare equal. For
457more info see |list-identity|.
Bram Moolenaard8b02732005-01-14 21:48:43 +0000458
459
460Dictionary modification ~
461 *dict-modification*
462To change an already existing entry of a Dictionary, or to add a new entry,
463use |:let| this way: >
464 :let dict[4] = "four"
465 :let dict['one'] = item
466
Bram Moolenaar9cd15162005-01-16 22:02:49 +0000467Removing an entry from a Dictionary is done with |remove()| or |:unlet|.
468Three ways to remove the entry with key "aaa" from dict: >
469 :let i = remove(dict, 'aaa')
470 :unlet dict.aaa
471 :unlet dict['aaa']
Bram Moolenaard8b02732005-01-14 21:48:43 +0000472
473Merging a Dictionary with another is done with |extend()|: >
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000474 :call extend(adict, bdict)
475This extends adict with all entries from bdict. Duplicate keys cause entries
476in adict to be overwritten. An optional third argument can change this.
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000477Note that the order of entries in a Dictionary is irrelevant, thus don't
478expect ":echo adict" to show the items from bdict after the older entries in
479adict.
Bram Moolenaard8b02732005-01-14 21:48:43 +0000480
481Weeding out entries from a Dictionary can be done with |filter()|: >
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000482 :call filter(dict 'v:val =~ "x"')
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000483This removes all entries from "dict" with a value not matching 'x'.
Bram Moolenaar2fda12f2005-01-15 22:14:15 +0000484
485
486Dictionary function ~
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000487 *Dictionary-function* *self* *E725*
Bram Moolenaar2fda12f2005-01-15 22:14:15 +0000488When a function is defined with the "dict" attribute it can be used in a
489special way with a dictionary. Example: >
490 :function Mylen() dict
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000491 : return len(self.data)
Bram Moolenaar2fda12f2005-01-15 22:14:15 +0000492 :endfunction
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000493 :let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")}
494 :echo mydict.len()
Bram Moolenaar2fda12f2005-01-15 22:14:15 +0000495
496This is like a method in object oriented programming. The entry in the
497Dictionary is a |Funcref|. The local variable "self" refers to the dictionary
498the function was invoked from.
499
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000500It is also possible to add a function without the "dict" attribute as a
501Funcref to a Dictionary, but the "self" variable is not available then.
502
503 *numbered-function*
Bram Moolenaar2fda12f2005-01-15 22:14:15 +0000504To avoid the extra name for the function it can be defined and directly
505assigned to a Dictionary in this way: >
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000506 :let mydict = {'data': [0, 1, 2, 3]}
507 :function mydict.len() dict
508 : return len(self.data)
Bram Moolenaar2fda12f2005-01-15 22:14:15 +0000509 :endfunction
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000510 :echo mydict.len()
Bram Moolenaar2fda12f2005-01-15 22:14:15 +0000511
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000512The function will then get a number and the value of dict.len is a |Funcref|
513that references this function. The function can only be used through a
514|Funcref|. It will automatically be deleted when there is no |Funcref|
515remaining that refers to it.
516
517It is not necessary to use the "dict" attribute for a numbered function.
Bram Moolenaar2fda12f2005-01-15 22:14:15 +0000518
519
520Functions for Dictionaries ~
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000521 *E715*
522Functions that can be used with a Dictionary: >
Bram Moolenaar2fda12f2005-01-15 22:14:15 +0000523 :if has_key(dict, 'foo') " TRUE if dict has entry with key "foo"
524 :if empty(dict) " TRUE if dict is empty
525 :let l = len(dict) " number of items in dict
526 :let big = max(dict) " maximum value in dict
527 :let small = min(dict) " minimum value in dict
528 :let xs = count(dict, 'x') " count nr of times 'x' appears in dict
529 :let s = string(dict) " String representation of dict
530 :call map(dict, '">> " . v:val') " prepend ">> " to each item
Bram Moolenaard8b02732005-01-14 21:48:43 +0000531
532
5331.5 More about variables ~
Bram Moolenaar13065c42005-01-08 16:08:21 +0000534 *more-variables*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535If you need to know the type of a variable or expression, use the |type()|
536function.
537
538When the '!' flag is included in the 'viminfo' option, global variables that
539start with an uppercase letter, and don't contain a lowercase letter, are
540stored in the viminfo file |viminfo-file|.
541
542When the 'sessionoptions' option contains "global", global variables that
543start with an uppercase letter and contain at least one lowercase letter are
544stored in the session file |session-file|.
545
546variable name can be stored where ~
547my_var_6 not
548My_Var_6 session file
549MY_VAR_6 viminfo file
550
551
552It's possible to form a variable name with curly braces, see
553|curly-braces-names|.
554
555==============================================================================
5562. Expression syntax *expression-syntax*
557
558Expression syntax summary, from least to most significant:
559
560|expr1| expr2 ? expr1 : expr1 if-then-else
561
562|expr2| expr3 || expr3 .. logical OR
563
564|expr3| expr4 && expr4 .. logical AND
565
566|expr4| expr5 == expr5 equal
567 expr5 != expr5 not equal
568 expr5 > expr5 greater than
569 expr5 >= expr5 greater than or equal
570 expr5 < expr5 smaller than
571 expr5 <= expr5 smaller than or equal
572 expr5 =~ expr5 regexp matches
573 expr5 !~ expr5 regexp doesn't match
574
575 expr5 ==? expr5 equal, ignoring case
576 expr5 ==# expr5 equal, match case
577 etc. As above, append ? for ignoring case, # for
578 matching case
579
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000580 expr5 is expr5 same |List| instance
581 expr5 isnot expr5 different |List| instance
Bram Moolenaarde8866b2005-01-06 23:24:37 +0000582
583|expr5| expr6 + expr6 .. number addition or list concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 expr6 - expr6 .. number subtraction
585 expr6 . expr6 .. string concatenation
586
587|expr6| expr7 * expr7 .. number multiplication
588 expr7 / expr7 .. number division
589 expr7 % expr7 .. number modulo
590
591|expr7| ! expr7 logical NOT
592 - expr7 unary minus
593 + expr7 unary plus
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000596|expr8| expr8[expr1] byte of a String or item of a |List|
597 expr8[expr1 : expr1] substring of a String or sublist of a |List|
598 expr8.name entry in a |Dictionary|
599 expr8(expr1, ...) function call with |Funcref| variable
Bram Moolenaar2fda12f2005-01-15 22:14:15 +0000600
601|expr9| number number constant
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000602 "string" string constant, backslash is special
Bram Moolenaard8b02732005-01-14 21:48:43 +0000603 'string' string constant, ' is doubled
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000604 [expr1, ...] |List|
605 {expr1: expr1, ...} |Dictionary|
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 &option option value
607 (expr1) nested expression
608 variable internal variable
609 va{ria}ble internal variable with curly braces
610 $VAR environment variable
611 @r contents of register 'r'
612 function(expr1, ...) function call
613 func{ti}on(expr1, ...) function call with curly braces
614
615
616".." indicates that the operations in this level can be concatenated.
617Example: >
618 &nu || &list && &shell == "csh"
619
620All expressions within one level are parsed from left to right.
621
622
623expr1 *expr1* *E109*
624-----
625
626expr2 ? expr1 : expr1
627
628The expression before the '?' is evaluated to a number. If it evaluates to
629non-zero, the result is the value of the expression between the '?' and ':',
630otherwise the result is the value of the expression after the ':'.
631Example: >
632 :echo lnum == 1 ? "top" : lnum
633
634Since the first expression is an "expr2", it cannot contain another ?:. The
635other two expressions can, thus allow for recursive use of ?:.
636Example: >
637 :echo lnum == 1 ? "top" : lnum == 1000 ? "last" : lnum
638
639To keep this readable, using |line-continuation| is suggested: >
640 :echo lnum == 1
641 :\ ? "top"
642 :\ : lnum == 1000
643 :\ ? "last"
644 :\ : lnum
645
646
647expr2 and expr3 *expr2* *expr3*
648---------------
649
650 *expr-barbar* *expr-&&*
651The "||" and "&&" operators take one argument on each side. The arguments
652are (converted to) Numbers. The result is:
653
654 input output ~
655n1 n2 n1 || n2 n1 && n2 ~
656zero zero zero zero
657zero non-zero non-zero zero
658non-zero zero non-zero zero
659non-zero non-zero non-zero non-zero
660
661The operators can be concatenated, for example: >
662
663 &nu || &list && &shell == "csh"
664
665Note that "&&" takes precedence over "||", so this has the meaning of: >
666
667 &nu || (&list && &shell == "csh")
668
669Once the result is known, the expression "short-circuits", that is, further
670arguments are not evaluated. This is like what happens in C. For example: >
671
672 let a = 1
673 echo a || b
674
675This is valid even if there is no variable called "b" because "a" is non-zero,
676so the result must be non-zero. Similarly below: >
677
678 echo exists("b") && b == "yes"
679
680This is valid whether "b" has been defined or not. The second clause will
681only be evaluated if "b" has been defined.
682
683
684expr4 *expr4*
685-----
686
687expr5 {cmp} expr5
688
689Compare two expr5 expressions, resulting in a 0 if it evaluates to false, or 1
690if it evaluates to true.
691
692 *expr-==* *expr-!=* *expr->* *expr->=*
693 *expr-<* *expr-<=* *expr-=~* *expr-!~*
694 *expr-==#* *expr-!=#* *expr->#* *expr->=#*
695 *expr-<#* *expr-<=#* *expr-=~#* *expr-!~#*
696 *expr-==?* *expr-!=?* *expr->?* *expr->=?*
697 *expr-<?* *expr-<=?* *expr-=~?* *expr-!~?*
Bram Moolenaarde8866b2005-01-06 23:24:37 +0000698 *expr-is*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 use 'ignorecase' match case ignore case ~
700equal == ==# ==?
701not equal != !=# !=?
702greater than > ># >?
703greater than or equal >= >=# >=?
704smaller than < <# <?
705smaller than or equal <= <=# <=?
706regexp matches =~ =~# =~?
707regexp doesn't match !~ !~# !~?
Bram Moolenaarde8866b2005-01-06 23:24:37 +0000708same instance is
709different instance isnot
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710
711Examples:
712"abc" ==# "Abc" evaluates to 0
713"abc" ==? "Abc" evaluates to 1
714"abc" == "Abc" evaluates to 1 if 'ignorecase' is set, 0 otherwise
715
Bram Moolenaar13065c42005-01-08 16:08:21 +0000716 *E691* *E692*
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000717A |List| can only be compared with a |List| and only "equal", "not equal" and
718"is" can be used. This compares the values of the list, recursively.
719Ignoring case means case is ignored when comparing item values.
Bram Moolenaarde8866b2005-01-06 23:24:37 +0000720
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000721 *E735* *E736*
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000722A |Dictionary| can only be compared with a |Dictionary| and only "equal", "not
723equal" and "is" can be used. This compares the key/values of the |Dictionary|
Bram Moolenaar3a3a7232005-01-17 22:16:15 +0000724recursively. Ignoring case means case is ignored when comparing item values.
725
Bram Moolenaar13065c42005-01-08 16:08:21 +0000726 *E693* *E694*
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000727A |Funcref| can only be compared with a |Funcref| and only "equal" and "not
728equal" can be used. Case is never ignored.
Bram Moolenaarde8866b2005-01-06 23:24:37 +0000729
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000730When using "is" or "isnot" with a |List| this checks if the expressions are
731referring to the same |List| instance. A copy of a |List| is different from
732the original |List|. When using "is" without a |List| it is equivalent to
733using "equal", using "isnot" equivalent to using "not equal". Except that a
Bram Moolenaarde8866b2005-01-06 23:24:37 +0000734different type means the values are different. "4 == '4'" is true, "4 is '4'"
735is false.
736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737When comparing a String with a Number, the String is converted to a Number,
738and the comparison is done on Numbers. This means that "0 == 'x'" is TRUE,
739because 'x' converted to a Number is zero.
740
741When comparing two Strings, this is done with strcmp() or stricmp(). This
742results in the mathematical difference (comparing byte values), not
743necessarily the alphabetical difference in the local language.
744
745When using the operators with a trailing '#", or the short version and
746'ignorecase' is off, the comparing is done with strcmp().
747
748When using the operators with a trailing '?', or the short version and
749'ignorecase' is set, the comparing is done with stricmp().
750
751The "=~" and "!~" operators match the lefthand argument with the righthand
752argument, which is used as a pattern. See |pattern| for what a pattern is.
753This matching is always done like 'magic' was set and 'cpoptions' is empty, no
754matter what the actual value of 'magic' or 'cpoptions' is. This makes scripts
755portable. To avoid backslashes in the regexp pattern to be doubled, use a
756single-quote string, see |literal-string|.
757Since a string is considered to be a single line, a multi-line pattern
758(containing \n, backslash-n) will not match. However, a literal NL character
759can be matched like an ordinary character. Examples:
760 "foo\nbar" =~ "\n" evaluates to 1
761 "foo\nbar" =~ "\\n" evaluates to 0
762
763
764expr5 and expr6 *expr5* *expr6*
765---------------
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000766expr6 + expr6 .. Number addition or |List| concatenation *expr-+*
Bram Moolenaarde8866b2005-01-06 23:24:37 +0000767expr6 - expr6 .. Number subtraction *expr--*
768expr6 . expr6 .. String concatenation *expr-.*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000770For |Lists| only "+" is possible and then both expr6 must be a list. The
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000771result is a new list with the two lists Concatenated.
Bram Moolenaarde8866b2005-01-06 23:24:37 +0000772
773expr7 * expr7 .. number multiplication *expr-star*
774expr7 / expr7 .. number division *expr-/*
775expr7 % expr7 .. number modulo *expr-%*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776
777For all, except ".", Strings are converted to Numbers.
778
779Note the difference between "+" and ".":
780 "123" + "456" = 579
781 "123" . "456" = "123456"
782
783When the righthand side of '/' is zero, the result is 0x7fffffff.
784When the righthand side of '%' is zero, the result is 0.
785
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000786None of these work for |Funcref|s.
Bram Moolenaarde8866b2005-01-06 23:24:37 +0000787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
789expr7 *expr7*
790-----
791! expr7 logical NOT *expr-!*
792- expr7 unary minus *expr-unary--*
793+ expr7 unary plus *expr-unary-+*
794
795For '!' non-zero becomes zero, zero becomes one.
796For '-' the sign of the number is changed.
797For '+' the number is unchanged.
798
799A String will be converted to a Number first.
800
801These three can be repeated and mixed. Examples:
802 !-1 == 0
803 !!8 == 1
804 --9 == 9
805
806
807expr8 *expr8*
808-----
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000809expr8[expr1] item of String or |List| *expr-[]* *E111*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
Bram Moolenaar2fda12f2005-01-15 22:14:15 +0000811If expr8 is a Number or String this results in a String that contains the
812expr1'th single byte from expr8. expr8 is used as a String, expr1 as a
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000813Number. Note that this doesn't recognize multi-byte encodings.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000815Index zero gives the first character. This is like it works in C. Careful:
816text column numbers start with one! Example, to get the character under the
817cursor: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 :let c = getline(line("."))[col(".") - 1]
819
820If the length of the String is less than the index, the result is an empty
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000821String. A negative index always results in an empty string (reason: backwards
822compatibility). Use [-1:] to get the last byte.
823
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000824If expr8 is a |List| then it results the item at index expr1. See |list-index|
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000825for possible index values. If the index is out of range this results in an
826error. Example: >
827 :let item = mylist[-1] " get last item
828
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000829Generally, if a |List| index is equal to or higher than the length of the
830|List|, or more negative than the length of the |List|, this results in an
831error.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000832
Bram Moolenaard8b02732005-01-14 21:48:43 +0000833
Bram Moolenaar2fda12f2005-01-15 22:14:15 +0000834expr8[expr1a : expr1b] substring or sublist *expr-[:]*
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000835
Bram Moolenaar2fda12f2005-01-15 22:14:15 +0000836If expr8 is a Number or String this results in the substring with the bytes
837from expr1a to and including expr1b. expr8 is used as a String, expr1a and
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000838expr1b are used as a Number. Note that this doesn't recognize multi-byte
839encodings.
840
841If expr1a is omitted zero is used. If expr1b is omitted the length of the
842string minus one is used.
843
844A negative number can be used to measure from the end of the string. -1 is
845the last character, -2 the last but one, etc.
846
847If an index goes out of range for the string characters are omitted. If
848expr1b is smaller than expr1a the result is an empty string.
849
850Examples: >
851 :let c = name[-1:] " last byte of a string
852 :let c = name[-2:-2] " last but one byte of a string
853 :let s = line(".")[4:] " from the fifth byte to the end
854 :let s = s[:-3] " remove last two bytes
855
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000856If expr8 is a |List| this results in a new |List| with the items indicated by
857the indexes expr1a and expr1b. This works like with a String, as explained
858just above, except that indexes out of range cause an error. Examples: >
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +0000859 :let l = mylist[:3] " first four items
860 :let l = mylist[4:4] " List with one item
861 :let l = mylist[:] " shallow copy of a List
862
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000863Using expr8[expr1] or expr8[expr1a : expr1b] on a |Funcref| results in an
864error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
Bram Moolenaard8b02732005-01-14 21:48:43 +0000866
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000867expr8.name entry in a |Dictionary| *expr-entry*
Bram Moolenaard8b02732005-01-14 21:48:43 +0000868
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000869If expr8 is a |Dictionary| and it is followed by a dot, then the following
870name will be used as a key in the |Dictionary|. This is just like:
871expr8[name].
Bram Moolenaard8b02732005-01-14 21:48:43 +0000872
873The name must consist of alphanumeric characters, just like a variable name,
874but it may start with a number. Curly braces cannot be used.
875
876There must not be white space before or after the dot.
877
878Examples: >
879 :let dict = {"one": 1, 2: "two"}
880 :echo dict.one
881 :echo dict .2
882
883Note that the dot is also used for String concatenation. To avoid confusion
884always put spaces around the dot for String concatenation.
885
886
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000887expr8(expr1, ...) |Funcref| function call
Bram Moolenaar2fda12f2005-01-15 22:14:15 +0000888
889When expr8 is a |Funcref| type variable, invoke the function it refers to.
890
891
892
893 *expr9*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894number
895------
896number number constant *expr-number*
897
898Decimal, Hexadecimal (starting with 0x or 0X), or Octal (starting with 0).
899
900
901string *expr-string* *E114*
902------
903"string" string constant *expr-quote*
904
905Note that double quotes are used.
906
907A string constant accepts these special characters:
908\... three-digit octal number (e.g., "\316")
909\.. two-digit octal number (must be followed by non-digit)
910\. one-digit octal number (must be followed by non-digit)
911\x.. byte specified with two hex numbers (e.g., "\x1f")
912\x. byte specified with one hex number (must be followed by non-hex char)
913\X.. same as \x..
914\X. same as \x.
915\u.... character specified with up to 4 hex numbers, stored according to the
916 current value of 'encoding' (e.g., "\u02a4")
917\U.... same as \u....
918\b backspace <BS>
919\e escape <Esc>
920\f formfeed <FF>
921\n newline <NL>
922\r return <CR>
923\t tab <Tab>
924\\ backslash
925\" double quote
926\<xxx> Special key named "xxx". e.g. "\<C-W>" for CTRL-W.
927
928Note that "\000" and "\x00" force the end of the string.
929
930
931literal-string *literal-string* *E115*
932---------------
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000933'string' string constant *expr-'*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
935Note that single quotes are used.
936
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000937This string is taken as it is. No backslashes are removed or have a special
Bram Moolenaard8b02732005-01-14 21:48:43 +0000938meaning. The only exception is that two quotes stand for one quote.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000939
940Single quoted strings are useful for patterns, so that backslashes do not need
941to be doubled. These two commands are equivalent: >
942 if a =~ "\\s*"
943 if a =~ '\s*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944
945
946option *expr-option* *E112* *E113*
947------
948&option option value, local value if possible
949&g:option global option value
950&l:option local option value
951
952Examples: >
953 echo "tabstop is " . &tabstop
954 if &insertmode
955
956Any option name can be used here. See |options|. When using the local value
957and there is no buffer-local or window-local value, the global value is used
958anyway.
959
960
961register *expr-register*
962--------
963@r contents of register 'r'
964
965The result is the contents of the named register, as a single string.
966Newlines are inserted where required. To get the contents of the unnamed
Bram Moolenaare7566042005-06-17 22:00:15 +0000967register use @" or @@. See |registers| for an explanation of the available
968registers.
969
970When using the '=' register you get the expression itself, not what it
971evaluates to. Use |eval()| to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972
973
974nesting *expr-nesting* *E110*
975-------
976(expr1) nested expression
977
978
979environment variable *expr-env*
980--------------------
981$VAR environment variable
982
983The String value of any environment variable. When it is not defined, the
984result is an empty string.
985 *expr-env-expand*
986Note that there is a difference between using $VAR directly and using
987expand("$VAR"). Using it directly will only expand environment variables that
988are known inside the current Vim session. Using expand() will first try using
989the environment variables known inside the current Vim session. If that
990fails, a shell will be used to expand the variable. This can be slow, but it
991does expand all variables that the shell knows about. Example: >
992 :echo $version
993 :echo expand("$version")
994The first one probably doesn't echo anything, the second echoes the $version
995variable (if your shell supports it).
996
997
998internal variable *expr-variable*
999-----------------
1000variable internal variable
1001See below |internal-variables|.
1002
1003
Bram Moolenaar05159a02005-02-26 23:04:13 +00001004function call *expr-function* *E116* *E118* *E119* *E120*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005-------------
1006function(expr1, ...) function call
1007See below |functions|.
1008
1009
1010==============================================================================
10113. Internal variable *internal-variables* *E121*
1012 *E461*
1013An internal variable name can be made up of letters, digits and '_'. But it
1014cannot start with a digit. It's also possible to use curly braces, see
1015|curly-braces-names|.
1016
1017An internal variable is created with the ":let" command |:let|.
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00001018An internal variable is explicitly destroyed with the ":unlet" command
1019|:unlet|.
1020Using a name that is not an internal variable or refers to a variable that has
1021been destroyed results in an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022
1023There are several name spaces for variables. Which one is to be used is
1024specified by what is prepended:
1025
1026 (nothing) In a function: local to a function; otherwise: global
1027|buffer-variable| b: Local to the current buffer.
1028|window-variable| w: Local to the current window.
1029|global-variable| g: Global.
1030|local-variable| l: Local to a function.
1031|script-variable| s: Local to a |:source|'ed Vim script.
1032|function-argument| a: Function argument (only inside a function).
1033|vim-variable| v: Global, predefined by Vim.
1034
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001035The scope name by itself can be used as a |Dictionary|. For example, to
1036delete all script-local variables: >
Bram Moolenaar8f999f12005-01-25 22:12:55 +00001037 :for k in keys(s:)
1038 : unlet s:[k]
1039 :endfor
1040<
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 *buffer-variable* *b:var*
1042A variable name that is preceded with "b:" is local to the current buffer.
1043Thus you can have several "b:foo" variables, one for each buffer.
1044This kind of variable is deleted when the buffer is wiped out or deleted with
1045|:bdelete|.
1046
1047One local buffer variable is predefined:
1048 *b:changedtick-variable* *changetick*
1049b:changedtick The total number of changes to the current buffer. It is
1050 incremented for each change. An undo command is also a change
1051 in this case. This can be used to perform an action only when
1052 the buffer has changed. Example: >
1053 :if my_changedtick != b:changedtick
1054 : let my_changedtick = b:changedtick
1055 : call My_Update()
1056 :endif
1057<
1058 *window-variable* *w:var*
1059A variable name that is preceded with "w:" is local to the current window. It
1060is deleted when the window is closed.
1061
1062 *global-variable* *g:var*
1063Inside functions global variables are accessed with "g:". Omitting this will
1064access a variable local to a function. But "g:" can also be used in any other
1065place if you like.
1066
1067 *local-variable* *l:var*
1068Inside functions local variables are accessed without prepending anything.
1069But you can also prepend "l:" if you like.
1070
1071 *script-variable* *s:var*
1072In a Vim script variables starting with "s:" can be used. They cannot be
1073accessed from outside of the scripts, thus are local to the script.
1074
1075They can be used in:
1076- commands executed while the script is sourced
1077- functions defined in the script
1078- autocommands defined in the script
1079- functions and autocommands defined in functions and autocommands which were
1080 defined in the script (recursively)
1081- user defined commands defined in the script
1082Thus not in:
1083- other scripts sourced from this one
1084- mappings
1085- etc.
1086
1087script variables can be used to avoid conflicts with global variable names.
1088Take this example:
1089
1090 let s:counter = 0
1091 function MyCounter()
1092 let s:counter = s:counter + 1
1093 echo s:counter
1094 endfunction
1095 command Tick call MyCounter()
1096
1097You can now invoke "Tick" from any script, and the "s:counter" variable in
1098that script will not be changed, only the "s:counter" in the script where
1099"Tick" was defined is used.
1100
1101Another example that does the same: >
1102
1103 let s:counter = 0
1104 command Tick let s:counter = s:counter + 1 | echo s:counter
1105
1106When calling a function and invoking a user-defined command, the context for
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001107script variables is set to the script where the function or command was
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108defined.
1109
1110The script variables are also available when a function is defined inside a
1111function that is defined in a script. Example: >
1112
1113 let s:counter = 0
1114 function StartCounting(incr)
1115 if a:incr
1116 function MyCounter()
1117 let s:counter = s:counter + 1
1118 endfunction
1119 else
1120 function MyCounter()
1121 let s:counter = s:counter - 1
1122 endfunction
1123 endif
1124 endfunction
1125
1126This defines the MyCounter() function either for counting up or counting down
1127when calling StartCounting(). It doesn't matter from where StartCounting() is
1128called, the s:counter variable will be accessible in MyCounter().
1129
1130When the same script is sourced again it will use the same script variables.
1131They will remain valid as long as Vim is running. This can be used to
1132maintain a counter: >
1133
1134 if !exists("s:counter")
1135 let s:counter = 1
1136 echo "script executed for the first time"
1137 else
1138 let s:counter = s:counter + 1
1139 echo "script executed " . s:counter . " times now"
1140 endif
1141
1142Note that this means that filetype plugins don't get a different set of script
1143variables for each buffer. Use local buffer variables instead |b:var|.
1144
1145
1146Predefined Vim variables: *vim-variable* *v:var*
1147
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00001148 *v:beval_col* *beval_col-variable*
1149v:beval_col The number of the column, over which the mouse pointer is.
1150 This is the byte index in the |v:beval_lnum| line.
1151 Only valid while evaluating the 'balloonexpr' option.
1152
1153 *v:beval_bufnr* *beval_bufnr-variable*
1154v:beval_bufnr The number of the buffer, over which the mouse pointer is. Only
1155 valid while evaluating the 'balloonexpr' option.
1156
1157 *v:beval_lnum* *beval_lnum-variable*
1158v:beval_lnum The number of the line, over which the mouse pointer is. Only
1159 valid while evaluating the 'balloonexpr' option.
1160
1161 *v:beval_text* *beval_text-variable*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001162v:beval_text The text under or after the mouse pointer. Usually a word as
1163 it is useful for debugging a C program. 'iskeyword' applies,
1164 but a dot and "->" before the position is included. When on a
1165 ']' the text before it is used, including the matching '[' and
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00001166 word before it. When on a Visual area within one line the
1167 highlighted text is used.
1168 Only valid while evaluating the 'balloonexpr' option.
1169
1170 *v:beval_winnr* *beval_winnr-variable*
1171v:beval_winnr The number of the window, over which the mouse pointer is. Only
1172 valid while evaluating the 'balloonexpr' option.
1173
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 *v:charconvert_from* *charconvert_from-variable*
1175v:charconvert_from
1176 The name of the character encoding of a file to be converted.
1177 Only valid while evaluating the 'charconvert' option.
1178
1179 *v:charconvert_to* *charconvert_to-variable*
1180v:charconvert_to
1181 The name of the character encoding of a file after conversion.
1182 Only valid while evaluating the 'charconvert' option.
1183
1184 *v:cmdarg* *cmdarg-variable*
1185v:cmdarg This variable is used for two purposes:
1186 1. The extra arguments given to a file read/write command.
1187 Currently these are "++enc=" and "++ff=". This variable is
1188 set before an autocommand event for a file read/write
1189 command is triggered. There is a leading space to make it
1190 possible to append this variable directly after the
1191 read/write command. Note: The "+cmd" argument isn't
1192 included here, because it will be executed anyway.
1193 2. When printing a PostScript file with ":hardcopy" this is
1194 the argument for the ":hardcopy" command. This can be used
1195 in 'printexpr'.
1196
1197 *v:cmdbang* *cmdbang-variable*
1198v:cmdbang Set like v:cmdarg for a file read/write command. When a "!"
1199 was used the value is 1, otherwise it is 0. Note that this
1200 can only be used in autocommands. For user commands |<bang>|
1201 can be used.
1202
1203 *v:count* *count-variable*
1204v:count The count given for the last Normal mode command. Can be used
1205 to get the count before a mapping. Read-only. Example: >
1206 :map _x :<C-U>echo "the count is " . v:count<CR>
1207< Note: The <C-U> is required to remove the line range that you
1208 get when typing ':' after a count.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001209 Also used for evaluating the 'formatexpr' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 "count" also works, for backwards compatibility.
1211
1212 *v:count1* *count1-variable*
1213v:count1 Just like "v:count", but defaults to one when no count is
1214 used.
1215
1216 *v:ctype* *ctype-variable*
1217v:ctype The current locale setting for characters of the runtime
1218 environment. This allows Vim scripts to be aware of the
1219 current locale encoding. Technical: it's the value of
1220 LC_CTYPE. When not using a locale the value is "C".
1221 This variable can not be set directly, use the |:language|
1222 command.
1223 See |multi-lang|.
1224
1225 *v:dying* *dying-variable*
1226v:dying Normally zero. When a deadly signal is caught it's set to
1227 one. When multiple signals are caught the number increases.
1228 Can be used in an autocommand to check if Vim didn't
1229 terminate normally. {only works on Unix}
1230 Example: >
1231 :au VimLeave * if v:dying | echo "\nAAAAaaaarrrggghhhh!!!\n" | endif
1232<
1233 *v:errmsg* *errmsg-variable*
1234v:errmsg Last given error message. It's allowed to set this variable.
1235 Example: >
1236 :let v:errmsg = ""
1237 :silent! next
1238 :if v:errmsg != ""
1239 : ... handle error
1240< "errmsg" also works, for backwards compatibility.
1241
1242 *v:exception* *exception-variable*
1243v:exception The value of the exception most recently caught and not
1244 finished. See also |v:throwpoint| and |throw-variables|.
1245 Example: >
1246 :try
1247 : throw "oops"
1248 :catch /.*/
1249 : echo "caught" v:exception
1250 :endtry
1251< Output: "caught oops".
1252
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001253 *v:fcs_reason* *fcs_reason-variable*
1254v:fcs_reason The reason why the |FileChangedShell| event was triggered.
1255 Can be used in an autocommand to decide what to do and/or what
1256 to set v:fcs_choice to. Possible values:
1257 deleted file no longer exists
1258 conflict file contents, mode or timestamp was
1259 changed and buffer is modified
1260 changed file contents has changed
1261 mode mode of file changed
1262 time only file timestamp changed
1263
1264 *v:fcs_choice* *fcs_choice-variable*
1265v:fcs_choice What should happen after a |FileChangedShell| event was
1266 triggered. Can be used in an autocommand to tell Vim what to
1267 do with the affected buffer:
1268 reload Reload the buffer (does not work if
1269 the file was deleted).
1270 ask Ask the user what to do, as if there
1271 was no autocommand. Except that when
1272 only the timestamp changed nothing
1273 will happen.
1274 <empty> Nothing, the autocommand should do
1275 everything that needs to be done.
1276 The default is empty. If another (invalid) value is used then
1277 Vim behaves like it is empty, there is no warning message.
1278
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 *v:fname_in* *fname_in-variable*
Bram Moolenaar4e330bb2005-12-07 21:04:31 +00001280v:fname_in The name of the input file. Valid while evaluating:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 option used for ~
1282 'charconvert' file to be converted
1283 'diffexpr' original file
1284 'patchexpr' original file
1285 'printexpr' file to be printed
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00001286 And set to the swap file name for |SwapExists|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287
1288 *v:fname_out* *fname_out-variable*
1289v:fname_out The name of the output file. Only valid while
1290 evaluating:
1291 option used for ~
1292 'charconvert' resulting converted file (*)
1293 'diffexpr' output of diff
1294 'patchexpr' resulting patched file
1295 (*) When doing conversion for a write command (e.g., ":w
1296 file") it will be equal to v:fname_in. When doing conversion
1297 for a read command (e.g., ":e file") it will be a temporary
1298 file and different from v:fname_in.
1299
1300 *v:fname_new* *fname_new-variable*
1301v:fname_new The name of the new version of the file. Only valid while
1302 evaluating 'diffexpr'.
1303
1304 *v:fname_diff* *fname_diff-variable*
1305v:fname_diff The name of the diff (patch) file. Only valid while
1306 evaluating 'patchexpr'.
1307
1308 *v:folddashes* *folddashes-variable*
1309v:folddashes Used for 'foldtext': dashes representing foldlevel of a closed
1310 fold.
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001311 Read-only in the |sandbox|. |fold-foldtext|
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312
1313 *v:foldlevel* *foldlevel-variable*
1314v:foldlevel Used for 'foldtext': foldlevel of closed fold.
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001315 Read-only in the |sandbox|. |fold-foldtext|
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316
1317 *v:foldend* *foldend-variable*
1318v:foldend Used for 'foldtext': last line of closed fold.
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001319 Read-only in the |sandbox|. |fold-foldtext|
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320
1321 *v:foldstart* *foldstart-variable*
1322v:foldstart Used for 'foldtext': first line of closed fold.
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001323 Read-only in the |sandbox|. |fold-foldtext|
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324
Bram Moolenaar843ee412004-06-30 16:16:41 +00001325 *v:insertmode* *insertmode-variable*
1326v:insertmode Used for the |InsertEnter| and |InsertChange| autocommand
1327 events. Values:
1328 i Insert mode
1329 r Replace mode
1330 v Virtual Replace mode
1331
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00001332 *v:key* *key-variable*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001333v:key Key of the current item of a |Dictionary|. Only valid while
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00001334 evaluating the expression used with |map()| and |filter()|.
1335 Read-only.
1336
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 *v:lang* *lang-variable*
1338v:lang The current locale setting for messages of the runtime
1339 environment. This allows Vim scripts to be aware of the
1340 current language. Technical: it's the value of LC_MESSAGES.
1341 The value is system dependent.
1342 This variable can not be set directly, use the |:language|
1343 command.
1344 It can be different from |v:ctype| when messages are desired
1345 in a different language than what is used for character
1346 encoding. See |multi-lang|.
1347
1348 *v:lc_time* *lc_time-variable*
1349v:lc_time The current locale setting for time messages of the runtime
1350 environment. This allows Vim scripts to be aware of the
1351 current language. Technical: it's the value of LC_TIME.
1352 This variable can not be set directly, use the |:language|
1353 command. See |multi-lang|.
1354
1355 *v:lnum* *lnum-variable*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001356v:lnum Line number for the 'foldexpr' |fold-expr| and 'indentexpr'
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00001357 expressions, tab page number for 'guitablabel'. Only valid
1358 while one of these expressions is being evaluated. Read-only
1359 when in the |sandbox|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
1361 *v:prevcount* *prevcount-variable*
1362v:prevcount The count given for the last but one Normal mode command.
1363 This is the v:count value of the previous command. Useful if
1364 you want to cancel Visual mode and then use the count. >
1365 :vmap % <Esc>:call MyFilter(v:prevcount)<CR>
1366< Read-only.
1367
Bram Moolenaar05159a02005-02-26 23:04:13 +00001368 *v:profiling* *profiling-variable*
1369v:profiling Normally zero. Set to one after using ":profile start".
1370 See |profiling|.
1371
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 *v:progname* *progname-variable*
1373v:progname Contains the name (with path removed) with which Vim was
1374 invoked. Allows you to do special initialisations for "view",
1375 "evim" etc., or any other name you might symlink to Vim.
1376 Read-only.
1377
1378 *v:register* *register-variable*
1379v:register The name of the register supplied to the last normal mode
1380 command. Empty if none were supplied. |getreg()| |setreg()|
1381
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001382 *v:scrollstart* *scrollstart-variable*
1383v:scrollstart String describing the script or function that caused the
1384 screen to scroll up. It's only set when it is empty, thus the
1385 first reason is remembered. It is set to "Unknown" for a
1386 typed command.
1387 This can be used to find out why your script causes the
1388 hit-enter prompt.
1389
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 *v:servername* *servername-variable*
1391v:servername The resulting registered |x11-clientserver| name if any.
1392 Read-only.
1393
1394 *v:shell_error* *shell_error-variable*
1395v:shell_error Result of the last shell command. When non-zero, the last
1396 shell command had an error. When zero, there was no problem.
1397 This only works when the shell returns the error code to Vim.
1398 The value -1 is often used when the command could not be
1399 executed. Read-only.
1400 Example: >
1401 :!mv foo bar
1402 :if v:shell_error
1403 : echo 'could not rename "foo" to "bar"!'
1404 :endif
1405< "shell_error" also works, for backwards compatibility.
1406
1407 *v:statusmsg* *statusmsg-variable*
1408v:statusmsg Last given status message. It's allowed to set this variable.
1409
Bram Moolenaar4e330bb2005-12-07 21:04:31 +00001410 *v:swapname* *swapname-variable*
1411v:swapname Only valid when executing |SwapExists| autocommands: Name of
1412 the swap file found. Read-only.
1413
1414 *v:swapchoice* *swapchoice-variable*
1415v:swapchoice |SwapExists| autocommands can set this to the selected choice
1416 for handling an existing swap file:
1417 'o' Open read-only
1418 'e' Edit anyway
1419 'r' Recover
1420 'd' Delete swapfile
1421 'q' Quit
1422 'a' Abort
1423 The value should be a single-character string. An empty value
1424 results in the user being asked, as would happen when there is
1425 no SwapExists autocommand. The default is empty.
1426
Bram Moolenaarb3480382005-12-11 21:33:32 +00001427 *v:swapcommand* *swapcommand-variable*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001428v:swapcommand Normal mode command to be executed after a file has been
Bram Moolenaarb3480382005-12-11 21:33:32 +00001429 opened. Can be used for a |SwapExists| autocommand to have
1430 another Vim open the file and jump to the right place. For
1431 example, when jumping to a tag the value is ":tag tagname\r".
1432
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 *v:termresponse* *termresponse-variable*
1434v:termresponse The escape sequence returned by the terminal for the |t_RV|
1435 termcap entry. It is set when Vim receives an escape sequence
1436 that starts with ESC [ or CSI and ends in a 'c', with only
1437 digits, ';' and '.' in between.
1438 When this option is set, the TermResponse autocommand event is
1439 fired, so that you can react to the response from the
1440 terminal.
1441 The response from a new xterm is: "<Esc>[ Pp ; Pv ; Pc c". Pp
1442 is the terminal type: 0 for vt100 and 1 for vt220. Pv is the
1443 patch level (since this was introduced in patch 95, it's
1444 always 95 or bigger). Pc is always zero.
1445 {only when compiled with |+termresponse| feature}
1446
1447 *v:this_session* *this_session-variable*
1448v:this_session Full filename of the last loaded or saved session file. See
1449 |:mksession|. It is allowed to set this variable. When no
1450 session file has been saved, this variable is empty.
1451 "this_session" also works, for backwards compatibility.
1452
1453 *v:throwpoint* *throwpoint-variable*
1454v:throwpoint The point where the exception most recently caught and not
1455 finished was thrown. Not set when commands are typed. See
1456 also |v:exception| and |throw-variables|.
1457 Example: >
1458 :try
1459 : throw "oops"
1460 :catch /.*/
1461 : echo "Exception from" v:throwpoint
1462 :endtry
1463< Output: "Exception from test.vim, line 2"
1464
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00001465 *v:val* *val-variable*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001466v:val Value of the current item of a |List| or |Dictionary|. Only
1467 valid while evaluating the expression used with |map()| and
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00001468 |filter()|. Read-only.
1469
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 *v:version* *version-variable*
1471v:version Version number of Vim: Major version number times 100 plus
1472 minor version number. Version 5.0 is 500. Version 5.1 (5.01)
1473 is 501. Read-only. "version" also works, for backwards
1474 compatibility.
1475 Use |has()| to check if a certain patch was included, e.g.: >
1476 if has("patch123")
1477< Note that patch numbers are specific to the version, thus both
1478 version 5.0 and 5.1 may have a patch 123, but these are
1479 completely different.
1480
1481 *v:warningmsg* *warningmsg-variable*
1482v:warningmsg Last given warning message. It's allowed to set this variable.
1483
1484==============================================================================
14854. Builtin Functions *functions*
1486
1487See |function-list| for a list grouped by what the function is used for.
1488
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001489(Use CTRL-] on the function name to jump to the full explanation.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
1491USAGE RESULT DESCRIPTION ~
1492
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001493add( {list}, {item}) List append {item} to |List| {list}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001494append( {lnum}, {string}) Number append {string} below line {lnum}
Bram Moolenaar7c626922005-02-07 22:01:03 +00001495append( {lnum}, {list}) Number append lines {list} below line {lnum}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496argc() Number number of files in the argument list
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001497argidx() Number current index in the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498argv( {nr}) String {nr} entry of the argument list
1499browse( {save}, {title}, {initdir}, {default})
1500 String put up a file requester
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001501browsedir( {title}, {initdir}) String put up a directory requester
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502bufexists( {expr}) Number TRUE if buffer {expr} exists
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001503buflisted( {expr}) Number TRUE if buffer {expr} is listed
1504bufloaded( {expr}) Number TRUE if buffer {expr} is loaded
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505bufname( {expr}) String Name of the buffer {expr}
1506bufnr( {expr}) Number Number of the buffer {expr}
1507bufwinnr( {expr}) Number window number of buffer {expr}
1508byte2line( {byte}) Number line number at byte count {byte}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001509byteidx( {expr}, {nr}) Number byte index of {nr}'th char in {expr}
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00001510call( {func}, {arglist} [, {dict}])
1511 any call {func} with arguments {arglist}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512char2nr( {expr}) Number ASCII value of first char in {expr}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001513cindent( {lnum}) Number C indent for line {lnum}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514col( {expr}) Number column nr of cursor or mark
Bram Moolenaar572cb562005-08-05 21:35:02 +00001515complete_add( {expr}) Number add completion match
1516complete_check() Number check for key typed during completion
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517confirm( {msg} [, {choices} [, {default} [, {type}]]])
1518 Number number of choice picked by user
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001519copy( {expr}) any make a shallow copy of {expr}
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001520count( {list}, {expr} [, {start} [, {ic}]])
1521 Number count how many {expr} are in {list}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522cscope_connection( [{num} , {dbpath} [, {prepend}]])
1523 Number checks existence of cscope connection
Bram Moolenaar0b238792006-03-02 22:49:12 +00001524cursor( {lnum}, {col} [, {coladd}])
1525 Number move cursor to {lnum}, {col}, {coladd}
1526cursor( {list}) Number move cursor to position in {list}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001527deepcopy( {expr}) any make a full copy of {expr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528delete( {fname}) Number delete file {fname}
1529did_filetype() Number TRUE if FileType autocommand event used
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001530diff_filler( {lnum}) Number diff filler lines about {lnum}
1531diff_hlID( {lnum}, {col}) Number diff highlighting at {lnum}/{col}
Bram Moolenaar13065c42005-01-08 16:08:21 +00001532empty( {expr}) Number TRUE if {expr} is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533escape( {string}, {chars}) String escape {chars} in {string} with '\'
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001534eval( {string}) any evaluate {string} into its value
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001535eventhandler( ) Number TRUE if inside an event handler
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536executable( {expr}) Number 1 if executable {expr} exists
1537exists( {expr}) Number TRUE if {expr} exists
1538expand( {expr}) String expand special keywords in {expr}
1539filereadable( {file}) Number TRUE if {file} is a readable file
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00001540filter( {expr}, {string}) List/Dict remove items from {expr} where
1541 {string} is 0
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001542finddir( {name}[, {path}[, {count}]])
1543 String Find directory {name} in {path}
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00001544findfile( {name}[, {path}[, {count}]])
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001545 String Find file {name} in {path}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546filewritable( {file}) Number TRUE if {file} is a writable file
1547fnamemodify( {fname}, {mods}) String modify file name
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001548foldclosed( {lnum}) Number first line of fold at {lnum} if closed
1549foldclosedend( {lnum}) Number last line of fold at {lnum} if closed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550foldlevel( {lnum}) Number fold level at {lnum}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001551foldtext( ) String line displayed for closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552foreground( ) Number bring the Vim window to the foreground
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001553function( {name}) Funcref reference to function {name}
Bram Moolenaara14de3d2005-01-07 21:48:26 +00001554get( {list}, {idx} [, {def}]) any get item {idx} from {list} or {def}
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00001555get( {dict}, {key} [, {def}]) any get item {key} from {dict} or {def}
Bram Moolenaar45360022005-07-21 21:08:21 +00001556getbufline( {expr}, {lnum} [, {end}])
1557 List lines {lnum} to {end} of buffer {expr}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001558getchar( [expr]) Number get one character from the user
1559getcharmod( ) Number modifiers for the last typed character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560getbufvar( {expr}, {varname}) variable {varname} in buffer {expr}
1561getcmdline() String return the current command-line
1562getcmdpos() Number return cursor position in command-line
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001563getcmdtype() String return the current command-line type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564getcwd() String the current working directory
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001565getfperm( {fname}) String file permissions of file {fname}
1566getfsize( {fname}) Number size in bytes of file {fname}
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001567getfontname( [{name}]) String name of font being used
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568getftime( {fname}) Number last modification time of file
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001569getftype( {fname}) String description of type of file {fname}
Bram Moolenaar7c626922005-02-07 22:01:03 +00001570getline( {lnum}) String line {lnum} of current buffer
1571getline( {lnum}, {end}) List lines {lnum} to {end} of current buffer
Bram Moolenaar17c7c012006-01-26 22:25:15 +00001572getloclist({nr}) List list of location list items
Bram Moolenaar0b238792006-03-02 22:49:12 +00001573getpos( {expr}) List position of cursor, mark, etc.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001574getqflist() List list of quickfix items
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00001575getreg( [{regname} [, 1]]) String contents of register
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001576getregtype( [{regname}]) String type of register
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577getwinposx() Number X coord in pixels of GUI Vim window
1578getwinposy() Number Y coord in pixels of GUI Vim window
1579getwinvar( {nr}, {varname}) variable {varname} in window {nr}
1580glob( {expr}) String expand file wildcards in {expr}
1581globpath( {path}, {expr}) String do glob({expr}) for all dirs in {path}
1582has( {feature}) Number TRUE if feature {feature} supported
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00001583has_key( {dict}, {key}) Number TRUE if {dict} has entry {key}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584hasmapto( {what} [, {mode}]) Number TRUE if mapping to {what} exists
1585histadd( {history},{item}) String add an item to a history
1586histdel( {history} [, {item}]) String remove an item from a history
1587histget( {history} [, {index}]) String get the item {index} from a history
1588histnr( {history}) Number highest index of a history
1589hlexists( {name}) Number TRUE if highlight group {name} exists
1590hlID( {name}) Number syntax ID of highlight group {name}
1591hostname() String name of the machine Vim is running on
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001592iconv( {expr}, {from}, {to}) String convert encoding of {expr}
1593indent( {lnum}) Number indent of line {lnum}
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001594index( {list}, {expr} [, {start} [, {ic}]])
1595 Number index in {list} where {expr} appears
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001596input( {prompt} [, {text} [, {completion}]])
1597 String get input from the user
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598inputdialog( {p} [, {t} [, {c}]]) String like input() but in a GUI dialog
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001599inputrestore() Number restore typeahead
1600inputsave() Number save and clear typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601inputsecret( {prompt} [, {text}]) String like input() but hiding the text
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001602insert( {list}, {item} [, {idx}]) List insert {item} in {list} [before {idx}]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603isdirectory( {directory}) Number TRUE if {directory} is a directory
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00001604islocked( {expr}) Number TRUE if {expr} is locked
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001605items( {dict}) List key-value pairs in {dict}
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001606join( {list} [, {sep}]) String join {list} items into one String
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001607keys( {dict}) List keys in {dict}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001608len( {expr}) Number the length of {expr}
1609libcall( {lib}, {func}, {arg}) String call {func} in library {lib} with {arg}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610libcallnr( {lib}, {func}, {arg}) Number idem, but return a Number
1611line( {expr}) Number line nr of cursor, last line or mark
1612line2byte( {lnum}) Number byte count of line {lnum}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001613lispindent( {lnum}) Number Lisp indent for line {lnum}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614localtime() Number current time
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00001615map( {expr}, {string}) List/Dict change each item in {expr} to {expr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616maparg( {name}[, {mode}]) String rhs of mapping {name} in mode {mode}
1617mapcheck( {name}[, {mode}]) String check for mappings matching {name}
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00001618match( {expr}, {pat}[, {start}[, {count}]])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 Number position where {pat} matches in {expr}
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00001620matchend( {expr}, {pat}[, {start}[, {count}]])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 Number position where {pat} ends in {expr}
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00001622matchlist( {expr}, {pat}[, {start}[, {count}]])
1623 List match and submatches of {pat} in {expr}
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00001624matchstr( {expr}, {pat}[, {start}[, {count}]])
1625 String {count}'th match of {pat} in {expr}
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00001626max({list}) Number maximum value of items in {list}
1627min({list}) Number minumum value of items in {list}
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001628mkdir({name} [, {path} [, {prot}]])
1629 Number create directory {name}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001630mode() String current editing mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631nextnonblank( {lnum}) Number line nr of non-blank line >= {lnum}
1632nr2char( {expr}) String single char with ASCII value {expr}
1633prevnonblank( {lnum}) Number line nr of non-blank line <= {lnum}
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001634printf( {fmt}, {expr1}...) String format text
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001635pumvisible() Number whether popup menu is visible
Bram Moolenaard8b02732005-01-14 21:48:43 +00001636range( {expr} [, {max} [, {stride}]])
1637 List items from {expr} to {max}
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001638readfile({fname} [, {binary} [, {max}]])
1639 List get list of lines from file {fname}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640remote_expr( {server}, {string} [, {idvar}])
1641 String send expression
1642remote_foreground( {server}) Number bring Vim server to the foreground
1643remote_peek( {serverid} [, {retvar}])
1644 Number check for reply string
1645remote_read( {serverid}) String read reply string
1646remote_send( {server}, {string} [, {idvar}])
1647 String send key sequence
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001648remove( {list}, {idx} [, {end}]) any remove items {idx}-{end} from {list}
Bram Moolenaard8b02732005-01-14 21:48:43 +00001649remove( {dict}, {key}) any remove entry {key} from {dict}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001650rename( {from}, {to}) Number rename (move) file from {from} to {to}
1651repeat( {expr}, {count}) String repeat {expr} {count} times
1652resolve( {filename}) String get filename a shortcut points to
Bram Moolenaara14de3d2005-01-07 21:48:26 +00001653reverse( {list}) List reverse {list} in-place
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001654search( {pattern} [, {flags}]) Number search for {pattern}
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001655searchdecl({name} [, {global} [, {thisblock}]])
1656 Number search for variable declaration
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001657searchpair( {start}, {middle}, {end} [, {flags} [, {skip} [, {stopline}]]])
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001658 Number search for other end of start/end pair
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001659searchpairpos( {start}, {middle}, {end} [, {flags} [, {skip} [, {stopline}]]])
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001660 List search for other end of start/end pair
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001661searchpos( {pattern} [, {flags} [, {stopline}]])
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001662 List search for {pattern}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663server2client( {clientid}, {string})
1664 Number send reply string
1665serverlist() String get a list of available servers
1666setbufvar( {expr}, {varname}, {val}) set {varname} in buffer {expr} to {val}
1667setcmdpos( {pos}) Number set cursor position in command-line
1668setline( {lnum}, {line}) Number set line {lnum} to {line}
Bram Moolenaar17c7c012006-01-26 22:25:15 +00001669setloclist( {nr}, {list}[, {action}])
1670 Number modify location list using {list}
1671setqflist( {list}[, {action}]) Number modify quickfix list using {list}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001672setreg( {n}, {v}[, {opt}]) Number set register to value and type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673setwinvar( {nr}, {varname}, {val}) set {varname} in window {nr} to {val}
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001674simplify( {filename}) String simplify filename as much as possible
Bram Moolenaara14de3d2005-01-07 21:48:26 +00001675sort( {list} [, {func}]) List sort {list}, using {func} to compare
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001676soundfold( {word}) String sound-fold {word}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001677spellbadword() String badly spelled word at cursor
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00001678spellsuggest( {word} [, {max} [, {capital}]])
1679 List spelling suggestions
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00001680split( {expr} [, {pat} [, {keepempty}]])
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001681 List make |List| from {pat} separated {expr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682strftime( {format}[, {time}]) String time in specified format
Bram Moolenaar8f999f12005-01-25 22:12:55 +00001683stridx( {haystack}, {needle}[, {start}])
1684 Number index of {needle} in {haystack}
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001685string( {expr}) String String representation of {expr} value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686strlen( {expr}) Number length of the String {expr}
1687strpart( {src}, {start}[, {len}])
1688 String {len} characters of {src} at {start}
Bram Moolenaar677ee682005-01-27 14:41:15 +00001689strridx( {haystack}, {needle} [, {start}])
1690 Number last index of {needle} in {haystack}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691strtrans( {expr}) String translate string to make it printable
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001692submatch( {nr}) String specific match in ":substitute"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693substitute( {expr}, {pat}, {sub}, {flags})
1694 String all {pat} in {expr} replaced with {sub}
Bram Moolenaar47136d72004-10-12 20:02:24 +00001695synID( {lnum}, {col}, {trans}) Number syntax ID at {lnum} and {col}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696synIDattr( {synID}, {what} [, {mode}])
1697 String attribute {what} of syntax ID {synID}
1698synIDtrans( {synID}) Number translated syntax ID of {synID}
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001699system( {expr} [, {input}]) String output of shell command/filter {expr}
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001700tabpagebuflist( [{arg}]) List list of buffer numbers in tab page
1701tabpagenr( [{arg}]) Number number of current or last tab page
1702tabpagewinnr( {tabarg}[, {arg}])
1703 Number number of current window in tab page
1704taglist( {expr}) List list of tags matching {expr}
Bram Moolenaare7eb9df2005-09-09 19:49:30 +00001705tagfiles() List tags files used
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706tempname() String name for a temporary file
1707tolower( {expr}) String the String {expr} switched to lowercase
1708toupper( {expr}) String the String {expr} switched to uppercase
Bram Moolenaar8299df92004-07-10 09:47:34 +00001709tr( {src}, {fromstr}, {tostr}) String translate chars of {src} in {fromstr}
1710 to chars in {tostr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711type( {name}) Number type of variable {name}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001712values( {dict}) List values in {dict}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713virtcol( {expr}) Number screen column of cursor or mark
1714visualmode( [expr]) String last visual mode used
1715winbufnr( {nr}) Number buffer number of window {nr}
1716wincol() Number window column of the cursor
1717winheight( {nr}) Number height of window {nr}
1718winline() Number window line of the cursor
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00001719winnr( [{expr}]) Number number of current window
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001720winrestcmd() String returns command to restore window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721winwidth( {nr}) Number width of window {nr}
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00001722writefile({list}, {fname} [, {binary}])
1723 Number write list of lines to file {fname}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724
Bram Moolenaara14de3d2005-01-07 21:48:26 +00001725add({list}, {expr}) *add()*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001726 Append the item {expr} to |List| {list}. Returns the
1727 resulting |List|. Examples: >
Bram Moolenaara14de3d2005-01-07 21:48:26 +00001728 :let alist = add([1, 2, 3], item)
1729 :call add(mylist, "woodstock")
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001730< Note that when {expr} is a |List| it is appended as a single
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001731 item. Use |extend()| to concatenate |Lists|.
Bram Moolenaar13065c42005-01-08 16:08:21 +00001732 Use |insert()| to add an item at another position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
Bram Moolenaara14de3d2005-01-07 21:48:26 +00001734
1735append({lnum}, {expr}) *append()*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001736 When {expr} is a |List|: Append each item of the |List| as a
1737 text line below line {lnum} in the current buffer.
Bram Moolenaar748bf032005-02-02 23:04:36 +00001738 Otherwise append {expr} as one text line below line {lnum} in
1739 the current buffer.
1740 {lnum} can be zero to insert a line before the first one.
Bram Moolenaara14de3d2005-01-07 21:48:26 +00001741 Returns 1 for failure ({lnum} out of range or out of memory),
1742 0 for success. Example: >
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001743 :let failed = append(line('$'), "# THE END")
Bram Moolenaara14de3d2005-01-07 21:48:26 +00001744 :let failed = append(0, ["Chapter 1", "the beginning"])
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00001745<
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 *argc()*
1747argc() The result is the number of files in the argument list of the
1748 current window. See |arglist|.
1749
1750 *argidx()*
1751argidx() The result is the current index in the argument list. 0 is
1752 the first file. argc() - 1 is the last one. See |arglist|.
1753
1754 *argv()*
1755argv({nr}) The result is the {nr}th file in the argument list of the
1756 current window. See |arglist|. "argv(0)" is the first one.
1757 Example: >
1758 :let i = 0
1759 :while i < argc()
1760 : let f = escape(argv(i), '. ')
1761 : exe 'amenu Arg.' . f . ' :e ' . f . '<CR>'
1762 : let i = i + 1
1763 :endwhile
1764<
1765 *browse()*
1766browse({save}, {title}, {initdir}, {default})
1767 Put up a file requester. This only works when "has("browse")"
1768 returns non-zero (only in some GUI versions).
1769 The input fields are:
1770 {save} when non-zero, select file to write
1771 {title} title for the requester
1772 {initdir} directory to start browsing in
1773 {default} default file name
1774 When the "Cancel" button is hit, something went wrong, or
1775 browsing is not possible, an empty string is returned.
1776
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001777 *browsedir()*
1778browsedir({title}, {initdir})
1779 Put up a directory requester. This only works when
1780 "has("browse")" returns non-zero (only in some GUI versions).
1781 On systems where a directory browser is not supported a file
1782 browser is used. In that case: select a file in the directory
1783 to be used.
1784 The input fields are:
1785 {title} title for the requester
1786 {initdir} directory to start browsing in
1787 When the "Cancel" button is hit, something went wrong, or
1788 browsing is not possible, an empty string is returned.
1789
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790bufexists({expr}) *bufexists()*
1791 The result is a Number, which is non-zero if a buffer called
1792 {expr} exists.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001793 If the {expr} argument is a number, buffer numbers are used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 If the {expr} argument is a string it must match a buffer name
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001795 exactly. The name can be:
1796 - Relative to the current directory.
1797 - A full path.
1798 - The name of a buffer with 'filetype' set to "nofile".
1799 - A URL name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 Unlisted buffers will be found.
1801 Note that help files are listed by their short name in the
1802 output of |:buffers|, but bufexists() requires using their
1803 long name to be able to find them.
1804 Use "bufexists(0)" to test for the existence of an alternate
1805 file name.
1806 *buffer_exists()*
1807 Obsolete name: buffer_exists().
1808
1809buflisted({expr}) *buflisted()*
1810 The result is a Number, which is non-zero if a buffer called
1811 {expr} exists and is listed (has the 'buflisted' option set).
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001812 The {expr} argument is used like with |bufexists()|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
1814bufloaded({expr}) *bufloaded()*
1815 The result is a Number, which is non-zero if a buffer called
1816 {expr} exists and is loaded (shown in a window or hidden).
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001817 The {expr} argument is used like with |bufexists()|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
1819bufname({expr}) *bufname()*
1820 The result is the name of a buffer, as it is displayed by the
1821 ":ls" command.
1822 If {expr} is a Number, that buffer number's name is given.
1823 Number zero is the alternate buffer for the current window.
1824 If {expr} is a String, it is used as a |file-pattern| to match
1825 with the buffer names. This is always done like 'magic' is
1826 set and 'cpoptions' is empty. When there is more than one
1827 match an empty string is returned.
1828 "" or "%" can be used for the current buffer, "#" for the
1829 alternate buffer.
1830 A full match is preferred, otherwise a match at the start, end
1831 or middle of the buffer name is accepted.
1832 Listed buffers are found first. If there is a single match
1833 with a listed buffer, that one is returned. Next unlisted
1834 buffers are searched for.
1835 If the {expr} is a String, but you want to use it as a buffer
1836 number, force it to be a Number by adding zero to it: >
1837 :echo bufname("3" + 0)
1838< If the buffer doesn't exist, or doesn't have a name, an empty
1839 string is returned. >
1840 bufname("#") alternate buffer name
1841 bufname(3) name of buffer 3
1842 bufname("%") name of current buffer
1843 bufname("file2") name of buffer where "file2" matches.
1844< *buffer_name()*
1845 Obsolete name: buffer_name().
1846
1847 *bufnr()*
Bram Moolenaar65c923a2006-03-03 22:56:30 +00001848bufnr({expr} [, {create}])
1849 The result is the number of a buffer, as it is displayed by
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 the ":ls" command. For the use of {expr}, see |bufname()|
Bram Moolenaar65c923a2006-03-03 22:56:30 +00001851 above.
1852 If the buffer doesn't exist, -1 is returned. Or, if the
1853 {create} argument is present and not zero, a new, unlisted,
1854 buffer is created and its number is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 bufnr("$") is the last buffer: >
1856 :let last_buffer = bufnr("$")
1857< The result is a Number, which is the highest buffer number
1858 of existing buffers. Note that not all buffers with a smaller
1859 number necessarily exist, because ":bwipeout" may have removed
1860 them. Use bufexists() to test for the existence of a buffer.
1861 *buffer_number()*
1862 Obsolete name: buffer_number().
1863 *last_buffer_nr()*
1864 Obsolete name for bufnr("$"): last_buffer_nr().
1865
1866bufwinnr({expr}) *bufwinnr()*
1867 The result is a Number, which is the number of the first
1868 window associated with buffer {expr}. For the use of {expr},
1869 see |bufname()| above. If buffer {expr} doesn't exist or
1870 there is no such window, -1 is returned. Example: >
1871
1872 echo "A window containing buffer 1 is " . (bufwinnr(1))
1873
1874< The number can be used with |CTRL-W_w| and ":wincmd w"
1875 |:wincmd|.
1876
1877
1878byte2line({byte}) *byte2line()*
1879 Return the line number that contains the character at byte
1880 count {byte} in the current buffer. This includes the
1881 end-of-line character, depending on the 'fileformat' option
1882 for the current buffer. The first character has byte count
1883 one.
1884 Also see |line2byte()|, |go| and |:goto|.
1885 {not available when compiled without the |+byte_offset|
1886 feature}
1887
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001888byteidx({expr}, {nr}) *byteidx()*
1889 Return byte index of the {nr}'th character in the string
1890 {expr}. Use zero for the first character, it returns zero.
1891 This function is only useful when there are multibyte
1892 characters, otherwise the returned value is equal to {nr}.
1893 Composing characters are counted as a separate character.
1894 Example : >
1895 echo matchstr(str, ".", byteidx(str, 3))
1896< will display the fourth character. Another way to do the
1897 same: >
1898 let s = strpart(str, byteidx(str, 3))
1899 echo strpart(s, 0, byteidx(s, 1))
1900< If there are less than {nr} characters -1 is returned.
1901 If there are exactly {nr} characters the length of the string
1902 is returned.
1903
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00001904call({func}, {arglist} [, {dict}]) *call()* *E699*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001905 Call function {func} with the items in |List| {arglist} as
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001906 arguments.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001907 {func} can either be a |Funcref| or the name of a function.
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001908 a:firstline and a:lastline are set to the cursor line.
1909 Returns the return value of the called function.
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00001910 {dict} is for functions with the "dict" attribute. It will be
1911 used to set the local variable "self". |Dictionary-function|
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001912
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913char2nr({expr}) *char2nr()*
1914 Return number value of the first char in {expr}. Examples: >
1915 char2nr(" ") returns 32
1916 char2nr("ABC") returns 65
1917< The current 'encoding' is used. Example for "utf-8": >
Bram Moolenaar98692072006-02-04 00:57:42 +00001918 char2nr("?") returns 225
1919 char2nr("?"[0]) returns 195
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001920< nr2char() does the opposite.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921
1922cindent({lnum}) *cindent()*
1923 Get the amount of indent for line {lnum} according the C
1924 indenting rules, as with 'cindent'.
1925 The indent is counted in spaces, the value of 'tabstop' is
1926 relevant. {lnum} is used just like in |getline()|.
1927 When {lnum} is invalid or Vim was not compiled the |+cindent|
1928 feature, -1 is returned.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00001929 See |C-indenting|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930
1931 *col()*
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001932col({expr}) The result is a Number, which is the byte index of the column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 position given with {expr}. The accepted positions are:
1934 . the cursor position
1935 $ the end of the cursor line (the result is the
1936 number of characters in the cursor line plus one)
1937 'x position of mark x (if the mark is not set, 0 is
1938 returned)
Bram Moolenaar0b238792006-03-02 22:49:12 +00001939 To get the line number use |col()|. To get both use
1940 |getpos()|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 For the screen column position use |virtcol()|.
1942 Note that only marks in the current file can be used.
1943 Examples: >
1944 col(".") column of cursor
1945 col("$") length of cursor line plus one
1946 col("'t") column of mark t
1947 col("'" . markname) column of mark markname
1948< The first column is 1. 0 is returned for an error.
1949 For the cursor position, when 'virtualedit' is active, the
1950 column is one higher if the cursor is after the end of the
1951 line. This can be used to obtain the column in Insert mode: >
1952 :imap <F2> <C-O>:let save_ve = &ve<CR>
1953 \<C-O>:set ve=all<CR>
1954 \<C-O>:echo col(".") . "\n" <Bar>
1955 \let &ve = save_ve<CR>
1956<
Bram Moolenaar572cb562005-08-05 21:35:02 +00001957
1958complete_add({expr}) *complete_add()*
1959 Add {expr} to the list of matches. Only to be used by the
1960 function specified with the 'completefunc' option.
1961 Returns 0 for failure (empty string or out of memory),
1962 1 when the match was added, 2 when the match was already in
1963 the list.
1964
1965complete_check() *complete_check()*
1966 Check for a key typed while looking for completion matches.
1967 This is to be used when looking for matches takes some time.
1968 Returns non-zero when searching for matches is to be aborted,
1969 zero otherwise.
1970 Only to be used by the function specified with the
1971 'completefunc' option.
1972
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 *confirm()*
1974confirm({msg} [, {choices} [, {default} [, {type}]]])
1975 Confirm() offers the user a dialog, from which a choice can be
1976 made. It returns the number of the choice. For the first
1977 choice this is 1.
1978 Note: confirm() is only supported when compiled with dialog
1979 support, see |+dialog_con| and |+dialog_gui|.
1980 {msg} is displayed in a |dialog| with {choices} as the
1981 alternatives. When {choices} is missing or empty, "&OK" is
1982 used (and translated).
1983 {msg} is a String, use '\n' to include a newline. Only on
1984 some systems the string is wrapped when it doesn't fit.
1985 {choices} is a String, with the individual choices separated
1986 by '\n', e.g. >
1987 confirm("Save changes?", "&Yes\n&No\n&Cancel")
1988< The letter after the '&' is the shortcut key for that choice.
1989 Thus you can type 'c' to select "Cancel". The shortcut does
1990 not need to be the first letter: >
1991 confirm("file has been modified", "&Save\nSave &All")
1992< For the console, the first letter of each choice is used as
1993 the default shortcut key.
1994 The optional {default} argument is the number of the choice
1995 that is made if the user hits <CR>. Use 1 to make the first
1996 choice the default one. Use 0 to not set a default. If
1997 {default} is omitted, 1 is used.
1998 The optional {type} argument gives the type of dialog. This
1999 is only used for the icon of the Win32 GUI. It can be one of
2000 these values: "Error", "Question", "Info", "Warning" or
2001 "Generic". Only the first character is relevant. When {type}
2002 is omitted, "Generic" is used.
2003 If the user aborts the dialog by pressing <Esc>, CTRL-C,
2004 or another valid interrupt key, confirm() returns 0.
2005
2006 An example: >
2007 :let choice = confirm("What do you want?", "&Apples\n&Oranges\n&Bananas", 2)
2008 :if choice == 0
2009 : echo "make up your mind!"
2010 :elseif choice == 3
2011 : echo "tasteful"
2012 :else
2013 : echo "I prefer bananas myself."
2014 :endif
2015< In a GUI dialog, buttons are used. The layout of the buttons
2016 depends on the 'v' flag in 'guioptions'. If it is included,
2017 the buttons are always put vertically. Otherwise, confirm()
2018 tries to put the buttons in one horizontal line. If they
2019 don't fit, a vertical layout is used anyway. For some systems
2020 the horizontal layout is always used.
2021
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00002022 *copy()*
2023copy({expr}) Make a copy of {expr}. For Numbers and Strings this isn't
2024 different from using {expr} directly.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002025 When {expr} is a |List| a shallow copy is created. This means
2026 that the original |List| can be changed without changing the
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00002027 copy, and vise versa. But the items are identical, thus
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002028 changing an item changes the contents of both |Lists|. Also
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002029 see |deepcopy()|.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00002030
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00002031count({comp}, {expr} [, {ic} [, {start}]]) *count()*
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002032 Return the number of times an item with value {expr} appears
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002033 in |List| or |Dictionary| {comp}.
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00002034 If {start} is given then start with the item with this index.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002035 {start} can only be used with a |List|.
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002036 When {ic} is given and it's non-zero then case is ignored.
2037
2038
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 *cscope_connection()*
2040cscope_connection([{num} , {dbpath} [, {prepend}]])
2041 Checks for the existence of a |cscope| connection. If no
2042 parameters are specified, then the function returns:
2043 0, if cscope was not available (not compiled in), or
2044 if there are no cscope connections;
2045 1, if there is at least one cscope connection.
2046
2047 If parameters are specified, then the value of {num}
2048 determines how existence of a cscope connection is checked:
2049
2050 {num} Description of existence check
2051 ----- ------------------------------
2052 0 Same as no parameters (e.g., "cscope_connection()").
2053 1 Ignore {prepend}, and use partial string matches for
2054 {dbpath}.
2055 2 Ignore {prepend}, and use exact string matches for
2056 {dbpath}.
2057 3 Use {prepend}, use partial string matches for both
2058 {dbpath} and {prepend}.
2059 4 Use {prepend}, use exact string matches for both
2060 {dbpath} and {prepend}.
2061
2062 Note: All string comparisons are case sensitive!
2063
2064 Examples. Suppose we had the following (from ":cs show"): >
2065
2066 # pid database name prepend path
2067 0 27664 cscope.out /usr/local
2068<
2069 Invocation Return Val ~
2070 ---------- ---------- >
2071 cscope_connection() 1
2072 cscope_connection(1, "out") 1
2073 cscope_connection(2, "out") 0
2074 cscope_connection(3, "out") 0
2075 cscope_connection(3, "out", "local") 1
2076 cscope_connection(4, "out") 0
2077 cscope_connection(4, "out", "local") 0
2078 cscope_connection(4, "cscope.out", "/usr/local") 1
2079<
Bram Moolenaar0b238792006-03-02 22:49:12 +00002080cursor({lnum}, {col} [, {off}]) *cursor()*
2081cursor({list})
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 Positions the cursor at the column {col} in the line {lnum}.
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002083 The first column is one.
Bram Moolenaar0b238792006-03-02 22:49:12 +00002084 When there is one argument {list} this is used as a |List|
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002085 with two or three items {lnum}, {col} and {off}. This is like
2086 the return value of |getpos()|, but without the first item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 Does not change the jumplist.
2088 If {lnum} is greater than the number of lines in the buffer,
2089 the cursor will be positioned at the last line in the buffer.
2090 If {lnum} is zero, the cursor will stay in the current line.
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002091 If {col} is greater than the number of bytes in the line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 the cursor will be positioned at the last character in the
2093 line.
2094 If {col} is zero, the cursor will stay in the current column.
Bram Moolenaar0b238792006-03-02 22:49:12 +00002095 When 'virtualedit' is used {off} specifies the offset in
2096 screen columns from the start of the character. E.g., a
2097 position within a Tab or after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00002099
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002100deepcopy({expr}[, {noref}]) *deepcopy()* *E698*
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00002101 Make a copy of {expr}. For Numbers and Strings this isn't
2102 different from using {expr} directly.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002103 When {expr} is a |List| a full copy is created. This means
2104 that the original |List| can be changed without changing the
2105 copy, and vise versa. When an item is a |List|, a copy for it
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00002106 is made, recursively. Thus changing an item in the copy does
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002107 not change the contents of the original |List|.
2108 When {noref} is omitted or zero a contained |List| or
2109 |Dictionary| is only copied once. All references point to
2110 this single copy. With {noref} set to 1 every occurrence of a
2111 |List| or |Dictionary| results in a new copy. This also means
2112 that a cyclic reference causes deepcopy() to fail.
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00002113 *E724*
2114 Nesting is possible up to 100 levels. When there is an item
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002115 that refers back to a higher level making a deep copy with
2116 {noref} set to 1 will fail.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00002117 Also see |copy()|.
2118
2119delete({fname}) *delete()*
2120 Deletes the file by the name {fname}. The result is a Number,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 which is 0 if the file was deleted successfully, and non-zero
2122 when the deletion failed.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002123 Use |remove()| to delete an item from a |List|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
2125 *did_filetype()*
2126did_filetype() Returns non-zero when autocommands are being executed and the
2127 FileType event has been triggered at least once. Can be used
2128 to avoid triggering the FileType event again in the scripts
2129 that detect the file type. |FileType|
2130 When editing another file, the counter is reset, thus this
2131 really checks if the FileType event has been triggered for the
2132 current buffer. This allows an autocommand that starts
2133 editing another buffer to set 'filetype' and load a syntax
2134 file.
2135
Bram Moolenaar47136d72004-10-12 20:02:24 +00002136diff_filler({lnum}) *diff_filler()*
2137 Returns the number of filler lines above line {lnum}.
2138 These are the lines that were inserted at this point in
2139 another diff'ed window. These filler lines are shown in the
2140 display but don't exist in the buffer.
2141 {lnum} is used like with |getline()|. Thus "." is the current
2142 line, "'m" mark m, etc.
2143 Returns 0 if the current window is not in diff mode.
2144
2145diff_hlID({lnum}, {col}) *diff_hlID()*
2146 Returns the highlight ID for diff mode at line {lnum} column
2147 {col} (byte index). When the current line does not have a
2148 diff change zero is returned.
2149 {lnum} is used like with |getline()|. Thus "." is the current
2150 line, "'m" mark m, etc.
2151 {col} is 1 for the leftmost column, {lnum} is 1 for the first
2152 line.
2153 The highlight ID can be used with |synIDattr()| to obtain
2154 syntax information about the highlighting.
2155
Bram Moolenaar13065c42005-01-08 16:08:21 +00002156empty({expr}) *empty()*
2157 Return the Number 1 if {expr} is empty, zero otherwise.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002158 A |List| or |Dictionary| is empty when it does not have any
2159 items. A Number is empty when its value is zero.
2160 For a long |List| this is much faster then comparing the
2161 length with zero.
Bram Moolenaar13065c42005-01-08 16:08:21 +00002162
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163escape({string}, {chars}) *escape()*
2164 Escape the characters in {chars} that occur in {string} with a
2165 backslash. Example: >
2166 :echo escape('c:\program files\vim', ' \')
2167< results in: >
2168 c:\\program\ files\\vim
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002169
2170< *eval()*
2171eval({string}) Evaluate {string} and return the result. Especially useful to
2172 turn the result of |string()| back into the original value.
2173 This works for Numbers, Strings and composites of them.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002174 Also works for |Funcref|s that refer to existing functions.
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002175
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176eventhandler() *eventhandler()*
2177 Returns 1 when inside an event handler. That is that Vim got
2178 interrupted while waiting for the user to type a character,
2179 e.g., when dropping a file on Vim. This means interactive
2180 commands cannot be used. Otherwise zero is returned.
2181
2182executable({expr}) *executable()*
2183 This function checks if an executable with the name {expr}
2184 exists. {expr} must be the name of the program without any
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002185 arguments.
2186 executable() uses the value of $PATH and/or the normal
2187 searchpath for programs. *PATHEXT*
2188 On MS-DOS and MS-Windows the ".exe", ".bat", etc. can
2189 optionally be included. Then the extensions in $PATHEXT are
2190 tried. Thus if "foo.exe" does not exist, "foo.exe.bat" can be
2191 found. If $PATHEXT is not set then ".exe;.com;.bat;.cmd" is
2192 used. A dot by itself can be used in $PATHEXT to try using
2193 the name without an extension. When 'shell' looks like a
2194 Unix shell, then the name is also tried without adding an
2195 extension.
2196 On MS-DOS and MS-Windows it only checks if the file exists and
2197 is not a directory, not if it's really executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 The result is a Number:
2199 1 exists
2200 0 does not exist
2201 -1 not implemented on this system
2202
2203 *exists()*
2204exists({expr}) The result is a Number, which is non-zero if {expr} is
2205 defined, zero otherwise. The {expr} argument is a string,
2206 which contains one of these:
2207 &option-name Vim option (only checks if it exists,
2208 not if it really works)
2209 +option-name Vim option that works.
2210 $ENVNAME environment variable (could also be
2211 done by comparing with an empty
2212 string)
2213 *funcname built-in function (see |functions|)
2214 or user defined function (see
2215 |user-functions|).
2216 varname internal variable (see
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002217 |internal-variables|). Also works
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002218 for |curly-braces-names|, |Dictionary|
2219 entries, |List| items, etc. Beware
2220 that this may cause functions to be
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00002221 invoked cause an error message for an
2222 invalid expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 :cmdname Ex command: built-in command, user
2224 command or command modifier |:command|.
2225 Returns:
2226 1 for match with start of a command
2227 2 full match with a command
2228 3 matches several user commands
2229 To check for a supported command
2230 always check the return value to be 2.
2231 #event autocommand defined for this event
2232 #event#pattern autocommand defined for this event and
2233 pattern (the pattern is taken
2234 literally and compared to the
2235 autocommand patterns character by
2236 character)
Bram Moolenaara9b1e742005-12-19 22:14:58 +00002237 #group autocommand group exists
2238 #group#event autocommand defined for this group and
2239 event.
2240 #group#event#pattern
2241 autocommand defined for this group,
2242 event and pattern.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00002243 ##event autocommand for this event is
2244 supported.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 For checking for a supported feature use |has()|.
2246
2247 Examples: >
2248 exists("&shortname")
2249 exists("$HOSTNAME")
2250 exists("*strftime")
2251 exists("*s:MyFunc")
2252 exists("bufcount")
2253 exists(":Make")
Bram Moolenaara9b1e742005-12-19 22:14:58 +00002254 exists("#CursorHold")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 exists("#BufReadPre#*.gz")
Bram Moolenaara9b1e742005-12-19 22:14:58 +00002256 exists("#filetypeindent")
2257 exists("#filetypeindent#FileType")
2258 exists("#filetypeindent#FileType#*")
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00002259 exists("##ColorScheme")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260< There must be no space between the symbol (&/$/*/#) and the
2261 name.
2262 Note that the argument must be a string, not the name of the
2263 variable itself! For example: >
2264 exists(bufcount)
2265< This doesn't check for existence of the "bufcount" variable,
2266 but gets the contents of "bufcount", and checks if that
2267 exists.
2268
2269expand({expr} [, {flag}]) *expand()*
2270 Expand wildcards and the following special keywords in {expr}.
2271 The result is a String.
2272
2273 When there are several matches, they are separated by <NL>
2274 characters. [Note: in version 5.0 a space was used, which
2275 caused problems when a file name contains a space]
2276
2277 If the expansion fails, the result is an empty string. A name
2278 for a non-existing file is not included.
2279
2280 When {expr} starts with '%', '#' or '<', the expansion is done
2281 like for the |cmdline-special| variables with their associated
2282 modifiers. Here is a short overview:
2283
2284 % current file name
2285 # alternate file name
2286 #n alternate file name n
2287 <cfile> file name under the cursor
2288 <afile> autocmd file name
2289 <abuf> autocmd buffer number (as a String!)
2290 <amatch> autocmd matched name
2291 <sfile> sourced script file name
2292 <cword> word under the cursor
2293 <cWORD> WORD under the cursor
2294 <client> the {clientid} of the last received
2295 message |server2client()|
2296 Modifiers:
2297 :p expand to full path
2298 :h head (last path component removed)
2299 :t tail (last path component only)
2300 :r root (one extension removed)
2301 :e extension only
2302
2303 Example: >
2304 :let &tags = expand("%:p:h") . "/tags"
2305< Note that when expanding a string that starts with '%', '#' or
2306 '<', any following text is ignored. This does NOT work: >
2307 :let doesntwork = expand("%:h.bak")
2308< Use this: >
2309 :let doeswork = expand("%:h") . ".bak"
2310< Also note that expanding "<cfile>" and others only returns the
2311 referenced file name without further expansion. If "<cfile>"
2312 is "~/.cshrc", you need to do another expand() to have the
2313 "~/" expanded into the path of the home directory: >
2314 :echo expand(expand("<cfile>"))
2315<
2316 There cannot be white space between the variables and the
2317 following modifier. The |fnamemodify()| function can be used
2318 to modify normal file names.
2319
2320 When using '%' or '#', and the current or alternate file name
2321 is not defined, an empty string is used. Using "%:p" in a
2322 buffer with no name, results in the current directory, with a
2323 '/' added.
2324
2325 When {expr} does not start with '%', '#' or '<', it is
2326 expanded like a file name is expanded on the command line.
2327 'suffixes' and 'wildignore' are used, unless the optional
2328 {flag} argument is given and it is non-zero. Names for
Bram Moolenaar02743632005-07-25 20:42:36 +00002329 non-existing files are included. The "**" item can be used to
2330 search in a directory tree. For example, to find all "README"
2331 files in the current directory and below: >
2332 :echo expand("**/README")
2333<
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 Expand() can also be used to expand variables and environment
2335 variables that are only known in a shell. But this can be
2336 slow, because a shell must be started. See |expr-env-expand|.
2337 The expanded variable is still handled like a list of file
2338 names. When an environment variable cannot be expanded, it is
2339 left unchanged. Thus ":echo expand('$FOOBAR')" results in
2340 "$FOOBAR".
2341
2342 See |glob()| for finding existing files. See |system()| for
2343 getting the raw output of an external command.
2344
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00002345extend({expr1}, {expr2} [, {expr3}]) *extend()*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002346 {expr1} and {expr2} must be both |Lists| or both
2347 |Dictionaries|.
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00002348
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002349 If they are |Lists|: Append {expr2} to {expr1}.
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00002350 If {expr3} is given insert the items of {expr2} before item
2351 {expr3} in {expr1}. When {expr3} is zero insert before the
2352 first item. When {expr3} is equal to len({expr1}) then
2353 {expr2} is appended.
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002354 Examples: >
2355 :echo sort(extend(mylist, [7, 5]))
2356 :call extend(mylist, [2, 3], 1)
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002357< Use |add()| to concatenate one item to a list. To concatenate
2358 two lists into a new list use the + operator: >
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002359 :let newlist = [1, 2, 3] + [4, 5]
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00002360<
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002361 If they are |Dictionaries|:
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00002362 Add all entries from {expr2} to {expr1}.
2363 If a key exists in both {expr1} and {expr2} then {expr3} is
2364 used to decide what to do:
2365 {expr3} = "keep": keep the value of {expr1}
2366 {expr3} = "force": use the value of {expr2}
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002367 {expr3} = "error": give an error message *E737*
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00002368 When {expr3} is omitted then "force" is assumed.
2369
2370 {expr1} is changed when {expr2} is not empty. If necessary
2371 make a copy of {expr1} first.
2372 {expr2} remains unchanged.
2373 Returns {expr1}.
2374
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002375
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376filereadable({file}) *filereadable()*
2377 The result is a Number, which is TRUE when a file with the
2378 name {file} exists, and can be read. If {file} doesn't exist,
2379 or is a directory, the result is FALSE. {file} is any
2380 expression, which is used as a String.
2381 *file_readable()*
2382 Obsolete name: file_readable().
2383
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002384
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00002385filter({expr}, {string}) *filter()*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002386 {expr} must be a |List| or a |Dictionary|.
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00002387 For each item in {expr} evaluate {string} and when the result
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002388 is zero remove the item from the |List| or |Dictionary|.
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00002389 Inside {string} |v:val| has the value of the current item.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002390 For a |Dictionary| |v:key| has the key of the current item.
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00002391 Examples: >
2392 :call filter(mylist, 'v:val !~ "OLD"')
2393< Removes the items where "OLD" appears. >
2394 :call filter(mydict, 'v:key >= 8')
2395< Removes the items with a key below 8. >
2396 :call filter(var, 0)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002397< Removes all the items, thus clears the |List| or |Dictionary|.
Bram Moolenaard8b02732005-01-14 21:48:43 +00002398
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00002399 Note that {string} is the result of expression and is then
2400 used as an expression again. Often it is good to use a
2401 |literal-string| to avoid having to double backslashes.
2402
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002403 The operation is done in-place. If you want a |List| or
2404 |Dictionary| to remain unmodified make a copy first: >
Bram Moolenaarafeb4fa2006-02-01 21:51:12 +00002405 :let l = filter(copy(mylist), 'v:val =~ "KEEP"')
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00002406
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002407< Returns {expr}, the |List| or |Dictionary| that was filtered.
Bram Moolenaar280f1262006-01-30 00:14:18 +00002408 When an error is encountered while evaluating {string} no
2409 further items in {expr} are processed.
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002410
2411
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00002412finddir({name}[, {path}[, {count}]]) *finddir()*
2413 Find directory {name} in {path}.
2414 If {path} is omitted or empty then 'path' is used.
2415 If the optional {count} is given, find {count}'s occurrence of
2416 {name} in {path}.
2417 This is quite similar to the ex-command |:find|.
2418 When the found directory is below the current directory a
2419 relative path is returned. Otherwise a full path is returned.
2420 Example: >
2421 :echo findfile("tags.vim", ".;")
2422< Searches from the current directory upwards until it finds
2423 the file "tags.vim".
2424 {only available when compiled with the +file_in_path feature}
2425
2426findfile({name}[, {path}[, {count}]]) *findfile()*
2427 Just like |finddir()|, but find a file instead of a directory.
2428
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429filewritable({file}) *filewritable()*
2430 The result is a Number, which is 1 when a file with the
2431 name {file} exists, and can be written. If {file} doesn't
2432 exist, or is not writable, the result is 0. If (file) is a
2433 directory, and we can write to it, the result is 2.
2434
2435fnamemodify({fname}, {mods}) *fnamemodify()*
2436 Modify file name {fname} according to {mods}. {mods} is a
2437 string of characters like it is used for file names on the
2438 command line. See |filename-modifiers|.
2439 Example: >
2440 :echo fnamemodify("main.c", ":p:h")
2441< results in: >
2442 /home/mool/vim/vim/src
2443< Note: Environment variables and "~" don't work in {fname}, use
2444 |expand()| first then.
2445
2446foldclosed({lnum}) *foldclosed()*
2447 The result is a Number. If the line {lnum} is in a closed
2448 fold, the result is the number of the first line in that fold.
2449 If the line {lnum} is not in a closed fold, -1 is returned.
2450
2451foldclosedend({lnum}) *foldclosedend()*
2452 The result is a Number. If the line {lnum} is in a closed
2453 fold, the result is the number of the last line in that fold.
2454 If the line {lnum} is not in a closed fold, -1 is returned.
2455
2456foldlevel({lnum}) *foldlevel()*
2457 The result is a Number, which is the foldlevel of line {lnum}
2458 in the current buffer. For nested folds the deepest level is
2459 returned. If there is no fold at line {lnum}, zero is
2460 returned. It doesn't matter if the folds are open or closed.
2461 When used while updating folds (from 'foldexpr') -1 is
2462 returned for lines where folds are still to be updated and the
2463 foldlevel is unknown. As a special case the level of the
2464 previous line is usually available.
2465
2466 *foldtext()*
2467foldtext() Returns a String, to be displayed for a closed fold. This is
2468 the default function used for the 'foldtext' option and should
2469 only be called from evaluating 'foldtext'. It uses the
2470 |v:foldstart|, |v:foldend| and |v:folddashes| variables.
2471 The returned string looks like this: >
2472 +-- 45 lines: abcdef
2473< The number of dashes depends on the foldlevel. The "45" is
2474 the number of lines in the fold. "abcdef" is the text in the
2475 first non-blank line of the fold. Leading white space, "//"
2476 or "/*" and the text from the 'foldmarker' and 'commentstring'
2477 options is removed.
2478 {not available when compiled without the |+folding| feature}
2479
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002480foldtextresult({lnum}) *foldtextresult()*
2481 Returns the text that is displayed for the closed fold at line
2482 {lnum}. Evaluates 'foldtext' in the appropriate context.
2483 When there is no closed fold at {lnum} an empty string is
2484 returned.
2485 {lnum} is used like with |getline()|. Thus "." is the current
2486 line, "'m" mark m, etc.
2487 Useful when exporting folded text, e.g., to HTML.
2488 {not available when compiled without the |+folding| feature}
2489
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 *foreground()*
2491foreground() Move the Vim window to the foreground. Useful when sent from
2492 a client to a Vim server. |remote_send()|
2493 On Win32 systems this might not work, the OS does not always
2494 allow a window to bring itself to the foreground. Use
2495 |remote_foreground()| instead.
2496 {only in the Win32, Athena, Motif and GTK GUI versions and the
2497 Win32 console version}
2498
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002499
Bram Moolenaar13065c42005-01-08 16:08:21 +00002500function({name}) *function()* *E700*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002501 Return a |Funcref| variable that refers to function {name}.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00002502 {name} can be a user defined function or an internal function.
2503
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002504
Bram Moolenaar39a58ca2005-06-27 22:42:44 +00002505garbagecollect() *garbagecollect()*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002506 Cleanup unused |Lists| and |Dictionaries| that have circular
Bram Moolenaar39a58ca2005-06-27 22:42:44 +00002507 references. There is hardly ever a need to invoke this
2508 function, as it is automatically done when Vim runs out of
2509 memory or is waiting for the user to press a key after
2510 'updatetime'. Items without circular references are always
2511 freed when they become unused.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002512 This is useful if you have deleted a very big |List| and/or
2513 |Dictionary| with circular references in a script that runs
2514 for a long time.
Bram Moolenaar39a58ca2005-06-27 22:42:44 +00002515
Bram Moolenaar677ee682005-01-27 14:41:15 +00002516get({list}, {idx} [, {default}]) *get()*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002517 Get item {idx} from |List| {list}. When this item is not
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002518 available return {default}. Return zero when {default} is
2519 omitted.
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00002520get({dict}, {key} [, {default}])
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002521 Get item with key {key} from |Dictionary| {dict}. When this
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00002522 item is not available return {default}. Return zero when
2523 {default} is omitted.
2524
Bram Moolenaar45360022005-07-21 21:08:21 +00002525 *getbufline()*
2526getbufline({expr}, {lnum} [, {end}])
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002527 Return a |List| with the lines starting from {lnum} to {end}
2528 (inclusive) in the buffer {expr}. If {end} is omitted, a
2529 |List| with only the line {lnum} is returned.
Bram Moolenaar45360022005-07-21 21:08:21 +00002530
2531 For the use of {expr}, see |bufname()| above.
2532
Bram Moolenaar661b1822005-07-28 22:36:45 +00002533 For {lnum} and {end} "$" can be used for the last line of the
2534 buffer. Otherwise a number must be used.
Bram Moolenaar45360022005-07-21 21:08:21 +00002535
2536 When {lnum} is smaller than 1 or bigger than the number of
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002537 lines in the buffer, an empty |List| is returned.
Bram Moolenaar45360022005-07-21 21:08:21 +00002538
2539 When {end} is greater than the number of lines in the buffer,
2540 it is treated as {end} is set to the number of lines in the
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002541 buffer. When {end} is before {lnum} an empty |List| is
Bram Moolenaar45360022005-07-21 21:08:21 +00002542 returned.
2543
Bram Moolenaar661b1822005-07-28 22:36:45 +00002544 This function works only for loaded buffers. For unloaded and
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002545 non-existing buffers, an empty |List| is returned.
Bram Moolenaar45360022005-07-21 21:08:21 +00002546
2547 Example: >
2548 :let lines = getbufline(bufnr("myfile"), 1, "$")
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002549
2550getbufvar({expr}, {varname}) *getbufvar()*
2551 The result is the value of option or local buffer variable
2552 {varname} in buffer {expr}. Note that the name without "b:"
2553 must be used.
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002554 This also works for a global or buffer-local option, but it
2555 doesn't work for a global variable, window-local variable or
2556 window-local option.
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002557 For the use of {expr}, see |bufname()| above.
2558 When the buffer or variable doesn't exist an empty string is
2559 returned, there is no error message.
2560 Examples: >
2561 :let bufmodified = getbufvar(1, "&mod")
2562 :echo "todo myvar = " . getbufvar("todo", "myvar")
2563<
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564getchar([expr]) *getchar()*
2565 Get a single character from the user. If it is an 8-bit
2566 character, the result is a number. Otherwise a String is
2567 returned with the encoded character. For a special key it's a
2568 sequence of bytes starting with 0x80 (decimal: 128).
2569 If [expr] is omitted, wait until a character is available.
2570 If [expr] is 0, only get a character when one is available.
2571 If [expr] is 1, only check if a character is available, it is
2572 not consumed. If a normal character is
2573 available, it is returned, otherwise a
2574 non-zero value is returned.
2575 If a normal character available, it is returned as a Number.
2576 Use nr2char() to convert it to a String.
2577 The returned value is zero if no character is available.
2578 The returned value is a string of characters for special keys
2579 and when a modifier (shift, control, alt) was used.
2580 There is no prompt, you will somehow have to make clear to the
2581 user that a character has to be typed.
2582 There is no mapping for the character.
2583 Key codes are replaced, thus when the user presses the <Del>
2584 key you get the code for the <Del> key, not the raw character
2585 sequence. Examples: >
2586 getchar() == "\<Del>"
2587 getchar() == "\<S-Left>"
2588< This example redefines "f" to ignore case: >
2589 :nmap f :call FindChar()<CR>
2590 :function FindChar()
2591 : let c = nr2char(getchar())
2592 : while col('.') < col('$') - 1
2593 : normal l
2594 : if getline('.')[col('.') - 1] ==? c
2595 : break
2596 : endif
2597 : endwhile
2598 :endfunction
2599
2600getcharmod() *getcharmod()*
2601 The result is a Number which is the state of the modifiers for
2602 the last obtained character with getchar() or in another way.
2603 These values are added together:
2604 2 shift
2605 4 control
2606 8 alt (meta)
2607 16 mouse double click
2608 32 mouse triple click
2609 64 mouse quadruple click
2610 128 Macintosh only: command
2611 Only the modifiers that have not been included in the
2612 character itself are obtained. Thus Shift-a results in "A"
2613 with no modifier.
2614
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615getcmdline() *getcmdline()*
2616 Return the current command-line. Only works when the command
2617 line is being edited, thus requires use of |c_CTRL-\_e| or
2618 |c_CTRL-R_=|.
2619 Example: >
2620 :cmap <F7> <C-\>eescape(getcmdline(), ' \')<CR>
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002621< Also see |getcmdtype()|, |getcmdpos()| and |setcmdpos()|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002623getcmdpos() *getcmdpos()*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 Return the position of the cursor in the command line as a
2625 byte count. The first column is 1.
2626 Only works when editing the command line, thus requires use of
2627 |c_CTRL-\_e| or |c_CTRL-R_=|. Returns 0 otherwise.
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002628 Also see |getcmdtype()|, |setcmdpos()| and |getcmdline()|.
2629
2630getcmdtype() *getcmdtype()*
2631 Return the current command-line type. Possible return values
2632 are:
Bram Moolenaar1e015462005-09-25 22:16:38 +00002633 : normal Ex command
2634 > debug mode command |debug-mode|
2635 / forward search command
2636 ? backward search command
2637 @ |input()| command
2638 - |:insert| or |:append| command
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002639 Only works when editing the command line, thus requires use of
2640 |c_CTRL-\_e| or |c_CTRL-R_=|. Returns an empty string
2641 otherwise.
2642 Also see |getcmdpos()|, |setcmdpos()| and |getcmdline()|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643
2644 *getcwd()*
2645getcwd() The result is a String, which is the name of the current
2646 working directory.
2647
2648getfsize({fname}) *getfsize()*
2649 The result is a Number, which is the size in bytes of the
2650 given file {fname}.
2651 If {fname} is a directory, 0 is returned.
2652 If the file {fname} can't be found, -1 is returned.
2653
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002654getfontname([{name}]) *getfontname()*
2655 Without an argument returns the name of the normal font being
2656 used. Like what is used for the Normal highlight group
2657 |hl-Normal|.
2658 With an argument a check is done whether {name} is a valid
2659 font name. If not then an empty string is returned.
2660 Otherwise the actual font name is returned, or {name} if the
2661 GUI does not support obtaining the real name.
2662 Only works when the GUI is running, thus not you your vimrc or
2663 Note that the GTK 2 GUI accepts any font name, thus checking
2664 for a valid name does not work.
2665 gvimrc file. Use the |GUIEnter| autocommand to use this
2666 function just after the GUI has started.
2667
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002668getfperm({fname}) *getfperm()*
2669 The result is a String, which is the read, write, and execute
2670 permissions of the given file {fname}.
2671 If {fname} does not exist or its directory cannot be read, an
2672 empty string is returned.
2673 The result is of the form "rwxrwxrwx", where each group of
2674 "rwx" flags represent, in turn, the permissions of the owner
2675 of the file, the group the file belongs to, and other users.
2676 If a user does not have a given permission the flag for this
2677 is replaced with the string "-". Example: >
2678 :echo getfperm("/etc/passwd")
2679< This will hopefully (from a security point of view) display
2680 the string "rw-r--r--" or even "rw-------".
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682getftime({fname}) *getftime()*
2683 The result is a Number, which is the last modification time of
2684 the given file {fname}. The value is measured as seconds
2685 since 1st Jan 1970, and may be passed to strftime(). See also
2686 |localtime()| and |strftime()|.
2687 If the file {fname} can't be found -1 is returned.
2688
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002689getftype({fname}) *getftype()*
2690 The result is a String, which is a description of the kind of
2691 file of the given file {fname}.
2692 If {fname} does not exist an empty string is returned.
2693 Here is a table over different kinds of files and their
2694 results:
2695 Normal file "file"
2696 Directory "dir"
2697 Symbolic link "link"
2698 Block device "bdev"
2699 Character device "cdev"
2700 Socket "socket"
2701 FIFO "fifo"
2702 All other "other"
2703 Example: >
2704 getftype("/home")
2705< Note that a type such as "link" will only be returned on
2706 systems that support it. On some systems only "dir" and
2707 "file" are returned.
2708
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 *getline()*
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002710getline({lnum} [, {end}])
2711 Without {end} the result is a String, which is line {lnum}
2712 from the current buffer. Example: >
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 getline(1)
2714< When {lnum} is a String that doesn't start with a
2715 digit, line() is called to translate the String into a Number.
2716 To get the line under the cursor: >
2717 getline(".")
2718< When {lnum} is smaller than 1 or bigger than the number of
2719 lines in the buffer, an empty string is returned.
2720
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002721 When {end} is given the result is a |List| where each item is
2722 a line from the current buffer in the range {lnum} to {end},
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002723 including line {end}.
2724 {end} is used in the same way as {lnum}.
2725 Non-existing lines are silently omitted.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002726 When {end} is before {lnum} an empty |List| is returned.
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002727 Example: >
2728 :let start = line('.')
2729 :let end = search("^$") - 1
2730 :let lines = getline(start, end)
2731
Bram Moolenaar17c7c012006-01-26 22:25:15 +00002732getloclist({nr}) *getloclist()*
2733 Returns a list with all the entries in the location list for
2734 window {nr}. When {nr} is zero the current window is used.
2735 For a location list window, the displayed location list is
Bram Moolenaar280f1262006-01-30 00:14:18 +00002736 returned. For an invalid window number {nr}, an empty list is
2737 returned. Otherwise, same as getqflist().
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002738
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002739getqflist() *getqflist()*
2740 Returns a list with all the current quickfix errors. Each
2741 list item is a dictionary with these entries:
2742 bufnr number of buffer that has the file name, use
2743 bufname() to get the name
2744 lnum line number in the buffer (first line is 1)
2745 col column number (first column is 1)
Bram Moolenaar582fd852005-03-28 20:58:01 +00002746 vcol non-zero: "col" is visual column
2747 zero: "col" is byte index
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002748 nr error number
2749 text description of the error
2750 type type of the error, 'E', '1', etc.
2751 valid non-zero: recognized error message
2752
Bram Moolenaare7eb9df2005-09-09 19:49:30 +00002753 When there is no error list or it's empty an empty list is
2754 returned.
2755
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002756 Useful application: Find pattern matches in multiple files and
2757 do something with them: >
2758 :vimgrep /theword/jg *.c
2759 :for d in getqflist()
2760 : echo bufname(d.bufnr) ':' d.lnum '=' d.text
2761 :endfor
2762
2763
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00002764getreg([{regname} [, 1]]) *getreg()*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 The result is a String, which is the contents of register
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002766 {regname}. Example: >
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 :let cliptext = getreg('*')
2768< getreg('=') returns the last evaluated value of the expression
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002769 register. (For use in maps.)
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00002770 getreg('=', 1) returns the expression itself, so that it can
2771 be restored with |setreg()|. For other registers the extra
2772 argument is ignored, thus you can always give it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 If {regname} is not specified, |v:register| is used.
2774
Bram Moolenaara14de3d2005-01-07 21:48:26 +00002775
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776getregtype([{regname}]) *getregtype()*
2777 The result is a String, which is type of register {regname}.
2778 The value will be one of:
2779 "v" for |characterwise| text
2780 "V" for |linewise| text
2781 "<CTRL-V>{width}" for |blockwise-visual| text
2782 0 for an empty or unknown register
2783 <CTRL-V> is one character with value 0x16.
2784 If {regname} is not specified, |v:register| is used.
2785
2786 *getwinposx()*
2787getwinposx() The result is a Number, which is the X coordinate in pixels of
2788 the left hand side of the GUI Vim window. The result will be
2789 -1 if the information is not available.
2790
2791 *getwinposy()*
2792getwinposy() The result is a Number, which is the Y coordinate in pixels of
2793 the top of the GUI Vim window. The result will be -1 if the
2794 information is not available.
2795
2796getwinvar({nr}, {varname}) *getwinvar()*
2797 The result is the value of option or local window variable
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002798 {varname} in window {nr}. When {nr} is zero the current
2799 window is used.
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002800 This also works for a global option, buffer-local option and
2801 window-local option, but it doesn't work for a global variable
2802 or buffer-local variable.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 Note that the name without "w:" must be used.
2804 Examples: >
2805 :let list_is_on = getwinvar(2, '&list')
2806 :echo "myvar = " . getwinvar(1, 'myvar')
2807<
2808 *glob()*
2809glob({expr}) Expand the file wildcards in {expr}. The result is a String.
2810 When there are several matches, they are separated by <NL>
2811 characters.
2812 If the expansion fails, the result is an empty string.
2813 A name for a non-existing file is not included.
2814
2815 For most systems backticks can be used to get files names from
2816 any external command. Example: >
2817 :let tagfiles = glob("`find . -name tags -print`")
2818 :let &tags = substitute(tagfiles, "\n", ",", "g")
2819< The result of the program inside the backticks should be one
2820 item per line. Spaces inside an item are allowed.
2821
2822 See |expand()| for expanding special Vim variables. See
2823 |system()| for getting the raw output of an external command.
2824
2825globpath({path}, {expr}) *globpath()*
2826 Perform glob() on all directories in {path} and concatenate
2827 the results. Example: >
2828 :echo globpath(&rtp, "syntax/c.vim")
2829< {path} is a comma-separated list of directory names. Each
2830 directory name is prepended to {expr} and expanded like with
2831 glob(). A path separator is inserted when needed.
2832 To add a comma inside a directory name escape it with a
2833 backslash. Note that on MS-Windows a directory may have a
2834 trailing backslash, remove it if you put a comma after it.
2835 If the expansion fails for one of the directories, there is no
2836 error message.
2837 The 'wildignore' option applies: Names matching one of the
2838 patterns in 'wildignore' will be skipped.
2839
Bram Moolenaar02743632005-07-25 20:42:36 +00002840 The "**" item can be used to search in a directory tree.
2841 For example, to find all "README.txt" files in the directories
2842 in 'runtimepath' and below: >
2843 :echo globpath(&rtp, "**/README.txt")
2844<
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 *has()*
2846has({feature}) The result is a Number, which is 1 if the feature {feature} is
2847 supported, zero otherwise. The {feature} argument is a
2848 string. See |feature-list| below.
2849 Also see |exists()|.
2850
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00002851
2852has_key({dict}, {key}) *has_key()*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002853 The result is a Number, which is 1 if |Dictionary| {dict} has
2854 an entry with key {key}. Zero otherwise.
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00002855
2856
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857hasmapto({what} [, {mode}]) *hasmapto()*
2858 The result is a Number, which is 1 if there is a mapping that
2859 contains {what} in somewhere in the rhs (what it is mapped to)
2860 and this mapping exists in one of the modes indicated by
2861 {mode}.
2862 Both the global mappings and the mappings local to the current
2863 buffer are checked for a match.
2864 If no matching mapping is found 0 is returned.
2865 The following characters are recognized in {mode}:
2866 n Normal mode
2867 v Visual mode
2868 o Operator-pending mode
2869 i Insert mode
2870 l Language-Argument ("r", "f", "t", etc.)
2871 c Command-line mode
2872 When {mode} is omitted, "nvo" is used.
2873
2874 This function is useful to check if a mapping already exists
2875 to a function in a Vim script. Example: >
2876 :if !hasmapto('\ABCdoit')
2877 : map <Leader>d \ABCdoit
2878 :endif
2879< This installs the mapping to "\ABCdoit" only if there isn't
2880 already a mapping to "\ABCdoit".
2881
2882histadd({history}, {item}) *histadd()*
2883 Add the String {item} to the history {history} which can be
2884 one of: *hist-names*
2885 "cmd" or ":" command line history
2886 "search" or "/" search pattern history
2887 "expr" or "=" typed expression history
2888 "input" or "@" input line history
2889 If {item} does already exist in the history, it will be
2890 shifted to become the newest entry.
2891 The result is a Number: 1 if the operation was successful,
2892 otherwise 0 is returned.
2893
2894 Example: >
2895 :call histadd("input", strftime("%Y %b %d"))
2896 :let date=input("Enter date: ")
2897< This function is not available in the |sandbox|.
2898
2899histdel({history} [, {item}]) *histdel()*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002900 Clear {history}, i.e. delete all its entries. See |hist-names|
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 for the possible values of {history}.
2902
2903 If the parameter {item} is given as String, this is seen
2904 as regular expression. All entries matching that expression
2905 will be removed from the history (if there are any).
2906 Upper/lowercase must match, unless "\c" is used |/\c|.
2907 If {item} is a Number, it will be interpreted as index, see
2908 |:history-indexing|. The respective entry will be removed
2909 if it exists.
2910
2911 The result is a Number: 1 for a successful operation,
2912 otherwise 0 is returned.
2913
2914 Examples:
2915 Clear expression register history: >
2916 :call histdel("expr")
2917<
2918 Remove all entries starting with "*" from the search history: >
2919 :call histdel("/", '^\*')
2920<
2921 The following three are equivalent: >
2922 :call histdel("search", histnr("search"))
2923 :call histdel("search", -1)
2924 :call histdel("search", '^'.histget("search", -1).'$')
2925<
2926 To delete the last search pattern and use the last-but-one for
2927 the "n" command and 'hlsearch': >
2928 :call histdel("search", -1)
2929 :let @/ = histget("search", -1)
2930
2931histget({history} [, {index}]) *histget()*
2932 The result is a String, the entry with Number {index} from
2933 {history}. See |hist-names| for the possible values of
2934 {history}, and |:history-indexing| for {index}. If there is
2935 no such entry, an empty String is returned. When {index} is
2936 omitted, the most recent item from the history is used.
2937
2938 Examples:
2939 Redo the second last search from history. >
2940 :execute '/' . histget("search", -2)
2941
2942< Define an Ex command ":H {num}" that supports re-execution of
2943 the {num}th entry from the output of |:history|. >
2944 :command -nargs=1 H execute histget("cmd", 0+<args>)
2945<
2946histnr({history}) *histnr()*
2947 The result is the Number of the current entry in {history}.
2948 See |hist-names| for the possible values of {history}.
2949 If an error occurred, -1 is returned.
2950
2951 Example: >
2952 :let inp_index = histnr("expr")
2953<
2954hlexists({name}) *hlexists()*
2955 The result is a Number, which is non-zero if a highlight group
2956 called {name} exists. This is when the group has been
2957 defined in some way. Not necessarily when highlighting has
2958 been defined for it, it may also have been used for a syntax
2959 item.
2960 *highlight_exists()*
2961 Obsolete name: highlight_exists().
2962
2963 *hlID()*
2964hlID({name}) The result is a Number, which is the ID of the highlight group
2965 with name {name}. When the highlight group doesn't exist,
2966 zero is returned.
2967 This can be used to retrieve information about the highlight
2968 group. For example, to get the background color of the
2969 "Comment" group: >
2970 :echo synIDattr(synIDtrans(hlID("Comment")), "bg")
2971< *highlightID()*
2972 Obsolete name: highlightID().
2973
2974hostname() *hostname()*
2975 The result is a String, which is the name of the machine on
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002976 which Vim is currently running. Machine names greater than
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 256 characters long are truncated.
2978
2979iconv({expr}, {from}, {to}) *iconv()*
2980 The result is a String, which is the text {expr} converted
2981 from encoding {from} to encoding {to}.
2982 When the conversion fails an empty string is returned.
2983 The encoding names are whatever the iconv() library function
2984 can accept, see ":!man 3 iconv".
2985 Most conversions require Vim to be compiled with the |+iconv|
2986 feature. Otherwise only UTF-8 to latin1 conversion and back
2987 can be done.
2988 This can be used to display messages with special characters,
2989 no matter what 'encoding' is set to. Write the message in
2990 UTF-8 and use: >
2991 echo iconv(utf8_str, "utf-8", &enc)
2992< Note that Vim uses UTF-8 for all Unicode encodings, conversion
2993 from/to UCS-2 is automatically changed to use UTF-8. You
2994 cannot use UCS-2 in a string anyway, because of the NUL bytes.
2995 {only available when compiled with the +multi_byte feature}
2996
2997 *indent()*
2998indent({lnum}) The result is a Number, which is indent of line {lnum} in the
2999 current buffer. The indent is counted in spaces, the value
3000 of 'tabstop' is relevant. {lnum} is used just like in
3001 |getline()|.
3002 When {lnum} is invalid -1 is returned.
3003
Bram Moolenaarde8866b2005-01-06 23:24:37 +00003004
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003005index({list}, {expr} [, {start} [, {ic}]]) *index()*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003006 Return the lowest index in |List| {list} where the item has a
Bram Moolenaarde8866b2005-01-06 23:24:37 +00003007 value equal to {expr}.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003008 If {start} is given then start looking at the item with index
3009 {start} (may be negative for an item relative to the end).
Bram Moolenaarde8866b2005-01-06 23:24:37 +00003010 When {ic} is given and it is non-zero, ignore case. Otherwise
3011 case must match.
3012 -1 is returned when {expr} is not found in {list}.
3013 Example: >
3014 :let idx = index(words, "the")
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003015 :if index(numbers, 123) >= 0
Bram Moolenaarde8866b2005-01-06 23:24:37 +00003016
3017
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003018input({prompt} [, {text} [, {completion}]]) *input()*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 The result is a String, which is whatever the user typed on
3020 the command-line. The parameter is either a prompt string, or
3021 a blank string (for no prompt). A '\n' can be used in the
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003022 prompt to start a new line.
3023 The highlighting set with |:echohl| is used for the prompt.
3024 The input is entered just like a command-line, with the same
3025 editing commands and mappings. There is a separate history
3026 for lines typed for input().
3027 Example: >
3028 :if input("Coffee or beer? ") == "beer"
3029 : echo "Cheers!"
3030 :endif
3031<
Bram Moolenaar1e015462005-09-25 22:16:38 +00003032 If the optional {text} is present and not empty, this is used
3033 for the default reply, as if the user typed this. Example: >
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003034 :let color = input("Color? ", "white")
3035
3036< The optional {completion} argument specifies the type of
3037 completion supported for the input. Without it completion is
3038 not performed. The supported completion types are the same as
3039 that can be supplied to a user-defined command using the
3040 "-complete=" argument. Refer to |:command-completion| for
3041 more information. Example: >
3042 let fname = input("File: ", "", "file")
3043<
3044 NOTE: This function must not be used in a startup file, for
3045 the versions that only run in GUI mode (e.g., the Win32 GUI).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 Note: When input() is called from within a mapping it will
3047 consume remaining characters from that mapping, because a
3048 mapping is handled like the characters were typed.
3049 Use |inputsave()| before input() and |inputrestore()|
3050 after input() to avoid that. Another solution is to avoid
3051 that further characters follow in the mapping, e.g., by using
3052 |:execute| or |:normal|.
3053
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003054 Example with a mapping: >
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 :nmap \x :call GetFoo()<CR>:exe "/" . Foo<CR>
3056 :function GetFoo()
3057 : call inputsave()
3058 : let g:Foo = input("enter search pattern: ")
3059 : call inputrestore()
3060 :endfunction
3061
3062inputdialog({prompt} [, {text} [, {cancelreturn}]]) *inputdialog()*
3063 Like input(), but when the GUI is running and text dialogs are
3064 supported, a dialog window pops up to input the text.
3065 Example: >
3066 :let n = inputdialog("value for shiftwidth", &sw)
3067 :if n != ""
3068 : let &sw = n
3069 :endif
3070< When the dialog is cancelled {cancelreturn} is returned. When
3071 omitted an empty string is returned.
3072 Hitting <Enter> works like pressing the OK button. Hitting
3073 <Esc> works like pressing the Cancel button.
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003074 NOTE: Command-line completion is not supported.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003076inputlist({textlist}) *inputlist()*
3077 {textlist} must be a list of strings. This list is displayed,
3078 one string per line. The user will be prompted to enter a
3079 number, which is returned.
3080 The user can also select an item by clicking on it with the
3081 mouse. For the first string 0 is returned. When clicking
3082 above the first item a negative number is returned. When
3083 clicking on the prompt one more than the length of {textlist}
3084 is returned.
3085 Make sure {textlist} has less then 'lines' entries, otherwise
3086 it won't work. It's a good idea to put the entry number at
3087 the start of the string. Example: >
3088 let color = inputlist(['Select color:', '1. red',
3089 \ '2. green', '3. blue'])
3090
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091inputrestore() *inputrestore()*
3092 Restore typeahead that was saved with a previous inputsave().
3093 Should be called the same number of times inputsave() is
3094 called. Calling it more often is harmless though.
3095 Returns 1 when there is nothing to restore, 0 otherwise.
3096
3097inputsave() *inputsave()*
3098 Preserve typeahead (also from mappings) and clear it, so that
3099 a following prompt gets input from the user. Should be
3100 followed by a matching inputrestore() after the prompt. Can
3101 be used several times, in which case there must be just as
3102 many inputrestore() calls.
3103 Returns 1 when out of memory, 0 otherwise.
3104
3105inputsecret({prompt} [, {text}]) *inputsecret()*
3106 This function acts much like the |input()| function with but
3107 two exceptions:
3108 a) the user's response will be displayed as a sequence of
3109 asterisks ("*") thereby keeping the entry secret, and
3110 b) the user's response will not be recorded on the input
3111 |history| stack.
3112 The result is a String, which is whatever the user actually
3113 typed on the command-line in response to the issued prompt.
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003114 NOTE: Command-line completion is not supported.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00003116insert({list}, {item} [, {idx}]) *insert()*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003117 Insert {item} at the start of |List| {list}.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00003118 If {idx} is specified insert {item} before the item with index
3119 {idx}. If {idx} is zero it goes before the first item, just
3120 like omitting {idx}. A negative {idx} is also possible, see
3121 |list-index|. -1 inserts just before the last item.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003122 Returns the resulting |List|. Examples: >
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00003123 :let mylist = insert([2, 3, 5], 1)
3124 :call insert(mylist, 4, -1)
3125 :call insert(mylist, 6, len(mylist))
Bram Moolenaara14de3d2005-01-07 21:48:26 +00003126< The last example can be done simpler with |add()|.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003127 Note that when {item} is a |List| it is inserted as a single
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003128 item. Use |extend()| to concatenate |Lists|.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00003129
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130isdirectory({directory}) *isdirectory()*
3131 The result is a Number, which is non-zero when a directory
3132 with the name {directory} exists. If {directory} doesn't
3133 exist, or isn't a directory, the result is FALSE. {directory}
3134 is any expression, which is used as a String.
3135
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00003136islocked({expr}) *islocked()*
3137 The result is a Number, which is non-zero when {expr} is the
3138 name of a locked variable.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003139 {expr} must be the name of a variable, |List| item or
3140 |Dictionary| entry, not the variable itself! Example: >
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00003141 :let alist = [0, ['a', 'b'], 2, 3]
3142 :lockvar 1 alist
3143 :echo islocked('alist') " 1
3144 :echo islocked('alist[1]') " 0
3145
3146< When {expr} is a variable that does not exist you get an error
3147 message. Use |exists()| to check for existance.
3148
Bram Moolenaar677ee682005-01-27 14:41:15 +00003149items({dict}) *items()*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003150 Return a |List| with all the key-value pairs of {dict}. Each
3151 |List| item is a list with two items: the key of a {dict}
3152 entry and the value of this entry. The |List| is in arbitrary
3153 order.
Bram Moolenaar677ee682005-01-27 14:41:15 +00003154
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003155
3156join({list} [, {sep}]) *join()*
3157 Join the items in {list} together into one String.
3158 When {sep} is specified it is put in between the items. If
3159 {sep} is omitted a single space is used.
3160 Note that {sep} is not added at the end. You might want to
3161 add it there too: >
3162 let lines = join(mylist, "\n") . "\n"
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003163< String items are used as-is. |Lists| and |Dictionaries| are
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003164 converted into a string like with |string()|.
3165 The opposite function is |split()|.
3166
Bram Moolenaard8b02732005-01-14 21:48:43 +00003167keys({dict}) *keys()*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003168 Return a |List| with all the keys of {dict}. The |List| is in
Bram Moolenaard8b02732005-01-14 21:48:43 +00003169 arbitrary order.
3170
Bram Moolenaar13065c42005-01-08 16:08:21 +00003171 *len()* *E701*
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00003172len({expr}) The result is a Number, which is the length of the argument.
3173 When {expr} is a String or a Number the length in bytes is
3174 used, as with |strlen()|.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003175 When {expr} is a |List| the number of items in the |List| is
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00003176 returned.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003177 When {expr} is a |Dictionary| the number of entries in the
3178 |Dictionary| is returned.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00003179 Otherwise an error is given.
3180
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 *libcall()* *E364* *E368*
3182libcall({libname}, {funcname}, {argument})
3183 Call function {funcname} in the run-time library {libname}
3184 with single argument {argument}.
3185 This is useful to call functions in a library that you
3186 especially made to be used with Vim. Since only one argument
3187 is possible, calling standard library functions is rather
3188 limited.
3189 The result is the String returned by the function. If the
3190 function returns NULL, this will appear as an empty string ""
3191 to Vim.
3192 If the function returns a number, use libcallnr()!
3193 If {argument} is a number, it is passed to the function as an
3194 int; if {argument} is a string, it is passed as a
3195 null-terminated string.
3196 This function will fail in |restricted-mode|.
3197
3198 libcall() allows you to write your own 'plug-in' extensions to
3199 Vim without having to recompile the program. It is NOT a
3200 means to call system functions! If you try to do so Vim will
3201 very probably crash.
3202
3203 For Win32, the functions you write must be placed in a DLL
3204 and use the normal C calling convention (NOT Pascal which is
3205 used in Windows System DLLs). The function must take exactly
3206 one parameter, either a character pointer or a long integer,
3207 and must return a character pointer or NULL. The character
3208 pointer returned must point to memory that will remain valid
3209 after the function has returned (e.g. in static data in the
3210 DLL). If it points to allocated memory, that memory will
3211 leak away. Using a static buffer in the function should work,
3212 it's then freed when the DLL is unloaded.
3213
3214 WARNING: If the function returns a non-valid pointer, Vim may
3215 crash! This also happens if the function returns a number,
3216 because Vim thinks it's a pointer.
3217 For Win32 systems, {libname} should be the filename of the DLL
3218 without the ".DLL" suffix. A full path is only required if
3219 the DLL is not in the usual places.
3220 For Unix: When compiling your own plugins, remember that the
3221 object code must be compiled as position-independent ('PIC').
3222 {only in Win32 on some Unix versions, when the |+libcall|
3223 feature is present}
3224 Examples: >
3225 :echo libcall("libc.so", "getenv", "HOME")
3226 :echo libcallnr("/usr/lib/libc.so", "getpid", "")
3227<
3228 *libcallnr()*
3229libcallnr({libname}, {funcname}, {argument})
3230 Just like libcall(), but used for a function that returns an
3231 int instead of a string.
3232 {only in Win32 on some Unix versions, when the |+libcall|
3233 feature is present}
3234 Example (not very useful...): >
3235 :call libcallnr("libc.so", "printf", "Hello World!\n")
3236 :call libcallnr("libc.so", "sleep", 10)
3237<
3238 *line()*
3239line({expr}) The result is a Number, which is the line number of the file
3240 position given with {expr}. The accepted positions are:
3241 . the cursor position
3242 $ the last line in the current buffer
3243 'x position of mark x (if the mark is not set, 0 is
3244 returned)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003245 w0 first line visible in current window
3246 w$ last line visible in current window
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003247 Note that a mark in another file can be used.
Bram Moolenaar0b238792006-03-02 22:49:12 +00003248 To get the column number use |col()|. To get both use
3249 |getpos()|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 Examples: >
3251 line(".") line number of the cursor
3252 line("'t") line number of mark t
3253 line("'" . marker) line number of mark marker
3254< *last-position-jump*
3255 This autocommand jumps to the last known position in a file
3256 just after opening it, if the '" mark is set: >
3257 :au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal g'\"" | endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003258
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259line2byte({lnum}) *line2byte()*
3260 Return the byte count from the start of the buffer for line
3261 {lnum}. This includes the end-of-line character, depending on
3262 the 'fileformat' option for the current buffer. The first
3263 line returns 1.
3264 This can also be used to get the byte count for the line just
3265 below the last line: >
3266 line2byte(line("$") + 1)
3267< This is the file size plus one.
3268 When {lnum} is invalid, or the |+byte_offset| feature has been
3269 disabled at compile time, -1 is returned.
3270 Also see |byte2line()|, |go| and |:goto|.
3271
3272lispindent({lnum}) *lispindent()*
3273 Get the amount of indent for line {lnum} according the lisp
3274 indenting rules, as with 'lisp'.
3275 The indent is counted in spaces, the value of 'tabstop' is
3276 relevant. {lnum} is used just like in |getline()|.
3277 When {lnum} is invalid or Vim was not compiled the
3278 |+lispindent| feature, -1 is returned.
3279
3280localtime() *localtime()*
3281 Return the current time, measured as seconds since 1st Jan
3282 1970. See also |strftime()| and |getftime()|.
3283
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003284
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00003285map({expr}, {string}) *map()*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003286 {expr} must be a |List| or a |Dictionary|.
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00003287 Replace each item in {expr} with the result of evaluating
3288 {string}.
3289 Inside {string} |v:val| has the value of the current item.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003290 For a |Dictionary| |v:key| has the key of the current item.
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00003291 Example: >
3292 :call map(mylist, '"> " . v:val . " <"')
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003293< This puts "> " before and " <" after each item in "mylist".
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00003294
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003295 Note that {string} is the result of an expression and is then
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00003296 used as an expression again. Often it is good to use a
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003297 |literal-string| to avoid having to double backslashes. You
3298 still have to double ' quotes
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00003299
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003300 The operation is done in-place. If you want a |List| or
3301 |Dictionary| to remain unmodified make a copy first: >
Bram Moolenaard8b02732005-01-14 21:48:43 +00003302 :let tlist = map(copy(mylist), ' & . "\t"')
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00003303
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003304< Returns {expr}, the |List| or |Dictionary| that was filtered.
Bram Moolenaar280f1262006-01-30 00:14:18 +00003305 When an error is encountered while evaluating {string} no
3306 further items in {expr} are processed.
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003307
3308
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309maparg({name}[, {mode}]) *maparg()*
3310 Return the rhs of mapping {name} in mode {mode}. When there
3311 is no mapping for {name}, an empty String is returned.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003312 {mode} can be one of these strings:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 "n" Normal
3314 "v" Visual
3315 "o" Operator-pending
3316 "i" Insert
3317 "c" Cmd-line
3318 "l" langmap |language-mapping|
3319 "" Normal, Visual and Operator-pending
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003320 When {mode} is omitted, the modes for "" are used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 The {name} can have special key names, like in the ":map"
3322 command. The returned String has special characters
3323 translated like in the output of the ":map" command listing.
3324 The mappings local to the current buffer are checked first,
3325 then the global mappings.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003326 This function can be used to map a key even when it's already
3327 mapped, and have it do the original mapping too. Sketch: >
3328 exe 'nnoremap <Tab> ==' . maparg('<Tab>', 'n')
3329
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330
3331mapcheck({name}[, {mode}]) *mapcheck()*
3332 Check if there is a mapping that matches with {name} in mode
3333 {mode}. See |maparg()| for {mode} and special names in
3334 {name}.
3335 A match happens with a mapping that starts with {name} and
3336 with a mapping which is equal to the start of {name}.
3337
3338 matches mapping "a" "ab" "abc" ~
3339 mapcheck("a") yes yes yes
3340 mapcheck("abc") yes yes yes
3341 mapcheck("ax") yes no no
3342 mapcheck("b") no no no
3343
3344 The difference with maparg() is that mapcheck() finds a
3345 mapping that matches with {name}, while maparg() only finds a
3346 mapping for {name} exactly.
3347 When there is no mapping that starts with {name}, an empty
3348 String is returned. If there is one, the rhs of that mapping
3349 is returned. If there are several mappings that start with
3350 {name}, the rhs of one of them is returned.
3351 The mappings local to the current buffer are checked first,
3352 then the global mappings.
3353 This function can be used to check if a mapping can be added
3354 without being ambiguous. Example: >
3355 :if mapcheck("_vv") == ""
3356 : map _vv :set guifont=7x13<CR>
3357 :endif
3358< This avoids adding the "_vv" mapping when there already is a
3359 mapping for "_v" or for "_vvv".
3360
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00003361match({expr}, {pat}[, {start}[, {count}]]) *match()*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003362 When {expr} is a |List| then this returns the index of the
3363 first item where {pat} matches. Each item is used as a
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003364 String, |Lists| and |Dictionaries| are used as echoed.
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003365 Otherwise, {expr} is used as a String. The result is a
3366 Number, which gives the index (byte offset) in {expr} where
3367 {pat} matches.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003368 A match at the first character or |List| item returns zero.
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00003369 If there is no match -1 is returned.
3370 Example: >
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003371 :echo match("testing", "ing") " results in 4
3372 :echo match([1, 'x'], '\a') " results in 2
3373< See |string-match| for how {pat} is used.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003374 *strpbrk()*
3375 Vim doesn't have a strpbrk() function. But you can do: >
3376 :let sepidx = match(line, '[.,;: \t]')
3377< *strcasestr()*
3378 Vim doesn't have a strcasestr() function. But you can add
3379 "\c" to the pattern to ignore case: >
3380 :let idx = match(haystack, '\cneedle')
3381<
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003382 If {start} is given, the search starts from byte index
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003383 {start} in a String or item {start} in a |List|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 The result, however, is still the index counted from the
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003385 first character/item. Example: >
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 :echo match("testing", "ing", 2)
3387< result is again "4". >
3388 :echo match("testing", "ing", 4)
3389< result is again "4". >
3390 :echo match("testing", "t", 2)
3391< result is "3".
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003392 For a String, if {start} > 0 then it is like the string starts
Bram Moolenaar0b238792006-03-02 22:49:12 +00003393 {start} bytes later, thus "^" will match at {start}. Except
3394 when {count} is given, then it's like matches before the
3395 {start} byte are ignored (this is a bit complicated to keep it
3396 backwards compatible).
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003397 For a String, if {start} < 0, it will be set to 0. For a list
3398 the index is counted from the end.
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003399 If {start} is out of range ({start} > strlen({expr}) for a
3400 String or {start} > len({expr}) for a |List|) -1 is returned.
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003401
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003402 When {count} is given use the {count}'th match. When a match
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003403 is found in a String the search for the next one starts one
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003404 character further. Thus this example results in 1: >
3405 echo match("testing", "..", 0, 2)
3406< In a |List| the search continues in the next item.
Bram Moolenaar0b238792006-03-02 22:49:12 +00003407 Note that when {count} is added the way {start} works changes,
3408 see above.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003409
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 See |pattern| for the patterns that are accepted.
3411 The 'ignorecase' option is used to set the ignore-caseness of
3412 the pattern. 'smartcase' is NOT used. The matching is always
3413 done like 'magic' is set and 'cpoptions' is empty.
3414
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00003415matchend({expr}, {pat}[, {start}[, {count}]]) *matchend()*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 Same as match(), but return the index of first character after
3417 the match. Example: >
3418 :echo matchend("testing", "ing")
3419< results in "7".
Bram Moolenaar05159a02005-02-26 23:04:13 +00003420 *strspn()* *strcspn()*
3421 Vim doesn't have a strspn() or strcspn() function, but you can
3422 do it with matchend(): >
3423 :let span = matchend(line, '[a-zA-Z]')
3424 :let span = matchend(line, '[^a-zA-Z]')
3425< Except that -1 is returned when there are no matches.
3426
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 The {start}, if given, has the same meaning as for match(). >
3428 :echo matchend("testing", "ing", 2)
3429< results in "7". >
3430 :echo matchend("testing", "ing", 5)
3431< result is "-1".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003432 When {expr} is a |List| the result is equal to match().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003434matchlist({expr}, {pat}[, {start}[, {count}]]) *matchlist()*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003435 Same as match(), but return a |List|. The first item in the
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003436 list is the matched string, same as what matchstr() would
3437 return. Following items are submatches, like "\1", "\2", etc.
3438 in |:substitute|.
3439 When there is no match an empty list is returned.
3440
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00003441matchstr({expr}, {pat}[, {start}[, {count}]]) *matchstr()*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 Same as match(), but return the matched string. Example: >
3443 :echo matchstr("testing", "ing")
3444< results in "ing".
3445 When there is no match "" is returned.
3446 The {start}, if given, has the same meaning as for match(). >
3447 :echo matchstr("testing", "ing", 2)
3448< results in "ing". >
3449 :echo matchstr("testing", "ing", 5)
3450< result is "".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003451 When {expr} is a |List| then the matching item is returned.
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003452 The type isn't changed, it's not necessarily a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003454 *max()*
3455max({list}) Return the maximum value of all items in {list}.
3456 If {list} is not a list or one of the items in {list} cannot
3457 be used as a Number this results in an error.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003458 An empty |List| results in zero.
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003459
3460 *min()*
3461min({list}) Return the minumum value of all items in {list}.
3462 If {list} is not a list or one of the items in {list} cannot
3463 be used as a Number this results in an error.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003464 An empty |List| results in zero.
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003465
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003466 *mkdir()* *E749*
3467mkdir({name} [, {path} [, {prot}]])
3468 Create directory {name}.
3469 If {path} is "p" then intermediate directories are created as
3470 necessary. Otherwise it must be "".
3471 If {prot} is given it is used to set the protection bits of
3472 the new directory. The default is 0755 (rwxr-xr-x: r/w for
3473 the user readable for others). Use 0700 to make it unreadable
3474 for others.
3475 This function is not available in the |sandbox|.
3476 Not available on all systems. To check use: >
3477 :if exists("*mkdir")
3478<
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 *mode()*
3480mode() Return a string that indicates the current mode:
3481 n Normal
3482 v Visual by character
3483 V Visual by line
3484 CTRL-V Visual blockwise
3485 s Select by character
3486 S Select by line
3487 CTRL-S Select blockwise
3488 i Insert
3489 R Replace
3490 c Command-line
3491 r Hit-enter prompt
3492 This is useful in the 'statusline' option. In most other
3493 places it always returns "c" or "n".
3494
3495nextnonblank({lnum}) *nextnonblank()*
3496 Return the line number of the first line at or below {lnum}
3497 that is not blank. Example: >
3498 if getline(nextnonblank(1)) =~ "Java"
3499< When {lnum} is invalid or there is no non-blank line at or
3500 below it, zero is returned.
3501 See also |prevnonblank()|.
3502
3503nr2char({expr}) *nr2char()*
3504 Return a string with a single character, which has the number
3505 value {expr}. Examples: >
3506 nr2char(64) returns "@"
3507 nr2char(32) returns " "
3508< The current 'encoding' is used. Example for "utf-8": >
3509 nr2char(300) returns I with bow character
3510< Note that a NUL character in the file is specified with
3511 nr2char(10), because NULs are represented with newline
3512 characters. nr2char(0) is a real NUL and terminates the
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00003513 string, thus results in an empty string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
Bram Moolenaar0b238792006-03-02 22:49:12 +00003515 *getpos()*
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003516getpos({expr}) Get the position for {expr}. For possible values of {expr}
3517 see |line()|.
3518 The result is a |List| with four numbers:
3519 [bufnum, lnum, col, off]
3520 "bufnum" is zero, unless a mark like '0 or 'A is used, then it
3521 is the buffer number of the mark.
3522 "lnum" and "col" are the position in the buffer. The first
3523 column is 1.
Bram Moolenaar0b238792006-03-02 22:49:12 +00003524 The "off" number is zero, unless 'virtualedit' is used. Then
3525 it is the offset in screen columns from the start of the
3526 character. E.g., a position within a Tab or after the last
3527 character.
3528 This can be used to save and restore the cursor position: >
3529 let save_cursor = getpos(".")
3530 MoveTheCursorAround
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003531 call setpos(save_cursor)
3532< Also see |setpos()|.
Bram Moolenaar0b238792006-03-02 22:49:12 +00003533
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003534prevnonblank({lnum}) *prevnonblank()*
3535 Return the line number of the first line at or above {lnum}
3536 that is not blank. Example: >
3537 let ind = indent(prevnonblank(v:lnum - 1))
3538< When {lnum} is invalid or there is no non-blank line at or
3539 above it, zero is returned.
3540 Also see |nextnonblank()|.
3541
3542
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003543printf({fmt}, {expr1} ...) *printf()*
3544 Return a String with {fmt}, where "%" items are replaced by
3545 the formatted form of their respective arguments. Example: >
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003546 printf("%4d: E%d %.30s", lnum, errno, msg)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003547< May result in:
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003548 " 99: E42 asdfasdfasdfasdfasdfasdfasdfas" ~
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003549
3550 Often used items are:
3551 %s string
Bram Moolenaar98692072006-02-04 00:57:42 +00003552 %6s string right-aligned in 6 bytes
3553 %.9s string truncated to 9 bytes
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003554 %c single byte
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003555 %d decimal number
3556 %5d decimal number padded with spaces to 5 characters
3557 %x hex number
3558 %04x hex number padded with zeros to at least 4 characters
3559 %X hex number using upper case letters
3560 %o octal number
Bram Moolenaar98692072006-02-04 00:57:42 +00003561 %% the % character itself
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003562
3563 Conversion specifications start with '%' and end with the
3564 conversion type. All other characters are copied unchanged to
3565 the result.
3566
3567 The "%" starts a conversion specification. The following
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003568 arguments appear in sequence:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003569
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003570 % [flags] [field-width] [.precision] type
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003571
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003572 flags
3573 Zero or more of the following flags:
3574
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003575 # The value should be converted to an "alternate
3576 form". For c, d, and s conversions, this option
3577 has no effect. For o conversions, the precision
3578 of the number is increased to force the first
3579 character of the output string to a zero (except
3580 if a zero value is printed with an explicit
3581 precision of zero).
3582 For x and X conversions, a non-zero result has
3583 the string "0x" (or "0X" for X conversions)
3584 prepended to it.
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003585
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003586 0 (zero) Zero padding. For all conversions the converted
3587 value is padded on the left with zeros rather
3588 than blanks. If a precision is given with a
3589 numeric conversion (d, o, x, and X), the 0 flag
3590 is ignored.
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003591
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003592 - A negative field width flag; the converted value
3593 is to be left adjusted on the field boundary.
3594 The converted value is padded on the right with
3595 blanks, rather than on the left with blanks or
3596 zeros. A - overrides a 0 if both are given.
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003597
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003598 ' ' (space) A blank should be left before a positive
3599 number produced by a signed conversion (d).
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003600
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003601 + A sign must always be placed before a number
3602 produced by a signed conversion. A + overrides
3603 a space if both are used.
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003604
3605 field-width
3606 An optional decimal digit string specifying a minimum
Bram Moolenaar98692072006-02-04 00:57:42 +00003607 field width. If the converted value has fewer bytes
3608 than the field width, it will be padded with spaces on
3609 the left (or right, if the left-adjustment flag has
3610 been given) to fill out the field width.
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003611
3612 .precision
3613 An optional precision, in the form of a period '.'
3614 followed by an optional digit string. If the digit
3615 string is omitted, the precision is taken as zero.
3616 This gives the minimum number of digits to appear for
3617 d, o, x, and X conversions, or the maximum number of
Bram Moolenaar98692072006-02-04 00:57:42 +00003618 bytes to be printed from a string for s conversions.
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003619
3620 type
3621 A character that specifies the type of conversion to
3622 be applied, see below.
3623
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003624 A field width or precision, or both, may be indicated by an
3625 asterisk '*' instead of a digit string. In this case, a
3626 Number argument supplies the field width or precision. A
3627 negative field width is treated as a left adjustment flag
3628 followed by a positive field width; a negative precision is
3629 treated as though it were missing. Example: >
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003630 :echo printf("%d: %.*s", nr, width, line)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003631< This limits the length of the text used from "line" to
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003632 "width" bytes.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003633
3634 The conversion specifiers and their meanings are:
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003635
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003636 doxX The Number argument is converted to signed decimal
3637 (d), unsigned octal (o), or unsigned hexadecimal (x
3638 and X) notation. The letters "abcdef" are used for
3639 x conversions; the letters "ABCDEF" are used for X
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003640 conversions.
3641 The precision, if any, gives the minimum number of
3642 digits that must appear; if the converted value
3643 requires fewer digits, it is padded on the left with
3644 zeros.
3645 In no case does a non-existent or small field width
3646 cause truncation of a numeric field; if the result of
3647 a conversion is wider than the field width, the field
3648 is expanded to contain the conversion result.
3649
3650 c The Number argument is converted to a byte, and the
3651 resulting character is written.
3652
3653 s The text of the String argument is used. If a
3654 precision is specified, no more bytes than the number
3655 specified are used.
3656
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003657 % A '%' is written. No argument is converted. The
3658 complete conversion specification is "%%".
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003659
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003660 Each argument can be Number or String and is converted
3661 automatically to fit the conversion specifier. Any other
3662 argument type results in an error message.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003663
Bram Moolenaar83bab712005-08-01 21:58:57 +00003664 *E766* *E767*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003665 The number of {exprN} arguments must exactly match the number
3666 of "%" items. If there are not sufficient or too many
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003667 arguments an error is given. Up to 18 arguments can be used.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003668
3669
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003670pumvisible() *pumvisible()*
3671 Returns non-zero when the popup menu is visible, zero
3672 otherwise. See |ins-completion-menu|.
3673
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00003675 *E726* *E727*
Bram Moolenaard8b02732005-01-14 21:48:43 +00003676range({expr} [, {max} [, {stride}]]) *range()*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003677 Returns a |List| with Numbers:
Bram Moolenaard8b02732005-01-14 21:48:43 +00003678 - If only {expr} is specified: [0, 1, ..., {expr} - 1]
3679 - If {max} is specified: [{expr}, {expr} + 1, ..., {max}]
3680 - If {stride} is specified: [{expr}, {expr} + {stride}, ...,
3681 {max}] (increasing {expr} with {stride} each time, not
3682 producing a value past {max}).
Bram Moolenaare7566042005-06-17 22:00:15 +00003683 When the maximum is one before the start the result is an
3684 empty list. When the maximum is more than one before the
3685 start this is an error.
Bram Moolenaard8b02732005-01-14 21:48:43 +00003686 Examples: >
3687 range(4) " [0, 1, 2, 3]
3688 range(2, 4) " [2, 3, 4]
3689 range(2, 9, 3) " [2, 5, 8]
3690 range(2, -2, -1) " [2, 1, 0, -1, -2]
Bram Moolenaare7566042005-06-17 22:00:15 +00003691 range(0) " []
3692 range(2, 0) " error!
Bram Moolenaard8b02732005-01-14 21:48:43 +00003693<
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003694 *readfile()*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003695readfile({fname} [, {binary} [, {max}]])
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003696 Read file {fname} and return a |List|, each line of the file
3697 as an item. Lines broken at NL characters. Macintosh files
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003698 separated with CR will result in a single long line (unless a
3699 NL appears somewhere).
3700 When {binary} is equal to "b" binary mode is used:
3701 - When the last line ends in a NL an extra empty list item is
3702 added.
3703 - No CR characters are removed.
3704 Otherwise:
3705 - CR characters that appear before a NL are removed.
3706 - Whether the last line ends in a NL or not does not matter.
3707 All NUL characters are replaced with a NL character.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003708 When {max} is given this specifies the maximum number of lines
3709 to be read. Useful if you only want to check the first ten
3710 lines of a file: >
3711 :for line in readfile(fname, '', 10)
3712 : if line =~ 'Date' | echo line | endif
3713 :endfor
Bram Moolenaar582fd852005-03-28 20:58:01 +00003714< When {max} is negative -{max} lines from the end of the file
3715 are returned, or as many as there are.
3716 When {max} is zero the result is an empty list.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003717 Note that without {max} the whole file is read into memory.
3718 Also note that there is no recognition of encoding. Read a
3719 file into a buffer if you need to.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00003720 When the file can't be opened an error message is given and
3721 the result is an empty list.
3722 Also see |writefile()|.
3723
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 *remote_expr()* *E449*
3725remote_expr({server}, {string} [, {idvar}])
3726 Send the {string} to {server}. The string is sent as an
3727 expression and the result is returned after evaluation.
3728 If {idvar} is present, it is taken as the name of a
3729 variable and a {serverid} for later use with
3730 remote_read() is stored there.
3731 See also |clientserver| |RemoteReply|.
3732 This function is not available in the |sandbox|.
3733 {only available when compiled with the |+clientserver| feature}
3734 Note: Any errors will cause a local error message to be issued
3735 and the result will be the empty string.
3736 Examples: >
3737 :echo remote_expr("gvim", "2+2")
3738 :echo remote_expr("gvim1", "b:current_syntax")
3739<
3740
3741remote_foreground({server}) *remote_foreground()*
3742 Move the Vim server with the name {server} to the foreground.
3743 This works like: >
3744 remote_expr({server}, "foreground()")
3745< Except that on Win32 systems the client does the work, to work
3746 around the problem that the OS doesn't always allow the server
3747 to bring itself to the foreground.
Bram Moolenaar9372a112005-12-06 19:59:18 +00003748 Note: This does not restore the window if it was minimized,
3749 like foreground() does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 This function is not available in the |sandbox|.
3751 {only in the Win32, Athena, Motif and GTK GUI versions and the
3752 Win32 console version}
3753
3754
3755remote_peek({serverid} [, {retvar}]) *remote_peek()*
3756 Returns a positive number if there are available strings
3757 from {serverid}. Copies any reply string into the variable
3758 {retvar} if specified. {retvar} must be a string with the
3759 name of a variable.
3760 Returns zero if none are available.
3761 Returns -1 if something is wrong.
3762 See also |clientserver|.
3763 This function is not available in the |sandbox|.
3764 {only available when compiled with the |+clientserver| feature}
3765 Examples: >
3766 :let repl = ""
3767 :echo "PEEK: ".remote_peek(id, "repl").": ".repl
3768
3769remote_read({serverid}) *remote_read()*
3770 Return the oldest available reply from {serverid} and consume
3771 it. It blocks until a reply is available.
3772 See also |clientserver|.
3773 This function is not available in the |sandbox|.
3774 {only available when compiled with the |+clientserver| feature}
3775 Example: >
3776 :echo remote_read(id)
3777<
3778 *remote_send()* *E241*
3779remote_send({server}, {string} [, {idvar}])
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003780 Send the {string} to {server}. The string is sent as input
3781 keys and the function returns immediately. At the Vim server
3782 the keys are not mapped |:map|.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003783 If {idvar} is present, it is taken as the name of a variable
3784 and a {serverid} for later use with remote_read() is stored
3785 there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 See also |clientserver| |RemoteReply|.
3787 This function is not available in the |sandbox|.
3788 {only available when compiled with the |+clientserver| feature}
3789 Note: Any errors will be reported in the server and may mess
3790 up the display.
3791 Examples: >
3792 :echo remote_send("gvim", ":DropAndReply ".file, "serverid").
3793 \ remote_read(serverid)
3794
3795 :autocmd NONE RemoteReply *
3796 \ echo remote_read(expand("<amatch>"))
3797 :echo remote_send("gvim", ":sleep 10 | echo ".
3798 \ 'server2client(expand("<client>"), "HELLO")<CR>')
Bram Moolenaara14de3d2005-01-07 21:48:26 +00003799<
Bram Moolenaarde8866b2005-01-06 23:24:37 +00003800remove({list}, {idx} [, {end}]) *remove()*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003801 Without {end}: Remove the item at {idx} from |List| {list} and
Bram Moolenaarde8866b2005-01-06 23:24:37 +00003802 return it.
3803 With {end}: Remove items from {idx} to {end} (inclusive) and
3804 return a list with these items. When {idx} points to the same
3805 item as {end} a list with one item is returned. When {end}
3806 points to an item before {idx} this is an error.
3807 See |list-index| for possible values of {idx} and {end}.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00003808 Example: >
3809 :echo "last item: " . remove(mylist, -1)
Bram Moolenaarde8866b2005-01-06 23:24:37 +00003810 :call remove(mylist, 0, 9)
Bram Moolenaard8b02732005-01-14 21:48:43 +00003811remove({dict}, {key})
3812 Remove the entry from {dict} with key {key}. Example: >
3813 :echo "removed " . remove(dict, "one")
3814< If there is no {key} in {dict} this is an error.
3815
3816 Use |delete()| to remove a file.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00003817
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818rename({from}, {to}) *rename()*
3819 Rename the file by the name {from} to the name {to}. This
3820 should also work to move files across file systems. The
3821 result is a Number, which is 0 if the file was renamed
3822 successfully, and non-zero when the renaming failed.
3823 This function is not available in the |sandbox|.
3824
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00003825repeat({expr}, {count}) *repeat()*
3826 Repeat {expr} {count} times and return the concatenated
3827 result. Example: >
3828 :let seperator = repeat('-', 80)
3829< When {count} is zero or negative the result is empty.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003830 When {expr} is a |List| the result is {expr} concatenated
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003831 {count} times. Example: >
Bram Moolenaarde8866b2005-01-06 23:24:37 +00003832 :let longlist = repeat(['a', 'b'], 3)
3833< Results in ['a', 'b', 'a', 'b', 'a', 'b'].
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00003834
Bram Moolenaara14de3d2005-01-07 21:48:26 +00003835
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836resolve({filename}) *resolve()* *E655*
3837 On MS-Windows, when {filename} is a shortcut (a .lnk file),
3838 returns the path the shortcut points to in a simplified form.
3839 On Unix, repeat resolving symbolic links in all path
3840 components of {filename} and return the simplified result.
3841 To cope with link cycles, resolving of symbolic links is
3842 stopped after 100 iterations.
3843 On other systems, return the simplified {filename}.
3844 The simplification step is done as by |simplify()|.
3845 resolve() keeps a leading path component specifying the
3846 current directory (provided the result is still a relative
3847 path name) and also keeps a trailing path separator.
3848
Bram Moolenaara14de3d2005-01-07 21:48:26 +00003849 *reverse()*
3850reverse({list}) Reverse the order of items in {list} in-place. Returns
3851 {list}.
3852 If you want a list to remain unmodified make a copy first: >
3853 :let revlist = reverse(copy(mylist))
3854
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003855search({pattern} [, {flags} [, {stopline}]]) *search()*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 Search for regexp pattern {pattern}. The search starts at the
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00003857 cursor position (you can use |cursor()| to set it).
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003858
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {flags} is a String, which can contain these character flags:
3860 'b' search backward instead of forward
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00003861 'n' do Not move the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 'w' wrap around the end of the file
3863 'W' don't wrap around the end of the file
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003864 's' set the ' mark at the previous location of the cursor
3865 'c' accept a match at the cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 If neither 'w' or 'W' is given, the 'wrapscan' option applies.
3867
Bram Moolenaar02743632005-07-25 20:42:36 +00003868 If the 's' flag is supplied, the ' mark is set, only if the
3869 cursor is moved. The 's' flag cannot be combined with the 'n'
3870 flag.
3871
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003872 When the {stopline} argument is given then the search stops
3873 after searching this line. This is useful to restrict the
3874 search to a range of lines. Examples: >
3875 let match = search('(', 'b', line("w0"))
3876 let end = search('END', '', line("w$"))
3877< When {stopline} is used and it is not zero this also implies
3878 that the search does not wrap around the end of the file.
3879
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00003880 When a match has been found its line number is returned.
3881 The cursor will be positioned at the match, unless the 'n'
Bram Moolenaar65c923a2006-03-03 22:56:30 +00003882 flag is used.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00003883 If there is no match a 0 is returned and the cursor doesn't
3884 move. No error message is given.
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003885 To get the column number too use |searchpos()|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886
3887 Example (goes over all files in the argument list): >
3888 :let n = 1
3889 :while n <= argc() " loop over all files in arglist
3890 : exe "argument " . n
3891 : " start at the last char in the file and wrap for the
3892 : " first search to find match at start of file
3893 : normal G$
3894 : let flags = "w"
3895 : while search("foo", flags) > 0
3896 : s/foo/bar/g
3897 : let flags = "W"
3898 : endwhile
3899 : update " write the file if modified
3900 : let n = n + 1
3901 :endwhile
3902<
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003903
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003904searchdecl({name} [, {global} [, {thisblock}]]) *searchdecl()*
3905 Search for the declaration of {name}.
3906
3907 With a non-zero {global} argument it works like |gD|, find
3908 first match in the file. Otherwise it works like |gd|, find
3909 first match in the function.
3910
3911 With a non-zero {thisblock} argument matches in a {} block
3912 that ends before the cursor position are ignored. Avoids
3913 finding variable declarations only valid in another scope.
3914
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003915 Moves the cursor to the found match.
3916 Returns zero for success, non-zero for failure.
3917 Example: >
3918 if searchdecl('myvar') == 0
3919 echo getline('.')
3920 endif
3921<
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 *searchpair()*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003923searchpair({start}, {middle}, {end} [, {flags} [, {skip} [, {stopline}]]])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 Search for the match of a nested start-end pair. This can be
3925 used to find the "endif" that matches an "if", while other
3926 if/endif pairs in between are ignored.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003927 The search starts at the cursor. The default is to search
3928 forward, include 'b' in {flags} to search backward.
3929 If a match is found, the cursor is positioned at it and the
3930 line number is returned. If no match is found 0 or -1 is
3931 returned and the cursor doesn't move. No error message is
3932 given.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933
3934 {start}, {middle} and {end} are patterns, see |pattern|. They
3935 must not contain \( \) pairs. Use of \%( \) is allowed. When
3936 {middle} is not empty, it is found when searching from either
3937 direction, but only when not in a nested start-end pair. A
3938 typical use is: >
3939 searchpair('\<if\>', '\<else\>', '\<endif\>')
3940< By leaving {middle} empty the "else" is skipped.
3941
3942 {flags} are used like with |search()|. Additionally:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 'r' Repeat until no more matches found; will find the
3944 outer pair
3945 'm' return number of Matches instead of line number with
3946 the match; will only be > 1 when 'r' is used.
3947
3948 When a match for {start}, {middle} or {end} is found, the
3949 {skip} expression is evaluated with the cursor positioned on
3950 the start of the match. It should return non-zero if this
3951 match is to be skipped. E.g., because it is inside a comment
3952 or a string.
3953 When {skip} is omitted or empty, every match is accepted.
3954 When evaluating {skip} causes an error the search is aborted
3955 and -1 returned.
3956
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003957 For {stopline} see |search()|.
3958
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 The value of 'ignorecase' is used. 'magic' is ignored, the
3960 patterns are used like it's on.
3961
3962 The search starts exactly at the cursor. A match with
3963 {start}, {middle} or {end} at the next character, in the
3964 direction of searching, is the first one found. Example: >
3965 if 1
3966 if 2
3967 endif 2
3968 endif 1
3969< When starting at the "if 2", with the cursor on the "i", and
3970 searching forwards, the "endif 2" is found. When starting on
3971 the character just before the "if 2", the "endif 1" will be
3972 found. That's because the "if 2" will be found first, and
3973 then this is considered to be a nested if/endif from "if 2" to
3974 "endif 2".
3975 When searching backwards and {end} is more than one character,
3976 it may be useful to put "\zs" at the end of the pattern, so
3977 that when the cursor is inside a match with the end it finds
3978 the matching start.
3979
3980 Example, to find the "endif" command in a Vim script: >
3981
3982 :echo searchpair('\<if\>', '\<el\%[seif]\>', '\<en\%[dif]\>', 'W',
3983 \ 'getline(".") =~ "^\\s*\""')
3984
3985< The cursor must be at or after the "if" for which a match is
3986 to be found. Note that single-quote strings are used to avoid
3987 having to double the backslashes. The skip expression only
3988 catches comments at the start of a line, not after a command.
3989 Also, a word "en" or "if" halfway a line is considered a
3990 match.
3991 Another example, to search for the matching "{" of a "}": >
3992
3993 :echo searchpair('{', '', '}', 'bW')
3994
3995< This works when the cursor is at or before the "}" for which a
3996 match is to be found. To reject matches that syntax
3997 highlighting recognized as strings: >
3998
3999 :echo searchpair('{', '', '}', 'bW',
4000 \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"')
4001<
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004002 *searchpairpos()*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004003searchpairpos({start}, {middle}, {end} [, {flags} [, {skip} [, {stopline}]]])
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004004 Same as searchpair(), but returns a |List| with the line and
4005 column position of the match. The first element of the |List|
4006 is the line number and the second element is the byte index of
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004007 the column position of the match. If no match is found,
4008 returns [0, 0].
4009>
4010 :let [lnum,col] = searchpairpos('{', '', '}', 'n')
4011<
4012 See |match-parens| for a bigger and more useful example.
4013
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004014searchpos({pattern} [, {flags} [, {stopline}]]) *searchpos()*
4015 Same as |search()|, but returns a |List| with the line and
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004016 column position of the match. The first element of the |List|
4017 is the line number and the second element is the byte index of
4018 the column position of the match. If no match is found,
4019 returns [0, 0].
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004020>
4021 :let [lnum,col] = searchpos('mypattern', 'n')
4022<
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023server2client( {clientid}, {string}) *server2client()*
4024 Send a reply string to {clientid}. The most recent {clientid}
4025 that sent a string can be retrieved with expand("<client>").
4026 {only available when compiled with the |+clientserver| feature}
4027 Note:
4028 This id has to be stored before the next command can be
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004029 received. I.e. before returning from the received command and
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 before calling any commands that waits for input.
4031 See also |clientserver|.
4032 Example: >
4033 :echo server2client(expand("<client>"), "HELLO")
4034<
4035serverlist() *serverlist()*
4036 Return a list of available server names, one per line.
4037 When there are no servers or the information is not available
4038 an empty string is returned. See also |clientserver|.
4039 {only available when compiled with the |+clientserver| feature}
4040 Example: >
4041 :echo serverlist()
4042<
4043setbufvar({expr}, {varname}, {val}) *setbufvar()*
4044 Set option or local variable {varname} in buffer {expr} to
4045 {val}.
4046 This also works for a global or local window option, but it
4047 doesn't work for a global or local window variable.
4048 For a local window option the global value is unchanged.
4049 For the use of {expr}, see |bufname()| above.
4050 Note that the variable name without "b:" must be used.
4051 Examples: >
4052 :call setbufvar(1, "&mod", 1)
4053 :call setbufvar("todo", "myvar", "foobar")
4054< This function is not available in the |sandbox|.
4055
4056setcmdpos({pos}) *setcmdpos()*
4057 Set the cursor position in the command line to byte position
4058 {pos}. The first position is 1.
4059 Use |getcmdpos()| to obtain the current position.
4060 Only works while editing the command line, thus you must use
Bram Moolenaard8b02732005-01-14 21:48:43 +00004061 |c_CTRL-\_e|, |c_CTRL-R_=| or |c_CTRL-R_CTRL-R| with '='. For
4062 |c_CTRL-\_e| and |c_CTRL-R_CTRL-R| with '=' the position is
4063 set after the command line is set to the expression. For
4064 |c_CTRL-R_=| it is set after evaluating the expression but
4065 before inserting the resulting text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 When the number is too big the cursor is put at the end of the
4067 line. A number smaller than one has undefined results.
4068 Returns 0 when successful, 1 when not editing the command
4069 line.
4070
4071setline({lnum}, {line}) *setline()*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004072 Set line {lnum} of the current buffer to {line}.
4073 {lnum} is used like with |getline()|.
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004074 When {lnum} is just below the last line the {line} will be
4075 added as a new line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004076 If this succeeds, 0 is returned. If this fails (most likely
4077 because {lnum} is invalid) 1 is returned. Example: >
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 :call setline(5, strftime("%c"))
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004079< When {line} is a |List| then line {lnum} and following lines
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004080 will be set to the items in the list. Example: >
4081 :call setline(5, ['aaa', 'bbb', 'ccc'])
4082< This is equivalent to: >
4083 :for [n, l] in [[5, 6, 7], ['aaa', 'bbb', 'ccc']]
4084 : call setline(n, l)
4085 :endfor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086< Note: The '[ and '] marks are not set.
4087
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004088setloclist({nr}, {list} [, {action}]) *setloclist()*
4089 Create or replace or add to the location list for window {nr}.
4090 When {nr} is zero the current window is used. For a location
Bram Moolenaar280f1262006-01-30 00:14:18 +00004091 list window, the displayed location list is modified. For an
4092 invalid window number {nr}, -1 is returned.
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004093 Otherwise, same as setqflist().
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004094
Bram Moolenaar65c923a2006-03-03 22:56:30 +00004095 *setpos()*
4096setpos({expr}, {list})
4097 Set the position for {expr}. Possible values:
4098 . the cursor
4099 'x mark x
4100
4101 {list} must be a |List| with four numbers:
4102 [bufnum, lnum, col, off]
4103
4104 "bufnum" is the buffer number. Zero can be used for the
4105 current buffer. Setting the cursor is only possible for
4106 the current buffer. To set a mark in another buffer you can
4107 use the |bufnr()| function to turn a file name into a buffer
4108 number.
4109
4110 "lnum" and "col" are the position in the buffer. The first
4111 column is 1. Use a zero "lnum" to delete a mark.
4112
4113 The "off" number is only used when 'virtualedit' is set. Then
4114 it is the offset in screen columns from the start of the
4115 character. E.g., a position within a Tab or after the last
4116 character.
4117
4118 Also see |getpos()|
4119
4120
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004121setqflist({list} [, {action}]) *setqflist()*
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004122 Create or replace or add to the quickfix list using the items
4123 in {list}. Each item in {list} is a dictionary.
4124 Non-dictionary items in {list} are ignored. Each dictionary
4125 item can contain the following entries:
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004126
4127 filename name of a file
4128 lnum line number in the file
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004129 pattern search pattern used to locate the error
Bram Moolenaar582fd852005-03-28 20:58:01 +00004130 col column number
4131 vcol when non-zero: "col" is visual column
4132 when zero: "col" is byte index
4133 nr error number
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004134 text description of the error
Bram Moolenaar582fd852005-03-28 20:58:01 +00004135 type single-character error type, 'E', 'W', etc.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004136
Bram Moolenaar582fd852005-03-28 20:58:01 +00004137 The "col", "vcol", "nr", "type" and "text" entries are
4138 optional. Either "lnum" or "pattern" entry can be used to
4139 locate a matching error line.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004140 If the "filename" entry is not present or neither the "lnum"
4141 or "pattern" entries are present, then the item will not be
4142 handled as an error line.
4143 If both "pattern" and "lnum" are present then "pattern" will
4144 be used.
4145
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004146 If {action} is set to 'a', then the items from {list} are
4147 added to the existing quickfix list. If there is no existing
4148 list, then a new list is created. If {action} is set to 'r',
4149 then the items from the current quickfix list are replaced
4150 with the items from {list}. If {action} is not present or is
4151 set to ' ', then a new list is created.
4152
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004153 Returns zero for success, -1 for failure.
4154
4155 This function can be used to create a quickfix list
4156 independent of the 'errorformat' setting. Use a command like
4157 ":cc 1" to jump to the first position.
4158
4159
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 *setreg()*
4161setreg({regname}, {value} [,{options}])
4162 Set the register {regname} to {value}.
4163 If {options} contains "a" or {regname} is upper case,
4164 then the value is appended.
4165 {options} can also contains a register type specification:
4166 "c" or "v" |characterwise| mode
4167 "l" or "V" |linewise| mode
4168 "b" or "<CTRL-V>" |blockwise-visual| mode
4169 If a number immediately follows "b" or "<CTRL-V>" then this is
4170 used as the width of the selection - if it is not specified
4171 then the width of the block is set to the number of characters
4172 in the longest line (counting a <TAB> as 1 character).
4173
4174 If {options} contains no register settings, then the default
4175 is to use character mode unless {value} ends in a <NL>.
4176 Setting the '=' register is not possible.
4177 Returns zero for success, non-zero for failure.
4178
4179 Examples: >
4180 :call setreg(v:register, @*)
4181 :call setreg('*', @%, 'ac')
4182 :call setreg('a', "1\n2\n3", 'b5')
4183
4184< This example shows using the functions to save and restore a
4185 register. >
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004186 :let var_a = getreg('a', 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 :let var_amode = getregtype('a')
4188 ....
4189 :call setreg('a', var_a, var_amode)
4190
4191< You can also change the type of a register by appending
4192 nothing: >
4193 :call setreg('a', '', 'al')
4194
4195setwinvar({nr}, {varname}, {val}) *setwinvar()*
4196 Set option or local variable {varname} in window {nr} to
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004197 {val}. When {nr} is zero the current window is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 This also works for a global or local buffer option, but it
4199 doesn't work for a global or local buffer variable.
4200 For a local buffer option the global value is unchanged.
4201 Note that the variable name without "w:" must be used.
4202 Examples: >
4203 :call setwinvar(1, "&list", 0)
4204 :call setwinvar(2, "myvar", "foobar")
4205< This function is not available in the |sandbox|.
4206
4207simplify({filename}) *simplify()*
4208 Simplify the file name as much as possible without changing
4209 the meaning. Shortcuts (on MS-Windows) or symbolic links (on
4210 Unix) are not resolved. If the first path component in
4211 {filename} designates the current directory, this will be
4212 valid for the result as well. A trailing path separator is
4213 not removed either.
4214 Example: >
4215 simplify("./dir/.././/file/") == "./file/"
4216< Note: The combination "dir/.." is only removed if "dir" is
4217 a searchable directory or does not exist. On Unix, it is also
4218 removed when "dir" is a symbolic link within the same
4219 directory. In order to resolve all the involved symbolic
4220 links before simplifying the path name, use |resolve()|.
4221
Bram Moolenaara14de3d2005-01-07 21:48:26 +00004222
Bram Moolenaar13065c42005-01-08 16:08:21 +00004223sort({list} [, {func}]) *sort()* *E702*
Bram Moolenaara14de3d2005-01-07 21:48:26 +00004224 Sort the items in {list} in-place. Returns {list}. If you
4225 want a list to remain unmodified make a copy first: >
4226 :let sortedlist = sort(copy(mylist))
4227< Uses the string representation of each item to sort on.
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004228 Numbers sort after Strings, |Lists| after Numbers.
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004229 For sorting text in the current buffer use |:sort|.
Bram Moolenaara14de3d2005-01-07 21:48:26 +00004230 When {func} is given and it is one then case is ignored.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004231 When {func} is a |Funcref| or a function name, this function
4232 is called to compare items. The function is invoked with two
Bram Moolenaara14de3d2005-01-07 21:48:26 +00004233 items as argument and must return zero if they are equal, 1 if
4234 the first one sorts after the second one, -1 if the first one
4235 sorts before the second one. Example: >
4236 func MyCompare(i1, i2)
4237 return a:i1 == a:i2 ? 0 : a:i1 > a:i2 ? 1 : -1
4238 endfunc
4239 let sortedlist = sort(mylist, "MyCompare")
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004240<
4241
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004242 *soundfold()*
4243soundfold({word})
4244 Return the sound-folded equivalent of {word}. Uses the first
4245 language in 'spellang' for the current window that supports
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004246 soundfolding. 'spell' must be set. When no sound folding is
4247 possible the {word} is returned unmodified.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004248 This can be used for making spelling suggestions. Note that
4249 the method can be quite slow.
4250
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004251 *spellbadword()*
Bram Moolenaar1e015462005-09-25 22:16:38 +00004252spellbadword([{sentence}])
4253 Without argument: The result is the badly spelled word under
4254 or after the cursor. The cursor is moved to the start of the
4255 bad word. When no bad word is found in the cursor line the
4256 result is an empty string and the cursor doesn't move.
4257
4258 With argument: The result is the first word in {sentence} that
4259 is badly spelled. If there are no spelling mistakes the
4260 result is an empty string.
4261
4262 The return value is a list with two items:
4263 - The badly spelled word or an empty string.
4264 - The type of the spelling error:
4265 "bad" spelling mistake
4266 "rare" rare word
4267 "local" word only valid in another region
4268 "caps" word should start with Capital
4269 Example: >
4270 echo spellbadword("the quik brown fox")
4271< ['quik', 'bad'] ~
4272
4273 The spelling information for the current window is used. The
4274 'spell' option must be set and the value of 'spelllang' is
4275 used.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004276
4277 *spellsuggest()*
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004278spellsuggest({word} [, {max} [, {capital}]])
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004279 Return a |List| with spelling suggestions to replace {word}.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004280 When {max} is given up to this number of suggestions are
4281 returned. Otherwise up to 25 suggestions are returned.
4282
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004283 When the {capital} argument is given and it's non-zero only
4284 suggestions with a leading capital will be given. Use this
4285 after a match with 'spellcapcheck'.
4286
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004287 {word} can be a badly spelled word followed by other text.
4288 This allows for joining two words that were split. The
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004289 suggestions also include the following text, thus you can
4290 replace a line.
4291
4292 {word} may also be a good word. Similar words will then be
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004293 returned. {word} itself is not included in the suggestions,
4294 although it may appear capitalized.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004295
4296 The spelling information for the current window is used. The
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004297 'spell' option must be set and the values of 'spelllang' and
4298 'spellsuggest' are used.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004299
Bram Moolenaara14de3d2005-01-07 21:48:26 +00004300
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004301split({expr} [, {pattern} [, {keepempty}]]) *split()*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004302 Make a |List| out of {expr}. When {pattern} is omitted or
4303 empty each white-separated sequence of characters becomes an
4304 item.
Bram Moolenaara14de3d2005-01-07 21:48:26 +00004305 Otherwise the string is split where {pattern} matches,
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004306 removing the matched characters.
4307 When the first or last item is empty it is omitted, unless the
4308 {keepempty} argument is given and it's non-zero.
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004309 Other empty items are kept when {pattern} matches at least one
4310 character or when {keepempty} is non-zero.
Bram Moolenaara14de3d2005-01-07 21:48:26 +00004311 Example: >
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004312 :let words = split(getline('.'), '\W\+')
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004313< To split a string in individual characters: >
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004314 :for c in split(mystring, '\zs')
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00004315< If you want to keep the separator you can also use '\zs': >
4316 :echo split('abc:def:ghi', ':\zs')
4317< ['abc:', 'def:', 'ghi'] ~
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004318 Splitting a table where the first element can be empty: >
4319 :let items = split(line, ':', 1)
4320< The opposite function is |join()|.
Bram Moolenaara14de3d2005-01-07 21:48:26 +00004321
4322
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323strftime({format} [, {time}]) *strftime()*
4324 The result is a String, which is a formatted date and time, as
4325 specified by the {format} string. The given {time} is used,
4326 or the current time if no time is given. The accepted
4327 {format} depends on your system, thus this is not portable!
4328 See the manual page of the C function strftime() for the
4329 format. The maximum length of the result is 80 characters.
4330 See also |localtime()| and |getftime()|.
4331 The language can be changed with the |:language| command.
4332 Examples: >
4333 :echo strftime("%c") Sun Apr 27 11:49:23 1997
4334 :echo strftime("%Y %b %d %X") 1997 Apr 27 11:53:25
4335 :echo strftime("%y%m%d %T") 970427 11:53:55
4336 :echo strftime("%H:%M") 11:55
4337 :echo strftime("%c", getftime("file.c"))
4338 Show mod time of file.c.
Bram Moolenaara14de3d2005-01-07 21:48:26 +00004339< Not available on all systems. To check use: >
4340 :if exists("*strftime")
4341
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004342stridx({haystack}, {needle} [, {start}]) *stridx()*
4343 The result is a Number, which gives the byte index in
4344 {haystack} of the first occurrence of the String {needle}.
Bram Moolenaar677ee682005-01-27 14:41:15 +00004345 If {start} is specified, the search starts at index {start}.
4346 This can be used to find a second match: >
4347 :let comma1 = stridx(line, ",")
4348 :let comma2 = stridx(line, ",", comma1 + 1)
4349< The search is done case-sensitive.
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004350 For pattern searches use |match()|.
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004351 -1 is returned if the {needle} does not occur in {haystack}.
Bram Moolenaar677ee682005-01-27 14:41:15 +00004352 See also |strridx()|.
4353 Examples: >
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 :echo stridx("An Example", "Example") 3
4355 :echo stridx("Starting point", "Start") 0
4356 :echo stridx("Starting point", "start") -1
Bram Moolenaar05159a02005-02-26 23:04:13 +00004357< *strstr()* *strchr()*
4358 stridx() works similar to the C function strstr(). When used
4359 with a single character it works similar to strchr().
4360
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00004361 *string()*
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004362string({expr}) Return {expr} converted to a String. If {expr} is a Number,
4363 String or a composition of them, then the result can be parsed
4364 back with |eval()|.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00004365 {expr} type result ~
Bram Moolenaard8b02732005-01-14 21:48:43 +00004366 String 'string'
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004367 Number 123
Bram Moolenaard8b02732005-01-14 21:48:43 +00004368 Funcref function('name')
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004369 List [item, item]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004370 Dictionary {key: value, key: value}
Bram Moolenaard8b02732005-01-14 21:48:43 +00004371 Note that in String values the ' character is doubled.
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00004372
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 *strlen()*
4374strlen({expr}) The result is a Number, which is the length of the String
Bram Moolenaare344bea2005-09-01 20:46:49 +00004375 {expr} in bytes.
4376 If you want to count the number of multi-byte characters (not
4377 counting composing characters) use something like this: >
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
4379 :let len = strlen(substitute(str, ".", "x", "g"))
Bram Moolenaare344bea2005-09-01 20:46:49 +00004380<
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00004381 If the argument is a Number it is first converted to a String.
4382 For other types an error is given.
4383 Also see |len()|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384
4385strpart({src}, {start}[, {len}]) *strpart()*
4386 The result is a String, which is part of {src}, starting from
Bram Moolenaar9372a112005-12-06 19:59:18 +00004387 byte {start}, with the byte length {len}.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 When non-existing bytes are included, this doesn't result in
4389 an error, the bytes are simply omitted.
4390 If {len} is missing, the copy continues from {start} till the
4391 end of the {src}. >
4392 strpart("abcdefg", 3, 2) == "de"
4393 strpart("abcdefg", -2, 4) == "ab"
4394 strpart("abcdefg", 5, 4) == "fg"
4395 strpart("abcdefg", 3) == "defg"
4396< Note: To get the first character, {start} must be 0. For
4397 example, to get three bytes under and after the cursor: >
4398 strpart(getline(line(".")), col(".") - 1, 3)
4399<
Bram Moolenaar677ee682005-01-27 14:41:15 +00004400strridx({haystack}, {needle} [, {start}]) *strridx()*
4401 The result is a Number, which gives the byte index in
4402 {haystack} of the last occurrence of the String {needle}.
4403 When {start} is specified, matches beyond this index are
4404 ignored. This can be used to find a match before a previous
4405 match: >
4406 :let lastcomma = strridx(line, ",")
4407 :let comma2 = strridx(line, ",", lastcomma - 1)
4408< The search is done case-sensitive.
Bram Moolenaar8f999f12005-01-25 22:12:55 +00004409 For pattern searches use |match()|.
4410 -1 is returned if the {needle} does not occur in {haystack}.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004411 If the {needle} is empty the length of {haystack} is returned.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004412 See also |stridx()|. Examples: >
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 :echo strridx("an angry armadillo", "an") 3
Bram Moolenaar05159a02005-02-26 23:04:13 +00004414< *strrchr()*
4415 When used with a single character it works similar to the C
4416 function strrchr().
4417
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418strtrans({expr}) *strtrans()*
4419 The result is a String, which is {expr} with all unprintable
4420 characters translated into printable characters |'isprint'|.
4421 Like they are shown in a window. Example: >
4422 echo strtrans(@a)
4423< This displays a newline in register a as "^@" instead of
4424 starting a new line.
4425
4426submatch({nr}) *submatch()*
4427 Only for an expression in a |:substitute| command. Returns
4428 the {nr}'th submatch of the matched text. When {nr} is 0
4429 the whole matched text is returned.
4430 Example: >
4431 :s/\d\+/\=submatch(0) + 1/
4432< This finds the first number in the line and adds one to it.
4433 A line break is included as a newline character.
4434
4435substitute({expr}, {pat}, {sub}, {flags}) *substitute()*
4436 The result is a String, which is a copy of {expr}, in which
4437 the first match of {pat} is replaced with {sub}. This works
4438 like the ":substitute" command (without any flags). But the
4439 matching with {pat} is always done like the 'magic' option is
4440 set and 'cpoptions' is empty (to make scripts portable).
4441 See |string-match| for how {pat} is used.
4442 And a "~" in {sub} is not replaced with the previous {sub}.
4443 Note that some codes in {sub} have a special meaning
4444 |sub-replace-special|. For example, to replace something with
4445 "\n" (two characters), use "\\\\n" or '\\n'.
4446 When {pat} does not match in {expr}, {expr} is returned
4447 unmodified.
4448 When {flags} is "g", all matches of {pat} in {expr} are
4449 replaced. Otherwise {flags} should be "".
4450 Example: >
4451 :let &path = substitute(&path, ",\\=[^,]*$", "", "")
4452< This removes the last component of the 'path' option. >
4453 :echo substitute("testing", ".*", "\\U\\0", "")
4454< results in "TESTING".
4455
Bram Moolenaar47136d72004-10-12 20:02:24 +00004456synID({lnum}, {col}, {trans}) *synID()*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 The result is a Number, which is the syntax ID at the position
Bram Moolenaar47136d72004-10-12 20:02:24 +00004458 {lnum} and {col} in the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 The syntax ID can be used with |synIDattr()| and
4460 |synIDtrans()| to obtain syntax information about text.
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004461
Bram Moolenaar47136d72004-10-12 20:02:24 +00004462 {col} is 1 for the leftmost column, {lnum} is 1 for the first
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004463 line. 'synmaxcol' applies, in a longer line zero is returned.
4464
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 When {trans} is non-zero, transparent items are reduced to the
4466 item that they reveal. This is useful when wanting to know
4467 the effective color. When {trans} is zero, the transparent
4468 item is returned. This is useful when wanting to know which
4469 syntax item is effective (e.g. inside parens).
4470 Warning: This function can be very slow. Best speed is
4471 obtained by going through the file in forward direction.
4472
4473 Example (echoes the name of the syntax item under the cursor): >
4474 :echo synIDattr(synID(line("."), col("."), 1), "name")
4475<
4476synIDattr({synID}, {what} [, {mode}]) *synIDattr()*
4477 The result is a String, which is the {what} attribute of
4478 syntax ID {synID}. This can be used to obtain information
4479 about a syntax item.
4480 {mode} can be "gui", "cterm" or "term", to get the attributes
4481 for that mode. When {mode} is omitted, or an invalid value is
4482 used, the attributes for the currently active highlighting are
4483 used (GUI, cterm or term).
4484 Use synIDtrans() to follow linked highlight groups.
4485 {what} result
4486 "name" the name of the syntax item
4487 "fg" foreground color (GUI: color name used to set
4488 the color, cterm: color number as a string,
4489 term: empty string)
4490 "bg" background color (like "fg")
4491 "fg#" like "fg", but for the GUI and the GUI is
4492 running the name in "#RRGGBB" form
4493 "bg#" like "fg#" for "bg"
4494 "bold" "1" if bold
4495 "italic" "1" if italic
4496 "reverse" "1" if reverse
4497 "inverse" "1" if inverse (= reverse)
4498 "underline" "1" if underlined
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004499 "undercurl" "1" if undercurled
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500
4501 Example (echoes the color of the syntax item under the
4502 cursor): >
4503 :echo synIDattr(synIDtrans(synID(line("."), col("."), 1)), "fg")
4504<
4505synIDtrans({synID}) *synIDtrans()*
4506 The result is a Number, which is the translated syntax ID of
4507 {synID}. This is the syntax group ID of what is being used to
4508 highlight the character. Highlight links given with
4509 ":highlight link" are followed.
4510
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004511system({expr} [, {input}]) *system()* *E677*
4512 Get the output of the shell command {expr}.
4513 When {input} is given, this string is written to a file and
4514 passed as stdin to the command. The string is written as-is,
4515 you need to take care of using the correct line separators
Bram Moolenaar05159a02005-02-26 23:04:13 +00004516 yourself. Pipes are not used.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004517 Note: newlines in {expr} may cause the command to fail. The
4518 characters in 'shellquote' and 'shellxquote' may also cause
4519 trouble.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 This is not to be used for interactive commands.
4521 The result is a String. Example: >
4522
4523 :let files = system("ls")
4524
4525< To make the result more system-independent, the shell output
4526 is filtered to replace <CR> with <NL> for Macintosh, and
4527 <CR><NL> with <NL> for DOS-like systems.
4528 The command executed is constructed using several options:
4529 'shell' 'shellcmdflag' 'shellxquote' {expr} 'shellredir' {tmp} 'shellxquote'
4530 ({tmp} is an automatically generated file name).
4531 For Unix and OS/2 braces are put around {expr} to allow for
4532 concatenated commands.
4533
4534 The resulting error code can be found in |v:shell_error|.
4535 This function will fail in |restricted-mode|.
Bram Moolenaar4770d092006-01-12 23:22:24 +00004536
4537 Note that any wrong value in the options mentioned above may
4538 make the function fail. It has also been reported to fail
4539 when using a security agent application.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 Unlike ":!cmd" there is no automatic check for changed files.
4541 Use |:checktime| to force a check.
4542
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004543
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004544tabpagebuflist([{arg}]) *tabpagebuflist()*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004545 The result is a |List|, where each item is the number of the
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004546 buffer associated with each window in the current tab page.
4547 {arg} specifies the number of tab page to be used. When
4548 omitted the current tab page is used.
4549 When {arg} is invalid the number zero is returned.
4550 To get a list of all buffers in all tabs use this: >
4551 tablist = []
4552 for i in range(tabpagenr('$'))
4553 call extend(tablist, tabpagebuflist(i + 1))
4554 endfor
4555< Note that a buffer may appear in more than one window.
4556
4557
4558tabpagenr([{arg}]) *tabpagenr()*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004559 The result is a Number, which is the number of the current
4560 tab page. The first tab page has number 1.
4561 When the optional argument is "$", the number of the last tab
4562 page is returned (the tab page count).
4563 The number can be used with the |:tab| command.
4564
4565
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004566tabpagewinnr({tabarg}, [{arg}]) *tabpagewinnr()*
4567 Like |winnr()| but for tab page {arg}.
4568 {tabarg} specifies the number of tab page to be used.
4569 {arg} is used like with |winnr()|:
4570 - When omitted the current window number is returned. This is
4571 the window which will be used when going to this tab page.
4572 - When "$" the number of windows is returned.
4573 - When "#" the previous window nr is returned.
4574 Useful examples: >
4575 tabpagewinnr(1) " current window of tab page 1
4576 tabpagewinnr(4, '$') " number of windows in tab page 4
4577< When {tabarg} is invalid zero is returned.
4578
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004579taglist({expr}) *taglist()*
4580 Returns a list of tags matching the regular expression {expr}.
Bram Moolenaard8c00872005-07-22 21:52:15 +00004581 Each list item is a dictionary with at least the following
4582 entries:
Bram Moolenaar280f1262006-01-30 00:14:18 +00004583 name Name of the tag.
4584 filename Name of the file where the tag is
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004585 defined.
4586 cmd Ex command used to locate the tag in
4587 the file.
Bram Moolenaar280f1262006-01-30 00:14:18 +00004588 kind Type of the tag. The value for this
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004589 entry depends on the language specific
4590 kind values generated by the ctags
4591 tool.
Bram Moolenaar280f1262006-01-30 00:14:18 +00004592 static A file specific tag. Refer to
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004593 |static-tag| for more information.
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00004594 The "kind" entry is only available when using Exuberant ctags
4595 generated tags file. More entries may be present, depending
4596 on the content of the tags file: access, implementation,
4597 inherits and signature. Refer to the ctags documentation for
4598 information about these fields. For C code the fields
4599 "struct", "class" and "enum" may appear, they give the name of
4600 the entity the tag is contained in.
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004601
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00004602 The ex-command 'cmd' can be either an ex search pattern, a
4603 line number or a line number followed by a byte number.
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004604
4605 If there are no matching tags, then an empty list is returned.
4606
4607 To get an exact tag match, the anchors '^' and '$' should be
4608 used in {expr}. Refer to |tag-regexp| for more information
4609 about the tag search regular expression pattern.
4610
4611 Refer to |'tags'| for information about how the tags file is
4612 located by Vim. Refer to |tags-file-format| for the format of
4613 the tags file generated by the different ctags tools.
4614
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004615 *tagfiles()*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004616tagfiles() Returns a |List| with the file names used to search for tags
4617 for the current buffer. This is the 'tags' option expanded.
Bram Moolenaare7eb9df2005-09-09 19:49:30 +00004618
Bram Moolenaare2cc9702005-03-15 22:43:58 +00004619
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620tempname() *tempname()* *temp-file-name*
4621 The result is a String, which is the name of a file that
4622 doesn't exist. It can be used for a temporary file. The name
4623 is different for at least 26 consecutive calls. Example: >
4624 :let tmpfile = tempname()
4625 :exe "redir > " . tmpfile
4626< For Unix, the file will be in a private directory (only
4627 accessible by the current user) to avoid security problems
4628 (e.g., a symlink attack or other people reading your file).
4629 When Vim exits the directory and all files in it are deleted.
4630 For MS-Windows forward slashes are used when the 'shellslash'
4631 option is set or when 'shellcmdflag' starts with '-'.
4632
4633tolower({expr}) *tolower()*
4634 The result is a copy of the String given, with all uppercase
4635 characters turned into lowercase (just like applying |gu| to
4636 the string).
4637
4638toupper({expr}) *toupper()*
4639 The result is a copy of the String given, with all lowercase
4640 characters turned into uppercase (just like applying |gU| to
4641 the string).
4642
Bram Moolenaar8299df92004-07-10 09:47:34 +00004643tr({src}, {fromstr}, {tostr}) *tr()*
4644 The result is a copy of the {src} string with all characters
4645 which appear in {fromstr} replaced by the character in that
4646 position in the {tostr} string. Thus the first character in
4647 {fromstr} is translated into the first character in {tostr}
4648 and so on. Exactly like the unix "tr" command.
4649 This code also deals with multibyte characters properly.
4650
4651 Examples: >
4652 echo tr("hello there", "ht", "HT")
4653< returns "Hello THere" >
4654 echo tr("<blob>", "<>", "{}")
4655< returns "{blob}"
4656
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004657 *type()*
4658type({expr}) The result is a Number, depending on the type of {expr}:
Bram Moolenaar748bf032005-02-02 23:04:36 +00004659 Number: 0
4660 String: 1
4661 Funcref: 2
4662 List: 3
4663 Dictionary: 4
4664 To avoid the magic numbers it should be used this way: >
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004665 :if type(myvar) == type(0)
4666 :if type(myvar) == type("")
4667 :if type(myvar) == type(function("tr"))
4668 :if type(myvar) == type([])
Bram Moolenaar748bf032005-02-02 23:04:36 +00004669 :if type(myvar) == type({})
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670
Bram Moolenaar677ee682005-01-27 14:41:15 +00004671values({dict}) *values()*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004672 Return a |List| with all the values of {dict}. The |List| is
4673 in arbitrary order.
Bram Moolenaar677ee682005-01-27 14:41:15 +00004674
4675
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676virtcol({expr}) *virtcol()*
4677 The result is a Number, which is the screen column of the file
4678 position given with {expr}. That is, the last screen position
4679 occupied by the character at that position, when the screen
4680 would be of unlimited width. When there is a <Tab> at the
4681 position, the returned Number will be the column at the end of
4682 the <Tab>. For example, for a <Tab> in column 1, with 'ts'
4683 set to 8, it returns 8.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004684 For the use of {expr} see |col()|. Additionally you can use
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00004685 [lnum, col]: a |List| with the line and column number. When
4686 "lnum" or "col" is out of range then virtcol() returns zero.
Bram Moolenaar0b238792006-03-02 22:49:12 +00004687 When 'virtualedit' is used it can be [lnum, col, off], where
4688 "off" is the offset in screen columns from the start of the
4689 character. E.g., a position within a Tab or after the last
4690 character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 For the byte position use |col()|.
4692 When Virtual editing is active in the current mode, a position
4693 beyond the end of the line can be returned. |'virtualedit'|
4694 The accepted positions are:
4695 . the cursor position
4696 $ the end of the cursor line (the result is the
4697 number of displayed characters in the cursor line
4698 plus one)
4699 'x position of mark x (if the mark is not set, 0 is
4700 returned)
4701 Note that only marks in the current file can be used.
4702 Examples: >
4703 virtcol(".") with text "foo^Lbar", with cursor on the "^L", returns 5
4704 virtcol("$") with text "foo^Lbar", returns 9
4705 virtcol("'t") with text " there", with 't at 'h', returns 6
4706< The first column is 1. 0 is returned for an error.
4707
4708visualmode([expr]) *visualmode()*
4709 The result is a String, which describes the last Visual mode
4710 used. Initially it returns an empty string, but once Visual
4711 mode has been used, it returns "v", "V", or "<CTRL-V>" (a
4712 single CTRL-V character) for character-wise, line-wise, or
4713 block-wise Visual mode respectively.
4714 Example: >
4715 :exe "normal " . visualmode()
4716< This enters the same Visual mode as before. It is also useful
4717 in scripts if you wish to act differently depending on the
4718 Visual mode that was used.
4719
4720 If an expression is supplied that results in a non-zero number
4721 or a non-empty string, then the Visual mode will be cleared
4722 and the old value is returned. Note that " " and "0" are also
4723 non-empty strings, thus cause the mode to be cleared.
4724
4725 *winbufnr()*
4726winbufnr({nr}) The result is a Number, which is the number of the buffer
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004727 associated with window {nr}. When {nr} is zero, the number of
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 the buffer in the current window is returned. When window
4729 {nr} doesn't exist, -1 is returned.
4730 Example: >
4731 :echo "The file in the current window is " . bufname(winbufnr(0))
4732<
4733 *wincol()*
4734wincol() The result is a Number, which is the virtual column of the
4735 cursor in the window. This is counting screen cells from the
4736 left side of the window. The leftmost column is one.
4737
4738winheight({nr}) *winheight()*
4739 The result is a Number, which is the height of window {nr}.
4740 When {nr} is zero, the height of the current window is
4741 returned. When window {nr} doesn't exist, -1 is returned.
4742 An existing window always has a height of zero or more.
4743 Examples: >
4744 :echo "The current window has " . winheight(0) . " lines."
4745<
4746 *winline()*
4747winline() The result is a Number, which is the screen line of the cursor
4748 in the window. This is counting screen lines from the top of
4749 the window. The first line is one.
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004750 If the cursor was moved the view on the file will be updated
4751 first, this may cause a scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752
4753 *winnr()*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004754winnr([{arg}]) The result is a Number, which is the number of the current
4755 window. The top window has number 1.
4756 When the optional argument is "$", the number of the
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004757 last window is returned (the window count).
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004758 When the optional argument is "#", the number of the last
4759 accessed window is returned (where |CTRL-W_p| goes to).
4760 If there is no previous window 0 is returned.
4761 The number can be used with |CTRL-W_w| and ":wincmd w"
4762 |:wincmd|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
4764 *winrestcmd()*
4765winrestcmd() Returns a sequence of |:resize| commands that should restore
4766 the current window sizes. Only works properly when no windows
4767 are opened or closed and the current window is unchanged.
4768 Example: >
4769 :let cmd = winrestcmd()
4770 :call MessWithWindowSizes()
4771 :exe cmd
4772
4773winwidth({nr}) *winwidth()*
4774 The result is a Number, which is the width of window {nr}.
4775 When {nr} is zero, the width of the current window is
4776 returned. When window {nr} doesn't exist, -1 is returned.
4777 An existing window always has a width of zero or more.
4778 Examples: >
4779 :echo "The current window has " . winwidth(0) . " columns."
4780 :if winwidth(0) <= 50
4781 : exe "normal 50\<C-W>|"
4782 :endif
4783<
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00004784 *writefile()*
4785writefile({list}, {fname} [, {binary}])
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004786 Write |List| {list} to file {fname}. Each list item is
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00004787 separated with a NL. Each list item must be a String or
4788 Number.
4789 When {binary} is equal to "b" binary mode is used: There will
4790 not be a NL after the last list item. An empty item at the
4791 end does cause the last line in the file to end in a NL.
4792 All NL characters are replaced with a NUL character.
4793 Inserting CR characters needs to be done before passing {list}
4794 to writefile().
4795 An existing file is overwritten, if possible.
4796 When the write fails -1 is returned, otherwise 0. There is an
4797 error message if the file can't be created or when writing
4798 fails.
4799 Also see |readfile()|.
4800 To copy a file byte for byte: >
4801 :let fl = readfile("foo", "b")
4802 :call writefile(fl, "foocopy", "b")
4803<
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804
4805 *feature-list*
4806There are three types of features:
48071. Features that are only supported when they have been enabled when Vim
4808 was compiled |+feature-list|. Example: >
4809 :if has("cindent")
48102. Features that are only supported when certain conditions have been met.
4811 Example: >
4812 :if has("gui_running")
4813< *has-patch*
48143. Included patches. First check |v:version| for the version of Vim.
4815 Then the "patch123" feature means that patch 123 has been included for
4816 this version. Example (checking version 6.2.148 or later): >
4817 :if v:version > 602 || v:version == 602 && has("patch148")
4818
4819all_builtin_terms Compiled with all builtin terminals enabled.
4820amiga Amiga version of Vim.
4821arabic Compiled with Arabic support |Arabic|.
4822arp Compiled with ARP support (Amiga).
Bram Moolenaara9b1e742005-12-19 22:14:58 +00004823autocmd Compiled with autocommand support. |autocommand|
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824balloon_eval Compiled with |balloon-eval| support.
Bram Moolenaar45360022005-07-21 21:08:21 +00004825balloon_multiline GUI supports multiline balloons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826beos BeOS version of Vim.
4827browse Compiled with |:browse| support, and browse() will
4828 work.
4829builtin_terms Compiled with some builtin terminals.
4830byte_offset Compiled with support for 'o' in 'statusline'
4831cindent Compiled with 'cindent' support.
4832clientserver Compiled with remote invocation support |clientserver|.
4833clipboard Compiled with 'clipboard' support.
4834cmdline_compl Compiled with |cmdline-completion| support.
4835cmdline_hist Compiled with |cmdline-history| support.
4836cmdline_info Compiled with 'showcmd' and 'ruler' support.
4837comments Compiled with |'comments'| support.
4838cryptv Compiled with encryption support |encryption|.
4839cscope Compiled with |cscope| support.
4840compatible Compiled to be very Vi compatible.
4841debug Compiled with "DEBUG" defined.
4842dialog_con Compiled with console dialog support.
4843dialog_gui Compiled with GUI dialog support.
4844diff Compiled with |vimdiff| and 'diff' support.
4845digraphs Compiled with support for digraphs.
4846dnd Compiled with support for the "~ register |quote_~|.
4847dos32 32 bits DOS (DJGPP) version of Vim.
4848dos16 16 bits DOS version of Vim.
4849ebcdic Compiled on a machine with ebcdic character set.
4850emacs_tags Compiled with support for Emacs tags.
4851eval Compiled with expression evaluation support. Always
4852 true, of course!
4853ex_extra Compiled with extra Ex commands |+ex_extra|.
4854extra_search Compiled with support for |'incsearch'| and
4855 |'hlsearch'|
4856farsi Compiled with Farsi support |farsi|.
4857file_in_path Compiled with support for |gf| and |<cfile>|
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004858filterpipe When 'shelltemp' is off pipes are used for shell
4859 read/write/filter commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860find_in_path Compiled with support for include file searches
4861 |+find_in_path|.
4862fname_case Case in file names matters (for Amiga, MS-DOS, and
4863 Windows this is not present).
4864folding Compiled with |folding| support.
4865footer Compiled with GUI footer support. |gui-footer|
4866fork Compiled to use fork()/exec() instead of system().
4867gettext Compiled with message translation |multi-lang|
4868gui Compiled with GUI enabled.
4869gui_athena Compiled with Athena GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870gui_gtk Compiled with GTK+ GUI (any version).
4871gui_gtk2 Compiled with GTK+ 2 GUI (gui_gtk is also defined).
Bram Moolenaar843ee412004-06-30 16:16:41 +00004872gui_kde Compiled with KDE GUI |KVim|
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873gui_mac Compiled with Macintosh GUI.
4874gui_motif Compiled with Motif GUI.
4875gui_photon Compiled with Photon GUI.
4876gui_win32 Compiled with MS Windows Win32 GUI.
4877gui_win32s idem, and Win32s system being used (Windows 3.1)
4878gui_running Vim is running in the GUI, or it will start soon.
4879hangul_input Compiled with Hangul input support. |hangul|
4880iconv Can use iconv() for conversion.
4881insert_expand Compiled with support for CTRL-X expansion commands in
4882 Insert mode.
4883jumplist Compiled with |jumplist| support.
4884keymap Compiled with 'keymap' support.
4885langmap Compiled with 'langmap' support.
4886libcall Compiled with |libcall()| support.
4887linebreak Compiled with 'linebreak', 'breakat' and 'showbreak'
4888 support.
4889lispindent Compiled with support for lisp indenting.
4890listcmds Compiled with commands for the buffer list |:files|
4891 and the argument list |arglist|.
4892localmap Compiled with local mappings and abbr. |:map-local|
4893mac Macintosh version of Vim.
4894macunix Macintosh version of Vim, using Unix files (OS-X).
4895menu Compiled with support for |:menu|.
4896mksession Compiled with support for |:mksession|.
4897modify_fname Compiled with file name modifiers. |filename-modifiers|
4898mouse Compiled with support mouse.
4899mouseshape Compiled with support for 'mouseshape'.
4900mouse_dec Compiled with support for Dec terminal mouse.
4901mouse_gpm Compiled with support for gpm (Linux console mouse)
4902mouse_netterm Compiled with support for netterm mouse.
4903mouse_pterm Compiled with support for qnx pterm mouse.
4904mouse_xterm Compiled with support for xterm mouse.
4905multi_byte Compiled with support for editing Korean et al.
4906multi_byte_ime Compiled with support for IME input method.
4907multi_lang Compiled with support for multiple languages.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004908mzscheme Compiled with MzScheme interface |mzscheme|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909netbeans_intg Compiled with support for |netbeans|.
Bram Moolenaar009b2592004-10-24 19:18:58 +00004910netbeans_enabled Compiled with support for |netbeans| and it's used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911ole Compiled with OLE automation support for Win32.
4912os2 OS/2 version of Vim.
4913osfiletype Compiled with support for osfiletypes |+osfiletype|
4914path_extra Compiled with up/downwards search in 'path' and 'tags'
4915perl Compiled with Perl interface.
4916postscript Compiled with PostScript file printing.
4917printer Compiled with |:hardcopy| support.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004918profile Compiled with |:profile| support.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919python Compiled with Python interface.
4920qnx QNX version of Vim.
4921quickfix Compiled with |quickfix| support.
4922rightleft Compiled with 'rightleft' support.
4923ruby Compiled with Ruby interface |ruby|.
4924scrollbind Compiled with 'scrollbind' support.
4925showcmd Compiled with 'showcmd' support.
4926signs Compiled with |:sign| support.
4927smartindent Compiled with 'smartindent' support.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004928sniff Compiled with SNiFF interface support.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929statusline Compiled with support for 'statusline', 'rulerformat'
4930 and special formats of 'titlestring' and 'iconstring'.
4931sun_workshop Compiled with support for Sun |workshop|.
Bram Moolenaar82cf9b62005-06-07 21:09:25 +00004932spell Compiled with spell checking support |spell|.
4933syntax Compiled with syntax highlighting support |syntax|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934syntax_items There are active syntax highlighting items for the
4935 current buffer.
4936system Compiled to use system() instead of fork()/exec().
4937tag_binary Compiled with binary searching in tags files
4938 |tag-binary-search|.
4939tag_old_static Compiled with support for old static tags
4940 |tag-old-static|.
4941tag_any_white Compiled with support for any white characters in tags
4942 files |tag-any-white|.
4943tcl Compiled with Tcl interface.
4944terminfo Compiled with terminfo instead of termcap.
4945termresponse Compiled with support for |t_RV| and |v:termresponse|.
4946textobjects Compiled with support for |text-objects|.
4947tgetent Compiled with tgetent support, able to use a termcap
4948 or terminfo file.
4949title Compiled with window title support |'title'|.
4950toolbar Compiled with support for |gui-toolbar|.
4951unix Unix version of Vim.
4952user_commands User-defined commands.
4953viminfo Compiled with viminfo support.
4954vim_starting True while initial source'ing takes place.
4955vertsplit Compiled with vertically split windows |:vsplit|.
4956virtualedit Compiled with 'virtualedit' option.
4957visual Compiled with Visual mode.
4958visualextra Compiled with extra Visual mode commands.
4959 |blockwise-operators|.
4960vms VMS version of Vim.
4961vreplace Compiled with |gR| and |gr| commands.
4962wildignore Compiled with 'wildignore' option.
4963wildmenu Compiled with 'wildmenu' option.
4964windows Compiled with support for more than one window.
4965winaltkeys Compiled with 'winaltkeys' option.
4966win16 Win16 version of Vim (MS-Windows 3.1).
4967win32 Win32 version of Vim (MS-Windows 95/98/ME/NT/2000/XP).
4968win64 Win64 version of Vim (MS-Windows 64 bit).
4969win32unix Win32 version of Vim, using Unix files (Cygwin)
4970win95 Win32 version for MS-Windows 95/98/ME.
4971writebackup Compiled with 'writebackup' default on.
4972xfontset Compiled with X fontset support |xfontset|.
4973xim Compiled with X input method support |xim|.
4974xsmp Compiled with X session management support.
4975xsmp_interact Compiled with interactive X session management support.
4976xterm_clipboard Compiled with support for xterm clipboard.
4977xterm_save Compiled with support for saving and restoring the
4978 xterm screen.
4979x11 Compiled with X11 support.
4980
4981 *string-match*
4982Matching a pattern in a String
4983
4984A regexp pattern as explained at |pattern| is normally used to find a match in
4985the buffer lines. When a pattern is used to find a match in a String, almost
4986everything works in the same way. The difference is that a String is handled
4987like it is one line. When it contains a "\n" character, this is not seen as a
4988line break for the pattern. It can be matched with a "\n" in the pattern, or
4989with ".". Example: >
4990 :let a = "aaaa\nxxxx"
4991 :echo matchstr(a, "..\n..")
4992 aa
4993 xx
4994 :echo matchstr(a, "a.x")
4995 a
4996 x
4997
4998Don't forget that "^" will only match at the first character of the String and
4999"$" at the last character of the string. They don't match after or before a
5000"\n".
5001
5002==============================================================================
50035. Defining functions *user-functions*
5004
5005New functions can be defined. These can be called just like builtin
5006functions. The function executes a sequence of Ex commands. Normal mode
5007commands can be executed with the |:normal| command.
5008
5009The function name must start with an uppercase letter, to avoid confusion with
5010builtin functions. To prevent from using the same name in different scripts
5011avoid obvious, short names. A good habit is to start the function name with
5012the name of the script, e.g., "HTMLcolor()".
5013
Bram Moolenaar92d640f2005-09-05 22:11:52 +00005014It's also possible to use curly braces, see |curly-braces-names|. And the
5015|autoload| facility is useful to define a function only when it's called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016
5017 *local-function*
5018A function local to a script must start with "s:". A local script function
5019can only be called from within the script and from functions, user commands
5020and autocommands defined in the script. It is also possible to call the
5021function from a mappings defined in the script, but then |<SID>| must be used
5022instead of "s:" when the mapping is expanded outside of the script.
5023
5024 *:fu* *:function* *E128* *E129* *E123*
5025:fu[nction] List all functions and their arguments.
5026
5027:fu[nction] {name} List function {name}.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005028 {name} can also be a |Dictionary| entry that is a
5029 |Funcref|: >
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00005030 :function dict.init
Bram Moolenaar92d640f2005-09-05 22:11:52 +00005031
5032:fu[nction] /{pattern} List functions with a name matching {pattern}.
5033 Example that lists all functions ending with "File": >
5034 :function /File$
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005035<
5036 *:function-verbose*
5037When 'verbose' is non-zero, listing a function will also display where it was
5038last defined. Example: >
5039
5040 :verbose function SetFileTypeSH
5041 function SetFileTypeSH(name)
5042 Last set from /usr/share/vim/vim-7.0/filetype.vim
5043<
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005044See |:verbose-cmd| for more information.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005045
5046 *E124* *E125*
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00005047:fu[nction][!] {name}([arguments]) [range] [abort] [dict]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 Define a new function by the name {name}. The name
5049 must be made of alphanumeric characters and '_', and
5050 must start with a capital or "s:" (see above).
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00005051
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005052 {name} can also be a |Dictionary| entry that is a
5053 |Funcref|: >
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00005054 :function dict.init(arg)
5055< "dict" must be an existing dictionary. The entry
5056 "init" is added if it didn't exist yet. Otherwise [!]
5057 is required to overwrite an existing function. The
5058 result is a |Funcref| to a numbered function. The
5059 function can only be used with a |Funcref| and will be
5060 deleted if there are no more references to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 *E127* *E122*
5062 When a function by this name already exists and [!] is
5063 not used an error message is given. When [!] is used,
5064 an existing function is silently replaced. Unless it
5065 is currently being executed, that is an error.
Bram Moolenaar8f999f12005-01-25 22:12:55 +00005066
5067 For the {arguments} see |function-argument|.
5068
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 *a:firstline* *a:lastline*
5070 When the [range] argument is added, the function is
5071 expected to take care of a range itself. The range is
5072 passed as "a:firstline" and "a:lastline". If [range]
5073 is excluded, ":{range}call" will call the function for
5074 each line in the range, with the cursor on the start
5075 of each line. See |function-range-example|.
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00005076
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 When the [abort] argument is added, the function will
5078 abort as soon as an error is detected.
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00005079
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00005080 When the [dict] argument is added, the function must
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005081 be invoked through an entry in a |Dictionary|. The
Bram Moolenaar2fda12f2005-01-15 22:14:15 +00005082 local variable "self" will then be set to the
5083 dictionary. See |Dictionary-function|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084
Bram Moolenaar98692072006-02-04 00:57:42 +00005085 The last used search pattern and the redo command "."
5086 will not be changed by the function.
5087
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 *:endf* *:endfunction* *E126* *E193*
5089:endf[unction] The end of a function definition. Must be on a line
5090 by its own, without other commands.
5091
5092 *:delf* *:delfunction* *E130* *E131*
5093:delf[unction] {name} Delete function {name}.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005094 {name} can also be a |Dictionary| entry that is a
5095 |Funcref|: >
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00005096 :delfunc dict.init
5097< This will remove the "init" entry from "dict". The
5098 function is deleted if there are no more references to
5099 it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 *:retu* *:return* *E133*
5101:retu[rn] [expr] Return from a function. When "[expr]" is given, it is
5102 evaluated and returned as the result of the function.
5103 If "[expr]" is not given, the number 0 is returned.
5104 When a function ends without an explicit ":return",
5105 the number 0 is returned.
5106 Note that there is no check for unreachable lines,
5107 thus there is no warning if commands follow ":return".
5108
5109 If the ":return" is used after a |:try| but before the
5110 matching |:finally| (if present), the commands
5111 following the ":finally" up to the matching |:endtry|
5112 are executed first. This process applies to all
5113 nested ":try"s inside the function. The function
5114 returns at the outermost ":endtry".
5115
Bram Moolenaar8f999f12005-01-25 22:12:55 +00005116 *function-argument* *a:var*
5117An argument can be defined by giving its name. In the function this can then
5118be used as "a:name" ("a:" for argument).
5119 *a:0* *a:1* *a:000* *E740*
5120Up to 20 arguments can be given, separated by commas. After the named
5121arguments an argument "..." can be specified, which means that more arguments
5122may optionally be following. In the function the extra arguments can be used
5123as "a:1", "a:2", etc. "a:0" is set to the number of extra arguments (which
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005124can be 0). "a:000" is set to a |List| that contains these arguments. Note
5125that "a:1" is the same as "a:000[0]".
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005126 *E742*
5127The a: scope and the variables in it cannot be changed, they are fixed.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005128However, if a |List| or |Dictionary| is used, you can changes their contents.
5129Thus you can pass a |List| to a function and have the function add an item to
5130it. If you want to make sure the function cannot change a |List| or
5131|Dictionary| use |:lockvar|.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132
Bram Moolenaar8f999f12005-01-25 22:12:55 +00005133When not using "...", the number of arguments in a function call must be equal
5134to the number of named arguments. When using "...", the number of arguments
5135may be larger.
5136
5137It is also possible to define a function without any arguments. You must
5138still supply the () then. The body of the function follows in the next lines,
5139until the matching |:endfunction|. It is allowed to define another function
5140inside a function body.
5141
5142 *local-variables*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143Inside a function variables can be used. These are local variables, which
5144will disappear when the function returns. Global variables need to be
5145accessed with "g:".
5146
5147Example: >
5148 :function Table(title, ...)
5149 : echohl Title
5150 : echo a:title
5151 : echohl None
Bram Moolenaar677ee682005-01-27 14:41:15 +00005152 : echo a:0 . " items:"
5153 : for s in a:000
5154 : echon ' ' . s
5155 : endfor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 :endfunction
5157
5158This function can then be called with: >
Bram Moolenaar677ee682005-01-27 14:41:15 +00005159 call Table("Table", "line1", "line2")
5160 call Table("Empty Table")
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161
5162To return more than one value, pass the name of a global variable: >
5163 :function Compute(n1, n2, divname)
5164 : if a:n2 == 0
5165 : return "fail"
5166 : endif
5167 : let g:{a:divname} = a:n1 / a:n2
5168 : return "ok"
5169 :endfunction
5170
5171This function can then be called with: >
5172 :let success = Compute(13, 1324, "div")
5173 :if success == "ok"
5174 : echo div
5175 :endif
5176
5177An alternative is to return a command that can be executed. This also works
5178with local variables in a calling function. Example: >
5179 :function Foo()
5180 : execute Bar()
5181 : echo "line " . lnum . " column " . col
5182 :endfunction
5183
5184 :function Bar()
5185 : return "let lnum = " . line(".") . " | let col = " . col(".")
5186 :endfunction
5187
5188The names "lnum" and "col" could also be passed as argument to Bar(), to allow
5189the caller to set the names.
5190
5191 *:cal* *:call* *E107*
5192:[range]cal[l] {name}([arguments])
5193 Call a function. The name of the function and its arguments
5194 are as specified with |:function|. Up to 20 arguments can be
5195 used.
5196 Without a range and for functions that accept a range, the
5197 function is called once. When a range is given the cursor is
5198 positioned at the start of the first line before executing the
5199 function.
5200 When a range is given and the function doesn't handle it
5201 itself, the function is executed for each line in the range,
5202 with the cursor in the first column of that line. The cursor
5203 is left at the last line (possibly moved by the last function
5204 call). The arguments are re-evaluated for each line. Thus
5205 this works:
5206 *function-range-example* >
5207 :function Mynumber(arg)
5208 : echo line(".") . " " . a:arg
5209 :endfunction
5210 :1,5call Mynumber(getline("."))
5211<
5212 The "a:firstline" and "a:lastline" are defined anyway, they
5213 can be used to do something different at the start or end of
5214 the range.
5215
5216 Example of a function that handles the range itself: >
5217
5218 :function Cont() range
5219 : execute (a:firstline + 1) . "," . a:lastline . 's/^/\t\\ '
5220 :endfunction
5221 :4,8call Cont()
5222<
5223 This function inserts the continuation character "\" in front
5224 of all the lines in the range, except the first one.
5225
5226 *E132*
5227The recursiveness of user functions is restricted with the |'maxfuncdepth'|
5228option.
5229
Bram Moolenaar7c626922005-02-07 22:01:03 +00005230
5231AUTOMATICALLY LOADING FUNCTIONS ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 *autoload-functions*
5233When using many or large functions, it's possible to automatically define them
Bram Moolenaar7c626922005-02-07 22:01:03 +00005234only when they are used. There are two methods: with an autocommand and with
5235the "autoload" directory in 'runtimepath'.
5236
5237
5238Using an autocommand ~
5239
Bram Moolenaar05159a02005-02-26 23:04:13 +00005240This is introduced in the user manual, section |41.14|.
5241
Bram Moolenaar7c626922005-02-07 22:01:03 +00005242The autocommand is useful if you have a plugin that is a long Vim script file.
5243You can define the autocommand and quickly quit the script with |:finish|.
5244That makes Vim startup faster. The autocommand should then load the same file
5245again, setting a variable to skip the |:finish| command.
5246
5247Use the FuncUndefined autocommand event with a pattern that matches the
5248function(s) to be defined. Example: >
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249
5250 :au FuncUndefined BufNet* source ~/vim/bufnetfuncs.vim
5251
5252The file "~/vim/bufnetfuncs.vim" should then define functions that start with
5253"BufNet". Also see |FuncUndefined|.
5254
Bram Moolenaar7c626922005-02-07 22:01:03 +00005255
5256Using an autoload script ~
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005257 *autoload* *E746*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005258This is introduced in the user manual, section |41.15|.
5259
Bram Moolenaar7c626922005-02-07 22:01:03 +00005260Using a script in the "autoload" directory is simpler, but requires using
5261exactly the right file name. A function that can be autoloaded has a name
5262like this: >
5263
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005264 :call filename#funcname()
Bram Moolenaar7c626922005-02-07 22:01:03 +00005265
5266When such a function is called, and it is not defined yet, Vim will search the
5267"autoload" directories in 'runtimepath' for a script file called
5268"filename.vim". For example "~/.vim/autoload/filename.vim". That file should
5269then define the function like this: >
5270
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005271 function filename#funcname()
Bram Moolenaar7c626922005-02-07 22:01:03 +00005272 echo "Done!"
5273 endfunction
5274
Bram Moolenaar60a795a2005-09-16 21:55:43 +00005275The file name and the name used before the # in the function must match
Bram Moolenaar7c626922005-02-07 22:01:03 +00005276exactly, and the defined function must have the name exactly as it will be
5277called.
5278
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005279It is possible to use subdirectories. Every # in the function name works like
5280a path separator. Thus when calling a function: >
Bram Moolenaar7c626922005-02-07 22:01:03 +00005281
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005282 :call foo#bar#func()
Bram Moolenaar7c626922005-02-07 22:01:03 +00005283
5284Vim will look for the file "autoload/foo/bar.vim" in 'runtimepath'.
5285
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005286This also works when reading a variable that has not been set yet: >
5287
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005288 :let l = foo#bar#lvar
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005289
Bram Moolenaara5792f52005-11-23 21:25:05 +00005290However, when the autoload script was already loaded it won't be loaded again
5291for an unknown variable.
5292
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005293When assigning a value to such a variable nothing special happens. This can
5294be used to pass settings to the autoload script before it's loaded: >
5295
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005296 :let foo#bar#toggle = 1
5297 :call foo#bar#func()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005298
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005299Note that when you make a mistake and call a function that is supposed to be
5300defined in an autoload script, but the script doesn't actually define the
5301function, the script will be sourced every time you try to call the function.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005302And you will get an error message every time.
5303
5304Also note that if you have two script files, and one calls a function in the
5305other and vise versa, before the used function is defined, it won't work.
5306Avoid using the autoload functionality at the toplevel.
Bram Moolenaar7c626922005-02-07 22:01:03 +00005307
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308==============================================================================
53096. Curly braces names *curly-braces-names*
5310
5311Wherever you can use a variable, you can use a "curly braces name" variable.
5312This is a regular variable name with one or more expressions wrapped in braces
5313{} like this: >
5314 my_{adjective}_variable
5315
5316When Vim encounters this, it evaluates the expression inside the braces, puts
5317that in place of the expression, and re-interprets the whole as a variable
5318name. So in the above example, if the variable "adjective" was set to
5319"noisy", then the reference would be to "my_noisy_variable", whereas if
5320"adjective" was set to "quiet", then it would be to "my_quiet_variable".
5321
5322One application for this is to create a set of variables governed by an option
5323value. For example, the statement >
5324 echo my_{&background}_message
5325
5326would output the contents of "my_dark_message" or "my_light_message" depending
5327on the current value of 'background'.
5328
5329You can use multiple brace pairs: >
5330 echo my_{adverb}_{adjective}_message
5331..or even nest them: >
5332 echo my_{ad{end_of_word}}_message
5333where "end_of_word" is either "verb" or "jective".
5334
5335However, the expression inside the braces must evaluate to a valid single
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005336variable name, e.g. this is invalid: >
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 :let foo='a + b'
5338 :echo c{foo}d
5339.. since the result of expansion is "ca + bd", which is not a variable name.
5340
5341 *curly-braces-function-names*
5342You can call and define functions by an evaluated name in a similar way.
5343Example: >
5344 :let func_end='whizz'
5345 :call my_func_{func_end}(parameter)
5346
5347This would call the function "my_func_whizz(parameter)".
5348
5349==============================================================================
53507. Commands *expression-commands*
5351
5352:let {var-name} = {expr1} *:let* *E18*
5353 Set internal variable {var-name} to the result of the
5354 expression {expr1}. The variable will get the type
5355 from the {expr}. If {var-name} didn't exist yet, it
5356 is created.
5357
Bram Moolenaar13065c42005-01-08 16:08:21 +00005358:let {var-name}[{idx}] = {expr1} *E689*
5359 Set a list item to the result of the expression
5360 {expr1}. {var-name} must refer to a list and {idx}
5361 must be a valid index in that list. For nested list
5362 the index can be repeated.
5363 This cannot be used to add an item to a list.
5364
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00005365 *E711* *E719*
5366:let {var-name}[{idx1}:{idx2}] = {expr1} *E708* *E709* *E710*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005367 Set a sequence of items in a |List| to the result of
5368 the expression {expr1}, which must be a list with the
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00005369 correct number of items.
5370 {idx1} can be omitted, zero is used instead.
5371 {idx2} can be omitted, meaning the end of the list.
5372 When the selected range of items is partly past the
5373 end of the list, items will be added.
5374
Bram Moolenaar748bf032005-02-02 23:04:36 +00005375 *:let+=* *:let-=* *:let.=* *E734*
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00005376:let {var} += {expr1} Like ":let {var} = {var} + {expr1}".
5377:let {var} -= {expr1} Like ":let {var} = {var} - {expr1}".
5378:let {var} .= {expr1} Like ":let {var} = {var} . {expr1}".
5379 These fail if {var} was not set yet and when the type
5380 of {var} and {expr1} don't fit the operator.
5381
5382
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383:let ${env-name} = {expr1} *:let-environment* *:let-$*
5384 Set environment variable {env-name} to the result of
5385 the expression {expr1}. The type is always String.
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00005386:let ${env-name} .= {expr1}
5387 Append {expr1} to the environment variable {env-name}.
5388 If the environment variable didn't exist yet this
5389 works like "=".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390
5391:let @{reg-name} = {expr1} *:let-register* *:let-@*
5392 Write the result of the expression {expr1} in register
5393 {reg-name}. {reg-name} must be a single letter, and
5394 must be the name of a writable register (see
5395 |registers|). "@@" can be used for the unnamed
5396 register, "@/" for the search pattern.
5397 If the result of {expr1} ends in a <CR> or <NL>, the
5398 register will be linewise, otherwise it will be set to
5399 characterwise.
5400 This can be used to clear the last search pattern: >
5401 :let @/ = ""
5402< This is different from searching for an empty string,
5403 that would match everywhere.
5404
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00005405:let @{reg-name} .= {expr1}
5406 Append {expr1} to register {reg-name}. If the
5407 register was empty it's like setting it to {expr1}.
5408
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409:let &{option-name} = {expr1} *:let-option* *:let-star*
5410 Set option {option-name} to the result of the
Bram Moolenaarfca34d62005-01-04 21:38:36 +00005411 expression {expr1}. A String or Number value is
5412 always converted to the type of the option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 For an option local to a window or buffer the effect
5414 is just like using the |:set| command: both the local
Bram Moolenaara5fac542005-10-12 20:58:49 +00005415 value and the global value are changed.
Bram Moolenaarfca34d62005-01-04 21:38:36 +00005416 Example: >
5417 :let &path = &path . ',/usr/local/include'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00005419:let &{option-name} .= {expr1}
5420 For a string option: Append {expr1} to the value.
5421 Does not insert a comma like |:set+=|.
5422
5423:let &{option-name} += {expr1}
5424:let &{option-name} -= {expr1}
5425 For a number or boolean option: Add or subtract
5426 {expr1}.
5427
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428:let &l:{option-name} = {expr1}
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00005429:let &l:{option-name} .= {expr1}
5430:let &l:{option-name} += {expr1}
5431:let &l:{option-name} -= {expr1}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 Like above, but only set the local value of an option
5433 (if there is one). Works like |:setlocal|.
5434
5435:let &g:{option-name} = {expr1}
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00005436:let &g:{option-name} .= {expr1}
5437:let &g:{option-name} += {expr1}
5438:let &g:{option-name} -= {expr1}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 Like above, but only set the global value of an option
5440 (if there is one). Works like |:setglobal|.
5441
Bram Moolenaar13065c42005-01-08 16:08:21 +00005442:let [{name1}, {name2}, ...] = {expr1} *:let-unpack* *E687* *E688*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005443 {expr1} must evaluate to a |List|. The first item in
Bram Moolenaarfca34d62005-01-04 21:38:36 +00005444 the list is assigned to {name1}, the second item to
5445 {name2}, etc.
5446 The number of names must match the number of items in
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005447 the |List|.
Bram Moolenaarfca34d62005-01-04 21:38:36 +00005448 Each name can be one of the items of the ":let"
5449 command as mentioned above.
5450 Example: >
5451 :let [s, item] = GetItem(s)
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00005452< Detail: {expr1} is evaluated first, then the
5453 assignments are done in sequence. This matters if
5454 {name2} depends on {name1}. Example: >
5455 :let x = [0, 1]
5456 :let i = 0
5457 :let [i, x[i]] = [1, 2]
5458 :echo x
5459< The result is [0, 2].
5460
5461:let [{name1}, {name2}, ...] .= {expr1}
5462:let [{name1}, {name2}, ...] += {expr1}
5463:let [{name1}, {name2}, ...] -= {expr1}
5464 Like above, but append/add/subtract the value for each
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005465 |List| item.
Bram Moolenaarfca34d62005-01-04 21:38:36 +00005466
5467:let [{name}, ..., ; {lastname}] = {expr1}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005468 Like |:let-unpack| above, but the |List| may have more
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00005469 items than there are names. A list of the remaining
5470 items is assigned to {lastname}. If there are no
5471 remaining items {lastname} is set to an empty list.
Bram Moolenaarfca34d62005-01-04 21:38:36 +00005472 Example: >
5473 :let [a, b; rest] = ["aval", "bval", 3, 4]
5474<
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00005475:let [{name}, ..., ; {lastname}] .= {expr1}
5476:let [{name}, ..., ; {lastname}] += {expr1}
5477:let [{name}, ..., ; {lastname}] -= {expr1}
5478 Like above, but append/add/subtract the value for each
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005479 |List| item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 *E106*
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00005481:let {var-name} .. List the value of variable {var-name}. Multiple
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005482 variable names may be given. Special names recognized
5483 here: *E738*
5484 g: global variables.
5485 b: local buffer variables.
5486 w: local window variables.
5487 v: Vim variables.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005489:let List the values of all variables. The type of the
5490 variable is indicated before the value:
5491 <nothing> String
5492 # Number
5493 * Funcref
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005495
5496:unl[et][!] {name} ... *:unlet* *:unl* *E108*
5497 Remove the internal variable {name}. Several variable
5498 names can be given, they are all removed. The name
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005499 may also be a |List| or |Dictionary| item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 With [!] no error message is given for non-existing
5501 variables.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005502 One or more items from a |List| can be removed: >
Bram Moolenaar9cd15162005-01-16 22:02:49 +00005503 :unlet list[3] " remove fourth item
5504 :unlet list[3:] " remove fourth item to last
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005505< One item from a |Dictionary| can be removed at a time: >
Bram Moolenaar9cd15162005-01-16 22:02:49 +00005506 :unlet dict['two']
5507 :unlet dict.two
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005509:lockv[ar][!] [depth] {name} ... *:lockvar* *:lockv*
5510 Lock the internal variable {name}. Locking means that
5511 it can no longer be changed (until it is unlocked).
5512 A locked variable can be deleted: >
5513 :lockvar v
5514 :let v = 'asdf' " fails!
5515 :unlet v
5516< *E741*
5517 If you try to change a locked variable you get an
5518 error message: "E741: Value of {name} is locked"
5519
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005520 [depth] is relevant when locking a |List| or
5521 |Dictionary|. It specifies how deep the locking goes:
5522 1 Lock the |List| or |Dictionary| itself,
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005523 cannot add or remove items, but can
5524 still change their values.
5525 2 Also lock the values, cannot change
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005526 the items. If an item is a |List| or
5527 |Dictionary|, cannot add or remove
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005528 items, but can still change the
5529 values.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005530 3 Like 2 but for the |List| /
5531 |Dictionary| in the |List| /
5532 |Dictionary|, one level deeper.
5533 The default [depth] is 2, thus when {name} is a |List|
5534 or |Dictionary| the values cannot be changed.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005535 *E743*
5536 For unlimited depth use [!] and omit [depth].
5537 However, there is a maximum depth of 100 to catch
5538 loops.
5539
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005540 Note that when two variables refer to the same |List|
5541 and you lock one of them, the |List| will also be
5542 locked when used through the other variable. Example:
5543 >
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005544 :let l = [0, 1, 2, 3]
5545 :let cl = l
5546 :lockvar l
5547 :let cl[1] = 99 " won't work!
5548< You may want to make a copy of a list to avoid this.
5549 See |deepcopy()|.
5550
5551
5552:unlo[ckvar][!] [depth] {name} ... *:unlockvar* *:unlo*
5553 Unlock the internal variable {name}. Does the
5554 opposite of |:lockvar|.
5555
5556
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557:if {expr1} *:if* *:endif* *:en* *E171* *E579* *E580*
5558:en[dif] Execute the commands until the next matching ":else"
5559 or ":endif" if {expr1} evaluates to non-zero.
5560
5561 From Vim version 4.5 until 5.0, every Ex command in
5562 between the ":if" and ":endif" is ignored. These two
5563 commands were just to allow for future expansions in a
5564 backwards compatible way. Nesting was allowed. Note
5565 that any ":else" or ":elseif" was ignored, the "else"
5566 part was not executed either.
5567
5568 You can use this to remain compatible with older
5569 versions: >
5570 :if version >= 500
5571 : version-5-specific-commands
5572 :endif
5573< The commands still need to be parsed to find the
5574 "endif". Sometimes an older Vim has a problem with a
5575 new command. For example, ":silent" is recognized as
5576 a ":substitute" command. In that case ":execute" can
5577 avoid problems: >
5578 :if version >= 600
5579 : execute "silent 1,$delete"
5580 :endif
5581<
5582 NOTE: The ":append" and ":insert" commands don't work
5583 properly in between ":if" and ":endif".
5584
5585 *:else* *:el* *E581* *E583*
5586:el[se] Execute the commands until the next matching ":else"
5587 or ":endif" if they previously were not being
5588 executed.
5589
5590 *:elseif* *:elsei* *E582* *E584*
5591:elsei[f] {expr1} Short for ":else" ":if", with the addition that there
5592 is no extra ":endif".
5593
5594:wh[ile] {expr1} *:while* *:endwhile* *:wh* *:endw*
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00005595 *E170* *E585* *E588* *E733*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596:endw[hile] Repeat the commands between ":while" and ":endwhile",
5597 as long as {expr1} evaluates to non-zero.
5598 When an error is detected from a command inside the
5599 loop, execution continues after the "endwhile".
Bram Moolenaar12805862005-01-05 22:16:17 +00005600 Example: >
5601 :let lnum = 1
5602 :while lnum <= line("$")
5603 :call FixLine(lnum)
5604 :let lnum = lnum + 1
5605 :endwhile
5606<
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 NOTE: The ":append" and ":insert" commands don't work
Bram Moolenaard8b02732005-01-14 21:48:43 +00005608 properly inside a ":while" and ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00005610:for {var} in {list} *:for* *E690* *E732*
Bram Moolenaar12805862005-01-05 22:16:17 +00005611:endfo[r] *:endfo* *:endfor*
5612 Repeat the commands between ":for" and ":endfor" for
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00005613 each item in {list}. Variable {var} is set to the
Bram Moolenaarde8866b2005-01-06 23:24:37 +00005614 value of each item.
5615 When an error is detected for a command inside the
Bram Moolenaar12805862005-01-05 22:16:17 +00005616 loop, execution continues after the "endfor".
Bram Moolenaar572cb562005-08-05 21:35:02 +00005617 Changing {list} inside the loop affects what items are
5618 used. Make a copy if this is unwanted: >
Bram Moolenaarde8866b2005-01-06 23:24:37 +00005619 :for item in copy(mylist)
5620< When not making a copy, Vim stores a reference to the
5621 next item in the list, before executing the commands
5622 with the current item. Thus the current item can be
5623 removed without effect. Removing any later item means
5624 it will not be found. Thus the following example
5625 works (an inefficient way to make a list empty): >
5626 :for item in mylist
Bram Moolenaar12805862005-01-05 22:16:17 +00005627 :call remove(mylist, 0)
5628 :endfor
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00005629< Note that reordering the list (e.g., with sort() or
5630 reverse()) may have unexpected effects.
5631 Note that the type of each list item should be
Bram Moolenaar12805862005-01-05 22:16:17 +00005632 identical to avoid errors for the type of {var}
5633 changing. Unlet the variable at the end of the loop
5634 to allow multiple item types.
5635
Bram Moolenaar12805862005-01-05 22:16:17 +00005636:for [{var1}, {var2}, ...] in {listlist}
5637:endfo[r]
5638 Like ":for" above, but each item in {listlist} must be
5639 a list, of which each item is assigned to {var1},
5640 {var2}, etc. Example: >
5641 :for [lnum, col] in [[1, 3], [2, 5], [3, 8]]
5642 :echo getline(lnum)[col]
5643 :endfor
5644<
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 *:continue* *:con* *E586*
Bram Moolenaar12805862005-01-05 22:16:17 +00005646:con[tinue] When used inside a ":while" or ":for" loop, jumps back
5647 to the start of the loop.
5648 If it is used after a |:try| inside the loop but
5649 before the matching |:finally| (if present), the
5650 commands following the ":finally" up to the matching
5651 |:endtry| are executed first. This process applies to
5652 all nested ":try"s inside the loop. The outermost
5653 ":endtry" then jumps back to the start of the loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654
5655 *:break* *:brea* *E587*
Bram Moolenaar12805862005-01-05 22:16:17 +00005656:brea[k] When used inside a ":while" or ":for" loop, skips to
5657 the command after the matching ":endwhile" or
5658 ":endfor".
5659 If it is used after a |:try| inside the loop but
5660 before the matching |:finally| (if present), the
5661 commands following the ":finally" up to the matching
5662 |:endtry| are executed first. This process applies to
5663 all nested ":try"s inside the loop. The outermost
5664 ":endtry" then jumps to the command after the loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665
5666:try *:try* *:endt* *:endtry* *E600* *E601* *E602*
5667:endt[ry] Change the error handling for the commands between
5668 ":try" and ":endtry" including everything being
5669 executed across ":source" commands, function calls,
5670 or autocommand invocations.
5671
5672 When an error or interrupt is detected and there is
5673 a |:finally| command following, execution continues
5674 after the ":finally". Otherwise, or when the
5675 ":endtry" is reached thereafter, the next
5676 (dynamically) surrounding ":try" is checked for
5677 a corresponding ":finally" etc. Then the script
5678 processing is terminated. (Whether a function
5679 definition has an "abort" argument does not matter.)
5680 Example: >
5681 :try | edit too much | finally | echo "cleanup" | endtry
5682 :echo "impossible" " not reached, script terminated above
5683<
5684 Moreover, an error or interrupt (dynamically) inside
5685 ":try" and ":endtry" is converted to an exception. It
5686 can be caught as if it were thrown by a |:throw|
5687 command (see |:catch|). In this case, the script
5688 processing is not terminated.
5689
5690 The value "Vim:Interrupt" is used for an interrupt
5691 exception. An error in a Vim command is converted
5692 to a value of the form "Vim({command}):{errmsg}",
5693 other errors are converted to a value of the form
5694 "Vim:{errmsg}". {command} is the full command name,
5695 and {errmsg} is the message that is displayed if the
5696 error exception is not caught, always beginning with
5697 the error number.
5698 Examples: >
5699 :try | sleep 100 | catch /^Vim:Interrupt$/ | endtry
5700 :try | edit | catch /^Vim(edit):E\d\+/ | echo "error" | endtry
5701<
5702 *:cat* *:catch* *E603* *E604* *E605*
5703:cat[ch] /{pattern}/ The following commands until the next ":catch",
5704 |:finally|, or |:endtry| that belongs to the same
5705 |:try| as the ":catch" are executed when an exception
5706 matching {pattern} is being thrown and has not yet
5707 been caught by a previous ":catch". Otherwise, these
5708 commands are skipped.
5709 When {pattern} is omitted all errors are caught.
5710 Examples: >
5711 :catch /^Vim:Interrupt$/ " catch interrupts (CTRL-C)
5712 :catch /^Vim\%((\a\+)\)\=:E/ " catch all Vim errors
5713 :catch /^Vim\%((\a\+)\)\=:/ " catch errors and interrupts
5714 :catch /^Vim(write):/ " catch all errors in :write
5715 :catch /^Vim\%((\a\+)\)\=:E123/ " catch error E123
5716 :catch /my-exception/ " catch user exception
5717 :catch /.*/ " catch everything
5718 :catch " same as /.*/
5719<
5720 Another character can be used instead of / around the
5721 {pattern}, so long as it does not have a special
5722 meaning (e.g., '|' or '"') and doesn't occur inside
5723 {pattern}.
5724 NOTE: It is not reliable to ":catch" the TEXT of
5725 an error message because it may vary in different
5726 locales.
5727
5728 *:fina* *:finally* *E606* *E607*
5729:fina[lly] The following commands until the matching |:endtry|
5730 are executed whenever the part between the matching
5731 |:try| and the ":finally" is left: either by falling
5732 through to the ":finally" or by a |:continue|,
5733 |:break|, |:finish|, or |:return|, or by an error or
5734 interrupt or exception (see |:throw|).
5735
5736 *:th* *:throw* *E608*
5737:th[row] {expr1} The {expr1} is evaluated and thrown as an exception.
5738 If the ":throw" is used after a |:try| but before the
5739 first corresponding |:catch|, commands are skipped
5740 until the first ":catch" matching {expr1} is reached.
5741 If there is no such ":catch" or if the ":throw" is
5742 used after a ":catch" but before the |:finally|, the
5743 commands following the ":finally" (if present) up to
5744 the matching |:endtry| are executed. If the ":throw"
5745 is after the ":finally", commands up to the ":endtry"
5746 are skipped. At the ":endtry", this process applies
5747 again for the next dynamically surrounding ":try"
5748 (which may be found in a calling function or sourcing
5749 script), until a matching ":catch" has been found.
5750 If the exception is not caught, the command processing
5751 is terminated.
5752 Example: >
5753 :try | throw "oops" | catch /^oo/ | echo "caught" | endtry
5754<
5755
5756 *:ec* *:echo*
5757:ec[ho] {expr1} .. Echoes each {expr1}, with a space in between. The
5758 first {expr1} starts on a new line.
5759 Also see |:comment|.
5760 Use "\n" to start a new line. Use "\r" to move the
5761 cursor to the first column.
5762 Uses the highlighting set by the |:echohl| command.
5763 Cannot be followed by a comment.
5764 Example: >
5765 :echo "the value of 'shell' is" &shell
5766< A later redraw may make the message disappear again.
5767 To avoid that a command from before the ":echo" causes
5768 a redraw afterwards (redraws are often postponed until
5769 you type something), force a redraw with the |:redraw|
5770 command. Example: >
5771 :new | redraw | echo "there is a new window"
5772<
5773 *:echon*
5774:echon {expr1} .. Echoes each {expr1}, without anything added. Also see
5775 |:comment|.
5776 Uses the highlighting set by the |:echohl| command.
5777 Cannot be followed by a comment.
5778 Example: >
5779 :echon "the value of 'shell' is " &shell
5780<
5781 Note the difference between using ":echo", which is a
5782 Vim command, and ":!echo", which is an external shell
5783 command: >
5784 :!echo % --> filename
5785< The arguments of ":!" are expanded, see |:_%|. >
5786 :!echo "%" --> filename or "filename"
5787< Like the previous example. Whether you see the double
5788 quotes or not depends on your 'shell'. >
5789 :echo % --> nothing
5790< The '%' is an illegal character in an expression. >
5791 :echo "%" --> %
5792< This just echoes the '%' character. >
5793 :echo expand("%") --> filename
5794< This calls the expand() function to expand the '%'.
5795
5796 *:echoh* *:echohl*
5797:echoh[l] {name} Use the highlight group {name} for the following
5798 |:echo|, |:echon| and |:echomsg| commands. Also used
5799 for the |input()| prompt. Example: >
5800 :echohl WarningMsg | echo "Don't panic!" | echohl None
5801< Don't forget to set the group back to "None",
5802 otherwise all following echo's will be highlighted.
5803
5804 *:echom* *:echomsg*
5805:echom[sg] {expr1} .. Echo the expression(s) as a true message, saving the
5806 message in the |message-history|.
5807 Spaces are placed between the arguments as with the
5808 |:echo| command. But unprintable characters are
5809 displayed, not interpreted.
5810 Uses the highlighting set by the |:echohl| command.
5811 Example: >
5812 :echomsg "It's a Zizzer Zazzer Zuzz, as you can plainly see."
5813<
5814 *:echoe* *:echoerr*
5815:echoe[rr] {expr1} .. Echo the expression(s) as an error message, saving the
5816 message in the |message-history|. When used in a
5817 script or function the line number will be added.
5818 Spaces are placed between the arguments as with the
5819 :echo command. When used inside a try conditional,
5820 the message is raised as an error exception instead
5821 (see |try-echoerr|).
5822 Example: >
5823 :echoerr "This script just failed!"
5824< If you just want a highlighted message use |:echohl|.
5825 And to get a beep: >
5826 :exe "normal \<Esc>"
5827<
5828 *:exe* *:execute*
5829:exe[cute] {expr1} .. Executes the string that results from the evaluation
5830 of {expr1} as an Ex command. Multiple arguments are
5831 concatenated, with a space in between. {expr1} is
5832 used as the processed command, command line editing
5833 keys are not recognized.
5834 Cannot be followed by a comment.
5835 Examples: >
5836 :execute "buffer " nextbuf
5837 :execute "normal " count . "w"
5838<
5839 ":execute" can be used to append a command to commands
5840 that don't accept a '|'. Example: >
5841 :execute '!ls' | echo "theend"
5842
5843< ":execute" is also a nice way to avoid having to type
5844 control characters in a Vim script for a ":normal"
5845 command: >
5846 :execute "normal ixxx\<Esc>"
5847< This has an <Esc> character, see |expr-string|.
5848
5849 Note: The executed string may be any command-line, but
Bram Moolenaard8b02732005-01-14 21:48:43 +00005850 you cannot start or end a "while", "for" or "if"
5851 command. Thus this is illegal: >
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 :execute 'while i > 5'
5853 :execute 'echo "test" | break'
5854<
5855 It is allowed to have a "while" or "if" command
5856 completely in the executed string: >
5857 :execute 'while i < 5 | echo i | let i = i + 1 | endwhile'
5858<
5859
5860 *:comment*
5861 ":execute", ":echo" and ":echon" cannot be followed by
5862 a comment directly, because they see the '"' as the
5863 start of a string. But, you can use '|' followed by a
5864 comment. Example: >
5865 :echo "foo" | "this is a comment
5866
5867==============================================================================
58688. Exception handling *exception-handling*
5869
5870The Vim script language comprises an exception handling feature. This section
5871explains how it can be used in a Vim script.
5872
5873Exceptions may be raised by Vim on an error or on interrupt, see
5874|catch-errors| and |catch-interrupt|. You can also explicitly throw an
5875exception by using the ":throw" command, see |throw-catch|.
5876
5877
5878TRY CONDITIONALS *try-conditionals*
5879
5880Exceptions can be caught or can cause cleanup code to be executed. You can
5881use a try conditional to specify catch clauses (that catch exceptions) and/or
5882a finally clause (to be executed for cleanup).
5883 A try conditional begins with a |:try| command and ends at the matching
5884|:endtry| command. In between, you can use a |:catch| command to start
5885a catch clause, or a |:finally| command to start a finally clause. There may
5886be none or multiple catch clauses, but there is at most one finally clause,
5887which must not be followed by any catch clauses. The lines before the catch
5888clauses and the finally clause is called a try block. >
5889
5890 :try
5891 : ...
5892 : ... TRY BLOCK
5893 : ...
5894 :catch /{pattern}/
5895 : ...
5896 : ... CATCH CLAUSE
5897 : ...
5898 :catch /{pattern}/
5899 : ...
5900 : ... CATCH CLAUSE
5901 : ...
5902 :finally
5903 : ...
5904 : ... FINALLY CLAUSE
5905 : ...
5906 :endtry
5907
5908The try conditional allows to watch code for exceptions and to take the
5909appropriate actions. Exceptions from the try block may be caught. Exceptions
5910from the try block and also the catch clauses may cause cleanup actions.
5911 When no exception is thrown during execution of the try block, the control
5912is transferred to the finally clause, if present. After its execution, the
5913script continues with the line following the ":endtry".
5914 When an exception occurs during execution of the try block, the remaining
5915lines in the try block are skipped. The exception is matched against the
5916patterns specified as arguments to the ":catch" commands. The catch clause
5917after the first matching ":catch" is taken, other catch clauses are not
5918executed. The catch clause ends when the next ":catch", ":finally", or
5919":endtry" command is reached - whatever is first. Then, the finally clause
5920(if present) is executed. When the ":endtry" is reached, the script execution
5921continues in the following line as usual.
5922 When an exception that does not match any of the patterns specified by the
5923":catch" commands is thrown in the try block, the exception is not caught by
5924that try conditional and none of the catch clauses is executed. Only the
5925finally clause, if present, is taken. The exception pends during execution of
5926the finally clause. It is resumed at the ":endtry", so that commands after
5927the ":endtry" are not executed and the exception might be caught elsewhere,
5928see |try-nesting|.
5929 When during execution of a catch clause another exception is thrown, the
5930remaining lines in that catch clause are not executed. The new exception is
5931not matched against the patterns in any of the ":catch" commands of the same
5932try conditional and none of its catch clauses is taken. If there is, however,
5933a finally clause, it is executed, and the exception pends during its
5934execution. The commands following the ":endtry" are not executed. The new
5935exception might, however, be caught elsewhere, see |try-nesting|.
5936 When during execution of the finally clause (if present) an exception is
5937thrown, the remaining lines in the finally clause are skipped. If the finally
5938clause has been taken because of an exception from the try block or one of the
5939catch clauses, the original (pending) exception is discarded. The commands
5940following the ":endtry" are not executed, and the exception from the finally
5941clause is propagated and can be caught elsewhere, see |try-nesting|.
5942
5943The finally clause is also executed, when a ":break" or ":continue" for
5944a ":while" loop enclosing the complete try conditional is executed from the
5945try block or a catch clause. Or when a ":return" or ":finish" is executed
5946from the try block or a catch clause of a try conditional in a function or
5947sourced script, respectively. The ":break", ":continue", ":return", or
5948":finish" pends during execution of the finally clause and is resumed when the
5949":endtry" is reached. It is, however, discarded when an exception is thrown
5950from the finally clause.
5951 When a ":break" or ":continue" for a ":while" loop enclosing the complete
5952try conditional or when a ":return" or ":finish" is encountered in the finally
5953clause, the rest of the finally clause is skipped, and the ":break",
5954":continue", ":return" or ":finish" is executed as usual. If the finally
5955clause has been taken because of an exception or an earlier ":break",
5956":continue", ":return", or ":finish" from the try block or a catch clause,
5957this pending exception or command is discarded.
5958
5959For examples see |throw-catch| and |try-finally|.
5960
5961
5962NESTING OF TRY CONDITIONALS *try-nesting*
5963
5964Try conditionals can be nested arbitrarily. That is, a complete try
5965conditional can be put into the try block, a catch clause, or the finally
5966clause of another try conditional. If the inner try conditional does not
5967catch an exception thrown in its try block or throws a new exception from one
5968of its catch clauses or its finally clause, the outer try conditional is
5969checked according to the rules above. If the inner try conditional is in the
5970try block of the outer try conditional, its catch clauses are checked, but
5971otherwise only the finally clause is executed. It does not matter for
5972nesting, whether the inner try conditional is directly contained in the outer
5973one, or whether the outer one sources a script or calls a function containing
5974the inner try conditional.
5975
5976When none of the active try conditionals catches an exception, just their
5977finally clauses are executed. Thereafter, the script processing terminates.
5978An error message is displayed in case of an uncaught exception explicitly
5979thrown by a ":throw" command. For uncaught error and interrupt exceptions
5980implicitly raised by Vim, the error message(s) or interrupt message are shown
5981as usual.
5982
5983For examples see |throw-catch|.
5984
5985
5986EXAMINING EXCEPTION HANDLING CODE *except-examine*
5987
5988Exception handling code can get tricky. If you are in doubt what happens, set
5989'verbose' to 13 or use the ":13verbose" command modifier when sourcing your
5990script file. Then you see when an exception is thrown, discarded, caught, or
5991finished. When using a verbosity level of at least 14, things pending in
5992a finally clause are also shown. This information is also given in debug mode
5993(see |debug-scripts|).
5994
5995
5996THROWING AND CATCHING EXCEPTIONS *throw-catch*
5997
5998You can throw any number or string as an exception. Use the |:throw| command
5999and pass the value to be thrown as argument: >
6000 :throw 4711
6001 :throw "string"
6002< *throw-expression*
6003You can also specify an expression argument. The expression is then evaluated
6004first, and the result is thrown: >
6005 :throw 4705 + strlen("string")
6006 :throw strpart("strings", 0, 6)
6007
6008An exception might be thrown during evaluation of the argument of the ":throw"
6009command. Unless it is caught there, the expression evaluation is abandoned.
6010The ":throw" command then does not throw a new exception.
6011 Example: >
6012
6013 :function! Foo(arg)
6014 : try
6015 : throw a:arg
6016 : catch /foo/
6017 : endtry
6018 : return 1
6019 :endfunction
6020 :
6021 :function! Bar()
6022 : echo "in Bar"
6023 : return 4710
6024 :endfunction
6025 :
6026 :throw Foo("arrgh") + Bar()
6027
6028This throws "arrgh", and "in Bar" is not displayed since Bar() is not
6029executed. >
6030 :throw Foo("foo") + Bar()
6031however displays "in Bar" and throws 4711.
6032
6033Any other command that takes an expression as argument might also be
6034abandoned by an (uncaught) exception during the expression evaluation. The
6035exception is then propagated to the caller of the command.
6036 Example: >
6037
6038 :if Foo("arrgh")
6039 : echo "then"
6040 :else
6041 : echo "else"
6042 :endif
6043
6044Here neither of "then" or "else" is displayed.
6045
6046 *catch-order*
6047Exceptions can be caught by a try conditional with one or more |:catch|
6048commands, see |try-conditionals|. The values to be caught by each ":catch"
6049command can be specified as a pattern argument. The subsequent catch clause
6050gets executed when a matching exception is caught.
6051 Example: >
6052
6053 :function! Foo(value)
6054 : try
6055 : throw a:value
6056 : catch /^\d\+$/
6057 : echo "Number thrown"
6058 : catch /.*/
6059 : echo "String thrown"
6060 : endtry
6061 :endfunction
6062 :
6063 :call Foo(0x1267)
6064 :call Foo('string')
6065
6066The first call to Foo() displays "Number thrown", the second "String thrown".
6067An exception is matched against the ":catch" commands in the order they are
6068specified. Only the first match counts. So you should place the more
6069specific ":catch" first. The following order does not make sense: >
6070
6071 : catch /.*/
6072 : echo "String thrown"
6073 : catch /^\d\+$/
6074 : echo "Number thrown"
6075
6076The first ":catch" here matches always, so that the second catch clause is
6077never taken.
6078
6079 *throw-variables*
6080If you catch an exception by a general pattern, you may access the exact value
6081in the variable |v:exception|: >
6082
6083 : catch /^\d\+$/
6084 : echo "Number thrown. Value is" v:exception
6085
6086You may also be interested where an exception was thrown. This is stored in
6087|v:throwpoint|. Note that "v:exception" and "v:throwpoint" are valid for the
6088exception most recently caught as long it is not finished.
6089 Example: >
6090
6091 :function! Caught()
6092 : if v:exception != ""
6093 : echo 'Caught "' . v:exception . '" in ' . v:throwpoint
6094 : else
6095 : echo 'Nothing caught'
6096 : endif
6097 :endfunction
6098 :
6099 :function! Foo()
6100 : try
6101 : try
6102 : try
6103 : throw 4711
6104 : finally
6105 : call Caught()
6106 : endtry
6107 : catch /.*/
6108 : call Caught()
6109 : throw "oops"
6110 : endtry
6111 : catch /.*/
6112 : call Caught()
6113 : finally
6114 : call Caught()
6115 : endtry
6116 :endfunction
6117 :
6118 :call Foo()
6119
6120This displays >
6121
6122 Nothing caught
6123 Caught "4711" in function Foo, line 4
6124 Caught "oops" in function Foo, line 10
6125 Nothing caught
6126
6127A practical example: The following command ":LineNumber" displays the line
6128number in the script or function where it has been used: >
6129
6130 :function! LineNumber()
6131 : return substitute(v:throwpoint, '.*\D\(\d\+\).*', '\1', "")
6132 :endfunction
6133 :command! LineNumber try | throw "" | catch | echo LineNumber() | endtry
6134<
6135 *try-nested*
6136An exception that is not caught by a try conditional can be caught by
6137a surrounding try conditional: >
6138
6139 :try
6140 : try
6141 : throw "foo"
6142 : catch /foobar/
6143 : echo "foobar"
6144 : finally
6145 : echo "inner finally"
6146 : endtry
6147 :catch /foo/
6148 : echo "foo"
6149 :endtry
6150
6151The inner try conditional does not catch the exception, just its finally
6152clause is executed. The exception is then caught by the outer try
6153conditional. The example displays "inner finally" and then "foo".
6154
6155 *throw-from-catch*
6156You can catch an exception and throw a new one to be caught elsewhere from the
6157catch clause: >
6158
6159 :function! Foo()
6160 : throw "foo"
6161 :endfunction
6162 :
6163 :function! Bar()
6164 : try
6165 : call Foo()
6166 : catch /foo/
6167 : echo "Caught foo, throw bar"
6168 : throw "bar"
6169 : endtry
6170 :endfunction
6171 :
6172 :try
6173 : call Bar()
6174 :catch /.*/
6175 : echo "Caught" v:exception
6176 :endtry
6177
6178This displays "Caught foo, throw bar" and then "Caught bar".
6179
6180 *rethrow*
6181There is no real rethrow in the Vim script language, but you may throw
6182"v:exception" instead: >
6183
6184 :function! Bar()
6185 : try
6186 : call Foo()
6187 : catch /.*/
6188 : echo "Rethrow" v:exception
6189 : throw v:exception
6190 : endtry
6191 :endfunction
6192< *try-echoerr*
6193Note that this method cannot be used to "rethrow" Vim error or interrupt
6194exceptions, because it is not possible to fake Vim internal exceptions.
6195Trying so causes an error exception. You should throw your own exception
6196denoting the situation. If you want to cause a Vim error exception containing
6197the original error exception value, you can use the |:echoerr| command: >
6198
6199 :try
6200 : try
6201 : asdf
6202 : catch /.*/
6203 : echoerr v:exception
6204 : endtry
6205 :catch /.*/
6206 : echo v:exception
6207 :endtry
6208
6209This code displays
6210
6211 Vim(echoerr):Vim:E492: Not an editor command: asdf ~
6212
6213
6214CLEANUP CODE *try-finally*
6215
6216Scripts often change global settings and restore them at their end. If the
6217user however interrupts the script by pressing CTRL-C, the settings remain in
6218an inconsistent state. The same may happen to you in the development phase of
6219a script when an error occurs or you explicitly throw an exception without
6220catching it. You can solve these problems by using a try conditional with
6221a finally clause for restoring the settings. Its execution is guaranteed on
6222normal control flow, on error, on an explicit ":throw", and on interrupt.
6223(Note that errors and interrupts from inside the try conditional are converted
6224to exceptions. When not caught, they terminate the script after the finally
6225clause has been executed.)
6226Example: >
6227
6228 :try
6229 : let s:saved_ts = &ts
6230 : set ts=17
6231 :
6232 : " Do the hard work here.
6233 :
6234 :finally
6235 : let &ts = s:saved_ts
6236 : unlet s:saved_ts
6237 :endtry
6238
6239This method should be used locally whenever a function or part of a script
6240changes global settings which need to be restored on failure or normal exit of
6241that function or script part.
6242
6243 *break-finally*
6244Cleanup code works also when the try block or a catch clause is left by
6245a ":continue", ":break", ":return", or ":finish".
6246 Example: >
6247
6248 :let first = 1
6249 :while 1
6250 : try
6251 : if first
6252 : echo "first"
6253 : let first = 0
6254 : continue
6255 : else
6256 : throw "second"
6257 : endif
6258 : catch /.*/
6259 : echo v:exception
6260 : break
6261 : finally
6262 : echo "cleanup"
6263 : endtry
6264 : echo "still in while"
6265 :endwhile
6266 :echo "end"
6267
6268This displays "first", "cleanup", "second", "cleanup", and "end". >
6269
6270 :function! Foo()
6271 : try
6272 : return 4711
6273 : finally
6274 : echo "cleanup\n"
6275 : endtry
6276 : echo "Foo still active"
6277 :endfunction
6278 :
6279 :echo Foo() "returned by Foo"
6280
6281This displays "cleanup" and "4711 returned by Foo". You don't need to add an
6282extra ":return" in the finally clause. (Above all, this would override the
6283return value.)
6284
6285 *except-from-finally*
6286Using either of ":continue", ":break", ":return", ":finish", or ":throw" in
6287a finally clause is possible, but not recommended since it abandons the
6288cleanup actions for the try conditional. But, of course, interrupt and error
6289exceptions might get raised from a finally clause.
6290 Example where an error in the finally clause stops an interrupt from
6291working correctly: >
6292
6293 :try
6294 : try
6295 : echo "Press CTRL-C for interrupt"
6296 : while 1
6297 : endwhile
6298 : finally
6299 : unlet novar
6300 : endtry
6301 :catch /novar/
6302 :endtry
6303 :echo "Script still running"
6304 :sleep 1
6305
6306If you need to put commands that could fail into a finally clause, you should
6307think about catching or ignoring the errors in these commands, see
6308|catch-errors| and |ignore-errors|.
6309
6310
6311CATCHING ERRORS *catch-errors*
6312
6313If you want to catch specific errors, you just have to put the code to be
6314watched in a try block and add a catch clause for the error message. The
6315presence of the try conditional causes all errors to be converted to an
6316exception. No message is displayed and |v:errmsg| is not set then. To find
6317the right pattern for the ":catch" command, you have to know how the format of
6318the error exception is.
6319 Error exceptions have the following format: >
6320
6321 Vim({cmdname}):{errmsg}
6322or >
6323 Vim:{errmsg}
6324
6325{cmdname} is the name of the command that failed; the second form is used when
6326the command name is not known. {errmsg} is the error message usually produced
6327when the error occurs outside try conditionals. It always begins with
6328a capital "E", followed by a two or three-digit error number, a colon, and
6329a space.
6330
6331Examples:
6332
6333The command >
6334 :unlet novar
6335normally produces the error message >
6336 E108: No such variable: "novar"
6337which is converted inside try conditionals to an exception >
6338 Vim(unlet):E108: No such variable: "novar"
6339
6340The command >
6341 :dwim
6342normally produces the error message >
6343 E492: Not an editor command: dwim
6344which is converted inside try conditionals to an exception >
6345 Vim:E492: Not an editor command: dwim
6346
6347You can catch all ":unlet" errors by a >
6348 :catch /^Vim(unlet):/
6349or all errors for misspelled command names by a >
6350 :catch /^Vim:E492:/
6351
6352Some error messages may be produced by different commands: >
6353 :function nofunc
6354and >
6355 :delfunction nofunc
6356both produce the error message >
6357 E128: Function name must start with a capital: nofunc
6358which is converted inside try conditionals to an exception >
6359 Vim(function):E128: Function name must start with a capital: nofunc
6360or >
6361 Vim(delfunction):E128: Function name must start with a capital: nofunc
6362respectively. You can catch the error by its number independently on the
6363command that caused it if you use the following pattern: >
6364 :catch /^Vim(\a\+):E128:/
6365
6366Some commands like >
6367 :let x = novar
6368produce multiple error messages, here: >
6369 E121: Undefined variable: novar
6370 E15: Invalid expression: novar
6371Only the first is used for the exception value, since it is the most specific
6372one (see |except-several-errors|). So you can catch it by >
6373 :catch /^Vim(\a\+):E121:/
6374
6375You can catch all errors related to the name "nofunc" by >
6376 :catch /\<nofunc\>/
6377
6378You can catch all Vim errors in the ":write" and ":read" commands by >
6379 :catch /^Vim(\(write\|read\)):E\d\+:/
6380
6381You can catch all Vim errors by the pattern >
6382 :catch /^Vim\((\a\+)\)\=:E\d\+:/
6383<
6384 *catch-text*
6385NOTE: You should never catch the error message text itself: >
6386 :catch /No such variable/
6387only works in the english locale, but not when the user has selected
6388a different language by the |:language| command. It is however helpful to
6389cite the message text in a comment: >
6390 :catch /^Vim(\a\+):E108:/ " No such variable
6391
6392
6393IGNORING ERRORS *ignore-errors*
6394
6395You can ignore errors in a specific Vim command by catching them locally: >
6396
6397 :try
6398 : write
6399 :catch
6400 :endtry
6401
6402But you are strongly recommended NOT to use this simple form, since it could
6403catch more than you want. With the ":write" command, some autocommands could
6404be executed and cause errors not related to writing, for instance: >
6405
6406 :au BufWritePre * unlet novar
6407
6408There could even be such errors you are not responsible for as a script
6409writer: a user of your script might have defined such autocommands. You would
6410then hide the error from the user.
6411 It is much better to use >
6412
6413 :try
6414 : write
6415 :catch /^Vim(write):/
6416 :endtry
6417
6418which only catches real write errors. So catch only what you'd like to ignore
6419intentionally.
6420
6421For a single command that does not cause execution of autocommands, you could
6422even suppress the conversion of errors to exceptions by the ":silent!"
6423command: >
6424 :silent! nunmap k
6425This works also when a try conditional is active.
6426
6427
6428CATCHING INTERRUPTS *catch-interrupt*
6429
6430When there are active try conditionals, an interrupt (CTRL-C) is converted to
6431the exception "Vim:Interrupt". You can catch it like every exception. The
6432script is not terminated, then.
6433 Example: >
6434
6435 :function! TASK1()
6436 : sleep 10
6437 :endfunction
6438
6439 :function! TASK2()
6440 : sleep 20
6441 :endfunction
6442
6443 :while 1
6444 : let command = input("Type a command: ")
6445 : try
6446 : if command == ""
6447 : continue
6448 : elseif command == "END"
6449 : break
6450 : elseif command == "TASK1"
6451 : call TASK1()
6452 : elseif command == "TASK2"
6453 : call TASK2()
6454 : else
6455 : echo "\nIllegal command:" command
6456 : continue
6457 : endif
6458 : catch /^Vim:Interrupt$/
6459 : echo "\nCommand interrupted"
6460 : " Caught the interrupt. Continue with next prompt.
6461 : endtry
6462 :endwhile
6463
6464You can interrupt a task here by pressing CTRL-C; the script then asks for
6465a new command. If you press CTRL-C at the prompt, the script is terminated.
6466
6467For testing what happens when CTRL-C would be pressed on a specific line in
6468your script, use the debug mode and execute the |>quit| or |>interrupt|
6469command on that line. See |debug-scripts|.
6470
6471
6472CATCHING ALL *catch-all*
6473
6474The commands >
6475
6476 :catch /.*/
6477 :catch //
6478 :catch
6479
6480catch everything, error exceptions, interrupt exceptions and exceptions
6481explicitly thrown by the |:throw| command. This is useful at the top level of
6482a script in order to catch unexpected things.
6483 Example: >
6484
6485 :try
6486 :
6487 : " do the hard work here
6488 :
6489 :catch /MyException/
6490 :
6491 : " handle known problem
6492 :
6493 :catch /^Vim:Interrupt$/
6494 : echo "Script interrupted"
6495 :catch /.*/
6496 : echo "Internal error (" . v:exception . ")"
6497 : echo " - occurred at " . v:throwpoint
6498 :endtry
6499 :" end of script
6500
6501Note: Catching all might catch more things than you want. Thus, you are
6502strongly encouraged to catch only for problems that you can really handle by
6503specifying a pattern argument to the ":catch".
6504 Example: Catching all could make it nearly impossible to interrupt a script
6505by pressing CTRL-C: >
6506
6507 :while 1
6508 : try
6509 : sleep 1
6510 : catch
6511 : endtry
6512 :endwhile
6513
6514
6515EXCEPTIONS AND AUTOCOMMANDS *except-autocmd*
6516
6517Exceptions may be used during execution of autocommands. Example: >
6518
6519 :autocmd User x try
6520 :autocmd User x throw "Oops!"
6521 :autocmd User x catch
6522 :autocmd User x echo v:exception
6523 :autocmd User x endtry
6524 :autocmd User x throw "Arrgh!"
6525 :autocmd User x echo "Should not be displayed"
6526 :
6527 :try
6528 : doautocmd User x
6529 :catch
6530 : echo v:exception
6531 :endtry
6532
6533This displays "Oops!" and "Arrgh!".
6534
6535 *except-autocmd-Pre*
6536For some commands, autocommands get executed before the main action of the
6537command takes place. If an exception is thrown and not caught in the sequence
6538of autocommands, the sequence and the command that caused its execution are
6539abandoned and the exception is propagated to the caller of the command.
6540 Example: >
6541
6542 :autocmd BufWritePre * throw "FAIL"
6543 :autocmd BufWritePre * echo "Should not be displayed"
6544 :
6545 :try
6546 : write
6547 :catch
6548 : echo "Caught:" v:exception "from" v:throwpoint
6549 :endtry
6550
6551Here, the ":write" command does not write the file currently being edited (as
6552you can see by checking 'modified'), since the exception from the BufWritePre
6553autocommand abandons the ":write". The exception is then caught and the
6554script displays: >
6555
6556 Caught: FAIL from BufWrite Auto commands for "*"
6557<
6558 *except-autocmd-Post*
6559For some commands, autocommands get executed after the main action of the
6560command has taken place. If this main action fails and the command is inside
6561an active try conditional, the autocommands are skipped and an error exception
6562is thrown that can be caught by the caller of the command.
6563 Example: >
6564
6565 :autocmd BufWritePost * echo "File successfully written!"
6566 :
6567 :try
6568 : write /i/m/p/o/s/s/i/b/l/e
6569 :catch
6570 : echo v:exception
6571 :endtry
6572
6573This just displays: >
6574
6575 Vim(write):E212: Can't open file for writing (/i/m/p/o/s/s/i/b/l/e)
6576
6577If you really need to execute the autocommands even when the main action
6578fails, trigger the event from the catch clause.
6579 Example: >
6580
6581 :autocmd BufWritePre * set noreadonly
6582 :autocmd BufWritePost * set readonly
6583 :
6584 :try
6585 : write /i/m/p/o/s/s/i/b/l/e
6586 :catch
6587 : doautocmd BufWritePost /i/m/p/o/s/s/i/b/l/e
6588 :endtry
6589<
6590You can also use ":silent!": >
6591
6592 :let x = "ok"
6593 :let v:errmsg = ""
6594 :autocmd BufWritePost * if v:errmsg != ""
6595 :autocmd BufWritePost * let x = "after fail"
6596 :autocmd BufWritePost * endif
6597 :try
6598 : silent! write /i/m/p/o/s/s/i/b/l/e
6599 :catch
6600 :endtry
6601 :echo x
6602
6603This displays "after fail".
6604
6605If the main action of the command does not fail, exceptions from the
6606autocommands will be catchable by the caller of the command: >
6607
6608 :autocmd BufWritePost * throw ":-("
6609 :autocmd BufWritePost * echo "Should not be displayed"
6610 :
6611 :try
6612 : write
6613 :catch
6614 : echo v:exception
6615 :endtry
6616<
6617 *except-autocmd-Cmd*
6618For some commands, the normal action can be replaced by a sequence of
6619autocommands. Exceptions from that sequence will be catchable by the caller
6620of the command.
6621 Example: For the ":write" command, the caller cannot know whether the file
6622had actually been written when the exception occurred. You need to tell it in
6623some way. >
6624
6625 :if !exists("cnt")
6626 : let cnt = 0
6627 :
6628 : autocmd BufWriteCmd * if &modified
6629 : autocmd BufWriteCmd * let cnt = cnt + 1
6630 : autocmd BufWriteCmd * if cnt % 3 == 2
6631 : autocmd BufWriteCmd * throw "BufWriteCmdError"
6632 : autocmd BufWriteCmd * endif
6633 : autocmd BufWriteCmd * write | set nomodified
6634 : autocmd BufWriteCmd * if cnt % 3 == 0
6635 : autocmd BufWriteCmd * throw "BufWriteCmdError"
6636 : autocmd BufWriteCmd * endif
6637 : autocmd BufWriteCmd * echo "File successfully written!"
6638 : autocmd BufWriteCmd * endif
6639 :endif
6640 :
6641 :try
6642 : write
6643 :catch /^BufWriteCmdError$/
6644 : if &modified
6645 : echo "Error on writing (file contents not changed)"
6646 : else
6647 : echo "Error after writing"
6648 : endif
6649 :catch /^Vim(write):/
6650 : echo "Error on writing"
6651 :endtry
6652
6653When this script is sourced several times after making changes, it displays
6654first >
6655 File successfully written!
6656then >
6657 Error on writing (file contents not changed)
6658then >
6659 Error after writing
6660etc.
6661
6662 *except-autocmd-ill*
6663You cannot spread a try conditional over autocommands for different events.
6664The following code is ill-formed: >
6665
6666 :autocmd BufWritePre * try
6667 :
6668 :autocmd BufWritePost * catch
6669 :autocmd BufWritePost * echo v:exception
6670 :autocmd BufWritePost * endtry
6671 :
6672 :write
6673
6674
6675EXCEPTION HIERARCHIES AND PARAMETERIZED EXCEPTIONS *except-hier-param*
6676
6677Some programming languages allow to use hierarchies of exception classes or to
6678pass additional information with the object of an exception class. You can do
6679similar things in Vim.
6680 In order to throw an exception from a hierarchy, just throw the complete
6681class name with the components separated by a colon, for instance throw the
6682string "EXCEPT:MATHERR:OVERFLOW" for an overflow in a mathematical library.
6683 When you want to pass additional information with your exception class, add
6684it in parentheses, for instance throw the string "EXCEPT:IO:WRITEERR(myfile)"
6685for an error when writing "myfile".
6686 With the appropriate patterns in the ":catch" command, you can catch for
6687base classes or derived classes of your hierarchy. Additional information in
6688parentheses can be cut out from |v:exception| with the ":substitute" command.
6689 Example: >
6690
6691 :function! CheckRange(a, func)
6692 : if a:a < 0
6693 : throw "EXCEPT:MATHERR:RANGE(" . a:func . ")"
6694 : endif
6695 :endfunction
6696 :
6697 :function! Add(a, b)
6698 : call CheckRange(a:a, "Add")
6699 : call CheckRange(a:b, "Add")
6700 : let c = a:a + a:b
6701 : if c < 0
6702 : throw "EXCEPT:MATHERR:OVERFLOW"
6703 : endif
6704 : return c
6705 :endfunction
6706 :
6707 :function! Div(a, b)
6708 : call CheckRange(a:a, "Div")
6709 : call CheckRange(a:b, "Div")
6710 : if (a:b == 0)
6711 : throw "EXCEPT:MATHERR:ZERODIV"
6712 : endif
6713 : return a:a / a:b
6714 :endfunction
6715 :
6716 :function! Write(file)
6717 : try
6718 : execute "write" a:file
6719 : catch /^Vim(write):/
6720 : throw "EXCEPT:IO(" . getcwd() . ", " . a:file . "):WRITEERR"
6721 : endtry
6722 :endfunction
6723 :
6724 :try
6725 :
6726 : " something with arithmetics and I/O
6727 :
6728 :catch /^EXCEPT:MATHERR:RANGE/
6729 : let function = substitute(v:exception, '.*(\(\a\+\)).*', '\1', "")
6730 : echo "Range error in" function
6731 :
6732 :catch /^EXCEPT:MATHERR/ " catches OVERFLOW and ZERODIV
6733 : echo "Math error"
6734 :
6735 :catch /^EXCEPT:IO/
6736 : let dir = substitute(v:exception, '.*(\(.\+\),\s*.\+).*', '\1', "")
6737 : let file = substitute(v:exception, '.*(.\+,\s*\(.\+\)).*', '\1', "")
6738 : if file !~ '^/'
6739 : let file = dir . "/" . file
6740 : endif
6741 : echo 'I/O error for "' . file . '"'
6742 :
6743 :catch /^EXCEPT/
6744 : echo "Unspecified error"
6745 :
6746 :endtry
6747
6748The exceptions raised by Vim itself (on error or when pressing CTRL-C) use
6749a flat hierarchy: they are all in the "Vim" class. You cannot throw yourself
6750exceptions with the "Vim" prefix; they are reserved for Vim.
6751 Vim error exceptions are parameterized with the name of the command that
6752failed, if known. See |catch-errors|.
6753
6754
6755PECULIARITIES
6756 *except-compat*
6757The exception handling concept requires that the command sequence causing the
6758exception is aborted immediately and control is transferred to finally clauses
6759and/or a catch clause.
6760
6761In the Vim script language there are cases where scripts and functions
6762continue after an error: in functions without the "abort" flag or in a command
6763after ":silent!", control flow goes to the following line, and outside
6764functions, control flow goes to the line following the outermost ":endwhile"
6765or ":endif". On the other hand, errors should be catchable as exceptions
6766(thus, requiring the immediate abortion).
6767
6768This problem has been solved by converting errors to exceptions and using
6769immediate abortion (if not suppressed by ":silent!") only when a try
6770conditional is active. This is no restriction since an (error) exception can
6771be caught only from an active try conditional. If you want an immediate
6772termination without catching the error, just use a try conditional without
6773catch clause. (You can cause cleanup code being executed before termination
6774by specifying a finally clause.)
6775
6776When no try conditional is active, the usual abortion and continuation
6777behavior is used instead of immediate abortion. This ensures compatibility of
6778scripts written for Vim 6.1 and earlier.
6779
6780However, when sourcing an existing script that does not use exception handling
6781commands (or when calling one of its functions) from inside an active try
6782conditional of a new script, you might change the control flow of the existing
6783script on error. You get the immediate abortion on error and can catch the
6784error in the new script. If however the sourced script suppresses error
6785messages by using the ":silent!" command (checking for errors by testing
6786|v:errmsg| if appropriate), its execution path is not changed. The error is
6787not converted to an exception. (See |:silent|.) So the only remaining cause
6788where this happens is for scripts that don't care about errors and produce
6789error messages. You probably won't want to use such code from your new
6790scripts.
6791
6792 *except-syntax-err*
6793Syntax errors in the exception handling commands are never caught by any of
6794the ":catch" commands of the try conditional they belong to. Its finally
6795clauses, however, is executed.
6796 Example: >
6797
6798 :try
6799 : try
6800 : throw 4711
6801 : catch /\(/
6802 : echo "in catch with syntax error"
6803 : catch
6804 : echo "inner catch-all"
6805 : finally
6806 : echo "inner finally"
6807 : endtry
6808 :catch
6809 : echo 'outer catch-all caught "' . v:exception . '"'
6810 : finally
6811 : echo "outer finally"
6812 :endtry
6813
6814This displays: >
6815 inner finally
6816 outer catch-all caught "Vim(catch):E54: Unmatched \("
6817 outer finally
6818The original exception is discarded and an error exception is raised, instead.
6819
6820 *except-single-line*
6821The ":try", ":catch", ":finally", and ":endtry" commands can be put on
6822a single line, but then syntax errors may make it difficult to recognize the
6823"catch" line, thus you better avoid this.
6824 Example: >
6825 :try | unlet! foo # | catch | endtry
6826raises an error exception for the trailing characters after the ":unlet!"
6827argument, but does not see the ":catch" and ":endtry" commands, so that the
6828error exception is discarded and the "E488: Trailing characters" message gets
6829displayed.
6830
6831 *except-several-errors*
6832When several errors appear in a single command, the first error message is
6833usually the most specific one and therefor converted to the error exception.
6834 Example: >
6835 echo novar
6836causes >
6837 E121: Undefined variable: novar
6838 E15: Invalid expression: novar
6839The value of the error exception inside try conditionals is: >
6840 Vim(echo):E121: Undefined variable: novar
6841< *except-syntax-error*
6842But when a syntax error is detected after a normal error in the same command,
6843the syntax error is used for the exception being thrown.
6844 Example: >
6845 unlet novar #
6846causes >
6847 E108: No such variable: "novar"
6848 E488: Trailing characters
6849The value of the error exception inside try conditionals is: >
6850 Vim(unlet):E488: Trailing characters
6851This is done because the syntax error might change the execution path in a way
6852not intended by the user. Example: >
6853 try
6854 try | unlet novar # | catch | echo v:exception | endtry
6855 catch /.*/
6856 echo "outer catch:" v:exception
6857 endtry
6858This displays "outer catch: Vim(unlet):E488: Trailing characters", and then
6859a "E600: Missing :endtry" error message is given, see |except-single-line|.
6860
6861==============================================================================
68629. Examples *eval-examples*
6863
6864Printing in Hex ~
6865>
6866 :" The function Nr2Hex() returns the Hex string of a number.
6867 :func Nr2Hex(nr)
6868 : let n = a:nr
6869 : let r = ""
6870 : while n
6871 : let r = '0123456789ABCDEF'[n % 16] . r
6872 : let n = n / 16
6873 : endwhile
6874 : return r
6875 :endfunc
6876
6877 :" The function String2Hex() converts each character in a string to a two
6878 :" character Hex string.
6879 :func String2Hex(str)
6880 : let out = ''
6881 : let ix = 0
6882 : while ix < strlen(a:str)
6883 : let out = out . Nr2Hex(char2nr(a:str[ix]))
6884 : let ix = ix + 1
6885 : endwhile
6886 : return out
6887 :endfunc
6888
6889Example of its use: >
6890 :echo Nr2Hex(32)
6891result: "20" >
6892 :echo String2Hex("32")
6893result: "3332"
6894
6895
6896Sorting lines (by Robert Webb) ~
6897
6898Here is a Vim script to sort lines. Highlight the lines in Vim and type
6899":Sort". This doesn't call any external programs so it'll work on any
6900platform. The function Sort() actually takes the name of a comparison
6901function as its argument, like qsort() does in C. So you could supply it
6902with different comparison functions in order to sort according to date etc.
6903>
6904 :" Function for use with Sort(), to compare two strings.
6905 :func! Strcmp(str1, str2)
6906 : if (a:str1 < a:str2)
6907 : return -1
6908 : elseif (a:str1 > a:str2)
6909 : return 1
6910 : else
6911 : return 0
6912 : endif
6913 :endfunction
6914
6915 :" Sort lines. SortR() is called recursively.
6916 :func! SortR(start, end, cmp)
6917 : if (a:start >= a:end)
6918 : return
6919 : endif
6920 : let partition = a:start - 1
6921 : let middle = partition
6922 : let partStr = getline((a:start + a:end) / 2)
6923 : let i = a:start
6924 : while (i <= a:end)
6925 : let str = getline(i)
6926 : exec "let result = " . a:cmp . "(str, partStr)"
6927 : if (result <= 0)
6928 : " Need to put it before the partition. Swap lines i and partition.
6929 : let partition = partition + 1
6930 : if (result == 0)
6931 : let middle = partition
6932 : endif
6933 : if (i != partition)
6934 : let str2 = getline(partition)
6935 : call setline(i, str2)
6936 : call setline(partition, str)
6937 : endif
6938 : endif
6939 : let i = i + 1
6940 : endwhile
6941
6942 : " Now we have a pointer to the "middle" element, as far as partitioning
6943 : " goes, which could be anywhere before the partition. Make sure it is at
6944 : " the end of the partition.
6945 : if (middle != partition)
6946 : let str = getline(middle)
6947 : let str2 = getline(partition)
6948 : call setline(middle, str2)
6949 : call setline(partition, str)
6950 : endif
6951 : call SortR(a:start, partition - 1, a:cmp)
6952 : call SortR(partition + 1, a:end, a:cmp)
6953 :endfunc
6954
6955 :" To Sort a range of lines, pass the range to Sort() along with the name of a
6956 :" function that will compare two lines.
6957 :func! Sort(cmp) range
6958 : call SortR(a:firstline, a:lastline, a:cmp)
6959 :endfunc
6960
6961 :" :Sort takes a range of lines and sorts them.
6962 :command! -nargs=0 -range Sort <line1>,<line2>call Sort("Strcmp")
6963<
6964 *sscanf*
6965There is no sscanf() function in Vim. If you need to extract parts from a
6966line, you can use matchstr() and substitute() to do it. This example shows
6967how to get the file name, line number and column number out of a line like
6968"foobar.txt, 123, 45". >
6969 :" Set up the match bit
6970 :let mx='\(\f\+\),\s*\(\d\+\),\s*\(\d\+\)'
6971 :"get the part matching the whole expression
6972 :let l = matchstr(line, mx)
6973 :"get each item out of the match
6974 :let file = substitute(l, mx, '\1', '')
6975 :let lnum = substitute(l, mx, '\2', '')
6976 :let col = substitute(l, mx, '\3', '')
6977
6978The input is in the variable "line", the results in the variables "file",
6979"lnum" and "col". (idea from Michael Geddes)
6980
6981==============================================================================
698210. No +eval feature *no-eval-feature*
6983
6984When the |+eval| feature was disabled at compile time, none of the expression
6985evaluation commands are available. To prevent this from causing Vim scripts
6986to generate all kinds of errors, the ":if" and ":endif" commands are still
6987recognized, though the argument of the ":if" and everything between the ":if"
6988and the matching ":endif" is ignored. Nesting of ":if" blocks is allowed, but
6989only if the commands are at the start of the line. The ":else" command is not
6990recognized.
6991
6992Example of how to avoid executing commands when the |+eval| feature is
6993missing: >
6994
6995 :if 1
6996 : echo "Expression evaluation is compiled in"
6997 :else
6998 : echo "You will _never_ see this message"
6999 :endif
7000
7001==============================================================================
700211. The sandbox *eval-sandbox* *sandbox* *E48*
7003
7004The 'foldexpr', 'includeexpr', 'indentexpr', 'statusline' and 'foldtext'
7005options are evaluated in a sandbox. This means that you are protected from
7006these expressions having nasty side effects. This gives some safety for when
7007these options are set from a modeline. It is also used when the command from
Bram Moolenaarebefac62005-12-28 22:39:57 +00007008a tags file is executed and for CTRL-R = in the command line.
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007009The sandbox is also used for the |:sandbox| command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010
7011These items are not allowed in the sandbox:
7012 - changing the buffer text
7013 - defining or changing mapping, autocommands, functions, user commands
7014 - setting certain options (see |option-summary|)
7015 - executing a shell command
7016 - reading or writing a file
7017 - jumping to another buffer or editing a file
Bram Moolenaar4770d092006-01-12 23:22:24 +00007018 - executing Python, Perl, etc. commands
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007019This is not guaranteed 100% secure, but it should block most attacks.
7020
7021 *:san* *:sandbox*
Bram Moolenaar045e82d2005-07-08 22:25:33 +00007022:san[dbox] {cmd} Execute {cmd} in the sandbox. Useful to evaluate an
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007023 option that may have been set from a modeline, e.g.
7024 'foldexpr'.
7025
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007026 *sandbox-option*
7027A few options contain an expression. When this expression is evaluated it may
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007028have to be done in the sandbox to avoid a security risc. But the sandbox is
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007029restrictive, thus this only happens when the option was set from an insecure
7030location. Insecure in this context are:
7031- sourcing a .vimrc or .exrc in the current directlry
7032- while executing in the sandbox
7033- value coming from a modeline
7034
7035Note that when in the sandbox and saving an option value and restoring it, the
7036option will still be marked as it was set in the sandbox.
7037
7038==============================================================================
703912. Textlock *textlock*
7040
7041In a few situations it is not allowed to change the text in the buffer, jump
7042to another window and some other things that might confuse or break what Vim
7043is currently doing. This mostly applies to things that happen when Vim is
7044actually doing something else. For example, evaluating the 'balloonexpr' may
7045happen any moment the mouse cursor is resting at some position.
7046
7047This is not allowed when the textlock is active:
7048 - changing the buffer text
7049 - jumping to another buffer or window
7050 - editing another file
7051 - closing a window or quitting Vim
7052 - etc.
7053
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054
7055 vim:tw=78:ts=8:ft=help:norl: