37
|
1 *eval.txt* For Vim version 7.0aa. Last change: 2004 Dec 10
|
7
|
2
|
|
3
|
|
4 VIM REFERENCE MANUAL by Bram Moolenaar
|
|
5
|
|
6
|
|
7 Expression evaluation *expression* *expr* *E15* *eval*
|
|
8
|
|
9 Using expressions is introduced in chapter 41 of the user manual |usr_41.txt|.
|
|
10
|
|
11 Note: Expression evaluation can be disabled at compile time. If this has been
|
|
12 done, the features in this document are not available. See |+eval| and the
|
|
13 last chapter below.
|
|
14
|
|
15 1. Variables |variables|
|
|
16 2. Expression syntax |expression-syntax|
|
|
17 3. Internal variable |internal-variables|
|
|
18 4. Builtin Functions |functions|
|
|
19 5. Defining functions |user-functions|
|
|
20 6. Curly braces names |curly-braces-names|
|
|
21 7. Commands |expression-commands|
|
|
22 8. Exception handling |exception-handling|
|
|
23 9. Examples |eval-examples|
|
|
24 10. No +eval feature |no-eval-feature|
|
|
25 11. The sandbox |eval-sandbox|
|
|
26
|
|
27 {Vi does not have any of these commands}
|
|
28
|
|
29 ==============================================================================
|
|
30 1. Variables *variables*
|
|
31
|
|
32 There are two types of variables:
|
|
33
|
|
34 Number a 32 bit signed number.
|
|
35 String a NUL terminated string of 8-bit unsigned characters.
|
|
36
|
|
37 These are converted automatically, depending on how they are used.
|
|
38
|
|
39 Conversion from a Number to a String is by making the ASCII representation of
|
|
40 the Number. Examples: >
|
|
41 Number 123 --> String "123"
|
|
42 Number 0 --> String "0"
|
|
43 Number -1 --> String "-1"
|
|
44
|
|
45 Conversion from a String to a Number is done by converting the first digits
|
|
46 to a number. Hexadecimal "0xf9" and Octal "017" numbers are recognized. If
|
|
47 the String doesn't start with digits, the result is zero. Examples: >
|
|
48 String "456" --> Number 456
|
|
49 String "6bar" --> Number 6
|
|
50 String "foo" --> Number 0
|
|
51 String "0xf1" --> Number 241
|
|
52 String "0100" --> Number 64
|
|
53 String "-8" --> Number -8
|
|
54 String "+8" --> Number 0
|
|
55
|
|
56 To force conversion from String to Number, add zero to it: >
|
|
57 :echo "0100" + 0
|
|
58
|
|
59 For boolean operators Numbers are used. Zero is FALSE, non-zero is TRUE.
|
|
60
|
|
61 Note that in the command >
|
|
62 :if "foo"
|
|
63 "foo" is converted to 0, which means FALSE. To test for a non-empty string,
|
|
64 use strlen(): >
|
|
65 :if strlen("foo")
|
|
66
|
|
67 If you need to know the type of a variable or expression, use the |type()|
|
|
68 function.
|
|
69
|
|
70 When the '!' flag is included in the 'viminfo' option, global variables that
|
|
71 start with an uppercase letter, and don't contain a lowercase letter, are
|
|
72 stored in the viminfo file |viminfo-file|.
|
|
73
|
|
74 When the 'sessionoptions' option contains "global", global variables that
|
|
75 start with an uppercase letter and contain at least one lowercase letter are
|
|
76 stored in the session file |session-file|.
|
|
77
|
|
78 variable name can be stored where ~
|
|
79 my_var_6 not
|
|
80 My_Var_6 session file
|
|
81 MY_VAR_6 viminfo file
|
|
82
|
|
83
|
|
84 It's possible to form a variable name with curly braces, see
|
|
85 |curly-braces-names|.
|
|
86
|
|
87 ==============================================================================
|
|
88 2. Expression syntax *expression-syntax*
|
|
89
|
|
90 Expression syntax summary, from least to most significant:
|
|
91
|
|
92 |expr1| expr2 ? expr1 : expr1 if-then-else
|
|
93
|
|
94 |expr2| expr3 || expr3 .. logical OR
|
|
95
|
|
96 |expr3| expr4 && expr4 .. logical AND
|
|
97
|
|
98 |expr4| expr5 == expr5 equal
|
|
99 expr5 != expr5 not equal
|
|
100 expr5 > expr5 greater than
|
|
101 expr5 >= expr5 greater than or equal
|
|
102 expr5 < expr5 smaller than
|
|
103 expr5 <= expr5 smaller than or equal
|
|
104 expr5 =~ expr5 regexp matches
|
|
105 expr5 !~ expr5 regexp doesn't match
|
|
106
|
|
107 expr5 ==? expr5 equal, ignoring case
|
|
108 expr5 ==# expr5 equal, match case
|
|
109 etc. As above, append ? for ignoring case, # for
|
|
110 matching case
|
|
111
|
|
112 |expr5| expr6 + expr6 .. number addition
|
|
113 expr6 - expr6 .. number subtraction
|
|
114 expr6 . expr6 .. string concatenation
|
|
115
|
|
116 |expr6| expr7 * expr7 .. number multiplication
|
|
117 expr7 / expr7 .. number division
|
|
118 expr7 % expr7 .. number modulo
|
|
119
|
|
120 |expr7| ! expr7 logical NOT
|
|
121 - expr7 unary minus
|
|
122 + expr7 unary plus
|
|
123 expr8
|
|
124
|
|
125 |expr8| expr9[expr1] index in String
|
|
126
|
|
127 |expr9| number number constant
|
26
|
128 "string" string constant, backslash is special
|
|
129 'string' string constant
|
7
|
130 &option option value
|
|
131 (expr1) nested expression
|
|
132 variable internal variable
|
|
133 va{ria}ble internal variable with curly braces
|
|
134 $VAR environment variable
|
|
135 @r contents of register 'r'
|
|
136 function(expr1, ...) function call
|
|
137 func{ti}on(expr1, ...) function call with curly braces
|
|
138
|
|
139
|
|
140 ".." indicates that the operations in this level can be concatenated.
|
|
141 Example: >
|
|
142 &nu || &list && &shell == "csh"
|
|
143
|
|
144 All expressions within one level are parsed from left to right.
|
|
145
|
|
146
|
|
147 expr1 *expr1* *E109*
|
|
148 -----
|
|
149
|
|
150 expr2 ? expr1 : expr1
|
|
151
|
|
152 The expression before the '?' is evaluated to a number. If it evaluates to
|
|
153 non-zero, the result is the value of the expression between the '?' and ':',
|
|
154 otherwise the result is the value of the expression after the ':'.
|
|
155 Example: >
|
|
156 :echo lnum == 1 ? "top" : lnum
|
|
157
|
|
158 Since the first expression is an "expr2", it cannot contain another ?:. The
|
|
159 other two expressions can, thus allow for recursive use of ?:.
|
|
160 Example: >
|
|
161 :echo lnum == 1 ? "top" : lnum == 1000 ? "last" : lnum
|
|
162
|
|
163 To keep this readable, using |line-continuation| is suggested: >
|
|
164 :echo lnum == 1
|
|
165 :\ ? "top"
|
|
166 :\ : lnum == 1000
|
|
167 :\ ? "last"
|
|
168 :\ : lnum
|
|
169
|
|
170
|
|
171 expr2 and expr3 *expr2* *expr3*
|
|
172 ---------------
|
|
173
|
|
174 *expr-barbar* *expr-&&*
|
|
175 The "||" and "&&" operators take one argument on each side. The arguments
|
|
176 are (converted to) Numbers. The result is:
|
|
177
|
|
178 input output ~
|
|
179 n1 n2 n1 || n2 n1 && n2 ~
|
|
180 zero zero zero zero
|
|
181 zero non-zero non-zero zero
|
|
182 non-zero zero non-zero zero
|
|
183 non-zero non-zero non-zero non-zero
|
|
184
|
|
185 The operators can be concatenated, for example: >
|
|
186
|
|
187 &nu || &list && &shell == "csh"
|
|
188
|
|
189 Note that "&&" takes precedence over "||", so this has the meaning of: >
|
|
190
|
|
191 &nu || (&list && &shell == "csh")
|
|
192
|
|
193 Once the result is known, the expression "short-circuits", that is, further
|
|
194 arguments are not evaluated. This is like what happens in C. For example: >
|
|
195
|
|
196 let a = 1
|
|
197 echo a || b
|
|
198
|
|
199 This is valid even if there is no variable called "b" because "a" is non-zero,
|
|
200 so the result must be non-zero. Similarly below: >
|
|
201
|
|
202 echo exists("b") && b == "yes"
|
|
203
|
|
204 This is valid whether "b" has been defined or not. The second clause will
|
|
205 only be evaluated if "b" has been defined.
|
|
206
|
|
207
|
|
208 expr4 *expr4*
|
|
209 -----
|
|
210
|
|
211 expr5 {cmp} expr5
|
|
212
|
|
213 Compare two expr5 expressions, resulting in a 0 if it evaluates to false, or 1
|
|
214 if it evaluates to true.
|
|
215
|
|
216 *expr-==* *expr-!=* *expr->* *expr->=*
|
|
217 *expr-<* *expr-<=* *expr-=~* *expr-!~*
|
|
218 *expr-==#* *expr-!=#* *expr->#* *expr->=#*
|
|
219 *expr-<#* *expr-<=#* *expr-=~#* *expr-!~#*
|
|
220 *expr-==?* *expr-!=?* *expr->?* *expr->=?*
|
|
221 *expr-<?* *expr-<=?* *expr-=~?* *expr-!~?*
|
|
222 use 'ignorecase' match case ignore case ~
|
|
223 equal == ==# ==?
|
|
224 not equal != !=# !=?
|
|
225 greater than > ># >?
|
|
226 greater than or equal >= >=# >=?
|
|
227 smaller than < <# <?
|
|
228 smaller than or equal <= <=# <=?
|
|
229 regexp matches =~ =~# =~?
|
|
230 regexp doesn't match !~ !~# !~?
|
|
231
|
|
232 Examples:
|
|
233 "abc" ==# "Abc" evaluates to 0
|
|
234 "abc" ==? "Abc" evaluates to 1
|
|
235 "abc" == "Abc" evaluates to 1 if 'ignorecase' is set, 0 otherwise
|
|
236
|
|
237 When comparing a String with a Number, the String is converted to a Number,
|
|
238 and the comparison is done on Numbers. This means that "0 == 'x'" is TRUE,
|
|
239 because 'x' converted to a Number is zero.
|
|
240
|
|
241 When comparing two Strings, this is done with strcmp() or stricmp(). This
|
|
242 results in the mathematical difference (comparing byte values), not
|
|
243 necessarily the alphabetical difference in the local language.
|
|
244
|
|
245 When using the operators with a trailing '#", or the short version and
|
|
246 'ignorecase' is off, the comparing is done with strcmp().
|
|
247
|
|
248 When using the operators with a trailing '?', or the short version and
|
|
249 'ignorecase' is set, the comparing is done with stricmp().
|
|
250
|
|
251 The "=~" and "!~" operators match the lefthand argument with the righthand
|
|
252 argument, which is used as a pattern. See |pattern| for what a pattern is.
|
|
253 This matching is always done like 'magic' was set and 'cpoptions' is empty, no
|
|
254 matter what the actual value of 'magic' or 'cpoptions' is. This makes scripts
|
|
255 portable. To avoid backslashes in the regexp pattern to be doubled, use a
|
|
256 single-quote string, see |literal-string|.
|
|
257 Since a string is considered to be a single line, a multi-line pattern
|
|
258 (containing \n, backslash-n) will not match. However, a literal NL character
|
|
259 can be matched like an ordinary character. Examples:
|
|
260 "foo\nbar" =~ "\n" evaluates to 1
|
|
261 "foo\nbar" =~ "\\n" evaluates to 0
|
|
262
|
|
263
|
|
264 expr5 and expr6 *expr5* *expr6*
|
|
265 ---------------
|
|
266 expr6 + expr6 .. number addition *expr-+*
|
|
267 expr6 - expr6 .. number subtraction *expr--*
|
|
268 expr6 . expr6 .. string concatenation *expr-.*
|
|
269
|
|
270 expr7 * expr7 .. number multiplication *expr-star*
|
|
271 expr7 / expr7 .. number division *expr-/*
|
|
272 expr7 % expr7 .. number modulo *expr-%*
|
|
273
|
|
274 For all, except ".", Strings are converted to Numbers.
|
|
275
|
|
276 Note the difference between "+" and ".":
|
|
277 "123" + "456" = 579
|
|
278 "123" . "456" = "123456"
|
|
279
|
|
280 When the righthand side of '/' is zero, the result is 0x7fffffff.
|
|
281 When the righthand side of '%' is zero, the result is 0.
|
|
282
|
|
283
|
|
284 expr7 *expr7*
|
|
285 -----
|
|
286 ! expr7 logical NOT *expr-!*
|
|
287 - expr7 unary minus *expr-unary--*
|
|
288 + expr7 unary plus *expr-unary-+*
|
|
289
|
|
290 For '!' non-zero becomes zero, zero becomes one.
|
|
291 For '-' the sign of the number is changed.
|
|
292 For '+' the number is unchanged.
|
|
293
|
|
294 A String will be converted to a Number first.
|
|
295
|
|
296 These three can be repeated and mixed. Examples:
|
|
297 !-1 == 0
|
|
298 !!8 == 1
|
|
299 --9 == 9
|
|
300
|
|
301
|
|
302 expr8 *expr8*
|
|
303 -----
|
|
304 expr9[expr1] index in String *expr-[]* *E111*
|
|
305
|
|
306 This results in a String that contains the expr1'th single byte from expr9.
|
|
307 expr9 is used as a String, expr1 as a Number. Note that this doesn't work for
|
|
308 multi-byte encodings.
|
|
309
|
|
310 Note that index zero gives the first character. This is like it works in C.
|
|
311 Careful: text column numbers start with one! Example, to get the character
|
|
312 under the cursor: >
|
|
313 :let c = getline(line("."))[col(".") - 1]
|
|
314
|
|
315 If the length of the String is less than the index, the result is an empty
|
|
316 String.
|
|
317
|
|
318 *expr9*
|
|
319 number
|
|
320 ------
|
|
321 number number constant *expr-number*
|
|
322
|
|
323 Decimal, Hexadecimal (starting with 0x or 0X), or Octal (starting with 0).
|
|
324
|
|
325
|
|
326 string *expr-string* *E114*
|
|
327 ------
|
|
328 "string" string constant *expr-quote*
|
|
329
|
|
330 Note that double quotes are used.
|
|
331
|
|
332 A string constant accepts these special characters:
|
|
333 \... three-digit octal number (e.g., "\316")
|
|
334 \.. two-digit octal number (must be followed by non-digit)
|
|
335 \. one-digit octal number (must be followed by non-digit)
|
|
336 \x.. byte specified with two hex numbers (e.g., "\x1f")
|
|
337 \x. byte specified with one hex number (must be followed by non-hex char)
|
|
338 \X.. same as \x..
|
|
339 \X. same as \x.
|
|
340 \u.... character specified with up to 4 hex numbers, stored according to the
|
|
341 current value of 'encoding' (e.g., "\u02a4")
|
|
342 \U.... same as \u....
|
|
343 \b backspace <BS>
|
|
344 \e escape <Esc>
|
|
345 \f formfeed <FF>
|
|
346 \n newline <NL>
|
|
347 \r return <CR>
|
|
348 \t tab <Tab>
|
|
349 \\ backslash
|
|
350 \" double quote
|
|
351 \<xxx> Special key named "xxx". e.g. "\<C-W>" for CTRL-W.
|
|
352
|
|
353 Note that "\000" and "\x00" force the end of the string.
|
|
354
|
|
355
|
|
356 literal-string *literal-string* *E115*
|
|
357 ---------------
|
26
|
358 'string' string constant *expr-'*
|
7
|
359
|
|
360 Note that single quotes are used.
|
|
361
|
26
|
362 This string is taken as it is. No backslashes are removed or have a special
|
|
363 meaning. A literal-string cannot contain a single quote. Use a normal,
|
|
364 double-quoted string for that.
|
|
365
|
|
366 Single quoted strings are useful for patterns, so that backslashes do not need
|
|
367 to be doubled. These two commands are equivalent: >
|
|
368 if a =~ "\\s*"
|
|
369 if a =~ '\s*'
|
7
|
370
|
|
371
|
|
372 option *expr-option* *E112* *E113*
|
|
373 ------
|
|
374 &option option value, local value if possible
|
|
375 &g:option global option value
|
|
376 &l:option local option value
|
|
377
|
|
378 Examples: >
|
|
379 echo "tabstop is " . &tabstop
|
|
380 if &insertmode
|
|
381
|
|
382 Any option name can be used here. See |options|. When using the local value
|
|
383 and there is no buffer-local or window-local value, the global value is used
|
|
384 anyway.
|
|
385
|
|
386
|
|
387 register *expr-register*
|
|
388 --------
|
|
389 @r contents of register 'r'
|
|
390
|
|
391 The result is the contents of the named register, as a single string.
|
|
392 Newlines are inserted where required. To get the contents of the unnamed
|
|
393 register use @" or @@. The '=' register can not be used here. See
|
|
394 |registers| for an explanation of the available registers.
|
|
395
|
|
396
|
|
397 nesting *expr-nesting* *E110*
|
|
398 -------
|
|
399 (expr1) nested expression
|
|
400
|
|
401
|
|
402 environment variable *expr-env*
|
|
403 --------------------
|
|
404 $VAR environment variable
|
|
405
|
|
406 The String value of any environment variable. When it is not defined, the
|
|
407 result is an empty string.
|
|
408 *expr-env-expand*
|
|
409 Note that there is a difference between using $VAR directly and using
|
|
410 expand("$VAR"). Using it directly will only expand environment variables that
|
|
411 are known inside the current Vim session. Using expand() will first try using
|
|
412 the environment variables known inside the current Vim session. If that
|
|
413 fails, a shell will be used to expand the variable. This can be slow, but it
|
|
414 does expand all variables that the shell knows about. Example: >
|
|
415 :echo $version
|
|
416 :echo expand("$version")
|
|
417 The first one probably doesn't echo anything, the second echoes the $version
|
|
418 variable (if your shell supports it).
|
|
419
|
|
420
|
|
421 internal variable *expr-variable*
|
|
422 -----------------
|
|
423 variable internal variable
|
|
424 See below |internal-variables|.
|
|
425
|
|
426
|
|
427 function call *expr-function* *E116* *E117* *E118* *E119* *E120*
|
|
428 -------------
|
|
429 function(expr1, ...) function call
|
|
430 See below |functions|.
|
|
431
|
|
432
|
|
433 ==============================================================================
|
|
434 3. Internal variable *internal-variables* *E121*
|
|
435 *E461*
|
|
436 An internal variable name can be made up of letters, digits and '_'. But it
|
|
437 cannot start with a digit. It's also possible to use curly braces, see
|
|
438 |curly-braces-names|.
|
|
439
|
|
440 An internal variable is created with the ":let" command |:let|.
|
|
441 An internal variable is destroyed with the ":unlet" command |:unlet|.
|
|
442 Using a name that isn't an internal variable, or an internal variable that has
|
|
443 been destroyed, results in an error.
|
|
444
|
|
445 There are several name spaces for variables. Which one is to be used is
|
|
446 specified by what is prepended:
|
|
447
|
|
448 (nothing) In a function: local to a function; otherwise: global
|
|
449 |buffer-variable| b: Local to the current buffer.
|
|
450 |window-variable| w: Local to the current window.
|
|
451 |global-variable| g: Global.
|
|
452 |local-variable| l: Local to a function.
|
|
453 |script-variable| s: Local to a |:source|'ed Vim script.
|
|
454 |function-argument| a: Function argument (only inside a function).
|
|
455 |vim-variable| v: Global, predefined by Vim.
|
|
456
|
|
457 *buffer-variable* *b:var*
|
|
458 A variable name that is preceded with "b:" is local to the current buffer.
|
|
459 Thus you can have several "b:foo" variables, one for each buffer.
|
|
460 This kind of variable is deleted when the buffer is wiped out or deleted with
|
|
461 |:bdelete|.
|
|
462
|
|
463 One local buffer variable is predefined:
|
|
464 *b:changedtick-variable* *changetick*
|
|
465 b:changedtick The total number of changes to the current buffer. It is
|
|
466 incremented for each change. An undo command is also a change
|
|
467 in this case. This can be used to perform an action only when
|
|
468 the buffer has changed. Example: >
|
|
469 :if my_changedtick != b:changedtick
|
|
470 : let my_changedtick = b:changedtick
|
|
471 : call My_Update()
|
|
472 :endif
|
|
473 <
|
|
474 *window-variable* *w:var*
|
|
475 A variable name that is preceded with "w:" is local to the current window. It
|
|
476 is deleted when the window is closed.
|
|
477
|
|
478 *global-variable* *g:var*
|
|
479 Inside functions global variables are accessed with "g:". Omitting this will
|
|
480 access a variable local to a function. But "g:" can also be used in any other
|
|
481 place if you like.
|
|
482
|
|
483 *local-variable* *l:var*
|
|
484 Inside functions local variables are accessed without prepending anything.
|
|
485 But you can also prepend "l:" if you like.
|
|
486
|
|
487 *script-variable* *s:var*
|
|
488 In a Vim script variables starting with "s:" can be used. They cannot be
|
|
489 accessed from outside of the scripts, thus are local to the script.
|
|
490
|
|
491 They can be used in:
|
|
492 - commands executed while the script is sourced
|
|
493 - functions defined in the script
|
|
494 - autocommands defined in the script
|
|
495 - functions and autocommands defined in functions and autocommands which were
|
|
496 defined in the script (recursively)
|
|
497 - user defined commands defined in the script
|
|
498 Thus not in:
|
|
499 - other scripts sourced from this one
|
|
500 - mappings
|
|
501 - etc.
|
|
502
|
|
503 script variables can be used to avoid conflicts with global variable names.
|
|
504 Take this example:
|
|
505
|
|
506 let s:counter = 0
|
|
507 function MyCounter()
|
|
508 let s:counter = s:counter + 1
|
|
509 echo s:counter
|
|
510 endfunction
|
|
511 command Tick call MyCounter()
|
|
512
|
|
513 You can now invoke "Tick" from any script, and the "s:counter" variable in
|
|
514 that script will not be changed, only the "s:counter" in the script where
|
|
515 "Tick" was defined is used.
|
|
516
|
|
517 Another example that does the same: >
|
|
518
|
|
519 let s:counter = 0
|
|
520 command Tick let s:counter = s:counter + 1 | echo s:counter
|
|
521
|
|
522 When calling a function and invoking a user-defined command, the context for
|
9
|
523 script variables is set to the script where the function or command was
|
7
|
524 defined.
|
|
525
|
|
526 The script variables are also available when a function is defined inside a
|
|
527 function that is defined in a script. Example: >
|
|
528
|
|
529 let s:counter = 0
|
|
530 function StartCounting(incr)
|
|
531 if a:incr
|
|
532 function MyCounter()
|
|
533 let s:counter = s:counter + 1
|
|
534 endfunction
|
|
535 else
|
|
536 function MyCounter()
|
|
537 let s:counter = s:counter - 1
|
|
538 endfunction
|
|
539 endif
|
|
540 endfunction
|
|
541
|
|
542 This defines the MyCounter() function either for counting up or counting down
|
|
543 when calling StartCounting(). It doesn't matter from where StartCounting() is
|
|
544 called, the s:counter variable will be accessible in MyCounter().
|
|
545
|
|
546 When the same script is sourced again it will use the same script variables.
|
|
547 They will remain valid as long as Vim is running. This can be used to
|
|
548 maintain a counter: >
|
|
549
|
|
550 if !exists("s:counter")
|
|
551 let s:counter = 1
|
|
552 echo "script executed for the first time"
|
|
553 else
|
|
554 let s:counter = s:counter + 1
|
|
555 echo "script executed " . s:counter . " times now"
|
|
556 endif
|
|
557
|
|
558 Note that this means that filetype plugins don't get a different set of script
|
|
559 variables for each buffer. Use local buffer variables instead |b:var|.
|
|
560
|
|
561
|
|
562 Predefined Vim variables: *vim-variable* *v:var*
|
|
563
|
|
564 *v:charconvert_from* *charconvert_from-variable*
|
|
565 v:charconvert_from
|
|
566 The name of the character encoding of a file to be converted.
|
|
567 Only valid while evaluating the 'charconvert' option.
|
|
568
|
|
569 *v:charconvert_to* *charconvert_to-variable*
|
|
570 v:charconvert_to
|
|
571 The name of the character encoding of a file after conversion.
|
|
572 Only valid while evaluating the 'charconvert' option.
|
|
573
|
|
574 *v:cmdarg* *cmdarg-variable*
|
|
575 v:cmdarg This variable is used for two purposes:
|
|
576 1. The extra arguments given to a file read/write command.
|
|
577 Currently these are "++enc=" and "++ff=". This variable is
|
|
578 set before an autocommand event for a file read/write
|
|
579 command is triggered. There is a leading space to make it
|
|
580 possible to append this variable directly after the
|
|
581 read/write command. Note: The "+cmd" argument isn't
|
|
582 included here, because it will be executed anyway.
|
|
583 2. When printing a PostScript file with ":hardcopy" this is
|
|
584 the argument for the ":hardcopy" command. This can be used
|
|
585 in 'printexpr'.
|
|
586
|
|
587 *v:cmdbang* *cmdbang-variable*
|
|
588 v:cmdbang Set like v:cmdarg for a file read/write command. When a "!"
|
|
589 was used the value is 1, otherwise it is 0. Note that this
|
|
590 can only be used in autocommands. For user commands |<bang>|
|
|
591 can be used.
|
|
592
|
|
593 *v:count* *count-variable*
|
|
594 v:count The count given for the last Normal mode command. Can be used
|
|
595 to get the count before a mapping. Read-only. Example: >
|
|
596 :map _x :<C-U>echo "the count is " . v:count<CR>
|
|
597 < Note: The <C-U> is required to remove the line range that you
|
|
598 get when typing ':' after a count.
|
|
599 "count" also works, for backwards compatibility.
|
|
600
|
|
601 *v:count1* *count1-variable*
|
|
602 v:count1 Just like "v:count", but defaults to one when no count is
|
|
603 used.
|
|
604
|
|
605 *v:ctype* *ctype-variable*
|
|
606 v:ctype The current locale setting for characters of the runtime
|
|
607 environment. This allows Vim scripts to be aware of the
|
|
608 current locale encoding. Technical: it's the value of
|
|
609 LC_CTYPE. When not using a locale the value is "C".
|
|
610 This variable can not be set directly, use the |:language|
|
|
611 command.
|
|
612 See |multi-lang|.
|
|
613
|
|
614 *v:dying* *dying-variable*
|
|
615 v:dying Normally zero. When a deadly signal is caught it's set to
|
|
616 one. When multiple signals are caught the number increases.
|
|
617 Can be used in an autocommand to check if Vim didn't
|
|
618 terminate normally. {only works on Unix}
|
|
619 Example: >
|
|
620 :au VimLeave * if v:dying | echo "\nAAAAaaaarrrggghhhh!!!\n" | endif
|
|
621 <
|
|
622 *v:errmsg* *errmsg-variable*
|
|
623 v:errmsg Last given error message. It's allowed to set this variable.
|
|
624 Example: >
|
|
625 :let v:errmsg = ""
|
|
626 :silent! next
|
|
627 :if v:errmsg != ""
|
|
628 : ... handle error
|
|
629 < "errmsg" also works, for backwards compatibility.
|
|
630
|
|
631 *v:exception* *exception-variable*
|
|
632 v:exception The value of the exception most recently caught and not
|
|
633 finished. See also |v:throwpoint| and |throw-variables|.
|
|
634 Example: >
|
|
635 :try
|
|
636 : throw "oops"
|
|
637 :catch /.*/
|
|
638 : echo "caught" v:exception
|
|
639 :endtry
|
|
640 < Output: "caught oops".
|
|
641
|
|
642 *v:fname_in* *fname_in-variable*
|
|
643 v:fname_in The name of the input file. Only valid while evaluating:
|
|
644 option used for ~
|
|
645 'charconvert' file to be converted
|
|
646 'diffexpr' original file
|
|
647 'patchexpr' original file
|
|
648 'printexpr' file to be printed
|
|
649
|
|
650 *v:fname_out* *fname_out-variable*
|
|
651 v:fname_out The name of the output file. Only valid while
|
|
652 evaluating:
|
|
653 option used for ~
|
|
654 'charconvert' resulting converted file (*)
|
|
655 'diffexpr' output of diff
|
|
656 'patchexpr' resulting patched file
|
|
657 (*) When doing conversion for a write command (e.g., ":w
|
|
658 file") it will be equal to v:fname_in. When doing conversion
|
|
659 for a read command (e.g., ":e file") it will be a temporary
|
|
660 file and different from v:fname_in.
|
|
661
|
|
662 *v:fname_new* *fname_new-variable*
|
|
663 v:fname_new The name of the new version of the file. Only valid while
|
|
664 evaluating 'diffexpr'.
|
|
665
|
|
666 *v:fname_diff* *fname_diff-variable*
|
|
667 v:fname_diff The name of the diff (patch) file. Only valid while
|
|
668 evaluating 'patchexpr'.
|
|
669
|
|
670 *v:folddashes* *folddashes-variable*
|
|
671 v:folddashes Used for 'foldtext': dashes representing foldlevel of a closed
|
|
672 fold.
|
29
|
673 Read-only in the |sandbox|. |fold-foldtext|
|
7
|
674
|
|
675 *v:foldlevel* *foldlevel-variable*
|
|
676 v:foldlevel Used for 'foldtext': foldlevel of closed fold.
|
29
|
677 Read-only in the |sandbox|. |fold-foldtext|
|
7
|
678
|
|
679 *v:foldend* *foldend-variable*
|
|
680 v:foldend Used for 'foldtext': last line of closed fold.
|
29
|
681 Read-only in the |sandbox|. |fold-foldtext|
|
7
|
682
|
|
683 *v:foldstart* *foldstart-variable*
|
|
684 v:foldstart Used for 'foldtext': first line of closed fold.
|
29
|
685 Read-only in the |sandbox|. |fold-foldtext|
|
7
|
686
|
11
|
687 *v:insertmode* *insertmode-variable*
|
|
688 v:insertmode Used for the |InsertEnter| and |InsertChange| autocommand
|
|
689 events. Values:
|
|
690 i Insert mode
|
|
691 r Replace mode
|
|
692 v Virtual Replace mode
|
|
693
|
7
|
694 *v:lang* *lang-variable*
|
|
695 v:lang The current locale setting for messages of the runtime
|
|
696 environment. This allows Vim scripts to be aware of the
|
|
697 current language. Technical: it's the value of LC_MESSAGES.
|
|
698 The value is system dependent.
|
|
699 This variable can not be set directly, use the |:language|
|
|
700 command.
|
|
701 It can be different from |v:ctype| when messages are desired
|
|
702 in a different language than what is used for character
|
|
703 encoding. See |multi-lang|.
|
|
704
|
|
705 *v:lc_time* *lc_time-variable*
|
|
706 v:lc_time The current locale setting for time messages of the runtime
|
|
707 environment. This allows Vim scripts to be aware of the
|
|
708 current language. Technical: it's the value of LC_TIME.
|
|
709 This variable can not be set directly, use the |:language|
|
|
710 command. See |multi-lang|.
|
|
711
|
|
712 *v:lnum* *lnum-variable*
|
29
|
713 v:lnum Line number for the 'foldexpr' |fold-expr| and 'indentexpr'
|
|
714 expressions. Only valid while one of these expressions is
|
|
715 being evaluated. Read-only when in the |sandbox|.
|
7
|
716
|
|
717 *v:prevcount* *prevcount-variable*
|
|
718 v:prevcount The count given for the last but one Normal mode command.
|
|
719 This is the v:count value of the previous command. Useful if
|
|
720 you want to cancel Visual mode and then use the count. >
|
|
721 :vmap % <Esc>:call MyFilter(v:prevcount)<CR>
|
|
722 < Read-only.
|
|
723
|
|
724 *v:progname* *progname-variable*
|
|
725 v:progname Contains the name (with path removed) with which Vim was
|
|
726 invoked. Allows you to do special initialisations for "view",
|
|
727 "evim" etc., or any other name you might symlink to Vim.
|
|
728 Read-only.
|
|
729
|
|
730 *v:register* *register-variable*
|
|
731 v:register The name of the register supplied to the last normal mode
|
|
732 command. Empty if none were supplied. |getreg()| |setreg()|
|
|
733
|
|
734 *v:servername* *servername-variable*
|
|
735 v:servername The resulting registered |x11-clientserver| name if any.
|
|
736 Read-only.
|
|
737
|
|
738 *v:shell_error* *shell_error-variable*
|
|
739 v:shell_error Result of the last shell command. When non-zero, the last
|
|
740 shell command had an error. When zero, there was no problem.
|
|
741 This only works when the shell returns the error code to Vim.
|
|
742 The value -1 is often used when the command could not be
|
|
743 executed. Read-only.
|
|
744 Example: >
|
|
745 :!mv foo bar
|
|
746 :if v:shell_error
|
|
747 : echo 'could not rename "foo" to "bar"!'
|
|
748 :endif
|
|
749 < "shell_error" also works, for backwards compatibility.
|
|
750
|
|
751 *v:statusmsg* *statusmsg-variable*
|
|
752 v:statusmsg Last given status message. It's allowed to set this variable.
|
|
753
|
|
754 *v:termresponse* *termresponse-variable*
|
|
755 v:termresponse The escape sequence returned by the terminal for the |t_RV|
|
|
756 termcap entry. It is set when Vim receives an escape sequence
|
|
757 that starts with ESC [ or CSI and ends in a 'c', with only
|
|
758 digits, ';' and '.' in between.
|
|
759 When this option is set, the TermResponse autocommand event is
|
|
760 fired, so that you can react to the response from the
|
|
761 terminal.
|
|
762 The response from a new xterm is: "<Esc>[ Pp ; Pv ; Pc c". Pp
|
|
763 is the terminal type: 0 for vt100 and 1 for vt220. Pv is the
|
|
764 patch level (since this was introduced in patch 95, it's
|
|
765 always 95 or bigger). Pc is always zero.
|
|
766 {only when compiled with |+termresponse| feature}
|
|
767
|
|
768 *v:this_session* *this_session-variable*
|
|
769 v:this_session Full filename of the last loaded or saved session file. See
|
|
770 |:mksession|. It is allowed to set this variable. When no
|
|
771 session file has been saved, this variable is empty.
|
|
772 "this_session" also works, for backwards compatibility.
|
|
773
|
|
774 *v:throwpoint* *throwpoint-variable*
|
|
775 v:throwpoint The point where the exception most recently caught and not
|
|
776 finished was thrown. Not set when commands are typed. See
|
|
777 also |v:exception| and |throw-variables|.
|
|
778 Example: >
|
|
779 :try
|
|
780 : throw "oops"
|
|
781 :catch /.*/
|
|
782 : echo "Exception from" v:throwpoint
|
|
783 :endtry
|
|
784 < Output: "Exception from test.vim, line 2"
|
|
785
|
|
786 *v:version* *version-variable*
|
|
787 v:version Version number of Vim: Major version number times 100 plus
|
|
788 minor version number. Version 5.0 is 500. Version 5.1 (5.01)
|
|
789 is 501. Read-only. "version" also works, for backwards
|
|
790 compatibility.
|
|
791 Use |has()| to check if a certain patch was included, e.g.: >
|
|
792 if has("patch123")
|
|
793 < Note that patch numbers are specific to the version, thus both
|
|
794 version 5.0 and 5.1 may have a patch 123, but these are
|
|
795 completely different.
|
|
796
|
|
797 *v:warningmsg* *warningmsg-variable*
|
|
798 v:warningmsg Last given warning message. It's allowed to set this variable.
|
|
799
|
|
800 ==============================================================================
|
|
801 4. Builtin Functions *functions*
|
|
802
|
|
803 See |function-list| for a list grouped by what the function is used for.
|
|
804
|
|
805 (Use CTRL-] on the function name to jump to the full explanation)
|
|
806
|
|
807 USAGE RESULT DESCRIPTION ~
|
|
808
|
|
809 append( {lnum}, {string}) Number append {string} below line {lnum}
|
|
810 argc() Number number of files in the argument list
|
|
811 argidx() Number current index in the argument list
|
|
812 argv( {nr}) String {nr} entry of the argument list
|
|
813 browse( {save}, {title}, {initdir}, {default})
|
|
814 String put up a file requester
|
29
|
815 browsedir( {title}, {initdir}) String put up a directory requester
|
7
|
816 bufexists( {expr}) Number TRUE if buffer {expr} exists
|
|
817 buflisted( {expr}) Number TRUE if buffer {expr} is listed
|
|
818 bufloaded( {expr}) Number TRUE if buffer {expr} is loaded
|
|
819 bufname( {expr}) String Name of the buffer {expr}
|
|
820 bufnr( {expr}) Number Number of the buffer {expr}
|
|
821 bufwinnr( {expr}) Number window number of buffer {expr}
|
|
822 byte2line( {byte}) Number line number at byte count {byte}
|
18
|
823 byteidx( {expr}, {nr}) Number byte index of {nr}'th char in {expr}
|
7
|
824 char2nr( {expr}) Number ASCII value of first char in {expr}
|
|
825 cindent( {lnum}) Number C indent for line {lnum}
|
|
826 col( {expr}) Number column nr of cursor or mark
|
|
827 confirm( {msg} [, {choices} [, {default} [, {type}]]])
|
|
828 Number number of choice picked by user
|
|
829 cscope_connection( [{num} , {dbpath} [, {prepend}]])
|
|
830 Number checks existence of cscope connection
|
|
831 cursor( {lnum}, {col}) Number position cursor at {lnum}, {col}
|
|
832 delete( {fname}) Number delete file {fname}
|
|
833 did_filetype() Number TRUE if FileType autocommand event used
|
32
|
834 diff_filler( {lnum}) Number diff filler lines about {lnum}
|
|
835 diff_hlID( {lnum}, {col}) Number diff highlighting at {lnum}/{col}
|
7
|
836 escape( {string}, {chars}) String escape {chars} in {string} with '\'
|
|
837 eventhandler( ) Number TRUE if inside an event handler
|
|
838 executable( {expr}) Number 1 if executable {expr} exists
|
|
839 exists( {expr}) Number TRUE if {expr} exists
|
|
840 expand( {expr}) String expand special keywords in {expr}
|
|
841 filereadable( {file}) Number TRUE if {file} is a readable file
|
19
|
842 findfile( {name}[, {path}[, {count}]])
|
|
843 String Find fine {name} in {path}
|
7
|
844 filewritable( {file}) Number TRUE if {file} is a writable file
|
|
845 fnamemodify( {fname}, {mods}) String modify file name
|
|
846 foldclosed( {lnum}) Number first line of fold at {lnum} if closed
|
|
847 foldclosedend( {lnum}) Number last line of fold at {lnum} if closed
|
|
848 foldlevel( {lnum}) Number fold level at {lnum}
|
|
849 foldtext( ) String line displayed for closed fold
|
|
850 foreground( ) Number bring the Vim window to the foreground
|
|
851 getchar( [expr]) Number get one character from the user
|
|
852 getcharmod( ) Number modifiers for the last typed character
|
|
853 getbufvar( {expr}, {varname}) variable {varname} in buffer {expr}
|
|
854 getcmdline() String return the current command-line
|
|
855 getcmdpos() Number return cursor position in command-line
|
|
856 getcwd() String the current working directory
|
20
|
857 getfperm( {fname}) String file permissions of file {fname}
|
|
858 getfsize( {fname}) Number size in bytes of file {fname}
|
37
|
859 getfontname( [{name}]) String name of font being used
|
7
|
860 getftime( {fname}) Number last modification time of file
|
20
|
861 getftype( {fname}) String description of type of file {fname}
|
7
|
862 getline( {lnum}) String line {lnum} from current buffer
|
|
863 getreg( [{regname}]) String contents of register
|
|
864 getregtype( [{regname}]) String type of register
|
|
865 getwinposx() Number X coord in pixels of GUI Vim window
|
|
866 getwinposy() Number Y coord in pixels of GUI Vim window
|
|
867 getwinvar( {nr}, {varname}) variable {varname} in window {nr}
|
|
868 glob( {expr}) String expand file wildcards in {expr}
|
|
869 globpath( {path}, {expr}) String do glob({expr}) for all dirs in {path}
|
|
870 has( {feature}) Number TRUE if feature {feature} supported
|
|
871 hasmapto( {what} [, {mode}]) Number TRUE if mapping to {what} exists
|
|
872 histadd( {history},{item}) String add an item to a history
|
|
873 histdel( {history} [, {item}]) String remove an item from a history
|
|
874 histget( {history} [, {index}]) String get the item {index} from a history
|
|
875 histnr( {history}) Number highest index of a history
|
|
876 hlexists( {name}) Number TRUE if highlight group {name} exists
|
|
877 hlID( {name}) Number syntax ID of highlight group {name}
|
|
878 hostname() String name of the machine Vim is running on
|
|
879 iconv( {expr}, {from}, {to}) String convert encoding of {expr}
|
|
880 indent( {lnum}) Number indent of line {lnum}
|
|
881 input( {prompt} [, {text}]) String get input from the user
|
|
882 inputdialog( {p} [, {t} [, {c}]]) String like input() but in a GUI dialog
|
|
883 inputrestore() Number restore typeahead
|
|
884 inputsave() Number save and clear typeahead
|
|
885 inputsecret( {prompt} [, {text}]) String like input() but hiding the text
|
|
886 isdirectory( {directory}) Number TRUE if {directory} is a directory
|
|
887 libcall( {lib}, {func}, {arg}) String call {func} in library {lib} with {arg}
|
|
888 libcallnr( {lib}, {func}, {arg}) Number idem, but return a Number
|
|
889 line( {expr}) Number line nr of cursor, last line or mark
|
|
890 line2byte( {lnum}) Number byte count of line {lnum}
|
|
891 lispindent( {lnum}) Number Lisp indent for line {lnum}
|
|
892 localtime() Number current time
|
|
893 maparg( {name}[, {mode}]) String rhs of mapping {name} in mode {mode}
|
|
894 mapcheck( {name}[, {mode}]) String check for mappings matching {name}
|
19
|
895 match( {expr}, {pat}[, {start}[, {count}]])
|
7
|
896 Number position where {pat} matches in {expr}
|
19
|
897 matchend( {expr}, {pat}[, {start}[, {count}]])
|
7
|
898 Number position where {pat} ends in {expr}
|
19
|
899 matchstr( {expr}, {pat}[, {start}[, {count}]])
|
|
900 String {count}'th match of {pat} in {expr}
|
7
|
901 mode() String current editing mode
|
|
902 nextnonblank( {lnum}) Number line nr of non-blank line >= {lnum}
|
|
903 nr2char( {expr}) String single char with ASCII value {expr}
|
|
904 prevnonblank( {lnum}) Number line nr of non-blank line <= {lnum}
|
|
905 remote_expr( {server}, {string} [, {idvar}])
|
|
906 String send expression
|
|
907 remote_foreground( {server}) Number bring Vim server to the foreground
|
|
908 remote_peek( {serverid} [, {retvar}])
|
|
909 Number check for reply string
|
|
910 remote_read( {serverid}) String read reply string
|
|
911 remote_send( {server}, {string} [, {idvar}])
|
|
912 String send key sequence
|
|
913 rename( {from}, {to}) Number rename (move) file from {from} to {to}
|
18
|
914 repeat( {expr}, {count}) String repeat {expr} {count} times
|
7
|
915 resolve( {filename}) String get filename a shortcut points to
|
|
916 search( {pattern} [, {flags}]) Number search for {pattern}
|
|
917 searchpair( {start}, {middle}, {end} [, {flags} [, {skip}]])
|
|
918 Number search for other end of start/end pair
|
|
919 server2client( {clientid}, {string})
|
|
920 Number send reply string
|
|
921 serverlist() String get a list of available servers
|
|
922 setbufvar( {expr}, {varname}, {val}) set {varname} in buffer {expr} to {val}
|
|
923 setcmdpos( {pos}) Number set cursor position in command-line
|
|
924 setline( {lnum}, {line}) Number set line {lnum} to {line}
|
|
925 setreg( {n}, {v}[, {opt}]) Number set register to value and type
|
|
926 setwinvar( {nr}, {varname}, {val}) set {varname} in window {nr} to {val}
|
|
927 simplify( {filename}) String simplify filename as much as possible
|
|
928 strftime( {format}[, {time}]) String time in specified format
|
|
929 stridx( {haystack}, {needle}) Number first index of {needle} in {haystack}
|
|
930 strlen( {expr}) Number length of the String {expr}
|
|
931 strpart( {src}, {start}[, {len}])
|
|
932 String {len} characters of {src} at {start}
|
|
933 strridx( {haystack}, {needle}) Number last index of {needle} in {haystack}
|
|
934 strtrans( {expr}) String translate string to make it printable
|
|
935 submatch( {nr}) String specific match in ":substitute"
|
|
936 substitute( {expr}, {pat}, {sub}, {flags})
|
|
937 String all {pat} in {expr} replaced with {sub}
|
32
|
938 synID( {lnum}, {col}, {trans}) Number syntax ID at {lnum} and {col}
|
7
|
939 synIDattr( {synID}, {what} [, {mode}])
|
|
940 String attribute {what} of syntax ID {synID}
|
|
941 synIDtrans( {synID}) Number translated syntax ID of {synID}
|
24
|
942 system( {expr} [, {input}]) String output of shell command/filter {expr}
|
7
|
943 tempname() String name for a temporary file
|
|
944 tolower( {expr}) String the String {expr} switched to lowercase
|
|
945 toupper( {expr}) String the String {expr} switched to uppercase
|
15
|
946 tr( {src}, {fromstr}, {tostr}) String translate chars of {src} in {fromstr}
|
|
947 to chars in {tostr}
|
7
|
948 type( {name}) Number type of variable {name}
|
|
949 virtcol( {expr}) Number screen column of cursor or mark
|
|
950 visualmode( [expr]) String last visual mode used
|
|
951 winbufnr( {nr}) Number buffer number of window {nr}
|
|
952 wincol() Number window column of the cursor
|
|
953 winheight( {nr}) Number height of window {nr}
|
|
954 winline() Number window line of the cursor
|
|
955 winnr() Number number of current window
|
|
956 winrestcmd() String returns command to restore window sizes
|
|
957 winwidth( {nr}) Number width of window {nr}
|
|
958
|
|
959 append({lnum}, {string}) *append()*
|
|
960 Append the text {string} after line {lnum} in the current
|
|
961 buffer. {lnum} can be zero, to insert a line before the first
|
|
962 one. Returns 1 for failure ({lnum} out of range) or 0 for
|
|
963 success.
|
|
964
|
|
965 *argc()*
|
|
966 argc() The result is the number of files in the argument list of the
|
|
967 current window. See |arglist|.
|
|
968
|
|
969 *argidx()*
|
|
970 argidx() The result is the current index in the argument list. 0 is
|
|
971 the first file. argc() - 1 is the last one. See |arglist|.
|
|
972
|
|
973 *argv()*
|
|
974 argv({nr}) The result is the {nr}th file in the argument list of the
|
|
975 current window. See |arglist|. "argv(0)" is the first one.
|
|
976 Example: >
|
|
977 :let i = 0
|
|
978 :while i < argc()
|
|
979 : let f = escape(argv(i), '. ')
|
|
980 : exe 'amenu Arg.' . f . ' :e ' . f . '<CR>'
|
|
981 : let i = i + 1
|
|
982 :endwhile
|
|
983 <
|
|
984 *browse()*
|
|
985 browse({save}, {title}, {initdir}, {default})
|
|
986 Put up a file requester. This only works when "has("browse")"
|
|
987 returns non-zero (only in some GUI versions).
|
|
988 The input fields are:
|
|
989 {save} when non-zero, select file to write
|
|
990 {title} title for the requester
|
|
991 {initdir} directory to start browsing in
|
|
992 {default} default file name
|
|
993 When the "Cancel" button is hit, something went wrong, or
|
|
994 browsing is not possible, an empty string is returned.
|
|
995
|
29
|
996 *browsedir()*
|
|
997 browsedir({title}, {initdir})
|
|
998 Put up a directory requester. This only works when
|
|
999 "has("browse")" returns non-zero (only in some GUI versions).
|
|
1000 On systems where a directory browser is not supported a file
|
|
1001 browser is used. In that case: select a file in the directory
|
|
1002 to be used.
|
|
1003 The input fields are:
|
|
1004 {title} title for the requester
|
|
1005 {initdir} directory to start browsing in
|
|
1006 When the "Cancel" button is hit, something went wrong, or
|
|
1007 browsing is not possible, an empty string is returned.
|
|
1008
|
7
|
1009 bufexists({expr}) *bufexists()*
|
|
1010 The result is a Number, which is non-zero if a buffer called
|
|
1011 {expr} exists.
|
9
|
1012 If the {expr} argument is a number, buffer numbers are used.
|
7
|
1013 If the {expr} argument is a string it must match a buffer name
|
9
|
1014 exactly. The name can be:
|
|
1015 - Relative to the current directory.
|
|
1016 - A full path.
|
|
1017 - The name of a buffer with 'filetype' set to "nofile".
|
|
1018 - A URL name.
|
7
|
1019 Unlisted buffers will be found.
|
|
1020 Note that help files are listed by their short name in the
|
|
1021 output of |:buffers|, but bufexists() requires using their
|
|
1022 long name to be able to find them.
|
|
1023 Use "bufexists(0)" to test for the existence of an alternate
|
|
1024 file name.
|
|
1025 *buffer_exists()*
|
|
1026 Obsolete name: buffer_exists().
|
|
1027
|
|
1028 buflisted({expr}) *buflisted()*
|
|
1029 The result is a Number, which is non-zero if a buffer called
|
|
1030 {expr} exists and is listed (has the 'buflisted' option set).
|
9
|
1031 The {expr} argument is used like with |bufexists()|.
|
7
|
1032
|
|
1033 bufloaded({expr}) *bufloaded()*
|
|
1034 The result is a Number, which is non-zero if a buffer called
|
|
1035 {expr} exists and is loaded (shown in a window or hidden).
|
9
|
1036 The {expr} argument is used like with |bufexists()|.
|
7
|
1037
|
|
1038 bufname({expr}) *bufname()*
|
|
1039 The result is the name of a buffer, as it is displayed by the
|
|
1040 ":ls" command.
|
|
1041 If {expr} is a Number, that buffer number's name is given.
|
|
1042 Number zero is the alternate buffer for the current window.
|
|
1043 If {expr} is a String, it is used as a |file-pattern| to match
|
|
1044 with the buffer names. This is always done like 'magic' is
|
|
1045 set and 'cpoptions' is empty. When there is more than one
|
|
1046 match an empty string is returned.
|
|
1047 "" or "%" can be used for the current buffer, "#" for the
|
|
1048 alternate buffer.
|
|
1049 A full match is preferred, otherwise a match at the start, end
|
|
1050 or middle of the buffer name is accepted.
|
|
1051 Listed buffers are found first. If there is a single match
|
|
1052 with a listed buffer, that one is returned. Next unlisted
|
|
1053 buffers are searched for.
|
|
1054 If the {expr} is a String, but you want to use it as a buffer
|
|
1055 number, force it to be a Number by adding zero to it: >
|
|
1056 :echo bufname("3" + 0)
|
|
1057 < If the buffer doesn't exist, or doesn't have a name, an empty
|
|
1058 string is returned. >
|
|
1059 bufname("#") alternate buffer name
|
|
1060 bufname(3) name of buffer 3
|
|
1061 bufname("%") name of current buffer
|
|
1062 bufname("file2") name of buffer where "file2" matches.
|
|
1063 < *buffer_name()*
|
|
1064 Obsolete name: buffer_name().
|
|
1065
|
|
1066 *bufnr()*
|
|
1067 bufnr({expr}) The result is the number of a buffer, as it is displayed by
|
|
1068 the ":ls" command. For the use of {expr}, see |bufname()|
|
|
1069 above. If the buffer doesn't exist, -1 is returned.
|
|
1070 bufnr("$") is the last buffer: >
|
|
1071 :let last_buffer = bufnr("$")
|
|
1072 < The result is a Number, which is the highest buffer number
|
|
1073 of existing buffers. Note that not all buffers with a smaller
|
|
1074 number necessarily exist, because ":bwipeout" may have removed
|
|
1075 them. Use bufexists() to test for the existence of a buffer.
|
|
1076 *buffer_number()*
|
|
1077 Obsolete name: buffer_number().
|
|
1078 *last_buffer_nr()*
|
|
1079 Obsolete name for bufnr("$"): last_buffer_nr().
|
|
1080
|
|
1081 bufwinnr({expr}) *bufwinnr()*
|
|
1082 The result is a Number, which is the number of the first
|
|
1083 window associated with buffer {expr}. For the use of {expr},
|
|
1084 see |bufname()| above. If buffer {expr} doesn't exist or
|
|
1085 there is no such window, -1 is returned. Example: >
|
|
1086
|
|
1087 echo "A window containing buffer 1 is " . (bufwinnr(1))
|
|
1088
|
|
1089 < The number can be used with |CTRL-W_w| and ":wincmd w"
|
|
1090 |:wincmd|.
|
|
1091
|
|
1092
|
|
1093 byte2line({byte}) *byte2line()*
|
|
1094 Return the line number that contains the character at byte
|
|
1095 count {byte} in the current buffer. This includes the
|
|
1096 end-of-line character, depending on the 'fileformat' option
|
|
1097 for the current buffer. The first character has byte count
|
|
1098 one.
|
|
1099 Also see |line2byte()|, |go| and |:goto|.
|
|
1100 {not available when compiled without the |+byte_offset|
|
|
1101 feature}
|
|
1102
|
18
|
1103 byteidx({expr}, {nr}) *byteidx()*
|
|
1104 Return byte index of the {nr}'th character in the string
|
|
1105 {expr}. Use zero for the first character, it returns zero.
|
|
1106 This function is only useful when there are multibyte
|
|
1107 characters, otherwise the returned value is equal to {nr}.
|
|
1108 Composing characters are counted as a separate character.
|
|
1109 Example : >
|
|
1110 echo matchstr(str, ".", byteidx(str, 3))
|
|
1111 < will display the fourth character. Another way to do the
|
|
1112 same: >
|
|
1113 let s = strpart(str, byteidx(str, 3))
|
|
1114 echo strpart(s, 0, byteidx(s, 1))
|
|
1115 < If there are less than {nr} characters -1 is returned.
|
|
1116 If there are exactly {nr} characters the length of the string
|
|
1117 is returned.
|
|
1118
|
7
|
1119 char2nr({expr}) *char2nr()*
|
|
1120 Return number value of the first char in {expr}. Examples: >
|
|
1121 char2nr(" ") returns 32
|
|
1122 char2nr("ABC") returns 65
|
|
1123 < The current 'encoding' is used. Example for "utf-8": >
|
|
1124 char2nr("á") returns 225
|
|
1125 char2nr("á"[0]) returns 195
|
|
1126
|
|
1127 cindent({lnum}) *cindent()*
|
|
1128 Get the amount of indent for line {lnum} according the C
|
|
1129 indenting rules, as with 'cindent'.
|
|
1130 The indent is counted in spaces, the value of 'tabstop' is
|
|
1131 relevant. {lnum} is used just like in |getline()|.
|
|
1132 When {lnum} is invalid or Vim was not compiled the |+cindent|
|
|
1133 feature, -1 is returned.
|
|
1134
|
|
1135 *col()*
|
24
|
1136 col({expr}) The result is a Number, which is the byte index of the column
|
7
|
1137 position given with {expr}. The accepted positions are:
|
|
1138 . the cursor position
|
|
1139 $ the end of the cursor line (the result is the
|
|
1140 number of characters in the cursor line plus one)
|
|
1141 'x position of mark x (if the mark is not set, 0 is
|
|
1142 returned)
|
|
1143 For the screen column position use |virtcol()|.
|
|
1144 Note that only marks in the current file can be used.
|
|
1145 Examples: >
|
|
1146 col(".") column of cursor
|
|
1147 col("$") length of cursor line plus one
|
|
1148 col("'t") column of mark t
|
|
1149 col("'" . markname) column of mark markname
|
|
1150 < The first column is 1. 0 is returned for an error.
|
|
1151 For the cursor position, when 'virtualedit' is active, the
|
|
1152 column is one higher if the cursor is after the end of the
|
|
1153 line. This can be used to obtain the column in Insert mode: >
|
|
1154 :imap <F2> <C-O>:let save_ve = &ve<CR>
|
|
1155 \<C-O>:set ve=all<CR>
|
|
1156 \<C-O>:echo col(".") . "\n" <Bar>
|
|
1157 \let &ve = save_ve<CR>
|
|
1158 <
|
|
1159 *confirm()*
|
|
1160 confirm({msg} [, {choices} [, {default} [, {type}]]])
|
|
1161 Confirm() offers the user a dialog, from which a choice can be
|
|
1162 made. It returns the number of the choice. For the first
|
|
1163 choice this is 1.
|
|
1164 Note: confirm() is only supported when compiled with dialog
|
|
1165 support, see |+dialog_con| and |+dialog_gui|.
|
|
1166 {msg} is displayed in a |dialog| with {choices} as the
|
|
1167 alternatives. When {choices} is missing or empty, "&OK" is
|
|
1168 used (and translated).
|
|
1169 {msg} is a String, use '\n' to include a newline. Only on
|
|
1170 some systems the string is wrapped when it doesn't fit.
|
|
1171 {choices} is a String, with the individual choices separated
|
|
1172 by '\n', e.g. >
|
|
1173 confirm("Save changes?", "&Yes\n&No\n&Cancel")
|
|
1174 < The letter after the '&' is the shortcut key for that choice.
|
|
1175 Thus you can type 'c' to select "Cancel". The shortcut does
|
|
1176 not need to be the first letter: >
|
|
1177 confirm("file has been modified", "&Save\nSave &All")
|
|
1178 < For the console, the first letter of each choice is used as
|
|
1179 the default shortcut key.
|
|
1180 The optional {default} argument is the number of the choice
|
|
1181 that is made if the user hits <CR>. Use 1 to make the first
|
|
1182 choice the default one. Use 0 to not set a default. If
|
|
1183 {default} is omitted, 1 is used.
|
|
1184 The optional {type} argument gives the type of dialog. This
|
|
1185 is only used for the icon of the Win32 GUI. It can be one of
|
|
1186 these values: "Error", "Question", "Info", "Warning" or
|
|
1187 "Generic". Only the first character is relevant. When {type}
|
|
1188 is omitted, "Generic" is used.
|
|
1189 If the user aborts the dialog by pressing <Esc>, CTRL-C,
|
|
1190 or another valid interrupt key, confirm() returns 0.
|
|
1191
|
|
1192 An example: >
|
|
1193 :let choice = confirm("What do you want?", "&Apples\n&Oranges\n&Bananas", 2)
|
|
1194 :if choice == 0
|
|
1195 : echo "make up your mind!"
|
|
1196 :elseif choice == 3
|
|
1197 : echo "tasteful"
|
|
1198 :else
|
|
1199 : echo "I prefer bananas myself."
|
|
1200 :endif
|
|
1201 < In a GUI dialog, buttons are used. The layout of the buttons
|
|
1202 depends on the 'v' flag in 'guioptions'. If it is included,
|
|
1203 the buttons are always put vertically. Otherwise, confirm()
|
|
1204 tries to put the buttons in one horizontal line. If they
|
|
1205 don't fit, a vertical layout is used anyway. For some systems
|
|
1206 the horizontal layout is always used.
|
|
1207
|
|
1208 *cscope_connection()*
|
|
1209 cscope_connection([{num} , {dbpath} [, {prepend}]])
|
|
1210 Checks for the existence of a |cscope| connection. If no
|
|
1211 parameters are specified, then the function returns:
|
|
1212 0, if cscope was not available (not compiled in), or
|
|
1213 if there are no cscope connections;
|
|
1214 1, if there is at least one cscope connection.
|
|
1215
|
|
1216 If parameters are specified, then the value of {num}
|
|
1217 determines how existence of a cscope connection is checked:
|
|
1218
|
|
1219 {num} Description of existence check
|
|
1220 ----- ------------------------------
|
|
1221 0 Same as no parameters (e.g., "cscope_connection()").
|
|
1222 1 Ignore {prepend}, and use partial string matches for
|
|
1223 {dbpath}.
|
|
1224 2 Ignore {prepend}, and use exact string matches for
|
|
1225 {dbpath}.
|
|
1226 3 Use {prepend}, use partial string matches for both
|
|
1227 {dbpath} and {prepend}.
|
|
1228 4 Use {prepend}, use exact string matches for both
|
|
1229 {dbpath} and {prepend}.
|
|
1230
|
|
1231 Note: All string comparisons are case sensitive!
|
|
1232
|
|
1233 Examples. Suppose we had the following (from ":cs show"): >
|
|
1234
|
|
1235 # pid database name prepend path
|
|
1236 0 27664 cscope.out /usr/local
|
|
1237 <
|
|
1238 Invocation Return Val ~
|
|
1239 ---------- ---------- >
|
|
1240 cscope_connection() 1
|
|
1241 cscope_connection(1, "out") 1
|
|
1242 cscope_connection(2, "out") 0
|
|
1243 cscope_connection(3, "out") 0
|
|
1244 cscope_connection(3, "out", "local") 1
|
|
1245 cscope_connection(4, "out") 0
|
|
1246 cscope_connection(4, "out", "local") 0
|
|
1247 cscope_connection(4, "cscope.out", "/usr/local") 1
|
|
1248 <
|
|
1249 cursor({lnum}, {col}) *cursor()*
|
|
1250 Positions the cursor at the column {col} in the line {lnum}.
|
|
1251 Does not change the jumplist.
|
|
1252 If {lnum} is greater than the number of lines in the buffer,
|
|
1253 the cursor will be positioned at the last line in the buffer.
|
|
1254 If {lnum} is zero, the cursor will stay in the current line.
|
|
1255 If {col} is greater than the number of characters in the line,
|
|
1256 the cursor will be positioned at the last character in the
|
|
1257 line.
|
|
1258 If {col} is zero, the cursor will stay in the current column.
|
|
1259
|
|
1260 *delete()*
|
|
1261 delete({fname}) Deletes the file by the name {fname}. The result is a Number,
|
|
1262 which is 0 if the file was deleted successfully, and non-zero
|
|
1263 when the deletion failed.
|
|
1264
|
|
1265 *did_filetype()*
|
|
1266 did_filetype() Returns non-zero when autocommands are being executed and the
|
|
1267 FileType event has been triggered at least once. Can be used
|
|
1268 to avoid triggering the FileType event again in the scripts
|
|
1269 that detect the file type. |FileType|
|
|
1270 When editing another file, the counter is reset, thus this
|
|
1271 really checks if the FileType event has been triggered for the
|
|
1272 current buffer. This allows an autocommand that starts
|
|
1273 editing another buffer to set 'filetype' and load a syntax
|
|
1274 file.
|
|
1275
|
32
|
1276 diff_filler({lnum}) *diff_filler()*
|
|
1277 Returns the number of filler lines above line {lnum}.
|
|
1278 These are the lines that were inserted at this point in
|
|
1279 another diff'ed window. These filler lines are shown in the
|
|
1280 display but don't exist in the buffer.
|
|
1281 {lnum} is used like with |getline()|. Thus "." is the current
|
|
1282 line, "'m" mark m, etc.
|
|
1283 Returns 0 if the current window is not in diff mode.
|
|
1284
|
|
1285 diff_hlID({lnum}, {col}) *diff_hlID()*
|
|
1286 Returns the highlight ID for diff mode at line {lnum} column
|
|
1287 {col} (byte index). When the current line does not have a
|
|
1288 diff change zero is returned.
|
|
1289 {lnum} is used like with |getline()|. Thus "." is the current
|
|
1290 line, "'m" mark m, etc.
|
|
1291 {col} is 1 for the leftmost column, {lnum} is 1 for the first
|
|
1292 line.
|
|
1293 The highlight ID can be used with |synIDattr()| to obtain
|
|
1294 syntax information about the highlighting.
|
|
1295
|
7
|
1296 escape({string}, {chars}) *escape()*
|
|
1297 Escape the characters in {chars} that occur in {string} with a
|
|
1298 backslash. Example: >
|
|
1299 :echo escape('c:\program files\vim', ' \')
|
|
1300 < results in: >
|
|
1301 c:\\program\ files\\vim
|
|
1302 <
|
|
1303 eventhandler() *eventhandler()*
|
|
1304 Returns 1 when inside an event handler. That is that Vim got
|
|
1305 interrupted while waiting for the user to type a character,
|
|
1306 e.g., when dropping a file on Vim. This means interactive
|
|
1307 commands cannot be used. Otherwise zero is returned.
|
|
1308
|
|
1309 executable({expr}) *executable()*
|
|
1310 This function checks if an executable with the name {expr}
|
|
1311 exists. {expr} must be the name of the program without any
|
10
|
1312 arguments.
|
|
1313 executable() uses the value of $PATH and/or the normal
|
|
1314 searchpath for programs. *PATHEXT*
|
|
1315 On MS-DOS and MS-Windows the ".exe", ".bat", etc. can
|
|
1316 optionally be included. Then the extensions in $PATHEXT are
|
|
1317 tried. Thus if "foo.exe" does not exist, "foo.exe.bat" can be
|
|
1318 found. If $PATHEXT is not set then ".exe;.com;.bat;.cmd" is
|
|
1319 used. A dot by itself can be used in $PATHEXT to try using
|
|
1320 the name without an extension. When 'shell' looks like a
|
|
1321 Unix shell, then the name is also tried without adding an
|
|
1322 extension.
|
|
1323 On MS-DOS and MS-Windows it only checks if the file exists and
|
|
1324 is not a directory, not if it's really executable.
|
7
|
1325 The result is a Number:
|
|
1326 1 exists
|
|
1327 0 does not exist
|
|
1328 -1 not implemented on this system
|
|
1329
|
|
1330 *exists()*
|
|
1331 exists({expr}) The result is a Number, which is non-zero if {expr} is
|
|
1332 defined, zero otherwise. The {expr} argument is a string,
|
|
1333 which contains one of these:
|
|
1334 &option-name Vim option (only checks if it exists,
|
|
1335 not if it really works)
|
|
1336 +option-name Vim option that works.
|
|
1337 $ENVNAME environment variable (could also be
|
|
1338 done by comparing with an empty
|
|
1339 string)
|
|
1340 *funcname built-in function (see |functions|)
|
|
1341 or user defined function (see
|
|
1342 |user-functions|).
|
|
1343 varname internal variable (see
|
|
1344 |internal-variables|). Does not work
|
|
1345 for |curly-braces-names|.
|
|
1346 :cmdname Ex command: built-in command, user
|
|
1347 command or command modifier |:command|.
|
|
1348 Returns:
|
|
1349 1 for match with start of a command
|
|
1350 2 full match with a command
|
|
1351 3 matches several user commands
|
|
1352 To check for a supported command
|
|
1353 always check the return value to be 2.
|
|
1354 #event autocommand defined for this event
|
|
1355 #event#pattern autocommand defined for this event and
|
|
1356 pattern (the pattern is taken
|
|
1357 literally and compared to the
|
|
1358 autocommand patterns character by
|
|
1359 character)
|
|
1360 For checking for a supported feature use |has()|.
|
|
1361
|
|
1362 Examples: >
|
|
1363 exists("&shortname")
|
|
1364 exists("$HOSTNAME")
|
|
1365 exists("*strftime")
|
|
1366 exists("*s:MyFunc")
|
|
1367 exists("bufcount")
|
|
1368 exists(":Make")
|
|
1369 exists("#CursorHold");
|
|
1370 exists("#BufReadPre#*.gz")
|
|
1371 < There must be no space between the symbol (&/$/*/#) and the
|
|
1372 name.
|
|
1373 Note that the argument must be a string, not the name of the
|
|
1374 variable itself! For example: >
|
|
1375 exists(bufcount)
|
|
1376 < This doesn't check for existence of the "bufcount" variable,
|
|
1377 but gets the contents of "bufcount", and checks if that
|
|
1378 exists.
|
|
1379
|
|
1380 expand({expr} [, {flag}]) *expand()*
|
|
1381 Expand wildcards and the following special keywords in {expr}.
|
|
1382 The result is a String.
|
|
1383
|
|
1384 When there are several matches, they are separated by <NL>
|
|
1385 characters. [Note: in version 5.0 a space was used, which
|
|
1386 caused problems when a file name contains a space]
|
|
1387
|
|
1388 If the expansion fails, the result is an empty string. A name
|
|
1389 for a non-existing file is not included.
|
|
1390
|
|
1391 When {expr} starts with '%', '#' or '<', the expansion is done
|
|
1392 like for the |cmdline-special| variables with their associated
|
|
1393 modifiers. Here is a short overview:
|
|
1394
|
|
1395 % current file name
|
|
1396 # alternate file name
|
|
1397 #n alternate file name n
|
|
1398 <cfile> file name under the cursor
|
|
1399 <afile> autocmd file name
|
|
1400 <abuf> autocmd buffer number (as a String!)
|
|
1401 <amatch> autocmd matched name
|
|
1402 <sfile> sourced script file name
|
|
1403 <cword> word under the cursor
|
|
1404 <cWORD> WORD under the cursor
|
|
1405 <client> the {clientid} of the last received
|
|
1406 message |server2client()|
|
|
1407 Modifiers:
|
|
1408 :p expand to full path
|
|
1409 :h head (last path component removed)
|
|
1410 :t tail (last path component only)
|
|
1411 :r root (one extension removed)
|
|
1412 :e extension only
|
|
1413
|
|
1414 Example: >
|
|
1415 :let &tags = expand("%:p:h") . "/tags"
|
|
1416 < Note that when expanding a string that starts with '%', '#' or
|
|
1417 '<', any following text is ignored. This does NOT work: >
|
|
1418 :let doesntwork = expand("%:h.bak")
|
|
1419 < Use this: >
|
|
1420 :let doeswork = expand("%:h") . ".bak"
|
|
1421 < Also note that expanding "<cfile>" and others only returns the
|
|
1422 referenced file name without further expansion. If "<cfile>"
|
|
1423 is "~/.cshrc", you need to do another expand() to have the
|
|
1424 "~/" expanded into the path of the home directory: >
|
|
1425 :echo expand(expand("<cfile>"))
|
|
1426 <
|
|
1427 There cannot be white space between the variables and the
|
|
1428 following modifier. The |fnamemodify()| function can be used
|
|
1429 to modify normal file names.
|
|
1430
|
|
1431 When using '%' or '#', and the current or alternate file name
|
|
1432 is not defined, an empty string is used. Using "%:p" in a
|
|
1433 buffer with no name, results in the current directory, with a
|
|
1434 '/' added.
|
|
1435
|
|
1436 When {expr} does not start with '%', '#' or '<', it is
|
|
1437 expanded like a file name is expanded on the command line.
|
|
1438 'suffixes' and 'wildignore' are used, unless the optional
|
|
1439 {flag} argument is given and it is non-zero. Names for
|
|
1440 non-existing files are included.
|
|
1441
|
|
1442 Expand() can also be used to expand variables and environment
|
|
1443 variables that are only known in a shell. But this can be
|
|
1444 slow, because a shell must be started. See |expr-env-expand|.
|
|
1445 The expanded variable is still handled like a list of file
|
|
1446 names. When an environment variable cannot be expanded, it is
|
|
1447 left unchanged. Thus ":echo expand('$FOOBAR')" results in
|
|
1448 "$FOOBAR".
|
|
1449
|
|
1450 See |glob()| for finding existing files. See |system()| for
|
|
1451 getting the raw output of an external command.
|
|
1452
|
|
1453 filereadable({file}) *filereadable()*
|
|
1454 The result is a Number, which is TRUE when a file with the
|
|
1455 name {file} exists, and can be read. If {file} doesn't exist,
|
|
1456 or is a directory, the result is FALSE. {file} is any
|
|
1457 expression, which is used as a String.
|
|
1458 *file_readable()*
|
|
1459 Obsolete name: file_readable().
|
|
1460
|
19
|
1461 finddir({name}[, {path}[, {count}]]) *finddir()*
|
|
1462 Find directory {name} in {path}.
|
|
1463 If {path} is omitted or empty then 'path' is used.
|
|
1464 If the optional {count} is given, find {count}'s occurrence of
|
|
1465 {name} in {path}.
|
|
1466 This is quite similar to the ex-command |:find|.
|
|
1467 When the found directory is below the current directory a
|
|
1468 relative path is returned. Otherwise a full path is returned.
|
|
1469 Example: >
|
|
1470 :echo findfile("tags.vim", ".;")
|
|
1471 < Searches from the current directory upwards until it finds
|
|
1472 the file "tags.vim".
|
|
1473 {only available when compiled with the +file_in_path feature}
|
|
1474
|
|
1475 findfile({name}[, {path}[, {count}]]) *findfile()*
|
|
1476 Just like |finddir()|, but find a file instead of a directory.
|
|
1477
|
7
|
1478 filewritable({file}) *filewritable()*
|
|
1479 The result is a Number, which is 1 when a file with the
|
|
1480 name {file} exists, and can be written. If {file} doesn't
|
|
1481 exist, or is not writable, the result is 0. If (file) is a
|
|
1482 directory, and we can write to it, the result is 2.
|
|
1483
|
|
1484 fnamemodify({fname}, {mods}) *fnamemodify()*
|
|
1485 Modify file name {fname} according to {mods}. {mods} is a
|
|
1486 string of characters like it is used for file names on the
|
|
1487 command line. See |filename-modifiers|.
|
|
1488 Example: >
|
|
1489 :echo fnamemodify("main.c", ":p:h")
|
|
1490 < results in: >
|
|
1491 /home/mool/vim/vim/src
|
|
1492 < Note: Environment variables and "~" don't work in {fname}, use
|
|
1493 |expand()| first then.
|
|
1494
|
|
1495 foldclosed({lnum}) *foldclosed()*
|
|
1496 The result is a Number. If the line {lnum} is in a closed
|
|
1497 fold, the result is the number of the first line in that fold.
|
|
1498 If the line {lnum} is not in a closed fold, -1 is returned.
|
|
1499
|
|
1500 foldclosedend({lnum}) *foldclosedend()*
|
|
1501 The result is a Number. If the line {lnum} is in a closed
|
|
1502 fold, the result is the number of the last line in that fold.
|
|
1503 If the line {lnum} is not in a closed fold, -1 is returned.
|
|
1504
|
|
1505 foldlevel({lnum}) *foldlevel()*
|
|
1506 The result is a Number, which is the foldlevel of line {lnum}
|
|
1507 in the current buffer. For nested folds the deepest level is
|
|
1508 returned. If there is no fold at line {lnum}, zero is
|
|
1509 returned. It doesn't matter if the folds are open or closed.
|
|
1510 When used while updating folds (from 'foldexpr') -1 is
|
|
1511 returned for lines where folds are still to be updated and the
|
|
1512 foldlevel is unknown. As a special case the level of the
|
|
1513 previous line is usually available.
|
|
1514
|
|
1515 *foldtext()*
|
|
1516 foldtext() Returns a String, to be displayed for a closed fold. This is
|
|
1517 the default function used for the 'foldtext' option and should
|
|
1518 only be called from evaluating 'foldtext'. It uses the
|
|
1519 |v:foldstart|, |v:foldend| and |v:folddashes| variables.
|
|
1520 The returned string looks like this: >
|
|
1521 +-- 45 lines: abcdef
|
|
1522 < The number of dashes depends on the foldlevel. The "45" is
|
|
1523 the number of lines in the fold. "abcdef" is the text in the
|
|
1524 first non-blank line of the fold. Leading white space, "//"
|
|
1525 or "/*" and the text from the 'foldmarker' and 'commentstring'
|
|
1526 options is removed.
|
|
1527 {not available when compiled without the |+folding| feature}
|
|
1528
|
29
|
1529 foldtextresult({lnum}) *foldtextresult()*
|
|
1530 Returns the text that is displayed for the closed fold at line
|
|
1531 {lnum}. Evaluates 'foldtext' in the appropriate context.
|
|
1532 When there is no closed fold at {lnum} an empty string is
|
|
1533 returned.
|
|
1534 {lnum} is used like with |getline()|. Thus "." is the current
|
|
1535 line, "'m" mark m, etc.
|
|
1536 Useful when exporting folded text, e.g., to HTML.
|
|
1537 {not available when compiled without the |+folding| feature}
|
|
1538
|
7
|
1539 *foreground()*
|
|
1540 foreground() Move the Vim window to the foreground. Useful when sent from
|
|
1541 a client to a Vim server. |remote_send()|
|
|
1542 On Win32 systems this might not work, the OS does not always
|
|
1543 allow a window to bring itself to the foreground. Use
|
|
1544 |remote_foreground()| instead.
|
|
1545 {only in the Win32, Athena, Motif and GTK GUI versions and the
|
|
1546 Win32 console version}
|
|
1547
|
|
1548 getchar([expr]) *getchar()*
|
|
1549 Get a single character from the user. If it is an 8-bit
|
|
1550 character, the result is a number. Otherwise a String is
|
|
1551 returned with the encoded character. For a special key it's a
|
|
1552 sequence of bytes starting with 0x80 (decimal: 128).
|
|
1553 If [expr] is omitted, wait until a character is available.
|
|
1554 If [expr] is 0, only get a character when one is available.
|
|
1555 If [expr] is 1, only check if a character is available, it is
|
|
1556 not consumed. If a normal character is
|
|
1557 available, it is returned, otherwise a
|
|
1558 non-zero value is returned.
|
|
1559 If a normal character available, it is returned as a Number.
|
|
1560 Use nr2char() to convert it to a String.
|
|
1561 The returned value is zero if no character is available.
|
|
1562 The returned value is a string of characters for special keys
|
|
1563 and when a modifier (shift, control, alt) was used.
|
|
1564 There is no prompt, you will somehow have to make clear to the
|
|
1565 user that a character has to be typed.
|
|
1566 There is no mapping for the character.
|
|
1567 Key codes are replaced, thus when the user presses the <Del>
|
|
1568 key you get the code for the <Del> key, not the raw character
|
|
1569 sequence. Examples: >
|
|
1570 getchar() == "\<Del>"
|
|
1571 getchar() == "\<S-Left>"
|
|
1572 < This example redefines "f" to ignore case: >
|
|
1573 :nmap f :call FindChar()<CR>
|
|
1574 :function FindChar()
|
|
1575 : let c = nr2char(getchar())
|
|
1576 : while col('.') < col('$') - 1
|
|
1577 : normal l
|
|
1578 : if getline('.')[col('.') - 1] ==? c
|
|
1579 : break
|
|
1580 : endif
|
|
1581 : endwhile
|
|
1582 :endfunction
|
|
1583
|
|
1584 getcharmod() *getcharmod()*
|
|
1585 The result is a Number which is the state of the modifiers for
|
|
1586 the last obtained character with getchar() or in another way.
|
|
1587 These values are added together:
|
|
1588 2 shift
|
|
1589 4 control
|
|
1590 8 alt (meta)
|
|
1591 16 mouse double click
|
|
1592 32 mouse triple click
|
|
1593 64 mouse quadruple click
|
|
1594 128 Macintosh only: command
|
|
1595 Only the modifiers that have not been included in the
|
|
1596 character itself are obtained. Thus Shift-a results in "A"
|
|
1597 with no modifier.
|
|
1598
|
|
1599 getbufvar({expr}, {varname}) *getbufvar()*
|
|
1600 The result is the value of option or local buffer variable
|
|
1601 {varname} in buffer {expr}. Note that the name without "b:"
|
|
1602 must be used.
|
|
1603 This also works for a global or local window option, but it
|
|
1604 doesn't work for a global or local window variable.
|
|
1605 For the use of {expr}, see |bufname()| above.
|
|
1606 When the buffer or variable doesn't exist an empty string is
|
|
1607 returned, there is no error message.
|
|
1608 Examples: >
|
|
1609 :let bufmodified = getbufvar(1, "&mod")
|
|
1610 :echo "todo myvar = " . getbufvar("todo", "myvar")
|
|
1611 <
|
|
1612 getcmdline() *getcmdline()*
|
|
1613 Return the current command-line. Only works when the command
|
|
1614 line is being edited, thus requires use of |c_CTRL-\_e| or
|
|
1615 |c_CTRL-R_=|.
|
|
1616 Example: >
|
|
1617 :cmap <F7> <C-\>eescape(getcmdline(), ' \')<CR>
|
|
1618 < Also see |getcmdpos()| and |setcmdpos()|.
|
|
1619
|
|
1620 getcmdpos({pos}) *getcmdpos()*
|
|
1621 Return the position of the cursor in the command line as a
|
|
1622 byte count. The first column is 1.
|
|
1623 Only works when editing the command line, thus requires use of
|
|
1624 |c_CTRL-\_e| or |c_CTRL-R_=|. Returns 0 otherwise.
|
|
1625 Also see |setcmdpos()| and |getcmdline()|.
|
|
1626
|
|
1627 *getcwd()*
|
|
1628 getcwd() The result is a String, which is the name of the current
|
|
1629 working directory.
|
|
1630
|
|
1631 getfsize({fname}) *getfsize()*
|
|
1632 The result is a Number, which is the size in bytes of the
|
|
1633 given file {fname}.
|
|
1634 If {fname} is a directory, 0 is returned.
|
|
1635 If the file {fname} can't be found, -1 is returned.
|
|
1636
|
37
|
1637 getfontname([{name}]) *getfontname()*
|
|
1638 Without an argument returns the name of the normal font being
|
|
1639 used. Like what is used for the Normal highlight group
|
|
1640 |hl-Normal|.
|
|
1641 With an argument a check is done whether {name} is a valid
|
|
1642 font name. If not then an empty string is returned.
|
|
1643 Otherwise the actual font name is returned, or {name} if the
|
|
1644 GUI does not support obtaining the real name.
|
|
1645 Only works when the GUI is running, thus not you your vimrc or
|
|
1646 Note that the GTK 2 GUI accepts any font name, thus checking
|
|
1647 for a valid name does not work.
|
|
1648 gvimrc file. Use the |GUIEnter| autocommand to use this
|
|
1649 function just after the GUI has started.
|
|
1650
|
20
|
1651 getfperm({fname}) *getfperm()*
|
|
1652 The result is a String, which is the read, write, and execute
|
|
1653 permissions of the given file {fname}.
|
|
1654 If {fname} does not exist or its directory cannot be read, an
|
|
1655 empty string is returned.
|
|
1656 The result is of the form "rwxrwxrwx", where each group of
|
|
1657 "rwx" flags represent, in turn, the permissions of the owner
|
|
1658 of the file, the group the file belongs to, and other users.
|
|
1659 If a user does not have a given permission the flag for this
|
|
1660 is replaced with the string "-". Example: >
|
|
1661 :echo getfperm("/etc/passwd")
|
|
1662 < This will hopefully (from a security point of view) display
|
|
1663 the string "rw-r--r--" or even "rw-------".
|
|
1664
|
7
|
1665 getftime({fname}) *getftime()*
|
|
1666 The result is a Number, which is the last modification time of
|
|
1667 the given file {fname}. The value is measured as seconds
|
|
1668 since 1st Jan 1970, and may be passed to strftime(). See also
|
|
1669 |localtime()| and |strftime()|.
|
|
1670 If the file {fname} can't be found -1 is returned.
|
|
1671
|
20
|
1672 getftype({fname}) *getftype()*
|
|
1673 The result is a String, which is a description of the kind of
|
|
1674 file of the given file {fname}.
|
|
1675 If {fname} does not exist an empty string is returned.
|
|
1676 Here is a table over different kinds of files and their
|
|
1677 results:
|
|
1678 Normal file "file"
|
|
1679 Directory "dir"
|
|
1680 Symbolic link "link"
|
|
1681 Block device "bdev"
|
|
1682 Character device "cdev"
|
|
1683 Socket "socket"
|
|
1684 FIFO "fifo"
|
|
1685 All other "other"
|
|
1686 Example: >
|
|
1687 getftype("/home")
|
|
1688 < Note that a type such as "link" will only be returned on
|
|
1689 systems that support it. On some systems only "dir" and
|
|
1690 "file" are returned.
|
|
1691
|
7
|
1692 *getline()*
|
|
1693 getline({lnum}) The result is a String, which is line {lnum} from the current
|
|
1694 buffer. Example: >
|
|
1695 getline(1)
|
|
1696 < When {lnum} is a String that doesn't start with a
|
|
1697 digit, line() is called to translate the String into a Number.
|
|
1698 To get the line under the cursor: >
|
|
1699 getline(".")
|
|
1700 < When {lnum} is smaller than 1 or bigger than the number of
|
|
1701 lines in the buffer, an empty string is returned.
|
|
1702
|
|
1703 getreg([{regname}]) *getreg()*
|
|
1704 The result is a String, which is the contents of register
|
|
1705 {regname}. Example: >
|
|
1706 :let cliptext = getreg('*')
|
|
1707 < getreg('=') returns the last evaluated value of the expression
|
|
1708 register. (For use in maps).
|
|
1709 If {regname} is not specified, |v:register| is used.
|
|
1710
|
|
1711 getregtype([{regname}]) *getregtype()*
|
|
1712 The result is a String, which is type of register {regname}.
|
|
1713 The value will be one of:
|
|
1714 "v" for |characterwise| text
|
|
1715 "V" for |linewise| text
|
|
1716 "<CTRL-V>{width}" for |blockwise-visual| text
|
|
1717 0 for an empty or unknown register
|
|
1718 <CTRL-V> is one character with value 0x16.
|
|
1719 If {regname} is not specified, |v:register| is used.
|
|
1720
|
|
1721 *getwinposx()*
|
|
1722 getwinposx() The result is a Number, which is the X coordinate in pixels of
|
|
1723 the left hand side of the GUI Vim window. The result will be
|
|
1724 -1 if the information is not available.
|
|
1725
|
|
1726 *getwinposy()*
|
|
1727 getwinposy() The result is a Number, which is the Y coordinate in pixels of
|
|
1728 the top of the GUI Vim window. The result will be -1 if the
|
|
1729 information is not available.
|
|
1730
|
|
1731 getwinvar({nr}, {varname}) *getwinvar()*
|
|
1732 The result is the value of option or local window variable
|
|
1733 {varname} in window {nr}.
|
|
1734 This also works for a global or local buffer option, but it
|
|
1735 doesn't work for a global or local buffer variable.
|
|
1736 Note that the name without "w:" must be used.
|
|
1737 Examples: >
|
|
1738 :let list_is_on = getwinvar(2, '&list')
|
|
1739 :echo "myvar = " . getwinvar(1, 'myvar')
|
|
1740 <
|
|
1741 *glob()*
|
|
1742 glob({expr}) Expand the file wildcards in {expr}. The result is a String.
|
|
1743 When there are several matches, they are separated by <NL>
|
|
1744 characters.
|
|
1745 If the expansion fails, the result is an empty string.
|
|
1746 A name for a non-existing file is not included.
|
|
1747
|
|
1748 For most systems backticks can be used to get files names from
|
|
1749 any external command. Example: >
|
|
1750 :let tagfiles = glob("`find . -name tags -print`")
|
|
1751 :let &tags = substitute(tagfiles, "\n", ",", "g")
|
|
1752 < The result of the program inside the backticks should be one
|
|
1753 item per line. Spaces inside an item are allowed.
|
|
1754
|
|
1755 See |expand()| for expanding special Vim variables. See
|
|
1756 |system()| for getting the raw output of an external command.
|
|
1757
|
|
1758 globpath({path}, {expr}) *globpath()*
|
|
1759 Perform glob() on all directories in {path} and concatenate
|
|
1760 the results. Example: >
|
|
1761 :echo globpath(&rtp, "syntax/c.vim")
|
|
1762 < {path} is a comma-separated list of directory names. Each
|
|
1763 directory name is prepended to {expr} and expanded like with
|
|
1764 glob(). A path separator is inserted when needed.
|
|
1765 To add a comma inside a directory name escape it with a
|
|
1766 backslash. Note that on MS-Windows a directory may have a
|
|
1767 trailing backslash, remove it if you put a comma after it.
|
|
1768 If the expansion fails for one of the directories, there is no
|
|
1769 error message.
|
|
1770 The 'wildignore' option applies: Names matching one of the
|
|
1771 patterns in 'wildignore' will be skipped.
|
|
1772
|
|
1773 *has()*
|
|
1774 has({feature}) The result is a Number, which is 1 if the feature {feature} is
|
|
1775 supported, zero otherwise. The {feature} argument is a
|
|
1776 string. See |feature-list| below.
|
|
1777 Also see |exists()|.
|
|
1778
|
|
1779 hasmapto({what} [, {mode}]) *hasmapto()*
|
|
1780 The result is a Number, which is 1 if there is a mapping that
|
|
1781 contains {what} in somewhere in the rhs (what it is mapped to)
|
|
1782 and this mapping exists in one of the modes indicated by
|
|
1783 {mode}.
|
|
1784 Both the global mappings and the mappings local to the current
|
|
1785 buffer are checked for a match.
|
|
1786 If no matching mapping is found 0 is returned.
|
|
1787 The following characters are recognized in {mode}:
|
|
1788 n Normal mode
|
|
1789 v Visual mode
|
|
1790 o Operator-pending mode
|
|
1791 i Insert mode
|
|
1792 l Language-Argument ("r", "f", "t", etc.)
|
|
1793 c Command-line mode
|
|
1794 When {mode} is omitted, "nvo" is used.
|
|
1795
|
|
1796 This function is useful to check if a mapping already exists
|
|
1797 to a function in a Vim script. Example: >
|
|
1798 :if !hasmapto('\ABCdoit')
|
|
1799 : map <Leader>d \ABCdoit
|
|
1800 :endif
|
|
1801 < This installs the mapping to "\ABCdoit" only if there isn't
|
|
1802 already a mapping to "\ABCdoit".
|
|
1803
|
|
1804 histadd({history}, {item}) *histadd()*
|
|
1805 Add the String {item} to the history {history} which can be
|
|
1806 one of: *hist-names*
|
|
1807 "cmd" or ":" command line history
|
|
1808 "search" or "/" search pattern history
|
|
1809 "expr" or "=" typed expression history
|
|
1810 "input" or "@" input line history
|
|
1811 If {item} does already exist in the history, it will be
|
|
1812 shifted to become the newest entry.
|
|
1813 The result is a Number: 1 if the operation was successful,
|
|
1814 otherwise 0 is returned.
|
|
1815
|
|
1816 Example: >
|
|
1817 :call histadd("input", strftime("%Y %b %d"))
|
|
1818 :let date=input("Enter date: ")
|
|
1819 < This function is not available in the |sandbox|.
|
|
1820
|
|
1821 histdel({history} [, {item}]) *histdel()*
|
|
1822 Clear {history}, ie. delete all its entries. See |hist-names|
|
|
1823 for the possible values of {history}.
|
|
1824
|
|
1825 If the parameter {item} is given as String, this is seen
|
|
1826 as regular expression. All entries matching that expression
|
|
1827 will be removed from the history (if there are any).
|
|
1828 Upper/lowercase must match, unless "\c" is used |/\c|.
|
|
1829 If {item} is a Number, it will be interpreted as index, see
|
|
1830 |:history-indexing|. The respective entry will be removed
|
|
1831 if it exists.
|
|
1832
|
|
1833 The result is a Number: 1 for a successful operation,
|
|
1834 otherwise 0 is returned.
|
|
1835
|
|
1836 Examples:
|
|
1837 Clear expression register history: >
|
|
1838 :call histdel("expr")
|
|
1839 <
|
|
1840 Remove all entries starting with "*" from the search history: >
|
|
1841 :call histdel("/", '^\*')
|
|
1842 <
|
|
1843 The following three are equivalent: >
|
|
1844 :call histdel("search", histnr("search"))
|
|
1845 :call histdel("search", -1)
|
|
1846 :call histdel("search", '^'.histget("search", -1).'$')
|
|
1847 <
|
|
1848 To delete the last search pattern and use the last-but-one for
|
|
1849 the "n" command and 'hlsearch': >
|
|
1850 :call histdel("search", -1)
|
|
1851 :let @/ = histget("search", -1)
|
|
1852
|
|
1853 histget({history} [, {index}]) *histget()*
|
|
1854 The result is a String, the entry with Number {index} from
|
|
1855 {history}. See |hist-names| for the possible values of
|
|
1856 {history}, and |:history-indexing| for {index}. If there is
|
|
1857 no such entry, an empty String is returned. When {index} is
|
|
1858 omitted, the most recent item from the history is used.
|
|
1859
|
|
1860 Examples:
|
|
1861 Redo the second last search from history. >
|
|
1862 :execute '/' . histget("search", -2)
|
|
1863
|
|
1864 < Define an Ex command ":H {num}" that supports re-execution of
|
|
1865 the {num}th entry from the output of |:history|. >
|
|
1866 :command -nargs=1 H execute histget("cmd", 0+<args>)
|
|
1867 <
|
|
1868 histnr({history}) *histnr()*
|
|
1869 The result is the Number of the current entry in {history}.
|
|
1870 See |hist-names| for the possible values of {history}.
|
|
1871 If an error occurred, -1 is returned.
|
|
1872
|
|
1873 Example: >
|
|
1874 :let inp_index = histnr("expr")
|
|
1875 <
|
|
1876 hlexists({name}) *hlexists()*
|
|
1877 The result is a Number, which is non-zero if a highlight group
|
|
1878 called {name} exists. This is when the group has been
|
|
1879 defined in some way. Not necessarily when highlighting has
|
|
1880 been defined for it, it may also have been used for a syntax
|
|
1881 item.
|
|
1882 *highlight_exists()*
|
|
1883 Obsolete name: highlight_exists().
|
|
1884
|
|
1885 *hlID()*
|
|
1886 hlID({name}) The result is a Number, which is the ID of the highlight group
|
|
1887 with name {name}. When the highlight group doesn't exist,
|
|
1888 zero is returned.
|
|
1889 This can be used to retrieve information about the highlight
|
|
1890 group. For example, to get the background color of the
|
|
1891 "Comment" group: >
|
|
1892 :echo synIDattr(synIDtrans(hlID("Comment")), "bg")
|
|
1893 < *highlightID()*
|
|
1894 Obsolete name: highlightID().
|
|
1895
|
|
1896 hostname() *hostname()*
|
|
1897 The result is a String, which is the name of the machine on
|
|
1898 which Vim is currently running. Machine names greater than
|
|
1899 256 characters long are truncated.
|
|
1900
|
|
1901 iconv({expr}, {from}, {to}) *iconv()*
|
|
1902 The result is a String, which is the text {expr} converted
|
|
1903 from encoding {from} to encoding {to}.
|
|
1904 When the conversion fails an empty string is returned.
|
|
1905 The encoding names are whatever the iconv() library function
|
|
1906 can accept, see ":!man 3 iconv".
|
|
1907 Most conversions require Vim to be compiled with the |+iconv|
|
|
1908 feature. Otherwise only UTF-8 to latin1 conversion and back
|
|
1909 can be done.
|
|
1910 This can be used to display messages with special characters,
|
|
1911 no matter what 'encoding' is set to. Write the message in
|
|
1912 UTF-8 and use: >
|
|
1913 echo iconv(utf8_str, "utf-8", &enc)
|
|
1914 < Note that Vim uses UTF-8 for all Unicode encodings, conversion
|
|
1915 from/to UCS-2 is automatically changed to use UTF-8. You
|
|
1916 cannot use UCS-2 in a string anyway, because of the NUL bytes.
|
|
1917 {only available when compiled with the +multi_byte feature}
|
|
1918
|
|
1919 *indent()*
|
|
1920 indent({lnum}) The result is a Number, which is indent of line {lnum} in the
|
|
1921 current buffer. The indent is counted in spaces, the value
|
|
1922 of 'tabstop' is relevant. {lnum} is used just like in
|
|
1923 |getline()|.
|
|
1924 When {lnum} is invalid -1 is returned.
|
|
1925
|
|
1926 input({prompt} [, {text}]) *input()*
|
|
1927 The result is a String, which is whatever the user typed on
|
|
1928 the command-line. The parameter is either a prompt string, or
|
|
1929 a blank string (for no prompt). A '\n' can be used in the
|
|
1930 prompt to start a new line. The highlighting set with
|
|
1931 |:echohl| is used for the prompt. The input is entered just
|
|
1932 like a command-line, with the same editing commands and
|
|
1933 mappings. There is a separate history for lines typed for
|
|
1934 input().
|
|
1935 If the optional {text} is present, this is used for the
|
|
1936 default reply, as if the user typed this.
|
|
1937 NOTE: This must not be used in a startup file, for the
|
|
1938 versions that only run in GUI mode (e.g., the Win32 GUI).
|
|
1939 Note: When input() is called from within a mapping it will
|
|
1940 consume remaining characters from that mapping, because a
|
|
1941 mapping is handled like the characters were typed.
|
|
1942 Use |inputsave()| before input() and |inputrestore()|
|
|
1943 after input() to avoid that. Another solution is to avoid
|
|
1944 that further characters follow in the mapping, e.g., by using
|
|
1945 |:execute| or |:normal|.
|
|
1946
|
|
1947 Example: >
|
|
1948 :if input("Coffee or beer? ") == "beer"
|
|
1949 : echo "Cheers!"
|
|
1950 :endif
|
|
1951 < Example with default text: >
|
|
1952 :let color = input("Color? ", "white")
|
|
1953 < Example with a mapping: >
|
|
1954 :nmap \x :call GetFoo()<CR>:exe "/" . Foo<CR>
|
|
1955 :function GetFoo()
|
|
1956 : call inputsave()
|
|
1957 : let g:Foo = input("enter search pattern: ")
|
|
1958 : call inputrestore()
|
|
1959 :endfunction
|
|
1960
|
|
1961 inputdialog({prompt} [, {text} [, {cancelreturn}]]) *inputdialog()*
|
|
1962 Like input(), but when the GUI is running and text dialogs are
|
|
1963 supported, a dialog window pops up to input the text.
|
|
1964 Example: >
|
|
1965 :let n = inputdialog("value for shiftwidth", &sw)
|
|
1966 :if n != ""
|
|
1967 : let &sw = n
|
|
1968 :endif
|
|
1969 < When the dialog is cancelled {cancelreturn} is returned. When
|
|
1970 omitted an empty string is returned.
|
|
1971 Hitting <Enter> works like pressing the OK button. Hitting
|
|
1972 <Esc> works like pressing the Cancel button.
|
|
1973
|
|
1974 inputrestore() *inputrestore()*
|
|
1975 Restore typeahead that was saved with a previous inputsave().
|
|
1976 Should be called the same number of times inputsave() is
|
|
1977 called. Calling it more often is harmless though.
|
|
1978 Returns 1 when there is nothing to restore, 0 otherwise.
|
|
1979
|
|
1980 inputsave() *inputsave()*
|
|
1981 Preserve typeahead (also from mappings) and clear it, so that
|
|
1982 a following prompt gets input from the user. Should be
|
|
1983 followed by a matching inputrestore() after the prompt. Can
|
|
1984 be used several times, in which case there must be just as
|
|
1985 many inputrestore() calls.
|
|
1986 Returns 1 when out of memory, 0 otherwise.
|
|
1987
|
|
1988 inputsecret({prompt} [, {text}]) *inputsecret()*
|
|
1989 This function acts much like the |input()| function with but
|
|
1990 two exceptions:
|
|
1991 a) the user's response will be displayed as a sequence of
|
|
1992 asterisks ("*") thereby keeping the entry secret, and
|
|
1993 b) the user's response will not be recorded on the input
|
|
1994 |history| stack.
|
|
1995 The result is a String, which is whatever the user actually
|
|
1996 typed on the command-line in response to the issued prompt.
|
|
1997
|
|
1998 isdirectory({directory}) *isdirectory()*
|
|
1999 The result is a Number, which is non-zero when a directory
|
|
2000 with the name {directory} exists. If {directory} doesn't
|
|
2001 exist, or isn't a directory, the result is FALSE. {directory}
|
|
2002 is any expression, which is used as a String.
|
|
2003
|
|
2004 *libcall()* *E364* *E368*
|
|
2005 libcall({libname}, {funcname}, {argument})
|
|
2006 Call function {funcname} in the run-time library {libname}
|
|
2007 with single argument {argument}.
|
|
2008 This is useful to call functions in a library that you
|
|
2009 especially made to be used with Vim. Since only one argument
|
|
2010 is possible, calling standard library functions is rather
|
|
2011 limited.
|
|
2012 The result is the String returned by the function. If the
|
|
2013 function returns NULL, this will appear as an empty string ""
|
|
2014 to Vim.
|
|
2015 If the function returns a number, use libcallnr()!
|
|
2016 If {argument} is a number, it is passed to the function as an
|
|
2017 int; if {argument} is a string, it is passed as a
|
|
2018 null-terminated string.
|
|
2019 This function will fail in |restricted-mode|.
|
|
2020
|
|
2021 libcall() allows you to write your own 'plug-in' extensions to
|
|
2022 Vim without having to recompile the program. It is NOT a
|
|
2023 means to call system functions! If you try to do so Vim will
|
|
2024 very probably crash.
|
|
2025
|
|
2026 For Win32, the functions you write must be placed in a DLL
|
|
2027 and use the normal C calling convention (NOT Pascal which is
|
|
2028 used in Windows System DLLs). The function must take exactly
|
|
2029 one parameter, either a character pointer or a long integer,
|
|
2030 and must return a character pointer or NULL. The character
|
|
2031 pointer returned must point to memory that will remain valid
|
|
2032 after the function has returned (e.g. in static data in the
|
|
2033 DLL). If it points to allocated memory, that memory will
|
|
2034 leak away. Using a static buffer in the function should work,
|
|
2035 it's then freed when the DLL is unloaded.
|
|
2036
|
|
2037 WARNING: If the function returns a non-valid pointer, Vim may
|
|
2038 crash! This also happens if the function returns a number,
|
|
2039 because Vim thinks it's a pointer.
|
|
2040 For Win32 systems, {libname} should be the filename of the DLL
|
|
2041 without the ".DLL" suffix. A full path is only required if
|
|
2042 the DLL is not in the usual places.
|
|
2043 For Unix: When compiling your own plugins, remember that the
|
|
2044 object code must be compiled as position-independent ('PIC').
|
|
2045 {only in Win32 on some Unix versions, when the |+libcall|
|
|
2046 feature is present}
|
|
2047 Examples: >
|
|
2048 :echo libcall("libc.so", "getenv", "HOME")
|
|
2049 :echo libcallnr("/usr/lib/libc.so", "getpid", "")
|
|
2050 <
|
|
2051 *libcallnr()*
|
|
2052 libcallnr({libname}, {funcname}, {argument})
|
|
2053 Just like libcall(), but used for a function that returns an
|
|
2054 int instead of a string.
|
|
2055 {only in Win32 on some Unix versions, when the |+libcall|
|
|
2056 feature is present}
|
|
2057 Example (not very useful...): >
|
|
2058 :call libcallnr("libc.so", "printf", "Hello World!\n")
|
|
2059 :call libcallnr("libc.so", "sleep", 10)
|
|
2060 <
|
|
2061 *line()*
|
|
2062 line({expr}) The result is a Number, which is the line number of the file
|
|
2063 position given with {expr}. The accepted positions are:
|
|
2064 . the cursor position
|
|
2065 $ the last line in the current buffer
|
|
2066 'x position of mark x (if the mark is not set, 0 is
|
|
2067 returned)
|
|
2068 Note that only marks in the current file can be used.
|
|
2069 Examples: >
|
|
2070 line(".") line number of the cursor
|
|
2071 line("'t") line number of mark t
|
|
2072 line("'" . marker) line number of mark marker
|
|
2073 < *last-position-jump*
|
|
2074 This autocommand jumps to the last known position in a file
|
|
2075 just after opening it, if the '" mark is set: >
|
|
2076 :au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal g'\"" | endif
|
9
|
2077
|
7
|
2078 line2byte({lnum}) *line2byte()*
|
|
2079 Return the byte count from the start of the buffer for line
|
|
2080 {lnum}. This includes the end-of-line character, depending on
|
|
2081 the 'fileformat' option for the current buffer. The first
|
|
2082 line returns 1.
|
|
2083 This can also be used to get the byte count for the line just
|
|
2084 below the last line: >
|
|
2085 line2byte(line("$") + 1)
|
|
2086 < This is the file size plus one.
|
|
2087 When {lnum} is invalid, or the |+byte_offset| feature has been
|
|
2088 disabled at compile time, -1 is returned.
|
|
2089 Also see |byte2line()|, |go| and |:goto|.
|
|
2090
|
|
2091 lispindent({lnum}) *lispindent()*
|
|
2092 Get the amount of indent for line {lnum} according the lisp
|
|
2093 indenting rules, as with 'lisp'.
|
|
2094 The indent is counted in spaces, the value of 'tabstop' is
|
|
2095 relevant. {lnum} is used just like in |getline()|.
|
|
2096 When {lnum} is invalid or Vim was not compiled the
|
|
2097 |+lispindent| feature, -1 is returned.
|
|
2098
|
|
2099 localtime() *localtime()*
|
|
2100 Return the current time, measured as seconds since 1st Jan
|
|
2101 1970. See also |strftime()| and |getftime()|.
|
|
2102
|
|
2103 maparg({name}[, {mode}]) *maparg()*
|
|
2104 Return the rhs of mapping {name} in mode {mode}. When there
|
|
2105 is no mapping for {name}, an empty String is returned.
|
|
2106 These characters can be used for {mode}:
|
|
2107 "n" Normal
|
|
2108 "v" Visual
|
|
2109 "o" Operator-pending
|
|
2110 "i" Insert
|
|
2111 "c" Cmd-line
|
|
2112 "l" langmap |language-mapping|
|
|
2113 "" Normal, Visual and Operator-pending
|
|
2114 When {mode} is omitted, the modes from "" are used.
|
|
2115 The {name} can have special key names, like in the ":map"
|
|
2116 command. The returned String has special characters
|
|
2117 translated like in the output of the ":map" command listing.
|
|
2118 The mappings local to the current buffer are checked first,
|
|
2119 then the global mappings.
|
|
2120
|
|
2121 mapcheck({name}[, {mode}]) *mapcheck()*
|
|
2122 Check if there is a mapping that matches with {name} in mode
|
|
2123 {mode}. See |maparg()| for {mode} and special names in
|
|
2124 {name}.
|
|
2125 A match happens with a mapping that starts with {name} and
|
|
2126 with a mapping which is equal to the start of {name}.
|
|
2127
|
|
2128 matches mapping "a" "ab" "abc" ~
|
|
2129 mapcheck("a") yes yes yes
|
|
2130 mapcheck("abc") yes yes yes
|
|
2131 mapcheck("ax") yes no no
|
|
2132 mapcheck("b") no no no
|
|
2133
|
|
2134 The difference with maparg() is that mapcheck() finds a
|
|
2135 mapping that matches with {name}, while maparg() only finds a
|
|
2136 mapping for {name} exactly.
|
|
2137 When there is no mapping that starts with {name}, an empty
|
|
2138 String is returned. If there is one, the rhs of that mapping
|
|
2139 is returned. If there are several mappings that start with
|
|
2140 {name}, the rhs of one of them is returned.
|
|
2141 The mappings local to the current buffer are checked first,
|
|
2142 then the global mappings.
|
|
2143 This function can be used to check if a mapping can be added
|
|
2144 without being ambiguous. Example: >
|
|
2145 :if mapcheck("_vv") == ""
|
|
2146 : map _vv :set guifont=7x13<CR>
|
|
2147 :endif
|
|
2148 < This avoids adding the "_vv" mapping when there already is a
|
|
2149 mapping for "_v" or for "_vvv".
|
|
2150
|
19
|
2151 match({expr}, {pat}[, {start}[, {count}]]) *match()*
|
7
|
2152 The result is a Number, which gives the index (byte offset) in
|
19
|
2153 {expr} where {pat} matches.
|
|
2154 A match at the first character returns zero.
|
|
2155 If there is no match -1 is returned.
|
|
2156 Example: >
|
7
|
2157 :echo match("testing", "ing")
|
|
2158 < results in "4".
|
|
2159 See |string-match| for how {pat} is used.
|
19
|
2160 When {count} is given use the {count}'th match. When a match
|
|
2161 is found the search for the next one starts on character
|
|
2162 further. Thus this example results in 1: >
|
|
2163 echo match("testing", "..", 0, 2)
|
|
2164 < If {start} is given, the search starts from index {start}.
|
7
|
2165 The result, however, is still the index counted from the
|
|
2166 first character. Example: >
|
|
2167 :echo match("testing", "ing", 2)
|
|
2168 < result is again "4". >
|
|
2169 :echo match("testing", "ing", 4)
|
|
2170 < result is again "4". >
|
|
2171 :echo match("testing", "t", 2)
|
|
2172 < result is "3".
|
|
2173 If {start} < 0, it will be set to 0.
|
|
2174 If {start} > strlen({expr}) -1 is returned.
|
|
2175 See |pattern| for the patterns that are accepted.
|
|
2176 The 'ignorecase' option is used to set the ignore-caseness of
|
|
2177 the pattern. 'smartcase' is NOT used. The matching is always
|
|
2178 done like 'magic' is set and 'cpoptions' is empty.
|
|
2179
|
19
|
2180 matchend({expr}, {pat}[, {start}[, {count}]]) *matchend()*
|
7
|
2181 Same as match(), but return the index of first character after
|
|
2182 the match. Example: >
|
|
2183 :echo matchend("testing", "ing")
|
|
2184 < results in "7".
|
|
2185 The {start}, if given, has the same meaning as for match(). >
|
|
2186 :echo matchend("testing", "ing", 2)
|
|
2187 < results in "7". >
|
|
2188 :echo matchend("testing", "ing", 5)
|
|
2189 < result is "-1".
|
|
2190
|
19
|
2191 matchstr({expr}, {pat}[, {start}[, {count}]]) *matchstr()*
|
7
|
2192 Same as match(), but return the matched string. Example: >
|
|
2193 :echo matchstr("testing", "ing")
|
|
2194 < results in "ing".
|
|
2195 When there is no match "" is returned.
|
|
2196 The {start}, if given, has the same meaning as for match(). >
|
|
2197 :echo matchstr("testing", "ing", 2)
|
|
2198 < results in "ing". >
|
|
2199 :echo matchstr("testing", "ing", 5)
|
|
2200 < result is "".
|
|
2201
|
|
2202 *mode()*
|
|
2203 mode() Return a string that indicates the current mode:
|
|
2204 n Normal
|
|
2205 v Visual by character
|
|
2206 V Visual by line
|
|
2207 CTRL-V Visual blockwise
|
|
2208 s Select by character
|
|
2209 S Select by line
|
|
2210 CTRL-S Select blockwise
|
|
2211 i Insert
|
|
2212 R Replace
|
|
2213 c Command-line
|
|
2214 r Hit-enter prompt
|
|
2215 This is useful in the 'statusline' option. In most other
|
|
2216 places it always returns "c" or "n".
|
|
2217
|
|
2218 nextnonblank({lnum}) *nextnonblank()*
|
|
2219 Return the line number of the first line at or below {lnum}
|
|
2220 that is not blank. Example: >
|
|
2221 if getline(nextnonblank(1)) =~ "Java"
|
|
2222 < When {lnum} is invalid or there is no non-blank line at or
|
|
2223 below it, zero is returned.
|
|
2224 See also |prevnonblank()|.
|
|
2225
|
|
2226 nr2char({expr}) *nr2char()*
|
|
2227 Return a string with a single character, which has the number
|
|
2228 value {expr}. Examples: >
|
|
2229 nr2char(64) returns "@"
|
|
2230 nr2char(32) returns " "
|
|
2231 < The current 'encoding' is used. Example for "utf-8": >
|
|
2232 nr2char(300) returns I with bow character
|
|
2233 < Note that a NUL character in the file is specified with
|
|
2234 nr2char(10), because NULs are represented with newline
|
|
2235 characters. nr2char(0) is a real NUL and terminates the
|
|
2236 string, thus isn't very useful.
|
|
2237
|
|
2238 prevnonblank({lnum}) *prevnonblank()*
|
|
2239 Return the line number of the first line at or above {lnum}
|
|
2240 that is not blank. Example: >
|
|
2241 let ind = indent(prevnonblank(v:lnum - 1))
|
|
2242 < When {lnum} is invalid or there is no non-blank line at or
|
|
2243 above it, zero is returned.
|
|
2244 Also see |nextnonblank()|.
|
|
2245
|
|
2246 *remote_expr()* *E449*
|
|
2247 remote_expr({server}, {string} [, {idvar}])
|
|
2248 Send the {string} to {server}. The string is sent as an
|
|
2249 expression and the result is returned after evaluation.
|
|
2250 If {idvar} is present, it is taken as the name of a
|
|
2251 variable and a {serverid} for later use with
|
|
2252 remote_read() is stored there.
|
|
2253 See also |clientserver| |RemoteReply|.
|
|
2254 This function is not available in the |sandbox|.
|
|
2255 {only available when compiled with the |+clientserver| feature}
|
|
2256 Note: Any errors will cause a local error message to be issued
|
|
2257 and the result will be the empty string.
|
|
2258 Examples: >
|
|
2259 :echo remote_expr("gvim", "2+2")
|
|
2260 :echo remote_expr("gvim1", "b:current_syntax")
|
|
2261 <
|
|
2262
|
|
2263 remote_foreground({server}) *remote_foreground()*
|
|
2264 Move the Vim server with the name {server} to the foreground.
|
|
2265 This works like: >
|
|
2266 remote_expr({server}, "foreground()")
|
|
2267 < Except that on Win32 systems the client does the work, to work
|
|
2268 around the problem that the OS doesn't always allow the server
|
|
2269 to bring itself to the foreground.
|
|
2270 This function is not available in the |sandbox|.
|
|
2271 {only in the Win32, Athena, Motif and GTK GUI versions and the
|
|
2272 Win32 console version}
|
|
2273
|
|
2274
|
|
2275 remote_peek({serverid} [, {retvar}]) *remote_peek()*
|
|
2276 Returns a positive number if there are available strings
|
|
2277 from {serverid}. Copies any reply string into the variable
|
|
2278 {retvar} if specified. {retvar} must be a string with the
|
|
2279 name of a variable.
|
|
2280 Returns zero if none are available.
|
|
2281 Returns -1 if something is wrong.
|
|
2282 See also |clientserver|.
|
|
2283 This function is not available in the |sandbox|.
|
|
2284 {only available when compiled with the |+clientserver| feature}
|
|
2285 Examples: >
|
|
2286 :let repl = ""
|
|
2287 :echo "PEEK: ".remote_peek(id, "repl").": ".repl
|
|
2288
|
|
2289 remote_read({serverid}) *remote_read()*
|
|
2290 Return the oldest available reply from {serverid} and consume
|
|
2291 it. It blocks until a reply is available.
|
|
2292 See also |clientserver|.
|
|
2293 This function is not available in the |sandbox|.
|
|
2294 {only available when compiled with the |+clientserver| feature}
|
|
2295 Example: >
|
|
2296 :echo remote_read(id)
|
|
2297 <
|
|
2298 *remote_send()* *E241*
|
|
2299 remote_send({server}, {string} [, {idvar}])
|
22
|
2300 Send the {string} to {server}. The string is sent as input
|
|
2301 keys and the function returns immediately. At the Vim server
|
|
2302 the keys are not mapped |:map|.
|
7
|
2303 If {idvar} is present, it is taken as the name of a
|
|
2304 variable and a {serverid} for later use with
|
|
2305 remote_read() is stored there.
|
|
2306 See also |clientserver| |RemoteReply|.
|
|
2307 This function is not available in the |sandbox|.
|
|
2308 {only available when compiled with the |+clientserver| feature}
|
|
2309 Note: Any errors will be reported in the server and may mess
|
|
2310 up the display.
|
|
2311 Examples: >
|
|
2312 :echo remote_send("gvim", ":DropAndReply ".file, "serverid").
|
|
2313 \ remote_read(serverid)
|
|
2314
|
|
2315 :autocmd NONE RemoteReply *
|
|
2316 \ echo remote_read(expand("<amatch>"))
|
|
2317 :echo remote_send("gvim", ":sleep 10 | echo ".
|
|
2318 \ 'server2client(expand("<client>"), "HELLO")<CR>')
|
|
2319
|
|
2320
|
|
2321 rename({from}, {to}) *rename()*
|
|
2322 Rename the file by the name {from} to the name {to}. This
|
|
2323 should also work to move files across file systems. The
|
|
2324 result is a Number, which is 0 if the file was renamed
|
|
2325 successfully, and non-zero when the renaming failed.
|
|
2326 This function is not available in the |sandbox|.
|
|
2327
|
18
|
2328 repeat({expr}, {count}) *repeat()*
|
|
2329 Repeat {expr} {count} times and return the concatenated
|
|
2330 result. Example: >
|
|
2331 :let seperator = repeat('-', 80)
|
|
2332 < When {count} is zero or negative the result is empty.
|
|
2333
|
7
|
2334 resolve({filename}) *resolve()* *E655*
|
|
2335 On MS-Windows, when {filename} is a shortcut (a .lnk file),
|
|
2336 returns the path the shortcut points to in a simplified form.
|
|
2337 On Unix, repeat resolving symbolic links in all path
|
|
2338 components of {filename} and return the simplified result.
|
|
2339 To cope with link cycles, resolving of symbolic links is
|
|
2340 stopped after 100 iterations.
|
|
2341 On other systems, return the simplified {filename}.
|
|
2342 The simplification step is done as by |simplify()|.
|
|
2343 resolve() keeps a leading path component specifying the
|
|
2344 current directory (provided the result is still a relative
|
|
2345 path name) and also keeps a trailing path separator.
|
|
2346
|
|
2347 search({pattern} [, {flags}]) *search()*
|
|
2348 Search for regexp pattern {pattern}. The search starts at the
|
|
2349 cursor position.
|
|
2350 {flags} is a String, which can contain these character flags:
|
|
2351 'b' search backward instead of forward
|
20
|
2352 'n' do Not move the cursor
|
7
|
2353 'w' wrap around the end of the file
|
|
2354 'W' don't wrap around the end of the file
|
|
2355 If neither 'w' or 'W' is given, the 'wrapscan' option applies.
|
|
2356
|
20
|
2357 When a match has been found its line number is returned.
|
|
2358 The cursor will be positioned at the match, unless the 'n'
|
|
2359 flag is used).
|
|
2360 If there is no match a 0 is returned and the cursor doesn't
|
|
2361 move. No error message is given.
|
7
|
2362
|
|
2363 Example (goes over all files in the argument list): >
|
|
2364 :let n = 1
|
|
2365 :while n <= argc() " loop over all files in arglist
|
|
2366 : exe "argument " . n
|
|
2367 : " start at the last char in the file and wrap for the
|
|
2368 : " first search to find match at start of file
|
|
2369 : normal G$
|
|
2370 : let flags = "w"
|
|
2371 : while search("foo", flags) > 0
|
|
2372 : s/foo/bar/g
|
|
2373 : let flags = "W"
|
|
2374 : endwhile
|
|
2375 : update " write the file if modified
|
|
2376 : let n = n + 1
|
|
2377 :endwhile
|
|
2378 <
|
|
2379 *searchpair()*
|
|
2380 searchpair({start}, {middle}, {end} [, {flags} [, {skip}]])
|
|
2381 Search for the match of a nested start-end pair. This can be
|
|
2382 used to find the "endif" that matches an "if", while other
|
|
2383 if/endif pairs in between are ignored.
|
|
2384 The search starts at the cursor. If a match is found, the
|
|
2385 cursor is positioned at it and the line number is returned.
|
|
2386 If no match is found 0 or -1 is returned and the cursor
|
|
2387 doesn't move. No error message is given.
|
|
2388
|
|
2389 {start}, {middle} and {end} are patterns, see |pattern|. They
|
|
2390 must not contain \( \) pairs. Use of \%( \) is allowed. When
|
|
2391 {middle} is not empty, it is found when searching from either
|
|
2392 direction, but only when not in a nested start-end pair. A
|
|
2393 typical use is: >
|
|
2394 searchpair('\<if\>', '\<else\>', '\<endif\>')
|
|
2395 < By leaving {middle} empty the "else" is skipped.
|
|
2396
|
|
2397 {flags} are used like with |search()|. Additionally:
|
|
2398 'n' do Not move the cursor
|
|
2399 'r' Repeat until no more matches found; will find the
|
|
2400 outer pair
|
|
2401 'm' return number of Matches instead of line number with
|
|
2402 the match; will only be > 1 when 'r' is used.
|
|
2403
|
|
2404 When a match for {start}, {middle} or {end} is found, the
|
|
2405 {skip} expression is evaluated with the cursor positioned on
|
|
2406 the start of the match. It should return non-zero if this
|
|
2407 match is to be skipped. E.g., because it is inside a comment
|
|
2408 or a string.
|
|
2409 When {skip} is omitted or empty, every match is accepted.
|
|
2410 When evaluating {skip} causes an error the search is aborted
|
|
2411 and -1 returned.
|
|
2412
|
|
2413 The value of 'ignorecase' is used. 'magic' is ignored, the
|
|
2414 patterns are used like it's on.
|
|
2415
|
|
2416 The search starts exactly at the cursor. A match with
|
|
2417 {start}, {middle} or {end} at the next character, in the
|
|
2418 direction of searching, is the first one found. Example: >
|
|
2419 if 1
|
|
2420 if 2
|
|
2421 endif 2
|
|
2422 endif 1
|
|
2423 < When starting at the "if 2", with the cursor on the "i", and
|
|
2424 searching forwards, the "endif 2" is found. When starting on
|
|
2425 the character just before the "if 2", the "endif 1" will be
|
|
2426 found. That's because the "if 2" will be found first, and
|
|
2427 then this is considered to be a nested if/endif from "if 2" to
|
|
2428 "endif 2".
|
|
2429 When searching backwards and {end} is more than one character,
|
|
2430 it may be useful to put "\zs" at the end of the pattern, so
|
|
2431 that when the cursor is inside a match with the end it finds
|
|
2432 the matching start.
|
|
2433
|
|
2434 Example, to find the "endif" command in a Vim script: >
|
|
2435
|
|
2436 :echo searchpair('\<if\>', '\<el\%[seif]\>', '\<en\%[dif]\>', 'W',
|
|
2437 \ 'getline(".") =~ "^\\s*\""')
|
|
2438
|
|
2439 < The cursor must be at or after the "if" for which a match is
|
|
2440 to be found. Note that single-quote strings are used to avoid
|
|
2441 having to double the backslashes. The skip expression only
|
|
2442 catches comments at the start of a line, not after a command.
|
|
2443 Also, a word "en" or "if" halfway a line is considered a
|
|
2444 match.
|
|
2445 Another example, to search for the matching "{" of a "}": >
|
|
2446
|
|
2447 :echo searchpair('{', '', '}', 'bW')
|
|
2448
|
|
2449 < This works when the cursor is at or before the "}" for which a
|
|
2450 match is to be found. To reject matches that syntax
|
|
2451 highlighting recognized as strings: >
|
|
2452
|
|
2453 :echo searchpair('{', '', '}', 'bW',
|
|
2454 \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"')
|
|
2455 <
|
|
2456 server2client( {clientid}, {string}) *server2client()*
|
|
2457 Send a reply string to {clientid}. The most recent {clientid}
|
|
2458 that sent a string can be retrieved with expand("<client>").
|
|
2459 {only available when compiled with the |+clientserver| feature}
|
|
2460 Note:
|
|
2461 This id has to be stored before the next command can be
|
|
2462 received. Ie. before returning from the received command and
|
|
2463 before calling any commands that waits for input.
|
|
2464 See also |clientserver|.
|
|
2465 Example: >
|
|
2466 :echo server2client(expand("<client>"), "HELLO")
|
|
2467 <
|
|
2468 serverlist() *serverlist()*
|
|
2469 Return a list of available server names, one per line.
|
|
2470 When there are no servers or the information is not available
|
|
2471 an empty string is returned. See also |clientserver|.
|
|
2472 {only available when compiled with the |+clientserver| feature}
|
|
2473 Example: >
|
|
2474 :echo serverlist()
|
|
2475 <
|
|
2476 setbufvar({expr}, {varname}, {val}) *setbufvar()*
|
|
2477 Set option or local variable {varname} in buffer {expr} to
|
|
2478 {val}.
|
|
2479 This also works for a global or local window option, but it
|
|
2480 doesn't work for a global or local window variable.
|
|
2481 For a local window option the global value is unchanged.
|
|
2482 For the use of {expr}, see |bufname()| above.
|
|
2483 Note that the variable name without "b:" must be used.
|
|
2484 Examples: >
|
|
2485 :call setbufvar(1, "&mod", 1)
|
|
2486 :call setbufvar("todo", "myvar", "foobar")
|
|
2487 < This function is not available in the |sandbox|.
|
|
2488
|
|
2489 setcmdpos({pos}) *setcmdpos()*
|
|
2490 Set the cursor position in the command line to byte position
|
|
2491 {pos}. The first position is 1.
|
|
2492 Use |getcmdpos()| to obtain the current position.
|
|
2493 Only works while editing the command line, thus you must use
|
|
2494 |c_CTRL-\_e| or |c_CTRL-R_=|. The position is set after the
|
|
2495 command line is set to the expression.
|
|
2496 When the number is too big the cursor is put at the end of the
|
|
2497 line. A number smaller than one has undefined results.
|
|
2498 Returns 0 when successful, 1 when not editing the command
|
|
2499 line.
|
|
2500
|
|
2501 setline({lnum}, {line}) *setline()*
|
|
2502 Set line {lnum} of the current buffer to {line}. If this
|
|
2503 succeeds, 0 is returned. If this fails (most likely because
|
|
2504 {lnum} is invalid) 1 is returned. Example: >
|
|
2505 :call setline(5, strftime("%c"))
|
|
2506 < Note: The '[ and '] marks are not set.
|
|
2507
|
|
2508 *setreg()*
|
|
2509 setreg({regname}, {value} [,{options}])
|
|
2510 Set the register {regname} to {value}.
|
|
2511 If {options} contains "a" or {regname} is upper case,
|
|
2512 then the value is appended.
|
|
2513 {options} can also contains a register type specification:
|
|
2514 "c" or "v" |characterwise| mode
|
|
2515 "l" or "V" |linewise| mode
|
|
2516 "b" or "<CTRL-V>" |blockwise-visual| mode
|
|
2517 If a number immediately follows "b" or "<CTRL-V>" then this is
|
|
2518 used as the width of the selection - if it is not specified
|
|
2519 then the width of the block is set to the number of characters
|
|
2520 in the longest line (counting a <TAB> as 1 character).
|
|
2521
|
|
2522 If {options} contains no register settings, then the default
|
|
2523 is to use character mode unless {value} ends in a <NL>.
|
|
2524 Setting the '=' register is not possible.
|
|
2525 Returns zero for success, non-zero for failure.
|
|
2526
|
|
2527 Examples: >
|
|
2528 :call setreg(v:register, @*)
|
|
2529 :call setreg('*', @%, 'ac')
|
|
2530 :call setreg('a', "1\n2\n3", 'b5')
|
|
2531
|
|
2532 < This example shows using the functions to save and restore a
|
|
2533 register. >
|
|
2534 :let var_a = getreg('a')
|
|
2535 :let var_amode = getregtype('a')
|
|
2536 ....
|
|
2537 :call setreg('a', var_a, var_amode)
|
|
2538
|
|
2539 < You can also change the type of a register by appending
|
|
2540 nothing: >
|
|
2541 :call setreg('a', '', 'al')
|
|
2542
|
|
2543 setwinvar({nr}, {varname}, {val}) *setwinvar()*
|
|
2544 Set option or local variable {varname} in window {nr} to
|
|
2545 {val}.
|
|
2546 This also works for a global or local buffer option, but it
|
|
2547 doesn't work for a global or local buffer variable.
|
|
2548 For a local buffer option the global value is unchanged.
|
|
2549 Note that the variable name without "w:" must be used.
|
|
2550 Examples: >
|
|
2551 :call setwinvar(1, "&list", 0)
|
|
2552 :call setwinvar(2, "myvar", "foobar")
|
|
2553 < This function is not available in the |sandbox|.
|
|
2554
|
|
2555 simplify({filename}) *simplify()*
|
|
2556 Simplify the file name as much as possible without changing
|
|
2557 the meaning. Shortcuts (on MS-Windows) or symbolic links (on
|
|
2558 Unix) are not resolved. If the first path component in
|
|
2559 {filename} designates the current directory, this will be
|
|
2560 valid for the result as well. A trailing path separator is
|
|
2561 not removed either.
|
|
2562 Example: >
|
|
2563 simplify("./dir/.././/file/") == "./file/"
|
|
2564 < Note: The combination "dir/.." is only removed if "dir" is
|
|
2565 a searchable directory or does not exist. On Unix, it is also
|
|
2566 removed when "dir" is a symbolic link within the same
|
|
2567 directory. In order to resolve all the involved symbolic
|
|
2568 links before simplifying the path name, use |resolve()|.
|
|
2569
|
|
2570 strftime({format} [, {time}]) *strftime()*
|
|
2571 The result is a String, which is a formatted date and time, as
|
|
2572 specified by the {format} string. The given {time} is used,
|
|
2573 or the current time if no time is given. The accepted
|
|
2574 {format} depends on your system, thus this is not portable!
|
|
2575 See the manual page of the C function strftime() for the
|
|
2576 format. The maximum length of the result is 80 characters.
|
|
2577 See also |localtime()| and |getftime()|.
|
|
2578 The language can be changed with the |:language| command.
|
|
2579 Examples: >
|
|
2580 :echo strftime("%c") Sun Apr 27 11:49:23 1997
|
|
2581 :echo strftime("%Y %b %d %X") 1997 Apr 27 11:53:25
|
|
2582 :echo strftime("%y%m%d %T") 970427 11:53:55
|
|
2583 :echo strftime("%H:%M") 11:55
|
|
2584 :echo strftime("%c", getftime("file.c"))
|
|
2585 Show mod time of file.c.
|
|
2586 <
|
|
2587 stridx({haystack}, {needle}) *stridx()*
|
|
2588 The result is a Number, which gives the index in {haystack} of
|
|
2589 the first occurrence of the String {needle} in the String
|
|
2590 {haystack}. The search is done case-sensitive. For advanced
|
|
2591 searches use |match()|.
|
|
2592 If the {needle} does not occur in {haystack} it returns -1.
|
|
2593 See also |strridx()|. Examples: >
|
|
2594 :echo stridx("An Example", "Example") 3
|
|
2595 :echo stridx("Starting point", "Start") 0
|
|
2596 :echo stridx("Starting point", "start") -1
|
|
2597 <
|
|
2598 *strlen()*
|
|
2599 strlen({expr}) The result is a Number, which is the length of the String
|
|
2600 {expr} in bytes. If you want to count the number of
|
|
2601 multi-byte characters use something like this: >
|
|
2602
|
|
2603 :let len = strlen(substitute(str, ".", "x", "g"))
|
|
2604
|
|
2605 < Composing characters are not counted.
|
|
2606
|
|
2607 strpart({src}, {start}[, {len}]) *strpart()*
|
|
2608 The result is a String, which is part of {src}, starting from
|
|
2609 byte {start}, with the length {len}.
|
|
2610 When non-existing bytes are included, this doesn't result in
|
|
2611 an error, the bytes are simply omitted.
|
|
2612 If {len} is missing, the copy continues from {start} till the
|
|
2613 end of the {src}. >
|
|
2614 strpart("abcdefg", 3, 2) == "de"
|
|
2615 strpart("abcdefg", -2, 4) == "ab"
|
|
2616 strpart("abcdefg", 5, 4) == "fg"
|
|
2617 strpart("abcdefg", 3) == "defg"
|
|
2618 < Note: To get the first character, {start} must be 0. For
|
|
2619 example, to get three bytes under and after the cursor: >
|
|
2620 strpart(getline(line(".")), col(".") - 1, 3)
|
|
2621 <
|
|
2622 strridx({haystack}, {needle}) *strridx()*
|
|
2623 The result is a Number, which gives the index in {haystack} of
|
|
2624 the last occurrence of the String {needle} in the String
|
|
2625 {haystack}. The search is done case-sensitive. For advanced
|
|
2626 searches use |match()|.
|
|
2627 If the {needle} does not occur in {haystack} it returns -1.
|
22
|
2628 If the {needle} is empty the length of {haystack} is returned.
|
7
|
2629 See also |stridx()|. Examples: >
|
|
2630 :echo strridx("an angry armadillo", "an") 3
|
|
2631 <
|
|
2632 strtrans({expr}) *strtrans()*
|
|
2633 The result is a String, which is {expr} with all unprintable
|
|
2634 characters translated into printable characters |'isprint'|.
|
|
2635 Like they are shown in a window. Example: >
|
|
2636 echo strtrans(@a)
|
|
2637 < This displays a newline in register a as "^@" instead of
|
|
2638 starting a new line.
|
|
2639
|
|
2640 submatch({nr}) *submatch()*
|
|
2641 Only for an expression in a |:substitute| command. Returns
|
|
2642 the {nr}'th submatch of the matched text. When {nr} is 0
|
|
2643 the whole matched text is returned.
|
|
2644 Example: >
|
|
2645 :s/\d\+/\=submatch(0) + 1/
|
|
2646 < This finds the first number in the line and adds one to it.
|
|
2647 A line break is included as a newline character.
|
|
2648
|
|
2649 substitute({expr}, {pat}, {sub}, {flags}) *substitute()*
|
|
2650 The result is a String, which is a copy of {expr}, in which
|
|
2651 the first match of {pat} is replaced with {sub}. This works
|
|
2652 like the ":substitute" command (without any flags). But the
|
|
2653 matching with {pat} is always done like the 'magic' option is
|
|
2654 set and 'cpoptions' is empty (to make scripts portable).
|
|
2655 See |string-match| for how {pat} is used.
|
|
2656 And a "~" in {sub} is not replaced with the previous {sub}.
|
|
2657 Note that some codes in {sub} have a special meaning
|
|
2658 |sub-replace-special|. For example, to replace something with
|
|
2659 "\n" (two characters), use "\\\\n" or '\\n'.
|
|
2660 When {pat} does not match in {expr}, {expr} is returned
|
|
2661 unmodified.
|
|
2662 When {flags} is "g", all matches of {pat} in {expr} are
|
|
2663 replaced. Otherwise {flags} should be "".
|
|
2664 Example: >
|
|
2665 :let &path = substitute(&path, ",\\=[^,]*$", "", "")
|
|
2666 < This removes the last component of the 'path' option. >
|
|
2667 :echo substitute("testing", ".*", "\\U\\0", "")
|
|
2668 < results in "TESTING".
|
|
2669
|
32
|
2670 synID({lnum}, {col}, {trans}) *synID()*
|
7
|
2671 The result is a Number, which is the syntax ID at the position
|
32
|
2672 {lnum} and {col} in the current window.
|
7
|
2673 The syntax ID can be used with |synIDattr()| and
|
|
2674 |synIDtrans()| to obtain syntax information about text.
|
32
|
2675 {col} is 1 for the leftmost column, {lnum} is 1 for the first
|
7
|
2676 line.
|
|
2677 When {trans} is non-zero, transparent items are reduced to the
|
|
2678 item that they reveal. This is useful when wanting to know
|
|
2679 the effective color. When {trans} is zero, the transparent
|
|
2680 item is returned. This is useful when wanting to know which
|
|
2681 syntax item is effective (e.g. inside parens).
|
|
2682 Warning: This function can be very slow. Best speed is
|
|
2683 obtained by going through the file in forward direction.
|
|
2684
|
|
2685 Example (echoes the name of the syntax item under the cursor): >
|
|
2686 :echo synIDattr(synID(line("."), col("."), 1), "name")
|
|
2687 <
|
|
2688 synIDattr({synID}, {what} [, {mode}]) *synIDattr()*
|
|
2689 The result is a String, which is the {what} attribute of
|
|
2690 syntax ID {synID}. This can be used to obtain information
|
|
2691 about a syntax item.
|
|
2692 {mode} can be "gui", "cterm" or "term", to get the attributes
|
|
2693 for that mode. When {mode} is omitted, or an invalid value is
|
|
2694 used, the attributes for the currently active highlighting are
|
|
2695 used (GUI, cterm or term).
|
|
2696 Use synIDtrans() to follow linked highlight groups.
|
|
2697 {what} result
|
|
2698 "name" the name of the syntax item
|
|
2699 "fg" foreground color (GUI: color name used to set
|
|
2700 the color, cterm: color number as a string,
|
|
2701 term: empty string)
|
|
2702 "bg" background color (like "fg")
|
|
2703 "fg#" like "fg", but for the GUI and the GUI is
|
|
2704 running the name in "#RRGGBB" form
|
|
2705 "bg#" like "fg#" for "bg"
|
|
2706 "bold" "1" if bold
|
|
2707 "italic" "1" if italic
|
|
2708 "reverse" "1" if reverse
|
|
2709 "inverse" "1" if inverse (= reverse)
|
|
2710 "underline" "1" if underlined
|
|
2711
|
|
2712 Example (echoes the color of the syntax item under the
|
|
2713 cursor): >
|
|
2714 :echo synIDattr(synIDtrans(synID(line("."), col("."), 1)), "fg")
|
|
2715 <
|
|
2716 synIDtrans({synID}) *synIDtrans()*
|
|
2717 The result is a Number, which is the translated syntax ID of
|
|
2718 {synID}. This is the syntax group ID of what is being used to
|
|
2719 highlight the character. Highlight links given with
|
|
2720 ":highlight link" are followed.
|
|
2721
|
24
|
2722 system({expr} [, {input}]) *system()* *E677*
|
|
2723 Get the output of the shell command {expr}.
|
|
2724 When {input} is given, this string is written to a file and
|
|
2725 passed as stdin to the command. The string is written as-is,
|
|
2726 you need to take care of using the correct line separators
|
|
2727 yourself.
|
|
2728 Note: newlines in {expr} may cause the command to fail. The
|
|
2729 characters in 'shellquote' and 'shellxquote' may also cause
|
|
2730 trouble.
|
7
|
2731 This is not to be used for interactive commands.
|
|
2732 The result is a String. Example: >
|
|
2733
|
|
2734 :let files = system("ls")
|
|
2735
|
|
2736 < To make the result more system-independent, the shell output
|
|
2737 is filtered to replace <CR> with <NL> for Macintosh, and
|
|
2738 <CR><NL> with <NL> for DOS-like systems.
|
|
2739 The command executed is constructed using several options:
|
|
2740 'shell' 'shellcmdflag' 'shellxquote' {expr} 'shellredir' {tmp} 'shellxquote'
|
|
2741 ({tmp} is an automatically generated file name).
|
|
2742 For Unix and OS/2 braces are put around {expr} to allow for
|
|
2743 concatenated commands.
|
|
2744
|
|
2745 The resulting error code can be found in |v:shell_error|.
|
|
2746 This function will fail in |restricted-mode|.
|
|
2747 Unlike ":!cmd" there is no automatic check for changed files.
|
|
2748 Use |:checktime| to force a check.
|
|
2749
|
|
2750 tempname() *tempname()* *temp-file-name*
|
|
2751 The result is a String, which is the name of a file that
|
|
2752 doesn't exist. It can be used for a temporary file. The name
|
|
2753 is different for at least 26 consecutive calls. Example: >
|
|
2754 :let tmpfile = tempname()
|
|
2755 :exe "redir > " . tmpfile
|
|
2756 < For Unix, the file will be in a private directory (only
|
|
2757 accessible by the current user) to avoid security problems
|
|
2758 (e.g., a symlink attack or other people reading your file).
|
|
2759 When Vim exits the directory and all files in it are deleted.
|
|
2760 For MS-Windows forward slashes are used when the 'shellslash'
|
|
2761 option is set or when 'shellcmdflag' starts with '-'.
|
|
2762
|
|
2763 tolower({expr}) *tolower()*
|
|
2764 The result is a copy of the String given, with all uppercase
|
|
2765 characters turned into lowercase (just like applying |gu| to
|
|
2766 the string).
|
|
2767
|
|
2768 toupper({expr}) *toupper()*
|
|
2769 The result is a copy of the String given, with all lowercase
|
|
2770 characters turned into uppercase (just like applying |gU| to
|
|
2771 the string).
|
|
2772
|
15
|
2773 tr({src}, {fromstr}, {tostr}) *tr()*
|
|
2774 The result is a copy of the {src} string with all characters
|
|
2775 which appear in {fromstr} replaced by the character in that
|
|
2776 position in the {tostr} string. Thus the first character in
|
|
2777 {fromstr} is translated into the first character in {tostr}
|
|
2778 and so on. Exactly like the unix "tr" command.
|
|
2779 This code also deals with multibyte characters properly.
|
|
2780
|
|
2781 Examples: >
|
|
2782 echo tr("hello there", "ht", "HT")
|
|
2783 < returns "Hello THere" >
|
|
2784 echo tr("<blob>", "<>", "{}")
|
|
2785 < returns "{blob}"
|
|
2786
|
7
|
2787 type({expr}) *type()*
|
|
2788 The result is a Number:
|
|
2789 0 if {expr} has the type Number
|
|
2790 1 if {expr} has the type String
|
|
2791
|
|
2792 virtcol({expr}) *virtcol()*
|
|
2793 The result is a Number, which is the screen column of the file
|
|
2794 position given with {expr}. That is, the last screen position
|
|
2795 occupied by the character at that position, when the screen
|
|
2796 would be of unlimited width. When there is a <Tab> at the
|
|
2797 position, the returned Number will be the column at the end of
|
|
2798 the <Tab>. For example, for a <Tab> in column 1, with 'ts'
|
|
2799 set to 8, it returns 8.
|
|
2800 For the byte position use |col()|.
|
|
2801 When Virtual editing is active in the current mode, a position
|
|
2802 beyond the end of the line can be returned. |'virtualedit'|
|
|
2803 The accepted positions are:
|
|
2804 . the cursor position
|
|
2805 $ the end of the cursor line (the result is the
|
|
2806 number of displayed characters in the cursor line
|
|
2807 plus one)
|
|
2808 'x position of mark x (if the mark is not set, 0 is
|
|
2809 returned)
|
|
2810 Note that only marks in the current file can be used.
|
|
2811 Examples: >
|
|
2812 virtcol(".") with text "foo^Lbar", with cursor on the "^L", returns 5
|
|
2813 virtcol("$") with text "foo^Lbar", returns 9
|
|
2814 virtcol("'t") with text " there", with 't at 'h', returns 6
|
|
2815 < The first column is 1. 0 is returned for an error.
|
|
2816
|
|
2817 visualmode([expr]) *visualmode()*
|
|
2818 The result is a String, which describes the last Visual mode
|
|
2819 used. Initially it returns an empty string, but once Visual
|
|
2820 mode has been used, it returns "v", "V", or "<CTRL-V>" (a
|
|
2821 single CTRL-V character) for character-wise, line-wise, or
|
|
2822 block-wise Visual mode respectively.
|
|
2823 Example: >
|
|
2824 :exe "normal " . visualmode()
|
|
2825 < This enters the same Visual mode as before. It is also useful
|
|
2826 in scripts if you wish to act differently depending on the
|
|
2827 Visual mode that was used.
|
|
2828
|
|
2829 If an expression is supplied that results in a non-zero number
|
|
2830 or a non-empty string, then the Visual mode will be cleared
|
|
2831 and the old value is returned. Note that " " and "0" are also
|
|
2832 non-empty strings, thus cause the mode to be cleared.
|
|
2833
|
|
2834 *winbufnr()*
|
|
2835 winbufnr({nr}) The result is a Number, which is the number of the buffer
|
|
2836 associated with window {nr}. When {nr} is zero, the number of
|
|
2837 the buffer in the current window is returned. When window
|
|
2838 {nr} doesn't exist, -1 is returned.
|
|
2839 Example: >
|
|
2840 :echo "The file in the current window is " . bufname(winbufnr(0))
|
|
2841 <
|
|
2842 *wincol()*
|
|
2843 wincol() The result is a Number, which is the virtual column of the
|
|
2844 cursor in the window. This is counting screen cells from the
|
|
2845 left side of the window. The leftmost column is one.
|
|
2846
|
|
2847 winheight({nr}) *winheight()*
|
|
2848 The result is a Number, which is the height of window {nr}.
|
|
2849 When {nr} is zero, the height of the current window is
|
|
2850 returned. When window {nr} doesn't exist, -1 is returned.
|
|
2851 An existing window always has a height of zero or more.
|
|
2852 Examples: >
|
|
2853 :echo "The current window has " . winheight(0) . " lines."
|
|
2854 <
|
|
2855 *winline()*
|
|
2856 winline() The result is a Number, which is the screen line of the cursor
|
|
2857 in the window. This is counting screen lines from the top of
|
|
2858 the window. The first line is one.
|
|
2859
|
|
2860 *winnr()*
|
20
|
2861 winnr([{arg}]) The result is a Number, which is the number of the current
|
|
2862 window. The top window has number 1.
|
|
2863 When the optional argument is "$", the number of the
|
|
2864 last window is returnd (the window count).
|
|
2865 When the optional argument is "#", the number of the last
|
|
2866 accessed window is returned (where |CTRL-W_p| goes to).
|
|
2867 If there is no previous window 0 is returned.
|
|
2868 The number can be used with |CTRL-W_w| and ":wincmd w"
|
|
2869 |:wincmd|.
|
7
|
2870
|
|
2871 *winrestcmd()*
|
|
2872 winrestcmd() Returns a sequence of |:resize| commands that should restore
|
|
2873 the current window sizes. Only works properly when no windows
|
|
2874 are opened or closed and the current window is unchanged.
|
|
2875 Example: >
|
|
2876 :let cmd = winrestcmd()
|
|
2877 :call MessWithWindowSizes()
|
|
2878 :exe cmd
|
|
2879
|
|
2880 winwidth({nr}) *winwidth()*
|
|
2881 The result is a Number, which is the width of window {nr}.
|
|
2882 When {nr} is zero, the width of the current window is
|
|
2883 returned. When window {nr} doesn't exist, -1 is returned.
|
|
2884 An existing window always has a width of zero or more.
|
|
2885 Examples: >
|
|
2886 :echo "The current window has " . winwidth(0) . " columns."
|
|
2887 :if winwidth(0) <= 50
|
|
2888 : exe "normal 50\<C-W>|"
|
|
2889 :endif
|
|
2890 <
|
|
2891
|
|
2892 *feature-list*
|
|
2893 There are three types of features:
|
|
2894 1. Features that are only supported when they have been enabled when Vim
|
|
2895 was compiled |+feature-list|. Example: >
|
|
2896 :if has("cindent")
|
|
2897 2. Features that are only supported when certain conditions have been met.
|
|
2898 Example: >
|
|
2899 :if has("gui_running")
|
|
2900 < *has-patch*
|
|
2901 3. Included patches. First check |v:version| for the version of Vim.
|
|
2902 Then the "patch123" feature means that patch 123 has been included for
|
|
2903 this version. Example (checking version 6.2.148 or later): >
|
|
2904 :if v:version > 602 || v:version == 602 && has("patch148")
|
|
2905
|
|
2906 all_builtin_terms Compiled with all builtin terminals enabled.
|
|
2907 amiga Amiga version of Vim.
|
|
2908 arabic Compiled with Arabic support |Arabic|.
|
|
2909 arp Compiled with ARP support (Amiga).
|
|
2910 autocmd Compiled with autocommands support.
|
|
2911 balloon_eval Compiled with |balloon-eval| support.
|
|
2912 beos BeOS version of Vim.
|
|
2913 browse Compiled with |:browse| support, and browse() will
|
|
2914 work.
|
|
2915 builtin_terms Compiled with some builtin terminals.
|
|
2916 byte_offset Compiled with support for 'o' in 'statusline'
|
|
2917 cindent Compiled with 'cindent' support.
|
|
2918 clientserver Compiled with remote invocation support |clientserver|.
|
|
2919 clipboard Compiled with 'clipboard' support.
|
|
2920 cmdline_compl Compiled with |cmdline-completion| support.
|
|
2921 cmdline_hist Compiled with |cmdline-history| support.
|
|
2922 cmdline_info Compiled with 'showcmd' and 'ruler' support.
|
|
2923 comments Compiled with |'comments'| support.
|
|
2924 cryptv Compiled with encryption support |encryption|.
|
|
2925 cscope Compiled with |cscope| support.
|
|
2926 compatible Compiled to be very Vi compatible.
|
|
2927 debug Compiled with "DEBUG" defined.
|
|
2928 dialog_con Compiled with console dialog support.
|
|
2929 dialog_gui Compiled with GUI dialog support.
|
|
2930 diff Compiled with |vimdiff| and 'diff' support.
|
|
2931 digraphs Compiled with support for digraphs.
|
|
2932 dnd Compiled with support for the "~ register |quote_~|.
|
|
2933 dos32 32 bits DOS (DJGPP) version of Vim.
|
|
2934 dos16 16 bits DOS version of Vim.
|
|
2935 ebcdic Compiled on a machine with ebcdic character set.
|
|
2936 emacs_tags Compiled with support for Emacs tags.
|
|
2937 eval Compiled with expression evaluation support. Always
|
|
2938 true, of course!
|
|
2939 ex_extra Compiled with extra Ex commands |+ex_extra|.
|
|
2940 extra_search Compiled with support for |'incsearch'| and
|
|
2941 |'hlsearch'|
|
|
2942 farsi Compiled with Farsi support |farsi|.
|
|
2943 file_in_path Compiled with support for |gf| and |<cfile>|
|
|
2944 find_in_path Compiled with support for include file searches
|
|
2945 |+find_in_path|.
|
|
2946 fname_case Case in file names matters (for Amiga, MS-DOS, and
|
|
2947 Windows this is not present).
|
|
2948 folding Compiled with |folding| support.
|
|
2949 footer Compiled with GUI footer support. |gui-footer|
|
|
2950 fork Compiled to use fork()/exec() instead of system().
|
|
2951 gettext Compiled with message translation |multi-lang|
|
|
2952 gui Compiled with GUI enabled.
|
|
2953 gui_athena Compiled with Athena GUI.
|
9
|
2954 gui_beos Compiled with BeOS GUI.
|
7
|
2955 gui_gtk Compiled with GTK+ GUI (any version).
|
|
2956 gui_gtk2 Compiled with GTK+ 2 GUI (gui_gtk is also defined).
|
11
|
2957 gui_kde Compiled with KDE GUI |KVim|
|
7
|
2958 gui_mac Compiled with Macintosh GUI.
|
|
2959 gui_motif Compiled with Motif GUI.
|
|
2960 gui_photon Compiled with Photon GUI.
|
|
2961 gui_win32 Compiled with MS Windows Win32 GUI.
|
|
2962 gui_win32s idem, and Win32s system being used (Windows 3.1)
|
|
2963 gui_running Vim is running in the GUI, or it will start soon.
|
|
2964 hangul_input Compiled with Hangul input support. |hangul|
|
|
2965 iconv Can use iconv() for conversion.
|
|
2966 insert_expand Compiled with support for CTRL-X expansion commands in
|
|
2967 Insert mode.
|
|
2968 jumplist Compiled with |jumplist| support.
|
|
2969 keymap Compiled with 'keymap' support.
|
|
2970 langmap Compiled with 'langmap' support.
|
|
2971 libcall Compiled with |libcall()| support.
|
|
2972 linebreak Compiled with 'linebreak', 'breakat' and 'showbreak'
|
|
2973 support.
|
|
2974 lispindent Compiled with support for lisp indenting.
|
|
2975 listcmds Compiled with commands for the buffer list |:files|
|
|
2976 and the argument list |arglist|.
|
|
2977 localmap Compiled with local mappings and abbr. |:map-local|
|
|
2978 mac Macintosh version of Vim.
|
|
2979 macunix Macintosh version of Vim, using Unix files (OS-X).
|
|
2980 menu Compiled with support for |:menu|.
|
|
2981 mksession Compiled with support for |:mksession|.
|
|
2982 modify_fname Compiled with file name modifiers. |filename-modifiers|
|
|
2983 mouse Compiled with support mouse.
|
|
2984 mouseshape Compiled with support for 'mouseshape'.
|
|
2985 mouse_dec Compiled with support for Dec terminal mouse.
|
|
2986 mouse_gpm Compiled with support for gpm (Linux console mouse)
|
|
2987 mouse_netterm Compiled with support for netterm mouse.
|
|
2988 mouse_pterm Compiled with support for qnx pterm mouse.
|
|
2989 mouse_xterm Compiled with support for xterm mouse.
|
|
2990 multi_byte Compiled with support for editing Korean et al.
|
|
2991 multi_byte_ime Compiled with support for IME input method.
|
|
2992 multi_lang Compiled with support for multiple languages.
|
14
|
2993 mzscheme Compiled with MzScheme interface |mzscheme|.
|
7
|
2994 netbeans_intg Compiled with support for |netbeans|.
|
33
|
2995 netbeans_enabled Compiled with support for |netbeans| and it's used.
|
7
|
2996 ole Compiled with OLE automation support for Win32.
|
|
2997 os2 OS/2 version of Vim.
|
|
2998 osfiletype Compiled with support for osfiletypes |+osfiletype|
|
|
2999 path_extra Compiled with up/downwards search in 'path' and 'tags'
|
|
3000 perl Compiled with Perl interface.
|
|
3001 postscript Compiled with PostScript file printing.
|
|
3002 printer Compiled with |:hardcopy| support.
|
|
3003 python Compiled with Python interface.
|
|
3004 qnx QNX version of Vim.
|
|
3005 quickfix Compiled with |quickfix| support.
|
|
3006 rightleft Compiled with 'rightleft' support.
|
|
3007 ruby Compiled with Ruby interface |ruby|.
|
|
3008 scrollbind Compiled with 'scrollbind' support.
|
|
3009 showcmd Compiled with 'showcmd' support.
|
|
3010 signs Compiled with |:sign| support.
|
|
3011 smartindent Compiled with 'smartindent' support.
|
9
|
3012 sniff Compiled with SNiFF interface support.
|
7
|
3013 statusline Compiled with support for 'statusline', 'rulerformat'
|
|
3014 and special formats of 'titlestring' and 'iconstring'.
|
|
3015 sun_workshop Compiled with support for Sun |workshop|.
|
|
3016 syntax Compiled with syntax highlighting support.
|
|
3017 syntax_items There are active syntax highlighting items for the
|
|
3018 current buffer.
|
|
3019 system Compiled to use system() instead of fork()/exec().
|
|
3020 tag_binary Compiled with binary searching in tags files
|
|
3021 |tag-binary-search|.
|
|
3022 tag_old_static Compiled with support for old static tags
|
|
3023 |tag-old-static|.
|
|
3024 tag_any_white Compiled with support for any white characters in tags
|
|
3025 files |tag-any-white|.
|
|
3026 tcl Compiled with Tcl interface.
|
|
3027 terminfo Compiled with terminfo instead of termcap.
|
|
3028 termresponse Compiled with support for |t_RV| and |v:termresponse|.
|
|
3029 textobjects Compiled with support for |text-objects|.
|
|
3030 tgetent Compiled with tgetent support, able to use a termcap
|
|
3031 or terminfo file.
|
|
3032 title Compiled with window title support |'title'|.
|
|
3033 toolbar Compiled with support for |gui-toolbar|.
|
|
3034 unix Unix version of Vim.
|
|
3035 user_commands User-defined commands.
|
|
3036 viminfo Compiled with viminfo support.
|
|
3037 vim_starting True while initial source'ing takes place.
|
|
3038 vertsplit Compiled with vertically split windows |:vsplit|.
|
|
3039 virtualedit Compiled with 'virtualedit' option.
|
|
3040 visual Compiled with Visual mode.
|
|
3041 visualextra Compiled with extra Visual mode commands.
|
|
3042 |blockwise-operators|.
|
|
3043 vms VMS version of Vim.
|
|
3044 vreplace Compiled with |gR| and |gr| commands.
|
|
3045 wildignore Compiled with 'wildignore' option.
|
|
3046 wildmenu Compiled with 'wildmenu' option.
|
|
3047 windows Compiled with support for more than one window.
|
|
3048 winaltkeys Compiled with 'winaltkeys' option.
|
|
3049 win16 Win16 version of Vim (MS-Windows 3.1).
|
|
3050 win32 Win32 version of Vim (MS-Windows 95/98/ME/NT/2000/XP).
|
|
3051 win64 Win64 version of Vim (MS-Windows 64 bit).
|
|
3052 win32unix Win32 version of Vim, using Unix files (Cygwin)
|
|
3053 win95 Win32 version for MS-Windows 95/98/ME.
|
|
3054 writebackup Compiled with 'writebackup' default on.
|
|
3055 xfontset Compiled with X fontset support |xfontset|.
|
|
3056 xim Compiled with X input method support |xim|.
|
|
3057 xsmp Compiled with X session management support.
|
|
3058 xsmp_interact Compiled with interactive X session management support.
|
|
3059 xterm_clipboard Compiled with support for xterm clipboard.
|
|
3060 xterm_save Compiled with support for saving and restoring the
|
|
3061 xterm screen.
|
|
3062 x11 Compiled with X11 support.
|
|
3063
|
|
3064 *string-match*
|
|
3065 Matching a pattern in a String
|
|
3066
|
|
3067 A regexp pattern as explained at |pattern| is normally used to find a match in
|
|
3068 the buffer lines. When a pattern is used to find a match in a String, almost
|
|
3069 everything works in the same way. The difference is that a String is handled
|
|
3070 like it is one line. When it contains a "\n" character, this is not seen as a
|
|
3071 line break for the pattern. It can be matched with a "\n" in the pattern, or
|
|
3072 with ".". Example: >
|
|
3073 :let a = "aaaa\nxxxx"
|
|
3074 :echo matchstr(a, "..\n..")
|
|
3075 aa
|
|
3076 xx
|
|
3077 :echo matchstr(a, "a.x")
|
|
3078 a
|
|
3079 x
|
|
3080
|
|
3081 Don't forget that "^" will only match at the first character of the String and
|
|
3082 "$" at the last character of the string. They don't match after or before a
|
|
3083 "\n".
|
|
3084
|
|
3085 ==============================================================================
|
|
3086 5. Defining functions *user-functions*
|
|
3087
|
|
3088 New functions can be defined. These can be called just like builtin
|
|
3089 functions. The function executes a sequence of Ex commands. Normal mode
|
|
3090 commands can be executed with the |:normal| command.
|
|
3091
|
|
3092 The function name must start with an uppercase letter, to avoid confusion with
|
|
3093 builtin functions. To prevent from using the same name in different scripts
|
|
3094 avoid obvious, short names. A good habit is to start the function name with
|
|
3095 the name of the script, e.g., "HTMLcolor()".
|
|
3096
|
|
3097 It's also possible to use curly braces, see |curly-braces-names|.
|
|
3098
|
|
3099 *local-function*
|
|
3100 A function local to a script must start with "s:". A local script function
|
|
3101 can only be called from within the script and from functions, user commands
|
|
3102 and autocommands defined in the script. It is also possible to call the
|
|
3103 function from a mappings defined in the script, but then |<SID>| must be used
|
|
3104 instead of "s:" when the mapping is expanded outside of the script.
|
|
3105
|
|
3106 *:fu* *:function* *E128* *E129* *E123*
|
|
3107 :fu[nction] List all functions and their arguments.
|
|
3108
|
|
3109 :fu[nction] {name} List function {name}.
|
|
3110 *E124* *E125*
|
|
3111 :fu[nction][!] {name}([arguments]) [range] [abort]
|
|
3112 Define a new function by the name {name}. The name
|
|
3113 must be made of alphanumeric characters and '_', and
|
|
3114 must start with a capital or "s:" (see above).
|
|
3115 *function-argument* *a:var*
|
|
3116 An argument can be defined by giving its name. In the
|
|
3117 function this can then be used as "a:name" ("a:" for
|
|
3118 argument).
|
|
3119 Up to 20 arguments can be given, separated by commas.
|
|
3120 Finally, an argument "..." can be specified, which
|
|
3121 means that more arguments may be following. In the
|
|
3122 function they can be used as "a:1", "a:2", etc. "a:0"
|
|
3123 is set to the number of extra arguments (which can be
|
|
3124 0).
|
|
3125 When not using "...", the number of arguments in a
|
|
3126 function call must be equal to the number of named
|
|
3127 arguments. When using "...", the number of arguments
|
|
3128 may be larger.
|
|
3129 It is also possible to define a function without any
|
|
3130 arguments. You must still supply the () then.
|
|
3131 The body of the function follows in the next lines,
|
|
3132 until the matching |:endfunction|. It is allowed to
|
|
3133 define another function inside a function body.
|
|
3134 *E127* *E122*
|
|
3135 When a function by this name already exists and [!] is
|
|
3136 not used an error message is given. When [!] is used,
|
|
3137 an existing function is silently replaced. Unless it
|
|
3138 is currently being executed, that is an error.
|
|
3139 *a:firstline* *a:lastline*
|
|
3140 When the [range] argument is added, the function is
|
|
3141 expected to take care of a range itself. The range is
|
|
3142 passed as "a:firstline" and "a:lastline". If [range]
|
|
3143 is excluded, ":{range}call" will call the function for
|
|
3144 each line in the range, with the cursor on the start
|
|
3145 of each line. See |function-range-example|.
|
|
3146 When the [abort] argument is added, the function will
|
|
3147 abort as soon as an error is detected.
|
|
3148 The last used search pattern and the redo command "."
|
|
3149 will not be changed by the function.
|
|
3150
|
|
3151 *:endf* *:endfunction* *E126* *E193*
|
|
3152 :endf[unction] The end of a function definition. Must be on a line
|
|
3153 by its own, without other commands.
|
|
3154
|
|
3155 *:delf* *:delfunction* *E130* *E131*
|
|
3156 :delf[unction] {name} Delete function {name}.
|
|
3157
|
|
3158 *:retu* *:return* *E133*
|
|
3159 :retu[rn] [expr] Return from a function. When "[expr]" is given, it is
|
|
3160 evaluated and returned as the result of the function.
|
|
3161 If "[expr]" is not given, the number 0 is returned.
|
|
3162 When a function ends without an explicit ":return",
|
|
3163 the number 0 is returned.
|
|
3164 Note that there is no check for unreachable lines,
|
|
3165 thus there is no warning if commands follow ":return".
|
|
3166
|
|
3167 If the ":return" is used after a |:try| but before the
|
|
3168 matching |:finally| (if present), the commands
|
|
3169 following the ":finally" up to the matching |:endtry|
|
|
3170 are executed first. This process applies to all
|
|
3171 nested ":try"s inside the function. The function
|
|
3172 returns at the outermost ":endtry".
|
|
3173
|
|
3174
|
|
3175 Inside a function variables can be used. These are local variables, which
|
|
3176 will disappear when the function returns. Global variables need to be
|
|
3177 accessed with "g:".
|
|
3178
|
|
3179 Example: >
|
|
3180 :function Table(title, ...)
|
|
3181 : echohl Title
|
|
3182 : echo a:title
|
|
3183 : echohl None
|
|
3184 : let idx = 1
|
|
3185 : while idx <= a:0
|
|
3186 : echo a:{idx} . ' '
|
|
3187 : let idx = idx + 1
|
|
3188 : endwhile
|
|
3189 : return idx
|
|
3190 :endfunction
|
|
3191
|
|
3192 This function can then be called with: >
|
|
3193 let lines = Table("Table", "line1", "line2")
|
|
3194 let lines = Table("Empty Table")
|
|
3195
|
|
3196 To return more than one value, pass the name of a global variable: >
|
|
3197 :function Compute(n1, n2, divname)
|
|
3198 : if a:n2 == 0
|
|
3199 : return "fail"
|
|
3200 : endif
|
|
3201 : let g:{a:divname} = a:n1 / a:n2
|
|
3202 : return "ok"
|
|
3203 :endfunction
|
|
3204
|
|
3205 This function can then be called with: >
|
|
3206 :let success = Compute(13, 1324, "div")
|
|
3207 :if success == "ok"
|
|
3208 : echo div
|
|
3209 :endif
|
|
3210
|
|
3211 An alternative is to return a command that can be executed. This also works
|
|
3212 with local variables in a calling function. Example: >
|
|
3213 :function Foo()
|
|
3214 : execute Bar()
|
|
3215 : echo "line " . lnum . " column " . col
|
|
3216 :endfunction
|
|
3217
|
|
3218 :function Bar()
|
|
3219 : return "let lnum = " . line(".") . " | let col = " . col(".")
|
|
3220 :endfunction
|
|
3221
|
|
3222 The names "lnum" and "col" could also be passed as argument to Bar(), to allow
|
|
3223 the caller to set the names.
|
|
3224
|
|
3225 *:cal* *:call* *E107*
|
|
3226 :[range]cal[l] {name}([arguments])
|
|
3227 Call a function. The name of the function and its arguments
|
|
3228 are as specified with |:function|. Up to 20 arguments can be
|
|
3229 used.
|
|
3230 Without a range and for functions that accept a range, the
|
|
3231 function is called once. When a range is given the cursor is
|
|
3232 positioned at the start of the first line before executing the
|
|
3233 function.
|
|
3234 When a range is given and the function doesn't handle it
|
|
3235 itself, the function is executed for each line in the range,
|
|
3236 with the cursor in the first column of that line. The cursor
|
|
3237 is left at the last line (possibly moved by the last function
|
|
3238 call). The arguments are re-evaluated for each line. Thus
|
|
3239 this works:
|
|
3240 *function-range-example* >
|
|
3241 :function Mynumber(arg)
|
|
3242 : echo line(".") . " " . a:arg
|
|
3243 :endfunction
|
|
3244 :1,5call Mynumber(getline("."))
|
|
3245 <
|
|
3246 The "a:firstline" and "a:lastline" are defined anyway, they
|
|
3247 can be used to do something different at the start or end of
|
|
3248 the range.
|
|
3249
|
|
3250 Example of a function that handles the range itself: >
|
|
3251
|
|
3252 :function Cont() range
|
|
3253 : execute (a:firstline + 1) . "," . a:lastline . 's/^/\t\\ '
|
|
3254 :endfunction
|
|
3255 :4,8call Cont()
|
|
3256 <
|
|
3257 This function inserts the continuation character "\" in front
|
|
3258 of all the lines in the range, except the first one.
|
|
3259
|
|
3260 *E132*
|
|
3261 The recursiveness of user functions is restricted with the |'maxfuncdepth'|
|
|
3262 option.
|
|
3263
|
|
3264 *autoload-functions*
|
|
3265 When using many or large functions, it's possible to automatically define them
|
|
3266 only when they are used. Use the FuncUndefined autocommand event with a
|
|
3267 pattern that matches the function(s) to be defined. Example: >
|
|
3268
|
|
3269 :au FuncUndefined BufNet* source ~/vim/bufnetfuncs.vim
|
|
3270
|
|
3271 The file "~/vim/bufnetfuncs.vim" should then define functions that start with
|
|
3272 "BufNet". Also see |FuncUndefined|.
|
|
3273
|
|
3274 ==============================================================================
|
|
3275 6. Curly braces names *curly-braces-names*
|
|
3276
|
|
3277 Wherever you can use a variable, you can use a "curly braces name" variable.
|
|
3278 This is a regular variable name with one or more expressions wrapped in braces
|
|
3279 {} like this: >
|
|
3280 my_{adjective}_variable
|
|
3281
|
|
3282 When Vim encounters this, it evaluates the expression inside the braces, puts
|
|
3283 that in place of the expression, and re-interprets the whole as a variable
|
|
3284 name. So in the above example, if the variable "adjective" was set to
|
|
3285 "noisy", then the reference would be to "my_noisy_variable", whereas if
|
|
3286 "adjective" was set to "quiet", then it would be to "my_quiet_variable".
|
|
3287
|
|
3288 One application for this is to create a set of variables governed by an option
|
|
3289 value. For example, the statement >
|
|
3290 echo my_{&background}_message
|
|
3291
|
|
3292 would output the contents of "my_dark_message" or "my_light_message" depending
|
|
3293 on the current value of 'background'.
|
|
3294
|
|
3295 You can use multiple brace pairs: >
|
|
3296 echo my_{adverb}_{adjective}_message
|
|
3297 ..or even nest them: >
|
|
3298 echo my_{ad{end_of_word}}_message
|
|
3299 where "end_of_word" is either "verb" or "jective".
|
|
3300
|
|
3301 However, the expression inside the braces must evaluate to a valid single
|
|
3302 variable name. e.g. this is invalid: >
|
|
3303 :let foo='a + b'
|
|
3304 :echo c{foo}d
|
|
3305 .. since the result of expansion is "ca + bd", which is not a variable name.
|
|
3306
|
|
3307 *curly-braces-function-names*
|
|
3308 You can call and define functions by an evaluated name in a similar way.
|
|
3309 Example: >
|
|
3310 :let func_end='whizz'
|
|
3311 :call my_func_{func_end}(parameter)
|
|
3312
|
|
3313 This would call the function "my_func_whizz(parameter)".
|
|
3314
|
|
3315 ==============================================================================
|
|
3316 7. Commands *expression-commands*
|
|
3317
|
|
3318 :let {var-name} = {expr1} *:let* *E18*
|
|
3319 Set internal variable {var-name} to the result of the
|
|
3320 expression {expr1}. The variable will get the type
|
|
3321 from the {expr}. If {var-name} didn't exist yet, it
|
|
3322 is created.
|
|
3323
|
|
3324 :let ${env-name} = {expr1} *:let-environment* *:let-$*
|
|
3325 Set environment variable {env-name} to the result of
|
|
3326 the expression {expr1}. The type is always String.
|
|
3327
|
|
3328 :let @{reg-name} = {expr1} *:let-register* *:let-@*
|
|
3329 Write the result of the expression {expr1} in register
|
|
3330 {reg-name}. {reg-name} must be a single letter, and
|
|
3331 must be the name of a writable register (see
|
|
3332 |registers|). "@@" can be used for the unnamed
|
|
3333 register, "@/" for the search pattern.
|
|
3334 If the result of {expr1} ends in a <CR> or <NL>, the
|
|
3335 register will be linewise, otherwise it will be set to
|
|
3336 characterwise.
|
|
3337 This can be used to clear the last search pattern: >
|
|
3338 :let @/ = ""
|
|
3339 < This is different from searching for an empty string,
|
|
3340 that would match everywhere.
|
|
3341
|
|
3342 :let &{option-name} = {expr1} *:let-option* *:let-star*
|
|
3343 Set option {option-name} to the result of the
|
|
3344 expression {expr1}. The value is always converted to
|
|
3345 the type of the option.
|
|
3346 For an option local to a window or buffer the effect
|
|
3347 is just like using the |:set| command: both the local
|
|
3348 value and the global value is changed.
|
|
3349
|
|
3350 :let &l:{option-name} = {expr1}
|
|
3351 Like above, but only set the local value of an option
|
|
3352 (if there is one). Works like |:setlocal|.
|
|
3353
|
|
3354 :let &g:{option-name} = {expr1}
|
|
3355 Like above, but only set the global value of an option
|
|
3356 (if there is one). Works like |:setglobal|.
|
|
3357
|
|
3358 *E106*
|
|
3359 :let {var-name} .. List the value of variable {var-name}. Several
|
|
3360 variable names may be given.
|
|
3361
|
|
3362 :let List the values of all variables.
|
|
3363
|
|
3364 *:unlet* *:unl* *E108*
|
|
3365 :unl[et][!] {var-name} ...
|
|
3366 Remove the internal variable {var-name}. Several
|
|
3367 variable names can be given, they are all removed.
|
|
3368 With [!] no error message is given for non-existing
|
|
3369 variables.
|
|
3370
|
|
3371 :if {expr1} *:if* *:endif* *:en* *E171* *E579* *E580*
|
|
3372 :en[dif] Execute the commands until the next matching ":else"
|
|
3373 or ":endif" if {expr1} evaluates to non-zero.
|
|
3374
|
|
3375 From Vim version 4.5 until 5.0, every Ex command in
|
|
3376 between the ":if" and ":endif" is ignored. These two
|
|
3377 commands were just to allow for future expansions in a
|
|
3378 backwards compatible way. Nesting was allowed. Note
|
|
3379 that any ":else" or ":elseif" was ignored, the "else"
|
|
3380 part was not executed either.
|
|
3381
|
|
3382 You can use this to remain compatible with older
|
|
3383 versions: >
|
|
3384 :if version >= 500
|
|
3385 : version-5-specific-commands
|
|
3386 :endif
|
|
3387 < The commands still need to be parsed to find the
|
|
3388 "endif". Sometimes an older Vim has a problem with a
|
|
3389 new command. For example, ":silent" is recognized as
|
|
3390 a ":substitute" command. In that case ":execute" can
|
|
3391 avoid problems: >
|
|
3392 :if version >= 600
|
|
3393 : execute "silent 1,$delete"
|
|
3394 :endif
|
|
3395 <
|
|
3396 NOTE: The ":append" and ":insert" commands don't work
|
|
3397 properly in between ":if" and ":endif".
|
|
3398
|
|
3399 *:else* *:el* *E581* *E583*
|
|
3400 :el[se] Execute the commands until the next matching ":else"
|
|
3401 or ":endif" if they previously were not being
|
|
3402 executed.
|
|
3403
|
|
3404 *:elseif* *:elsei* *E582* *E584*
|
|
3405 :elsei[f] {expr1} Short for ":else" ":if", with the addition that there
|
|
3406 is no extra ":endif".
|
|
3407
|
|
3408 :wh[ile] {expr1} *:while* *:endwhile* *:wh* *:endw*
|
|
3409 *E170* *E585* *E588*
|
|
3410 :endw[hile] Repeat the commands between ":while" and ":endwhile",
|
|
3411 as long as {expr1} evaluates to non-zero.
|
|
3412 When an error is detected from a command inside the
|
|
3413 loop, execution continues after the "endwhile".
|
|
3414
|
|
3415 NOTE: The ":append" and ":insert" commands don't work
|
|
3416 properly inside a ":while" loop.
|
|
3417
|
|
3418 *:continue* *:con* *E586*
|
|
3419 :con[tinue] When used inside a ":while", jumps back to the
|
|
3420 ":while". If it is used after a |:try| inside the
|
|
3421 ":while" but before the matching |:finally| (if
|
|
3422 present), the commands following the ":finally" up to
|
|
3423 the matching |:endtry| are executed first. This
|
|
3424 process applies to all nested ":try"s inside the
|
|
3425 ":while". The outermost ":endtry" then jumps back to
|
|
3426 the ":while".
|
|
3427
|
|
3428 *:break* *:brea* *E587*
|
|
3429 :brea[k] When used inside a ":while", skips to the command
|
|
3430 after the matching ":endwhile". If it is used after
|
|
3431 a |:try| inside the ":while" but before the matching
|
|
3432 |:finally| (if present), the commands following the
|
|
3433 ":finally" up to the matching |:endtry| are executed
|
|
3434 first. This process applies to all nested ":try"s
|
|
3435 inside the ":while". The outermost ":endtry" then
|
|
3436 jumps to the command after the ":endwhile".
|
|
3437
|
|
3438 :try *:try* *:endt* *:endtry* *E600* *E601* *E602*
|
|
3439 :endt[ry] Change the error handling for the commands between
|
|
3440 ":try" and ":endtry" including everything being
|
|
3441 executed across ":source" commands, function calls,
|
|
3442 or autocommand invocations.
|
|
3443
|
|
3444 When an error or interrupt is detected and there is
|
|
3445 a |:finally| command following, execution continues
|
|
3446 after the ":finally". Otherwise, or when the
|
|
3447 ":endtry" is reached thereafter, the next
|
|
3448 (dynamically) surrounding ":try" is checked for
|
|
3449 a corresponding ":finally" etc. Then the script
|
|
3450 processing is terminated. (Whether a function
|
|
3451 definition has an "abort" argument does not matter.)
|
|
3452 Example: >
|
|
3453 :try | edit too much | finally | echo "cleanup" | endtry
|
|
3454 :echo "impossible" " not reached, script terminated above
|
|
3455 <
|
|
3456 Moreover, an error or interrupt (dynamically) inside
|
|
3457 ":try" and ":endtry" is converted to an exception. It
|
|
3458 can be caught as if it were thrown by a |:throw|
|
|
3459 command (see |:catch|). In this case, the script
|
|
3460 processing is not terminated.
|
|
3461
|
|
3462 The value "Vim:Interrupt" is used for an interrupt
|
|
3463 exception. An error in a Vim command is converted
|
|
3464 to a value of the form "Vim({command}):{errmsg}",
|
|
3465 other errors are converted to a value of the form
|
|
3466 "Vim:{errmsg}". {command} is the full command name,
|
|
3467 and {errmsg} is the message that is displayed if the
|
|
3468 error exception is not caught, always beginning with
|
|
3469 the error number.
|
|
3470 Examples: >
|
|
3471 :try | sleep 100 | catch /^Vim:Interrupt$/ | endtry
|
|
3472 :try | edit | catch /^Vim(edit):E\d\+/ | echo "error" | endtry
|
|
3473 <
|
|
3474 *:cat* *:catch* *E603* *E604* *E605*
|
|
3475 :cat[ch] /{pattern}/ The following commands until the next ":catch",
|
|
3476 |:finally|, or |:endtry| that belongs to the same
|
|
3477 |:try| as the ":catch" are executed when an exception
|
|
3478 matching {pattern} is being thrown and has not yet
|
|
3479 been caught by a previous ":catch". Otherwise, these
|
|
3480 commands are skipped.
|
|
3481 When {pattern} is omitted all errors are caught.
|
|
3482 Examples: >
|
|
3483 :catch /^Vim:Interrupt$/ " catch interrupts (CTRL-C)
|
|
3484 :catch /^Vim\%((\a\+)\)\=:E/ " catch all Vim errors
|
|
3485 :catch /^Vim\%((\a\+)\)\=:/ " catch errors and interrupts
|
|
3486 :catch /^Vim(write):/ " catch all errors in :write
|
|
3487 :catch /^Vim\%((\a\+)\)\=:E123/ " catch error E123
|
|
3488 :catch /my-exception/ " catch user exception
|
|
3489 :catch /.*/ " catch everything
|
|
3490 :catch " same as /.*/
|
|
3491 <
|
|
3492 Another character can be used instead of / around the
|
|
3493 {pattern}, so long as it does not have a special
|
|
3494 meaning (e.g., '|' or '"') and doesn't occur inside
|
|
3495 {pattern}.
|
|
3496 NOTE: It is not reliable to ":catch" the TEXT of
|
|
3497 an error message because it may vary in different
|
|
3498 locales.
|
|
3499
|
|
3500 *:fina* *:finally* *E606* *E607*
|
|
3501 :fina[lly] The following commands until the matching |:endtry|
|
|
3502 are executed whenever the part between the matching
|
|
3503 |:try| and the ":finally" is left: either by falling
|
|
3504 through to the ":finally" or by a |:continue|,
|
|
3505 |:break|, |:finish|, or |:return|, or by an error or
|
|
3506 interrupt or exception (see |:throw|).
|
|
3507
|
|
3508 *:th* *:throw* *E608*
|
|
3509 :th[row] {expr1} The {expr1} is evaluated and thrown as an exception.
|
|
3510 If the ":throw" is used after a |:try| but before the
|
|
3511 first corresponding |:catch|, commands are skipped
|
|
3512 until the first ":catch" matching {expr1} is reached.
|
|
3513 If there is no such ":catch" or if the ":throw" is
|
|
3514 used after a ":catch" but before the |:finally|, the
|
|
3515 commands following the ":finally" (if present) up to
|
|
3516 the matching |:endtry| are executed. If the ":throw"
|
|
3517 is after the ":finally", commands up to the ":endtry"
|
|
3518 are skipped. At the ":endtry", this process applies
|
|
3519 again for the next dynamically surrounding ":try"
|
|
3520 (which may be found in a calling function or sourcing
|
|
3521 script), until a matching ":catch" has been found.
|
|
3522 If the exception is not caught, the command processing
|
|
3523 is terminated.
|
|
3524 Example: >
|
|
3525 :try | throw "oops" | catch /^oo/ | echo "caught" | endtry
|
|
3526 <
|
|
3527
|
|
3528 *:ec* *:echo*
|
|
3529 :ec[ho] {expr1} .. Echoes each {expr1}, with a space in between. The
|
|
3530 first {expr1} starts on a new line.
|
|
3531 Also see |:comment|.
|
|
3532 Use "\n" to start a new line. Use "\r" to move the
|
|
3533 cursor to the first column.
|
|
3534 Uses the highlighting set by the |:echohl| command.
|
|
3535 Cannot be followed by a comment.
|
|
3536 Example: >
|
|
3537 :echo "the value of 'shell' is" &shell
|
|
3538 < A later redraw may make the message disappear again.
|
|
3539 To avoid that a command from before the ":echo" causes
|
|
3540 a redraw afterwards (redraws are often postponed until
|
|
3541 you type something), force a redraw with the |:redraw|
|
|
3542 command. Example: >
|
|
3543 :new | redraw | echo "there is a new window"
|
|
3544 <
|
|
3545 *:echon*
|
|
3546 :echon {expr1} .. Echoes each {expr1}, without anything added. Also see
|
|
3547 |:comment|.
|
|
3548 Uses the highlighting set by the |:echohl| command.
|
|
3549 Cannot be followed by a comment.
|
|
3550 Example: >
|
|
3551 :echon "the value of 'shell' is " &shell
|
|
3552 <
|
|
3553 Note the difference between using ":echo", which is a
|
|
3554 Vim command, and ":!echo", which is an external shell
|
|
3555 command: >
|
|
3556 :!echo % --> filename
|
|
3557 < The arguments of ":!" are expanded, see |:_%|. >
|
|
3558 :!echo "%" --> filename or "filename"
|
|
3559 < Like the previous example. Whether you see the double
|
|
3560 quotes or not depends on your 'shell'. >
|
|
3561 :echo % --> nothing
|
|
3562 < The '%' is an illegal character in an expression. >
|
|
3563 :echo "%" --> %
|
|
3564 < This just echoes the '%' character. >
|
|
3565 :echo expand("%") --> filename
|
|
3566 < This calls the expand() function to expand the '%'.
|
|
3567
|
|
3568 *:echoh* *:echohl*
|
|
3569 :echoh[l] {name} Use the highlight group {name} for the following
|
|
3570 |:echo|, |:echon| and |:echomsg| commands. Also used
|
|
3571 for the |input()| prompt. Example: >
|
|
3572 :echohl WarningMsg | echo "Don't panic!" | echohl None
|
|
3573 < Don't forget to set the group back to "None",
|
|
3574 otherwise all following echo's will be highlighted.
|
|
3575
|
|
3576 *:echom* *:echomsg*
|
|
3577 :echom[sg] {expr1} .. Echo the expression(s) as a true message, saving the
|
|
3578 message in the |message-history|.
|
|
3579 Spaces are placed between the arguments as with the
|
|
3580 |:echo| command. But unprintable characters are
|
|
3581 displayed, not interpreted.
|
|
3582 Uses the highlighting set by the |:echohl| command.
|
|
3583 Example: >
|
|
3584 :echomsg "It's a Zizzer Zazzer Zuzz, as you can plainly see."
|
|
3585 <
|
|
3586 *:echoe* *:echoerr*
|
|
3587 :echoe[rr] {expr1} .. Echo the expression(s) as an error message, saving the
|
|
3588 message in the |message-history|. When used in a
|
|
3589 script or function the line number will be added.
|
|
3590 Spaces are placed between the arguments as with the
|
|
3591 :echo command. When used inside a try conditional,
|
|
3592 the message is raised as an error exception instead
|
|
3593 (see |try-echoerr|).
|
|
3594 Example: >
|
|
3595 :echoerr "This script just failed!"
|
|
3596 < If you just want a highlighted message use |:echohl|.
|
|
3597 And to get a beep: >
|
|
3598 :exe "normal \<Esc>"
|
|
3599 <
|
|
3600 *:exe* *:execute*
|
|
3601 :exe[cute] {expr1} .. Executes the string that results from the evaluation
|
|
3602 of {expr1} as an Ex command. Multiple arguments are
|
|
3603 concatenated, with a space in between. {expr1} is
|
|
3604 used as the processed command, command line editing
|
|
3605 keys are not recognized.
|
|
3606 Cannot be followed by a comment.
|
|
3607 Examples: >
|
|
3608 :execute "buffer " nextbuf
|
|
3609 :execute "normal " count . "w"
|
|
3610 <
|
|
3611 ":execute" can be used to append a command to commands
|
|
3612 that don't accept a '|'. Example: >
|
|
3613 :execute '!ls' | echo "theend"
|
|
3614
|
|
3615 < ":execute" is also a nice way to avoid having to type
|
|
3616 control characters in a Vim script for a ":normal"
|
|
3617 command: >
|
|
3618 :execute "normal ixxx\<Esc>"
|
|
3619 < This has an <Esc> character, see |expr-string|.
|
|
3620
|
|
3621 Note: The executed string may be any command-line, but
|
|
3622 you cannot start or end a "while" or "if" command.
|
|
3623 Thus this is illegal: >
|
|
3624 :execute 'while i > 5'
|
|
3625 :execute 'echo "test" | break'
|
|
3626 <
|
|
3627 It is allowed to have a "while" or "if" command
|
|
3628 completely in the executed string: >
|
|
3629 :execute 'while i < 5 | echo i | let i = i + 1 | endwhile'
|
|
3630 <
|
|
3631
|
|
3632 *:comment*
|
|
3633 ":execute", ":echo" and ":echon" cannot be followed by
|
|
3634 a comment directly, because they see the '"' as the
|
|
3635 start of a string. But, you can use '|' followed by a
|
|
3636 comment. Example: >
|
|
3637 :echo "foo" | "this is a comment
|
|
3638
|
|
3639 ==============================================================================
|
|
3640 8. Exception handling *exception-handling*
|
|
3641
|
|
3642 The Vim script language comprises an exception handling feature. This section
|
|
3643 explains how it can be used in a Vim script.
|
|
3644
|
|
3645 Exceptions may be raised by Vim on an error or on interrupt, see
|
|
3646 |catch-errors| and |catch-interrupt|. You can also explicitly throw an
|
|
3647 exception by using the ":throw" command, see |throw-catch|.
|
|
3648
|
|
3649
|
|
3650 TRY CONDITIONALS *try-conditionals*
|
|
3651
|
|
3652 Exceptions can be caught or can cause cleanup code to be executed. You can
|
|
3653 use a try conditional to specify catch clauses (that catch exceptions) and/or
|
|
3654 a finally clause (to be executed for cleanup).
|
|
3655 A try conditional begins with a |:try| command and ends at the matching
|
|
3656 |:endtry| command. In between, you can use a |:catch| command to start
|
|
3657 a catch clause, or a |:finally| command to start a finally clause. There may
|
|
3658 be none or multiple catch clauses, but there is at most one finally clause,
|
|
3659 which must not be followed by any catch clauses. The lines before the catch
|
|
3660 clauses and the finally clause is called a try block. >
|
|
3661
|
|
3662 :try
|
|
3663 : ...
|
|
3664 : ... TRY BLOCK
|
|
3665 : ...
|
|
3666 :catch /{pattern}/
|
|
3667 : ...
|
|
3668 : ... CATCH CLAUSE
|
|
3669 : ...
|
|
3670 :catch /{pattern}/
|
|
3671 : ...
|
|
3672 : ... CATCH CLAUSE
|
|
3673 : ...
|
|
3674 :finally
|
|
3675 : ...
|
|
3676 : ... FINALLY CLAUSE
|
|
3677 : ...
|
|
3678 :endtry
|
|
3679
|
|
3680 The try conditional allows to watch code for exceptions and to take the
|
|
3681 appropriate actions. Exceptions from the try block may be caught. Exceptions
|
|
3682 from the try block and also the catch clauses may cause cleanup actions.
|
|
3683 When no exception is thrown during execution of the try block, the control
|
|
3684 is transferred to the finally clause, if present. After its execution, the
|
|
3685 script continues with the line following the ":endtry".
|
|
3686 When an exception occurs during execution of the try block, the remaining
|
|
3687 lines in the try block are skipped. The exception is matched against the
|
|
3688 patterns specified as arguments to the ":catch" commands. The catch clause
|
|
3689 after the first matching ":catch" is taken, other catch clauses are not
|
|
3690 executed. The catch clause ends when the next ":catch", ":finally", or
|
|
3691 ":endtry" command is reached - whatever is first. Then, the finally clause
|
|
3692 (if present) is executed. When the ":endtry" is reached, the script execution
|
|
3693 continues in the following line as usual.
|
|
3694 When an exception that does not match any of the patterns specified by the
|
|
3695 ":catch" commands is thrown in the try block, the exception is not caught by
|
|
3696 that try conditional and none of the catch clauses is executed. Only the
|
|
3697 finally clause, if present, is taken. The exception pends during execution of
|
|
3698 the finally clause. It is resumed at the ":endtry", so that commands after
|
|
3699 the ":endtry" are not executed and the exception might be caught elsewhere,
|
|
3700 see |try-nesting|.
|
|
3701 When during execution of a catch clause another exception is thrown, the
|
|
3702 remaining lines in that catch clause are not executed. The new exception is
|
|
3703 not matched against the patterns in any of the ":catch" commands of the same
|
|
3704 try conditional and none of its catch clauses is taken. If there is, however,
|
|
3705 a finally clause, it is executed, and the exception pends during its
|
|
3706 execution. The commands following the ":endtry" are not executed. The new
|
|
3707 exception might, however, be caught elsewhere, see |try-nesting|.
|
|
3708 When during execution of the finally clause (if present) an exception is
|
|
3709 thrown, the remaining lines in the finally clause are skipped. If the finally
|
|
3710 clause has been taken because of an exception from the try block or one of the
|
|
3711 catch clauses, the original (pending) exception is discarded. The commands
|
|
3712 following the ":endtry" are not executed, and the exception from the finally
|
|
3713 clause is propagated and can be caught elsewhere, see |try-nesting|.
|
|
3714
|
|
3715 The finally clause is also executed, when a ":break" or ":continue" for
|
|
3716 a ":while" loop enclosing the complete try conditional is executed from the
|
|
3717 try block or a catch clause. Or when a ":return" or ":finish" is executed
|
|
3718 from the try block or a catch clause of a try conditional in a function or
|
|
3719 sourced script, respectively. The ":break", ":continue", ":return", or
|
|
3720 ":finish" pends during execution of the finally clause and is resumed when the
|
|
3721 ":endtry" is reached. It is, however, discarded when an exception is thrown
|
|
3722 from the finally clause.
|
|
3723 When a ":break" or ":continue" for a ":while" loop enclosing the complete
|
|
3724 try conditional or when a ":return" or ":finish" is encountered in the finally
|
|
3725 clause, the rest of the finally clause is skipped, and the ":break",
|
|
3726 ":continue", ":return" or ":finish" is executed as usual. If the finally
|
|
3727 clause has been taken because of an exception or an earlier ":break",
|
|
3728 ":continue", ":return", or ":finish" from the try block or a catch clause,
|
|
3729 this pending exception or command is discarded.
|
|
3730
|
|
3731 For examples see |throw-catch| and |try-finally|.
|
|
3732
|
|
3733
|
|
3734 NESTING OF TRY CONDITIONALS *try-nesting*
|
|
3735
|
|
3736 Try conditionals can be nested arbitrarily. That is, a complete try
|
|
3737 conditional can be put into the try block, a catch clause, or the finally
|
|
3738 clause of another try conditional. If the inner try conditional does not
|
|
3739 catch an exception thrown in its try block or throws a new exception from one
|
|
3740 of its catch clauses or its finally clause, the outer try conditional is
|
|
3741 checked according to the rules above. If the inner try conditional is in the
|
|
3742 try block of the outer try conditional, its catch clauses are checked, but
|
|
3743 otherwise only the finally clause is executed. It does not matter for
|
|
3744 nesting, whether the inner try conditional is directly contained in the outer
|
|
3745 one, or whether the outer one sources a script or calls a function containing
|
|
3746 the inner try conditional.
|
|
3747
|
|
3748 When none of the active try conditionals catches an exception, just their
|
|
3749 finally clauses are executed. Thereafter, the script processing terminates.
|
|
3750 An error message is displayed in case of an uncaught exception explicitly
|
|
3751 thrown by a ":throw" command. For uncaught error and interrupt exceptions
|
|
3752 implicitly raised by Vim, the error message(s) or interrupt message are shown
|
|
3753 as usual.
|
|
3754
|
|
3755 For examples see |throw-catch|.
|
|
3756
|
|
3757
|
|
3758 EXAMINING EXCEPTION HANDLING CODE *except-examine*
|
|
3759
|
|
3760 Exception handling code can get tricky. If you are in doubt what happens, set
|
|
3761 'verbose' to 13 or use the ":13verbose" command modifier when sourcing your
|
|
3762 script file. Then you see when an exception is thrown, discarded, caught, or
|
|
3763 finished. When using a verbosity level of at least 14, things pending in
|
|
3764 a finally clause are also shown. This information is also given in debug mode
|
|
3765 (see |debug-scripts|).
|
|
3766
|
|
3767
|
|
3768 THROWING AND CATCHING EXCEPTIONS *throw-catch*
|
|
3769
|
|
3770 You can throw any number or string as an exception. Use the |:throw| command
|
|
3771 and pass the value to be thrown as argument: >
|
|
3772 :throw 4711
|
|
3773 :throw "string"
|
|
3774 < *throw-expression*
|
|
3775 You can also specify an expression argument. The expression is then evaluated
|
|
3776 first, and the result is thrown: >
|
|
3777 :throw 4705 + strlen("string")
|
|
3778 :throw strpart("strings", 0, 6)
|
|
3779
|
|
3780 An exception might be thrown during evaluation of the argument of the ":throw"
|
|
3781 command. Unless it is caught there, the expression evaluation is abandoned.
|
|
3782 The ":throw" command then does not throw a new exception.
|
|
3783 Example: >
|
|
3784
|
|
3785 :function! Foo(arg)
|
|
3786 : try
|
|
3787 : throw a:arg
|
|
3788 : catch /foo/
|
|
3789 : endtry
|
|
3790 : return 1
|
|
3791 :endfunction
|
|
3792 :
|
|
3793 :function! Bar()
|
|
3794 : echo "in Bar"
|
|
3795 : return 4710
|
|
3796 :endfunction
|
|
3797 :
|
|
3798 :throw Foo("arrgh") + Bar()
|
|
3799
|
|
3800 This throws "arrgh", and "in Bar" is not displayed since Bar() is not
|
|
3801 executed. >
|
|
3802 :throw Foo("foo") + Bar()
|
|
3803 however displays "in Bar" and throws 4711.
|
|
3804
|
|
3805 Any other command that takes an expression as argument might also be
|
|
3806 abandoned by an (uncaught) exception during the expression evaluation. The
|
|
3807 exception is then propagated to the caller of the command.
|
|
3808 Example: >
|
|
3809
|
|
3810 :if Foo("arrgh")
|
|
3811 : echo "then"
|
|
3812 :else
|
|
3813 : echo "else"
|
|
3814 :endif
|
|
3815
|
|
3816 Here neither of "then" or "else" is displayed.
|
|
3817
|
|
3818 *catch-order*
|
|
3819 Exceptions can be caught by a try conditional with one or more |:catch|
|
|
3820 commands, see |try-conditionals|. The values to be caught by each ":catch"
|
|
3821 command can be specified as a pattern argument. The subsequent catch clause
|
|
3822 gets executed when a matching exception is caught.
|
|
3823 Example: >
|
|
3824
|
|
3825 :function! Foo(value)
|
|
3826 : try
|
|
3827 : throw a:value
|
|
3828 : catch /^\d\+$/
|
|
3829 : echo "Number thrown"
|
|
3830 : catch /.*/
|
|
3831 : echo "String thrown"
|
|
3832 : endtry
|
|
3833 :endfunction
|
|
3834 :
|
|
3835 :call Foo(0x1267)
|
|
3836 :call Foo('string')
|
|
3837
|
|
3838 The first call to Foo() displays "Number thrown", the second "String thrown".
|
|
3839 An exception is matched against the ":catch" commands in the order they are
|
|
3840 specified. Only the first match counts. So you should place the more
|
|
3841 specific ":catch" first. The following order does not make sense: >
|
|
3842
|
|
3843 : catch /.*/
|
|
3844 : echo "String thrown"
|
|
3845 : catch /^\d\+$/
|
|
3846 : echo "Number thrown"
|
|
3847
|
|
3848 The first ":catch" here matches always, so that the second catch clause is
|
|
3849 never taken.
|
|
3850
|
|
3851 *throw-variables*
|
|
3852 If you catch an exception by a general pattern, you may access the exact value
|
|
3853 in the variable |v:exception|: >
|
|
3854
|
|
3855 : catch /^\d\+$/
|
|
3856 : echo "Number thrown. Value is" v:exception
|
|
3857
|
|
3858 You may also be interested where an exception was thrown. This is stored in
|
|
3859 |v:throwpoint|. Note that "v:exception" and "v:throwpoint" are valid for the
|
|
3860 exception most recently caught as long it is not finished.
|
|
3861 Example: >
|
|
3862
|
|
3863 :function! Caught()
|
|
3864 : if v:exception != ""
|
|
3865 : echo 'Caught "' . v:exception . '" in ' . v:throwpoint
|
|
3866 : else
|
|
3867 : echo 'Nothing caught'
|
|
3868 : endif
|
|
3869 :endfunction
|
|
3870 :
|
|
3871 :function! Foo()
|
|
3872 : try
|
|
3873 : try
|
|
3874 : try
|
|
3875 : throw 4711
|
|
3876 : finally
|
|
3877 : call Caught()
|
|
3878 : endtry
|
|
3879 : catch /.*/
|
|
3880 : call Caught()
|
|
3881 : throw "oops"
|
|
3882 : endtry
|
|
3883 : catch /.*/
|
|
3884 : call Caught()
|
|
3885 : finally
|
|
3886 : call Caught()
|
|
3887 : endtry
|
|
3888 :endfunction
|
|
3889 :
|
|
3890 :call Foo()
|
|
3891
|
|
3892 This displays >
|
|
3893
|
|
3894 Nothing caught
|
|
3895 Caught "4711" in function Foo, line 4
|
|
3896 Caught "oops" in function Foo, line 10
|
|
3897 Nothing caught
|
|
3898
|
|
3899 A practical example: The following command ":LineNumber" displays the line
|
|
3900 number in the script or function where it has been used: >
|
|
3901
|
|
3902 :function! LineNumber()
|
|
3903 : return substitute(v:throwpoint, '.*\D\(\d\+\).*', '\1', "")
|
|
3904 :endfunction
|
|
3905 :command! LineNumber try | throw "" | catch | echo LineNumber() | endtry
|
|
3906 <
|
|
3907 *try-nested*
|
|
3908 An exception that is not caught by a try conditional can be caught by
|
|
3909 a surrounding try conditional: >
|
|
3910
|
|
3911 :try
|
|
3912 : try
|
|
3913 : throw "foo"
|
|
3914 : catch /foobar/
|
|
3915 : echo "foobar"
|
|
3916 : finally
|
|
3917 : echo "inner finally"
|
|
3918 : endtry
|
|
3919 :catch /foo/
|
|
3920 : echo "foo"
|
|
3921 :endtry
|
|
3922
|
|
3923 The inner try conditional does not catch the exception, just its finally
|
|
3924 clause is executed. The exception is then caught by the outer try
|
|
3925 conditional. The example displays "inner finally" and then "foo".
|
|
3926
|
|
3927 *throw-from-catch*
|
|
3928 You can catch an exception and throw a new one to be caught elsewhere from the
|
|
3929 catch clause: >
|
|
3930
|
|
3931 :function! Foo()
|
|
3932 : throw "foo"
|
|
3933 :endfunction
|
|
3934 :
|
|
3935 :function! Bar()
|
|
3936 : try
|
|
3937 : call Foo()
|
|
3938 : catch /foo/
|
|
3939 : echo "Caught foo, throw bar"
|
|
3940 : throw "bar"
|
|
3941 : endtry
|
|
3942 :endfunction
|
|
3943 :
|
|
3944 :try
|
|
3945 : call Bar()
|
|
3946 :catch /.*/
|
|
3947 : echo "Caught" v:exception
|
|
3948 :endtry
|
|
3949
|
|
3950 This displays "Caught foo, throw bar" and then "Caught bar".
|
|
3951
|
|
3952 *rethrow*
|
|
3953 There is no real rethrow in the Vim script language, but you may throw
|
|
3954 "v:exception" instead: >
|
|
3955
|
|
3956 :function! Bar()
|
|
3957 : try
|
|
3958 : call Foo()
|
|
3959 : catch /.*/
|
|
3960 : echo "Rethrow" v:exception
|
|
3961 : throw v:exception
|
|
3962 : endtry
|
|
3963 :endfunction
|
|
3964 < *try-echoerr*
|
|
3965 Note that this method cannot be used to "rethrow" Vim error or interrupt
|
|
3966 exceptions, because it is not possible to fake Vim internal exceptions.
|
|
3967 Trying so causes an error exception. You should throw your own exception
|
|
3968 denoting the situation. If you want to cause a Vim error exception containing
|
|
3969 the original error exception value, you can use the |:echoerr| command: >
|
|
3970
|
|
3971 :try
|
|
3972 : try
|
|
3973 : asdf
|
|
3974 : catch /.*/
|
|
3975 : echoerr v:exception
|
|
3976 : endtry
|
|
3977 :catch /.*/
|
|
3978 : echo v:exception
|
|
3979 :endtry
|
|
3980
|
|
3981 This code displays
|
|
3982
|
|
3983 Vim(echoerr):Vim:E492: Not an editor command: asdf ~
|
|
3984
|
|
3985
|
|
3986 CLEANUP CODE *try-finally*
|
|
3987
|
|
3988 Scripts often change global settings and restore them at their end. If the
|
|
3989 user however interrupts the script by pressing CTRL-C, the settings remain in
|
|
3990 an inconsistent state. The same may happen to you in the development phase of
|
|
3991 a script when an error occurs or you explicitly throw an exception without
|
|
3992 catching it. You can solve these problems by using a try conditional with
|
|
3993 a finally clause for restoring the settings. Its execution is guaranteed on
|
|
3994 normal control flow, on error, on an explicit ":throw", and on interrupt.
|
|
3995 (Note that errors and interrupts from inside the try conditional are converted
|
|
3996 to exceptions. When not caught, they terminate the script after the finally
|
|
3997 clause has been executed.)
|
|
3998 Example: >
|
|
3999
|
|
4000 :try
|
|
4001 : let s:saved_ts = &ts
|
|
4002 : set ts=17
|
|
4003 :
|
|
4004 : " Do the hard work here.
|
|
4005 :
|
|
4006 :finally
|
|
4007 : let &ts = s:saved_ts
|
|
4008 : unlet s:saved_ts
|
|
4009 :endtry
|
|
4010
|
|
4011 This method should be used locally whenever a function or part of a script
|
|
4012 changes global settings which need to be restored on failure or normal exit of
|
|
4013 that function or script part.
|
|
4014
|
|
4015 *break-finally*
|
|
4016 Cleanup code works also when the try block or a catch clause is left by
|
|
4017 a ":continue", ":break", ":return", or ":finish".
|
|
4018 Example: >
|
|
4019
|
|
4020 :let first = 1
|
|
4021 :while 1
|
|
4022 : try
|
|
4023 : if first
|
|
4024 : echo "first"
|
|
4025 : let first = 0
|
|
4026 : continue
|
|
4027 : else
|
|
4028 : throw "second"
|
|
4029 : endif
|
|
4030 : catch /.*/
|
|
4031 : echo v:exception
|
|
4032 : break
|
|
4033 : finally
|
|
4034 : echo "cleanup"
|
|
4035 : endtry
|
|
4036 : echo "still in while"
|
|
4037 :endwhile
|
|
4038 :echo "end"
|
|
4039
|
|
4040 This displays "first", "cleanup", "second", "cleanup", and "end". >
|
|
4041
|
|
4042 :function! Foo()
|
|
4043 : try
|
|
4044 : return 4711
|
|
4045 : finally
|
|
4046 : echo "cleanup\n"
|
|
4047 : endtry
|
|
4048 : echo "Foo still active"
|
|
4049 :endfunction
|
|
4050 :
|
|
4051 :echo Foo() "returned by Foo"
|
|
4052
|
|
4053 This displays "cleanup" and "4711 returned by Foo". You don't need to add an
|
|
4054 extra ":return" in the finally clause. (Above all, this would override the
|
|
4055 return value.)
|
|
4056
|
|
4057 *except-from-finally*
|
|
4058 Using either of ":continue", ":break", ":return", ":finish", or ":throw" in
|
|
4059 a finally clause is possible, but not recommended since it abandons the
|
|
4060 cleanup actions for the try conditional. But, of course, interrupt and error
|
|
4061 exceptions might get raised from a finally clause.
|
|
4062 Example where an error in the finally clause stops an interrupt from
|
|
4063 working correctly: >
|
|
4064
|
|
4065 :try
|
|
4066 : try
|
|
4067 : echo "Press CTRL-C for interrupt"
|
|
4068 : while 1
|
|
4069 : endwhile
|
|
4070 : finally
|
|
4071 : unlet novar
|
|
4072 : endtry
|
|
4073 :catch /novar/
|
|
4074 :endtry
|
|
4075 :echo "Script still running"
|
|
4076 :sleep 1
|
|
4077
|
|
4078 If you need to put commands that could fail into a finally clause, you should
|
|
4079 think about catching or ignoring the errors in these commands, see
|
|
4080 |catch-errors| and |ignore-errors|.
|
|
4081
|
|
4082
|
|
4083 CATCHING ERRORS *catch-errors*
|
|
4084
|
|
4085 If you want to catch specific errors, you just have to put the code to be
|
|
4086 watched in a try block and add a catch clause for the error message. The
|
|
4087 presence of the try conditional causes all errors to be converted to an
|
|
4088 exception. No message is displayed and |v:errmsg| is not set then. To find
|
|
4089 the right pattern for the ":catch" command, you have to know how the format of
|
|
4090 the error exception is.
|
|
4091 Error exceptions have the following format: >
|
|
4092
|
|
4093 Vim({cmdname}):{errmsg}
|
|
4094 or >
|
|
4095 Vim:{errmsg}
|
|
4096
|
|
4097 {cmdname} is the name of the command that failed; the second form is used when
|
|
4098 the command name is not known. {errmsg} is the error message usually produced
|
|
4099 when the error occurs outside try conditionals. It always begins with
|
|
4100 a capital "E", followed by a two or three-digit error number, a colon, and
|
|
4101 a space.
|
|
4102
|
|
4103 Examples:
|
|
4104
|
|
4105 The command >
|
|
4106 :unlet novar
|
|
4107 normally produces the error message >
|
|
4108 E108: No such variable: "novar"
|
|
4109 which is converted inside try conditionals to an exception >
|
|
4110 Vim(unlet):E108: No such variable: "novar"
|
|
4111
|
|
4112 The command >
|
|
4113 :dwim
|
|
4114 normally produces the error message >
|
|
4115 E492: Not an editor command: dwim
|
|
4116 which is converted inside try conditionals to an exception >
|
|
4117 Vim:E492: Not an editor command: dwim
|
|
4118
|
|
4119 You can catch all ":unlet" errors by a >
|
|
4120 :catch /^Vim(unlet):/
|
|
4121 or all errors for misspelled command names by a >
|
|
4122 :catch /^Vim:E492:/
|
|
4123
|
|
4124 Some error messages may be produced by different commands: >
|
|
4125 :function nofunc
|
|
4126 and >
|
|
4127 :delfunction nofunc
|
|
4128 both produce the error message >
|
|
4129 E128: Function name must start with a capital: nofunc
|
|
4130 which is converted inside try conditionals to an exception >
|
|
4131 Vim(function):E128: Function name must start with a capital: nofunc
|
|
4132 or >
|
|
4133 Vim(delfunction):E128: Function name must start with a capital: nofunc
|
|
4134 respectively. You can catch the error by its number independently on the
|
|
4135 command that caused it if you use the following pattern: >
|
|
4136 :catch /^Vim(\a\+):E128:/
|
|
4137
|
|
4138 Some commands like >
|
|
4139 :let x = novar
|
|
4140 produce multiple error messages, here: >
|
|
4141 E121: Undefined variable: novar
|
|
4142 E15: Invalid expression: novar
|
|
4143 Only the first is used for the exception value, since it is the most specific
|
|
4144 one (see |except-several-errors|). So you can catch it by >
|
|
4145 :catch /^Vim(\a\+):E121:/
|
|
4146
|
|
4147 You can catch all errors related to the name "nofunc" by >
|
|
4148 :catch /\<nofunc\>/
|
|
4149
|
|
4150 You can catch all Vim errors in the ":write" and ":read" commands by >
|
|
4151 :catch /^Vim(\(write\|read\)):E\d\+:/
|
|
4152
|
|
4153 You can catch all Vim errors by the pattern >
|
|
4154 :catch /^Vim\((\a\+)\)\=:E\d\+:/
|
|
4155 <
|
|
4156 *catch-text*
|
|
4157 NOTE: You should never catch the error message text itself: >
|
|
4158 :catch /No such variable/
|
|
4159 only works in the english locale, but not when the user has selected
|
|
4160 a different language by the |:language| command. It is however helpful to
|
|
4161 cite the message text in a comment: >
|
|
4162 :catch /^Vim(\a\+):E108:/ " No such variable
|
|
4163
|
|
4164
|
|
4165 IGNORING ERRORS *ignore-errors*
|
|
4166
|
|
4167 You can ignore errors in a specific Vim command by catching them locally: >
|
|
4168
|
|
4169 :try
|
|
4170 : write
|
|
4171 :catch
|
|
4172 :endtry
|
|
4173
|
|
4174 But you are strongly recommended NOT to use this simple form, since it could
|
|
4175 catch more than you want. With the ":write" command, some autocommands could
|
|
4176 be executed and cause errors not related to writing, for instance: >
|
|
4177
|
|
4178 :au BufWritePre * unlet novar
|
|
4179
|
|
4180 There could even be such errors you are not responsible for as a script
|
|
4181 writer: a user of your script might have defined such autocommands. You would
|
|
4182 then hide the error from the user.
|
|
4183 It is much better to use >
|
|
4184
|
|
4185 :try
|
|
4186 : write
|
|
4187 :catch /^Vim(write):/
|
|
4188 :endtry
|
|
4189
|
|
4190 which only catches real write errors. So catch only what you'd like to ignore
|
|
4191 intentionally.
|
|
4192
|
|
4193 For a single command that does not cause execution of autocommands, you could
|
|
4194 even suppress the conversion of errors to exceptions by the ":silent!"
|
|
4195 command: >
|
|
4196 :silent! nunmap k
|
|
4197 This works also when a try conditional is active.
|
|
4198
|
|
4199
|
|
4200 CATCHING INTERRUPTS *catch-interrupt*
|
|
4201
|
|
4202 When there are active try conditionals, an interrupt (CTRL-C) is converted to
|
|
4203 the exception "Vim:Interrupt". You can catch it like every exception. The
|
|
4204 script is not terminated, then.
|
|
4205 Example: >
|
|
4206
|
|
4207 :function! TASK1()
|
|
4208 : sleep 10
|
|
4209 :endfunction
|
|
4210
|
|
4211 :function! TASK2()
|
|
4212 : sleep 20
|
|
4213 :endfunction
|
|
4214
|
|
4215 :while 1
|
|
4216 : let command = input("Type a command: ")
|
|
4217 : try
|
|
4218 : if command == ""
|
|
4219 : continue
|
|
4220 : elseif command == "END"
|
|
4221 : break
|
|
4222 : elseif command == "TASK1"
|
|
4223 : call TASK1()
|
|
4224 : elseif command == "TASK2"
|
|
4225 : call TASK2()
|
|
4226 : else
|
|
4227 : echo "\nIllegal command:" command
|
|
4228 : continue
|
|
4229 : endif
|
|
4230 : catch /^Vim:Interrupt$/
|
|
4231 : echo "\nCommand interrupted"
|
|
4232 : " Caught the interrupt. Continue with next prompt.
|
|
4233 : endtry
|
|
4234 :endwhile
|
|
4235
|
|
4236 You can interrupt a task here by pressing CTRL-C; the script then asks for
|
|
4237 a new command. If you press CTRL-C at the prompt, the script is terminated.
|
|
4238
|
|
4239 For testing what happens when CTRL-C would be pressed on a specific line in
|
|
4240 your script, use the debug mode and execute the |>quit| or |>interrupt|
|
|
4241 command on that line. See |debug-scripts|.
|
|
4242
|
|
4243
|
|
4244 CATCHING ALL *catch-all*
|
|
4245
|
|
4246 The commands >
|
|
4247
|
|
4248 :catch /.*/
|
|
4249 :catch //
|
|
4250 :catch
|
|
4251
|
|
4252 catch everything, error exceptions, interrupt exceptions and exceptions
|
|
4253 explicitly thrown by the |:throw| command. This is useful at the top level of
|
|
4254 a script in order to catch unexpected things.
|
|
4255 Example: >
|
|
4256
|
|
4257 :try
|
|
4258 :
|
|
4259 : " do the hard work here
|
|
4260 :
|
|
4261 :catch /MyException/
|
|
4262 :
|
|
4263 : " handle known problem
|
|
4264 :
|
|
4265 :catch /^Vim:Interrupt$/
|
|
4266 : echo "Script interrupted"
|
|
4267 :catch /.*/
|
|
4268 : echo "Internal error (" . v:exception . ")"
|
|
4269 : echo " - occurred at " . v:throwpoint
|
|
4270 :endtry
|
|
4271 :" end of script
|
|
4272
|
|
4273 Note: Catching all might catch more things than you want. Thus, you are
|
|
4274 strongly encouraged to catch only for problems that you can really handle by
|
|
4275 specifying a pattern argument to the ":catch".
|
|
4276 Example: Catching all could make it nearly impossible to interrupt a script
|
|
4277 by pressing CTRL-C: >
|
|
4278
|
|
4279 :while 1
|
|
4280 : try
|
|
4281 : sleep 1
|
|
4282 : catch
|
|
4283 : endtry
|
|
4284 :endwhile
|
|
4285
|
|
4286
|
|
4287 EXCEPTIONS AND AUTOCOMMANDS *except-autocmd*
|
|
4288
|
|
4289 Exceptions may be used during execution of autocommands. Example: >
|
|
4290
|
|
4291 :autocmd User x try
|
|
4292 :autocmd User x throw "Oops!"
|
|
4293 :autocmd User x catch
|
|
4294 :autocmd User x echo v:exception
|
|
4295 :autocmd User x endtry
|
|
4296 :autocmd User x throw "Arrgh!"
|
|
4297 :autocmd User x echo "Should not be displayed"
|
|
4298 :
|
|
4299 :try
|
|
4300 : doautocmd User x
|
|
4301 :catch
|
|
4302 : echo v:exception
|
|
4303 :endtry
|
|
4304
|
|
4305 This displays "Oops!" and "Arrgh!".
|
|
4306
|
|
4307 *except-autocmd-Pre*
|
|
4308 For some commands, autocommands get executed before the main action of the
|
|
4309 command takes place. If an exception is thrown and not caught in the sequence
|
|
4310 of autocommands, the sequence and the command that caused its execution are
|
|
4311 abandoned and the exception is propagated to the caller of the command.
|
|
4312 Example: >
|
|
4313
|
|
4314 :autocmd BufWritePre * throw "FAIL"
|
|
4315 :autocmd BufWritePre * echo "Should not be displayed"
|
|
4316 :
|
|
4317 :try
|
|
4318 : write
|
|
4319 :catch
|
|
4320 : echo "Caught:" v:exception "from" v:throwpoint
|
|
4321 :endtry
|
|
4322
|
|
4323 Here, the ":write" command does not write the file currently being edited (as
|
|
4324 you can see by checking 'modified'), since the exception from the BufWritePre
|
|
4325 autocommand abandons the ":write". The exception is then caught and the
|
|
4326 script displays: >
|
|
4327
|
|
4328 Caught: FAIL from BufWrite Auto commands for "*"
|
|
4329 <
|
|
4330 *except-autocmd-Post*
|
|
4331 For some commands, autocommands get executed after the main action of the
|
|
4332 command has taken place. If this main action fails and the command is inside
|
|
4333 an active try conditional, the autocommands are skipped and an error exception
|
|
4334 is thrown that can be caught by the caller of the command.
|
|
4335 Example: >
|
|
4336
|
|
4337 :autocmd BufWritePost * echo "File successfully written!"
|
|
4338 :
|
|
4339 :try
|
|
4340 : write /i/m/p/o/s/s/i/b/l/e
|
|
4341 :catch
|
|
4342 : echo v:exception
|
|
4343 :endtry
|
|
4344
|
|
4345 This just displays: >
|
|
4346
|
|
4347 Vim(write):E212: Can't open file for writing (/i/m/p/o/s/s/i/b/l/e)
|
|
4348
|
|
4349 If you really need to execute the autocommands even when the main action
|
|
4350 fails, trigger the event from the catch clause.
|
|
4351 Example: >
|
|
4352
|
|
4353 :autocmd BufWritePre * set noreadonly
|
|
4354 :autocmd BufWritePost * set readonly
|
|
4355 :
|
|
4356 :try
|
|
4357 : write /i/m/p/o/s/s/i/b/l/e
|
|
4358 :catch
|
|
4359 : doautocmd BufWritePost /i/m/p/o/s/s/i/b/l/e
|
|
4360 :endtry
|
|
4361 <
|
|
4362 You can also use ":silent!": >
|
|
4363
|
|
4364 :let x = "ok"
|
|
4365 :let v:errmsg = ""
|
|
4366 :autocmd BufWritePost * if v:errmsg != ""
|
|
4367 :autocmd BufWritePost * let x = "after fail"
|
|
4368 :autocmd BufWritePost * endif
|
|
4369 :try
|
|
4370 : silent! write /i/m/p/o/s/s/i/b/l/e
|
|
4371 :catch
|
|
4372 :endtry
|
|
4373 :echo x
|
|
4374
|
|
4375 This displays "after fail".
|
|
4376
|
|
4377 If the main action of the command does not fail, exceptions from the
|
|
4378 autocommands will be catchable by the caller of the command: >
|
|
4379
|
|
4380 :autocmd BufWritePost * throw ":-("
|
|
4381 :autocmd BufWritePost * echo "Should not be displayed"
|
|
4382 :
|
|
4383 :try
|
|
4384 : write
|
|
4385 :catch
|
|
4386 : echo v:exception
|
|
4387 :endtry
|
|
4388 <
|
|
4389 *except-autocmd-Cmd*
|
|
4390 For some commands, the normal action can be replaced by a sequence of
|
|
4391 autocommands. Exceptions from that sequence will be catchable by the caller
|
|
4392 of the command.
|
|
4393 Example: For the ":write" command, the caller cannot know whether the file
|
|
4394 had actually been written when the exception occurred. You need to tell it in
|
|
4395 some way. >
|
|
4396
|
|
4397 :if !exists("cnt")
|
|
4398 : let cnt = 0
|
|
4399 :
|
|
4400 : autocmd BufWriteCmd * if &modified
|
|
4401 : autocmd BufWriteCmd * let cnt = cnt + 1
|
|
4402 : autocmd BufWriteCmd * if cnt % 3 == 2
|
|
4403 : autocmd BufWriteCmd * throw "BufWriteCmdError"
|
|
4404 : autocmd BufWriteCmd * endif
|
|
4405 : autocmd BufWriteCmd * write | set nomodified
|
|
4406 : autocmd BufWriteCmd * if cnt % 3 == 0
|
|
4407 : autocmd BufWriteCmd * throw "BufWriteCmdError"
|
|
4408 : autocmd BufWriteCmd * endif
|
|
4409 : autocmd BufWriteCmd * echo "File successfully written!"
|
|
4410 : autocmd BufWriteCmd * endif
|
|
4411 :endif
|
|
4412 :
|
|
4413 :try
|
|
4414 : write
|
|
4415 :catch /^BufWriteCmdError$/
|
|
4416 : if &modified
|
|
4417 : echo "Error on writing (file contents not changed)"
|
|
4418 : else
|
|
4419 : echo "Error after writing"
|
|
4420 : endif
|
|
4421 :catch /^Vim(write):/
|
|
4422 : echo "Error on writing"
|
|
4423 :endtry
|
|
4424
|
|
4425 When this script is sourced several times after making changes, it displays
|
|
4426 first >
|
|
4427 File successfully written!
|
|
4428 then >
|
|
4429 Error on writing (file contents not changed)
|
|
4430 then >
|
|
4431 Error after writing
|
|
4432 etc.
|
|
4433
|
|
4434 *except-autocmd-ill*
|
|
4435 You cannot spread a try conditional over autocommands for different events.
|
|
4436 The following code is ill-formed: >
|
|
4437
|
|
4438 :autocmd BufWritePre * try
|
|
4439 :
|
|
4440 :autocmd BufWritePost * catch
|
|
4441 :autocmd BufWritePost * echo v:exception
|
|
4442 :autocmd BufWritePost * endtry
|
|
4443 :
|
|
4444 :write
|
|
4445
|
|
4446
|
|
4447 EXCEPTION HIERARCHIES AND PARAMETERIZED EXCEPTIONS *except-hier-param*
|
|
4448
|
|
4449 Some programming languages allow to use hierarchies of exception classes or to
|
|
4450 pass additional information with the object of an exception class. You can do
|
|
4451 similar things in Vim.
|
|
4452 In order to throw an exception from a hierarchy, just throw the complete
|
|
4453 class name with the components separated by a colon, for instance throw the
|
|
4454 string "EXCEPT:MATHERR:OVERFLOW" for an overflow in a mathematical library.
|
|
4455 When you want to pass additional information with your exception class, add
|
|
4456 it in parentheses, for instance throw the string "EXCEPT:IO:WRITEERR(myfile)"
|
|
4457 for an error when writing "myfile".
|
|
4458 With the appropriate patterns in the ":catch" command, you can catch for
|
|
4459 base classes or derived classes of your hierarchy. Additional information in
|
|
4460 parentheses can be cut out from |v:exception| with the ":substitute" command.
|
|
4461 Example: >
|
|
4462
|
|
4463 :function! CheckRange(a, func)
|
|
4464 : if a:a < 0
|
|
4465 : throw "EXCEPT:MATHERR:RANGE(" . a:func . ")"
|
|
4466 : endif
|
|
4467 :endfunction
|
|
4468 :
|
|
4469 :function! Add(a, b)
|
|
4470 : call CheckRange(a:a, "Add")
|
|
4471 : call CheckRange(a:b, "Add")
|
|
4472 : let c = a:a + a:b
|
|
4473 : if c < 0
|
|
4474 : throw "EXCEPT:MATHERR:OVERFLOW"
|
|
4475 : endif
|
|
4476 : return c
|
|
4477 :endfunction
|
|
4478 :
|
|
4479 :function! Div(a, b)
|
|
4480 : call CheckRange(a:a, "Div")
|
|
4481 : call CheckRange(a:b, "Div")
|
|
4482 : if (a:b == 0)
|
|
4483 : throw "EXCEPT:MATHERR:ZERODIV"
|
|
4484 : endif
|
|
4485 : return a:a / a:b
|
|
4486 :endfunction
|
|
4487 :
|
|
4488 :function! Write(file)
|
|
4489 : try
|
|
4490 : execute "write" a:file
|
|
4491 : catch /^Vim(write):/
|
|
4492 : throw "EXCEPT:IO(" . getcwd() . ", " . a:file . "):WRITEERR"
|
|
4493 : endtry
|
|
4494 :endfunction
|
|
4495 :
|
|
4496 :try
|
|
4497 :
|
|
4498 : " something with arithmetics and I/O
|
|
4499 :
|
|
4500 :catch /^EXCEPT:MATHERR:RANGE/
|
|
4501 : let function = substitute(v:exception, '.*(\(\a\+\)).*', '\1', "")
|
|
4502 : echo "Range error in" function
|
|
4503 :
|
|
4504 :catch /^EXCEPT:MATHERR/ " catches OVERFLOW and ZERODIV
|
|
4505 : echo "Math error"
|
|
4506 :
|
|
4507 :catch /^EXCEPT:IO/
|
|
4508 : let dir = substitute(v:exception, '.*(\(.\+\),\s*.\+).*', '\1', "")
|
|
4509 : let file = substitute(v:exception, '.*(.\+,\s*\(.\+\)).*', '\1', "")
|
|
4510 : if file !~ '^/'
|
|
4511 : let file = dir . "/" . file
|
|
4512 : endif
|
|
4513 : echo 'I/O error for "' . file . '"'
|
|
4514 :
|
|
4515 :catch /^EXCEPT/
|
|
4516 : echo "Unspecified error"
|
|
4517 :
|
|
4518 :endtry
|
|
4519
|
|
4520 The exceptions raised by Vim itself (on error or when pressing CTRL-C) use
|
|
4521 a flat hierarchy: they are all in the "Vim" class. You cannot throw yourself
|
|
4522 exceptions with the "Vim" prefix; they are reserved for Vim.
|
|
4523 Vim error exceptions are parameterized with the name of the command that
|
|
4524 failed, if known. See |catch-errors|.
|
|
4525
|
|
4526
|
|
4527 PECULIARITIES
|
|
4528 *except-compat*
|
|
4529 The exception handling concept requires that the command sequence causing the
|
|
4530 exception is aborted immediately and control is transferred to finally clauses
|
|
4531 and/or a catch clause.
|
|
4532
|
|
4533 In the Vim script language there are cases where scripts and functions
|
|
4534 continue after an error: in functions without the "abort" flag or in a command
|
|
4535 after ":silent!", control flow goes to the following line, and outside
|
|
4536 functions, control flow goes to the line following the outermost ":endwhile"
|
|
4537 or ":endif". On the other hand, errors should be catchable as exceptions
|
|
4538 (thus, requiring the immediate abortion).
|
|
4539
|
|
4540 This problem has been solved by converting errors to exceptions and using
|
|
4541 immediate abortion (if not suppressed by ":silent!") only when a try
|
|
4542 conditional is active. This is no restriction since an (error) exception can
|
|
4543 be caught only from an active try conditional. If you want an immediate
|
|
4544 termination without catching the error, just use a try conditional without
|
|
4545 catch clause. (You can cause cleanup code being executed before termination
|
|
4546 by specifying a finally clause.)
|
|
4547
|
|
4548 When no try conditional is active, the usual abortion and continuation
|
|
4549 behavior is used instead of immediate abortion. This ensures compatibility of
|
|
4550 scripts written for Vim 6.1 and earlier.
|
|
4551
|
|
4552 However, when sourcing an existing script that does not use exception handling
|
|
4553 commands (or when calling one of its functions) from inside an active try
|
|
4554 conditional of a new script, you might change the control flow of the existing
|
|
4555 script on error. You get the immediate abortion on error and can catch the
|
|
4556 error in the new script. If however the sourced script suppresses error
|
|
4557 messages by using the ":silent!" command (checking for errors by testing
|
|
4558 |v:errmsg| if appropriate), its execution path is not changed. The error is
|
|
4559 not converted to an exception. (See |:silent|.) So the only remaining cause
|
|
4560 where this happens is for scripts that don't care about errors and produce
|
|
4561 error messages. You probably won't want to use such code from your new
|
|
4562 scripts.
|
|
4563
|
|
4564 *except-syntax-err*
|
|
4565 Syntax errors in the exception handling commands are never caught by any of
|
|
4566 the ":catch" commands of the try conditional they belong to. Its finally
|
|
4567 clauses, however, is executed.
|
|
4568 Example: >
|
|
4569
|
|
4570 :try
|
|
4571 : try
|
|
4572 : throw 4711
|
|
4573 : catch /\(/
|
|
4574 : echo "in catch with syntax error"
|
|
4575 : catch
|
|
4576 : echo "inner catch-all"
|
|
4577 : finally
|
|
4578 : echo "inner finally"
|
|
4579 : endtry
|
|
4580 :catch
|
|
4581 : echo 'outer catch-all caught "' . v:exception . '"'
|
|
4582 : finally
|
|
4583 : echo "outer finally"
|
|
4584 :endtry
|
|
4585
|
|
4586 This displays: >
|
|
4587 inner finally
|
|
4588 outer catch-all caught "Vim(catch):E54: Unmatched \("
|
|
4589 outer finally
|
|
4590 The original exception is discarded and an error exception is raised, instead.
|
|
4591
|
|
4592 *except-single-line*
|
|
4593 The ":try", ":catch", ":finally", and ":endtry" commands can be put on
|
|
4594 a single line, but then syntax errors may make it difficult to recognize the
|
|
4595 "catch" line, thus you better avoid this.
|
|
4596 Example: >
|
|
4597 :try | unlet! foo # | catch | endtry
|
|
4598 raises an error exception for the trailing characters after the ":unlet!"
|
|
4599 argument, but does not see the ":catch" and ":endtry" commands, so that the
|
|
4600 error exception is discarded and the "E488: Trailing characters" message gets
|
|
4601 displayed.
|
|
4602
|
|
4603 *except-several-errors*
|
|
4604 When several errors appear in a single command, the first error message is
|
|
4605 usually the most specific one and therefor converted to the error exception.
|
|
4606 Example: >
|
|
4607 echo novar
|
|
4608 causes >
|
|
4609 E121: Undefined variable: novar
|
|
4610 E15: Invalid expression: novar
|
|
4611 The value of the error exception inside try conditionals is: >
|
|
4612 Vim(echo):E121: Undefined variable: novar
|
|
4613 < *except-syntax-error*
|
|
4614 But when a syntax error is detected after a normal error in the same command,
|
|
4615 the syntax error is used for the exception being thrown.
|
|
4616 Example: >
|
|
4617 unlet novar #
|
|
4618 causes >
|
|
4619 E108: No such variable: "novar"
|
|
4620 E488: Trailing characters
|
|
4621 The value of the error exception inside try conditionals is: >
|
|
4622 Vim(unlet):E488: Trailing characters
|
|
4623 This is done because the syntax error might change the execution path in a way
|
|
4624 not intended by the user. Example: >
|
|
4625 try
|
|
4626 try | unlet novar # | catch | echo v:exception | endtry
|
|
4627 catch /.*/
|
|
4628 echo "outer catch:" v:exception
|
|
4629 endtry
|
|
4630 This displays "outer catch: Vim(unlet):E488: Trailing characters", and then
|
|
4631 a "E600: Missing :endtry" error message is given, see |except-single-line|.
|
|
4632
|
|
4633 ==============================================================================
|
|
4634 9. Examples *eval-examples*
|
|
4635
|
|
4636 Printing in Hex ~
|
|
4637 >
|
|
4638 :" The function Nr2Hex() returns the Hex string of a number.
|
|
4639 :func Nr2Hex(nr)
|
|
4640 : let n = a:nr
|
|
4641 : let r = ""
|
|
4642 : while n
|
|
4643 : let r = '0123456789ABCDEF'[n % 16] . r
|
|
4644 : let n = n / 16
|
|
4645 : endwhile
|
|
4646 : return r
|
|
4647 :endfunc
|
|
4648
|
|
4649 :" The function String2Hex() converts each character in a string to a two
|
|
4650 :" character Hex string.
|
|
4651 :func String2Hex(str)
|
|
4652 : let out = ''
|
|
4653 : let ix = 0
|
|
4654 : while ix < strlen(a:str)
|
|
4655 : let out = out . Nr2Hex(char2nr(a:str[ix]))
|
|
4656 : let ix = ix + 1
|
|
4657 : endwhile
|
|
4658 : return out
|
|
4659 :endfunc
|
|
4660
|
|
4661 Example of its use: >
|
|
4662 :echo Nr2Hex(32)
|
|
4663 result: "20" >
|
|
4664 :echo String2Hex("32")
|
|
4665 result: "3332"
|
|
4666
|
|
4667
|
|
4668 Sorting lines (by Robert Webb) ~
|
|
4669
|
|
4670 Here is a Vim script to sort lines. Highlight the lines in Vim and type
|
|
4671 ":Sort". This doesn't call any external programs so it'll work on any
|
|
4672 platform. The function Sort() actually takes the name of a comparison
|
|
4673 function as its argument, like qsort() does in C. So you could supply it
|
|
4674 with different comparison functions in order to sort according to date etc.
|
|
4675 >
|
|
4676 :" Function for use with Sort(), to compare two strings.
|
|
4677 :func! Strcmp(str1, str2)
|
|
4678 : if (a:str1 < a:str2)
|
|
4679 : return -1
|
|
4680 : elseif (a:str1 > a:str2)
|
|
4681 : return 1
|
|
4682 : else
|
|
4683 : return 0
|
|
4684 : endif
|
|
4685 :endfunction
|
|
4686
|
|
4687 :" Sort lines. SortR() is called recursively.
|
|
4688 :func! SortR(start, end, cmp)
|
|
4689 : if (a:start >= a:end)
|
|
4690 : return
|
|
4691 : endif
|
|
4692 : let partition = a:start - 1
|
|
4693 : let middle = partition
|
|
4694 : let partStr = getline((a:start + a:end) / 2)
|
|
4695 : let i = a:start
|
|
4696 : while (i <= a:end)
|
|
4697 : let str = getline(i)
|
|
4698 : exec "let result = " . a:cmp . "(str, partStr)"
|
|
4699 : if (result <= 0)
|
|
4700 : " Need to put it before the partition. Swap lines i and partition.
|
|
4701 : let partition = partition + 1
|
|
4702 : if (result == 0)
|
|
4703 : let middle = partition
|
|
4704 : endif
|
|
4705 : if (i != partition)
|
|
4706 : let str2 = getline(partition)
|
|
4707 : call setline(i, str2)
|
|
4708 : call setline(partition, str)
|
|
4709 : endif
|
|
4710 : endif
|
|
4711 : let i = i + 1
|
|
4712 : endwhile
|
|
4713
|
|
4714 : " Now we have a pointer to the "middle" element, as far as partitioning
|
|
4715 : " goes, which could be anywhere before the partition. Make sure it is at
|
|
4716 : " the end of the partition.
|
|
4717 : if (middle != partition)
|
|
4718 : let str = getline(middle)
|
|
4719 : let str2 = getline(partition)
|
|
4720 : call setline(middle, str2)
|
|
4721 : call setline(partition, str)
|
|
4722 : endif
|
|
4723 : call SortR(a:start, partition - 1, a:cmp)
|
|
4724 : call SortR(partition + 1, a:end, a:cmp)
|
|
4725 :endfunc
|
|
4726
|
|
4727 :" To Sort a range of lines, pass the range to Sort() along with the name of a
|
|
4728 :" function that will compare two lines.
|
|
4729 :func! Sort(cmp) range
|
|
4730 : call SortR(a:firstline, a:lastline, a:cmp)
|
|
4731 :endfunc
|
|
4732
|
|
4733 :" :Sort takes a range of lines and sorts them.
|
|
4734 :command! -nargs=0 -range Sort <line1>,<line2>call Sort("Strcmp")
|
|
4735 <
|
|
4736 *sscanf*
|
|
4737 There is no sscanf() function in Vim. If you need to extract parts from a
|
|
4738 line, you can use matchstr() and substitute() to do it. This example shows
|
|
4739 how to get the file name, line number and column number out of a line like
|
|
4740 "foobar.txt, 123, 45". >
|
|
4741 :" Set up the match bit
|
|
4742 :let mx='\(\f\+\),\s*\(\d\+\),\s*\(\d\+\)'
|
|
4743 :"get the part matching the whole expression
|
|
4744 :let l = matchstr(line, mx)
|
|
4745 :"get each item out of the match
|
|
4746 :let file = substitute(l, mx, '\1', '')
|
|
4747 :let lnum = substitute(l, mx, '\2', '')
|
|
4748 :let col = substitute(l, mx, '\3', '')
|
|
4749
|
|
4750 The input is in the variable "line", the results in the variables "file",
|
|
4751 "lnum" and "col". (idea from Michael Geddes)
|
|
4752
|
|
4753 ==============================================================================
|
|
4754 10. No +eval feature *no-eval-feature*
|
|
4755
|
|
4756 When the |+eval| feature was disabled at compile time, none of the expression
|
|
4757 evaluation commands are available. To prevent this from causing Vim scripts
|
|
4758 to generate all kinds of errors, the ":if" and ":endif" commands are still
|
|
4759 recognized, though the argument of the ":if" and everything between the ":if"
|
|
4760 and the matching ":endif" is ignored. Nesting of ":if" blocks is allowed, but
|
|
4761 only if the commands are at the start of the line. The ":else" command is not
|
|
4762 recognized.
|
|
4763
|
|
4764 Example of how to avoid executing commands when the |+eval| feature is
|
|
4765 missing: >
|
|
4766
|
|
4767 :if 1
|
|
4768 : echo "Expression evaluation is compiled in"
|
|
4769 :else
|
|
4770 : echo "You will _never_ see this message"
|
|
4771 :endif
|
|
4772
|
|
4773 ==============================================================================
|
|
4774 11. The sandbox *eval-sandbox* *sandbox* *E48*
|
|
4775
|
|
4776 The 'foldexpr', 'includeexpr', 'indentexpr', 'statusline' and 'foldtext'
|
|
4777 options are evaluated in a sandbox. This means that you are protected from
|
|
4778 these expressions having nasty side effects. This gives some safety for when
|
|
4779 these options are set from a modeline. It is also used when the command from
|
|
4780 a tags file is executed.
|
29
|
4781 The sandbox is also used for the |:sandbox| command.
|
7
|
4782
|
|
4783 These items are not allowed in the sandbox:
|
|
4784 - changing the buffer text
|
|
4785 - defining or changing mapping, autocommands, functions, user commands
|
|
4786 - setting certain options (see |option-summary|)
|
|
4787 - executing a shell command
|
|
4788 - reading or writing a file
|
|
4789 - jumping to another buffer or editing a file
|
29
|
4790 This is not guaranteed 100% secure, but it should block most attacks.
|
|
4791
|
|
4792 *:san* *:sandbox*
|
|
4793 :sandbox {cmd} Execute {cmd} in the sandbox. Useful to evaluate an
|
|
4794 option that may have been set from a modeline, e.g.
|
|
4795 'foldexpr'.
|
|
4796
|
7
|
4797
|
|
4798 vim:tw=78:ts=8:ft=help:norl:
|