2022-01-29 22:20:48 +00:00
|
|
|
*usr_41.txt* For Vim version 8.2. Last change: 2022 Jan 28
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
VIM USER MANUAL - by Bram Moolenaar
|
|
|
|
|
|
|
|
Write a Vim script
|
|
|
|
|
|
|
|
|
|
|
|
The Vim script language is used for the startup vimrc file, syntax files, and
|
|
|
|
many other things. This chapter explains the items that can be used in a Vim
|
|
|
|
script. There are a lot of them, thus this is a long chapter.
|
|
|
|
|
|
|
|
|41.1| Introduction
|
|
|
|
|41.2| Variables
|
|
|
|
|41.3| Expressions
|
|
|
|
|41.4| Conditionals
|
|
|
|
|41.5| Executing an expression
|
|
|
|
|41.6| Using functions
|
|
|
|
|41.7| Defining a function
|
2005-02-07 22:01:03 +00:00
|
|
|
|41.8| Lists and Dictionaries
|
|
|
|
|41.9| Exceptions
|
|
|
|
|41.10| Various remarks
|
|
|
|
|41.11| Writing a plugin
|
|
|
|
|41.12| Writing a filetype plugin
|
|
|
|
|41.13| Writing a compiler plugin
|
2005-02-26 23:04:13 +00:00
|
|
|
|41.14| Writing a plugin that loads quickly
|
|
|
|
|41.15| Writing library scripts
|
2006-03-21 21:23:25 +00:00
|
|
|
|41.16| Distributing Vim scripts
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
Next chapter: |usr_42.txt| Add new menus
|
|
|
|
Previous chapter: |usr_40.txt| Make new commands
|
|
|
|
Table of contents: |usr_toc.txt|
|
|
|
|
|
|
|
|
==============================================================================
|
2005-01-25 21:57:23 +00:00
|
|
|
*41.1* Introduction *vim-script-intro* *script*
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
Your first experience with Vim scripts is the vimrc file. Vim reads it when
|
|
|
|
it starts up and executes the commands. You can set options to values you
|
|
|
|
prefer. And you can use any colon command in it (commands that start with a
|
|
|
|
":"; these are sometimes referred to as Ex commands or command-line commands).
|
2021-12-30 20:24:12 +00:00
|
|
|
|
|
|
|
Syntax files are also Vim scripts. As are files that set options for a
|
2004-06-13 20:20:40 +00:00
|
|
|
specific file type. A complicated macro can be defined by a separate Vim
|
|
|
|
script file. You can think of other uses yourself.
|
|
|
|
|
2020-06-14 17:29:55 +02:00
|
|
|
If you are familiar with Python, you can find a comparison between
|
|
|
|
Python and Vim script here, with pointers to other documents:
|
|
|
|
https://gist.github.com/yegappan/16d964a37ead0979b05e655aa036cad0
|
2020-08-07 19:54:59 +02:00
|
|
|
And if you are familiar with JavaScript:
|
2020-06-14 17:29:55 +02:00
|
|
|
https://w0rp.com/blog/post/vim-script-for-the-javascripter/
|
2020-06-04 15:22:21 +02:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Vim script comes in two flavors: legacy and |Vim9|. Since this help file is
|
|
|
|
for new users, we'll teach you the newer and more convenient |Vim9| syntax.
|
|
|
|
|
|
|
|
To try out Vim script the best way is to edit a script file and source it.
|
|
|
|
Basically: >
|
|
|
|
:edit test.vim
|
|
|
|
[insert the script lines you want]
|
|
|
|
:w
|
|
|
|
:source %
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
Let's start with a simple example: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
vim9script
|
|
|
|
var i = 1
|
|
|
|
while i < 5
|
|
|
|
echo "count is" i
|
|
|
|
i += 1
|
|
|
|
endwhile
|
2004-06-13 20:20:40 +00:00
|
|
|
<
|
2005-02-07 22:01:03 +00:00
|
|
|
The output of the example code is:
|
|
|
|
|
|
|
|
count is 1 ~
|
|
|
|
count is 2 ~
|
|
|
|
count is 3 ~
|
|
|
|
count is 4 ~
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
In the first line the `vim9script` command makes clear this is a new, |Vim9|
|
|
|
|
script file. That matters for how the rest of the file is used.
|
|
|
|
|
|
|
|
The `var i = 1` command declares the "i" variable and initializes it. The
|
2005-02-07 22:01:03 +00:00
|
|
|
generic form is: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var {name} = {expression}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
In this case the variable name is "i" and the expression is a simple value,
|
|
|
|
the number one.
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The `while` command starts a loop. The generic form is: >
|
|
|
|
|
|
|
|
while {condition}
|
|
|
|
{statements}
|
|
|
|
endwhile
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The statements until the matching `endwhile` are executed for as long as the
|
2004-06-13 20:20:40 +00:00
|
|
|
condition is true. The condition used here is the expression "i < 5". This
|
|
|
|
is true when the variable i is smaller than five.
|
2005-02-07 22:01:03 +00:00
|
|
|
Note:
|
|
|
|
If you happen to write a while loop that keeps on running, you can
|
|
|
|
interrupt it by pressing CTRL-C (CTRL-Break on MS-Windows).
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The `echo` command prints its arguments. In this case the string "count is"
|
2005-02-07 22:01:03 +00:00
|
|
|
and the value of the variable i. Since i is one, this will print:
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
count is 1 ~
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Then there is the `i += 1` command. This does the same thing as "i = i + 1",
|
|
|
|
it adds one to the variable i and assigns the new value to the same variable.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2005-02-07 22:01:03 +00:00
|
|
|
The example was given to explain the commands, but would you really want to
|
2017-03-05 17:04:09 +01:00
|
|
|
make such a loop, it can be written much more compact: >
|
2005-01-17 22:11:23 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
for i in range(1, 4)
|
|
|
|
echo "count is" i
|
|
|
|
endfor
|
2005-01-17 22:11:23 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
We won't explain how `for` and `range()` work until later. Follow the links
|
2005-02-07 22:01:03 +00:00
|
|
|
if you are impatient.
|
2005-01-17 22:11:23 +00:00
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-05-31 21:41:05 +02:00
|
|
|
FOUR KINDS OF NUMBERS
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-04-21 18:09:37 +02:00
|
|
|
Numbers can be decimal, hexadecimal, octal or binary.
|
|
|
|
|
|
|
|
A hexadecimal number starts with "0x" or "0X". For example "0x1f" is decimal
|
|
|
|
31.
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
An octal number starts with "0o", "0O". "0o17" is decimal 15.
|
2021-04-21 18:09:37 +02:00
|
|
|
|
|
|
|
A binary number starts with "0b" or "0B". For example "0b101" is decimal 5.
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
A decimal number is just digits. Careful: In legacy script don't put a zero
|
|
|
|
before a decimal number, it will be interpreted as an octal number!
|
2021-04-21 18:09:37 +02:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The `echo` command evaluates its argument and always prints decimal numbers.
|
|
|
|
Example: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
echo 0x7f 0o36
|
2004-06-13 20:20:40 +00:00
|
|
|
< 127 30 ~
|
|
|
|
|
2019-05-31 21:41:05 +02:00
|
|
|
A number is made negative with a minus sign. This also works for hexadecimal,
|
2021-12-30 20:24:12 +00:00
|
|
|
octal and binary numbers: >
|
|
|
|
|
|
|
|
echo -0x7f
|
|
|
|
< -127 ~
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
A minus sign is also used for subtraction. This can sometimes lead to
|
|
|
|
confusion. If we put a minus sign before both numbers we get an error: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
echo -0x7f -0o36
|
|
|
|
< E1004: White space required before and after '-' at "-0o36" ~
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Note: if you are not using a |Vim9| script to try out these commands but type
|
|
|
|
them directly, they will be executed as legacy script. Then the echo command
|
|
|
|
sees the second minus sign as subtraction. To get the error, prefix the
|
|
|
|
command with `vim9cmd`: >
|
|
|
|
|
|
|
|
vim9cmd echo -0x7f -0o36
|
|
|
|
< E1004: White space required before and after '-' at "-0o36" ~
|
|
|
|
|
|
|
|
White space in an expression is often required to make sure it is easy to read
|
|
|
|
and avoid errors. Such as thinking that the "-0o36" above makes the number
|
|
|
|
negative, while it is actually seen as a subtraction.
|
|
|
|
|
|
|
|
To actually have the minus sign be used for negation, you can put the second
|
|
|
|
expression in parenthesis: >
|
|
|
|
|
|
|
|
echo -0x7f (-0o36)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
==============================================================================
|
|
|
|
*41.2* Variables
|
|
|
|
|
|
|
|
A variable name consists of ASCII letters, digits and the underscore. It
|
|
|
|
cannot start with a digit. Valid variable names are:
|
|
|
|
|
|
|
|
counter
|
|
|
|
_aap3
|
|
|
|
very_long_variable_name_with_underscores
|
|
|
|
FuncLength
|
|
|
|
LENGTH
|
|
|
|
|
|
|
|
Invalid names are "foo+bar" and "6var".
|
2021-12-30 20:24:12 +00:00
|
|
|
|
|
|
|
Some variables are global. To see a list of currently defined global
|
|
|
|
variables type this command: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
:let
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
You can use global variables everywhere. However, it is easy to use the same
|
|
|
|
name in two unrelated scripts. Therefore variables declared in a script are
|
|
|
|
local to that script. For example, if you have this in "script1.vim": >
|
|
|
|
|
|
|
|
vim9script
|
|
|
|
var counter = 5
|
|
|
|
echo counter
|
|
|
|
< 5 ~
|
|
|
|
|
|
|
|
And you try to use the variable in "script2.vim": >
|
|
|
|
|
|
|
|
vim9script
|
|
|
|
echo counter
|
|
|
|
< E121: Undefined variable: counter ~
|
|
|
|
|
|
|
|
Using a script-local variable means you can be sure that it is only changed in
|
|
|
|
that script and not elsewhere.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
If you do want to share variables between scripts, use the "g:" prefix and
|
|
|
|
assign the value directly, do not use `var`. Thus in "script1.vim": >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
vim9script
|
|
|
|
g:counter = 5
|
|
|
|
echo g:counter
|
|
|
|
< 5 ~
|
|
|
|
|
|
|
|
And then in "script2.vim": >
|
|
|
|
|
|
|
|
vim9script
|
|
|
|
echo g:counter
|
|
|
|
< 5 ~
|
|
|
|
|
|
|
|
More about script-local variables here: |script-variable|.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
There are more kinds of variables, see |internal-variables|. The most often
|
|
|
|
used ones are:
|
|
|
|
|
|
|
|
b:name variable local to a buffer
|
|
|
|
w:name variable local to a window
|
|
|
|
g:name global variable (also in a function)
|
|
|
|
v:name variable predefined by Vim
|
|
|
|
|
|
|
|
|
|
|
|
DELETING VARIABLES
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Variables take up memory and show up in the output of the `let` command. To
|
|
|
|
delete a global variable use the `unlet` command. Example: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
unlet g:counter
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
This deletes the global variable "g:counter" to free up the memory it uses.
|
|
|
|
If you are not sure if the variable exists, and don't want an error message
|
|
|
|
when it doesn't, append !: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
unlet! g:counter
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
You cannot `unlet` script-local variables in |Vim9| script. You can in legacy
|
|
|
|
script.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
When a script finishes, the local variables declared there will not be
|
|
|
|
deleted. Functions defined in the script can use them. Example:
|
|
|
|
>
|
|
|
|
vim9script
|
|
|
|
var counter = 0
|
|
|
|
def g:GetCount(): number
|
|
|
|
s:counter += 1
|
|
|
|
return s:counter
|
|
|
|
enddef
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Every time you call the function it will return the next count: >
|
|
|
|
:echo g:GetCount()
|
|
|
|
< 1 ~
|
|
|
|
>
|
|
|
|
:echo g:GetCount()
|
|
|
|
< 2 ~
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
If you are worried a script-local variable is consuming too much
|
|
|
|
memory, set it to an empty value after you no longer need it.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Note: below we'll leave out the `vim9script` line, so we can concentrate on
|
|
|
|
the relevant commands, but you'll still need to put it at the top of your
|
|
|
|
script file.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
STRING VARIABLES AND CONSTANTS
|
|
|
|
|
|
|
|
So far only numbers were used for the variable value. Strings can be used as
|
2005-02-07 22:01:03 +00:00
|
|
|
well. Numbers and strings are the basic types of variables that Vim supports.
|
2021-12-30 20:24:12 +00:00
|
|
|
Example: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var name = "Peter"
|
|
|
|
echo name
|
2022-01-08 21:51:59 +00:00
|
|
|
< Peter ~
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Every variable has a type. Very often, as in this example, the type is
|
|
|
|
defined by assigning a value. This is called type inference. If you do not
|
|
|
|
want to give the variable a value yet, you need to specify the type: >
|
|
|
|
|
|
|
|
var name: string
|
|
|
|
var age: number
|
|
|
|
...
|
|
|
|
name = "Peter"
|
|
|
|
age = 42
|
|
|
|
|
|
|
|
If you make a mistake and try to assign the wrong type of value you'll get an
|
|
|
|
error: >
|
|
|
|
|
|
|
|
age = "Peter"
|
|
|
|
< E1012: Type mismatch; expected number but got string ~
|
|
|
|
|
|
|
|
More about types in |41.8|.
|
|
|
|
|
|
|
|
To assign a string value to a variable, you need to use a string constant.
|
|
|
|
There are two types of these. First the string in double quotes, as we used
|
|
|
|
already. If you want to include a double quote inside the string, put a
|
|
|
|
backslash in front of it: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var name = "he is \"Peter\""
|
|
|
|
echo name
|
|
|
|
< he is "Peter" ~
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
To avoid the need for a backslash, you can use a string in single quotes: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var name = 'he is "Peter"'
|
|
|
|
echo name
|
|
|
|
< he is "Peter" ~
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2005-02-07 22:01:03 +00:00
|
|
|
Inside a single-quote string all the characters are as they are. Only the
|
|
|
|
single quote itself is special: you need to use two to get one. A backslash
|
|
|
|
is taken literally, thus you can't use it to change the meaning of the
|
2021-12-30 20:24:12 +00:00
|
|
|
character after it: >
|
|
|
|
|
|
|
|
var name = 'P\e''ter'''
|
|
|
|
echo name
|
|
|
|
< P\e'ter' ~
|
|
|
|
|
|
|
|
In double-quote strings it is possible to use special characters. Here are a
|
|
|
|
few useful ones:
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
\t <Tab>
|
|
|
|
\n <NL>, line break
|
|
|
|
\r <CR>, <Enter>
|
|
|
|
\e <Esc>
|
|
|
|
\b <BS>, backspace
|
|
|
|
\" "
|
|
|
|
\\ \, backslash
|
|
|
|
\<Esc> <Esc>
|
|
|
|
\<C-W> CTRL-W
|
|
|
|
|
|
|
|
The last two are just examples. The "\<name>" form can be used to include
|
|
|
|
the special key "name".
|
2021-12-30 20:24:12 +00:00
|
|
|
|
|
|
|
See |expr-quote| for the full list of special items in a string.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
==============================================================================
|
|
|
|
*41.3* Expressions
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Vim has a fairly standard way to handle expressions. You can read the
|
2004-06-13 20:20:40 +00:00
|
|
|
definition here: |expression-syntax|. Here we will show the most common
|
|
|
|
items.
|
2021-12-30 20:24:12 +00:00
|
|
|
|
|
|
|
The numbers, strings and variables mentioned above are expressions by
|
2004-06-13 20:20:40 +00:00
|
|
|
themselves. Thus everywhere an expression is expected, you can use a number,
|
|
|
|
string or variable. Other basic items in an expression are:
|
|
|
|
|
|
|
|
$NAME environment variable
|
|
|
|
&name option
|
|
|
|
@r register
|
|
|
|
|
|
|
|
Examples: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
echo "The value of 'tabstop' is" &ts
|
|
|
|
echo "Your home directory is" $HOME
|
|
|
|
if @a == 'text'
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The &name form can also be used to set an option value, do something and
|
|
|
|
restore the old value. Example: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var save_ic = &ic
|
|
|
|
set noic
|
|
|
|
s/The Start/The Beginning/
|
|
|
|
&ic = save_ic
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
This makes sure the "The Start" pattern is used with the 'ignorecase' option
|
2005-02-07 22:01:03 +00:00
|
|
|
off. Still, it keeps the value that the user had set. (Another way to do
|
|
|
|
this would be to add "\C" to the pattern, see |/\C|.)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
MATHEMATICS
|
|
|
|
|
|
|
|
It becomes more interesting if we combine these basic items. Let's start with
|
|
|
|
mathematics on numbers:
|
|
|
|
|
|
|
|
a + b add
|
|
|
|
a - b subtract
|
|
|
|
a * b multiply
|
|
|
|
a / b divide
|
|
|
|
a % b modulo
|
|
|
|
|
|
|
|
The usual precedence is used. Example: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
echo 10 + 5 * 2
|
2004-06-13 20:20:40 +00:00
|
|
|
< 20 ~
|
|
|
|
|
2011-02-25 14:42:19 +01:00
|
|
|
Grouping is done with parentheses. No surprises here. Example: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
echo (10 + 5) * 2
|
2004-06-13 20:20:40 +00:00
|
|
|
< 30 ~
|
|
|
|
|
2020-09-07 22:18:52 +02:00
|
|
|
Strings can be concatenated with ".." (see |expr6|). Example: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
echo "foo" .. "bar"
|
2004-06-13 20:20:40 +00:00
|
|
|
< foobar ~
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
When the "echo" command gets multiple arguments, it separates them with a
|
2004-06-13 20:20:40 +00:00
|
|
|
space. In the example the argument is a single expression, thus no space is
|
|
|
|
inserted.
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Borrowed from the C language is the conditional expression: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
a ? b : c
|
|
|
|
|
|
|
|
If "a" evaluates to true "b" is used, otherwise "c" is used. Example: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var nr = 4
|
|
|
|
echo nr > 5 ? "nr is big" : "nr is small"
|
|
|
|
< nr is small ~
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
The three parts of the constructs are always evaluated first, thus you could
|
2021-12-30 20:24:12 +00:00
|
|
|
see it works as: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
(a) ? (b) : (c)
|
|
|
|
|
|
|
|
==============================================================================
|
|
|
|
*41.4* Conditionals
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The `if` commands executes the following statements, until the matching
|
|
|
|
`endif`, only when a condition is met. The generic form is:
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
if {condition}
|
2004-06-13 20:20:40 +00:00
|
|
|
{statements}
|
2021-12-30 20:24:12 +00:00
|
|
|
endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Only when the expression {condition} evaluates to true or one will the
|
|
|
|
{statements} be executed. If they are not executed they must still be valid
|
|
|
|
commands. If they contain garbage, Vim won't be able to find the matching
|
|
|
|
`endif`.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
You can also use `else`. The generic form for this is:
|
|
|
|
|
|
|
|
if {condition}
|
2004-06-13 20:20:40 +00:00
|
|
|
{statements}
|
2021-12-30 20:24:12 +00:00
|
|
|
else
|
2004-06-13 20:20:40 +00:00
|
|
|
{statements}
|
2021-12-30 20:24:12 +00:00
|
|
|
endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The second {statements} block is only executed if the first one isn't.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Finally, there is `elseif`
|
|
|
|
|
|
|
|
if {condition}
|
2004-06-13 20:20:40 +00:00
|
|
|
{statements}
|
2021-12-30 20:24:12 +00:00
|
|
|
elseif {condition}
|
2004-06-13 20:20:40 +00:00
|
|
|
{statements}
|
2021-12-30 20:24:12 +00:00
|
|
|
endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
This works just like using `else` and then `if`, but without the need for an
|
|
|
|
extra `endif`.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
A useful example for your vimrc file is checking the 'term' option and doing
|
|
|
|
something depending upon its value: >
|
|
|
|
|
|
|
|
if &term == "xterm"
|
|
|
|
# Do stuff for xterm
|
|
|
|
elseif &term == "vt100"
|
|
|
|
# Do stuff for a vt100 terminal
|
|
|
|
else
|
|
|
|
# Do something for other terminals
|
|
|
|
endif
|
|
|
|
|
|
|
|
This uses "#" to start a comment, more about that later.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
LOGIC OPERATIONS
|
|
|
|
|
|
|
|
We already used some of them in the examples. These are the most often used
|
|
|
|
ones:
|
|
|
|
|
|
|
|
a == b equal to
|
|
|
|
a != b not equal to
|
|
|
|
a > b greater than
|
|
|
|
a >= b greater than or equal to
|
|
|
|
a < b less than
|
|
|
|
a <= b less than or equal to
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The result is true if the condition is met and false otherwise. An example: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
if v:version >= 700
|
|
|
|
echo "congratulations"
|
|
|
|
else
|
|
|
|
echo "you are using an old version, upgrade!"
|
|
|
|
endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
Here "v:version" is a variable defined by Vim, which has the value of the Vim
|
2021-12-30 20:24:12 +00:00
|
|
|
version. 600 is for version 6.0, version 6.1 has the value 601. This is
|
2004-06-13 20:20:40 +00:00
|
|
|
very useful to write a script that works with multiple versions of Vim.
|
|
|
|
|v:version|
|
|
|
|
|
|
|
|
The logic operators work both for numbers and strings. When comparing two
|
|
|
|
strings, the mathematical difference is used. This compares byte values,
|
|
|
|
which may not be right for some languages.
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
If you try to compare a string with a number you will get an error.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
For strings there are two more useful items:
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
str =~ pat matches with
|
|
|
|
str !~ pat does not match with
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The left item "str" is used as a string. The right item "pat" is used as a
|
2004-06-13 20:20:40 +00:00
|
|
|
pattern, like what's used for searching. Example: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
if str =~ " "
|
|
|
|
echo "str contains a space"
|
|
|
|
endif
|
|
|
|
if str !~ '\.$'
|
|
|
|
echo "str does not end in a full stop"
|
|
|
|
endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
Notice the use of a single-quote string for the pattern. This is useful,
|
2005-02-07 22:01:03 +00:00
|
|
|
because backslashes would need to be doubled in a double-quote string and
|
|
|
|
patterns tend to contain many backslashes.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The match is not anchored, if you want to match the whole string start with
|
|
|
|
"^" and end with "$".
|
|
|
|
|
|
|
|
The 'ignorecase' option is not used when comparing strings. When you do want
|
|
|
|
to ignore case append "?". Thus "==?" compares two strings to be equal while
|
|
|
|
ignoring case. For the full table see |expr-==|.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
MORE LOOPING
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The `while` command was already mentioned. Two more statements can be used in
|
|
|
|
between the `while` and the `endwhile`:
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
continue Jump back to the start of the while loop; the
|
2004-06-13 20:20:40 +00:00
|
|
|
loop continues.
|
2021-12-30 20:24:12 +00:00
|
|
|
break Jump forward to the `endwhile`; the loop is
|
2004-06-13 20:20:40 +00:00
|
|
|
discontinued.
|
|
|
|
|
|
|
|
Example: >
|
|
|
|
|
2022-01-08 21:51:59 +00:00
|
|
|
var counter = 1
|
2021-12-30 20:24:12 +00:00
|
|
|
while counter < 40
|
2022-01-08 21:51:59 +00:00
|
|
|
if skip_number(counter)
|
2021-12-30 20:24:12 +00:00
|
|
|
continue
|
|
|
|
endif
|
2022-01-08 21:51:59 +00:00
|
|
|
if last_number(counter)
|
2021-12-30 20:24:12 +00:00
|
|
|
break
|
|
|
|
endif
|
|
|
|
sleep 50m
|
2022-01-08 21:51:59 +00:00
|
|
|
++counter
|
2021-12-30 20:24:12 +00:00
|
|
|
endwhile
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The `sleep` command makes Vim take a nap. The "50m" specifies fifty
|
|
|
|
milliseconds. Another example is `sleep 4`, which sleeps for four seconds.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Even more looping can be done with the `for` command, see below in |41.8|.
|
2005-02-07 22:01:03 +00:00
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
==============================================================================
|
|
|
|
*41.5* Executing an expression
|
|
|
|
|
|
|
|
So far the commands in the script were executed by Vim directly. The
|
2021-12-30 20:24:12 +00:00
|
|
|
`execute` command allows executing the result of an expression. This is a
|
2004-06-13 20:20:40 +00:00
|
|
|
very powerful way to build commands and execute them.
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
An example is to jump to a tag, which is contained in a variable: >
|
|
|
|
|
|
|
|
execute "tag " .. tag_name
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2020-09-07 22:18:52 +02:00
|
|
|
The ".." is used to concatenate the string "tag " with the value of variable
|
2004-06-13 20:20:40 +00:00
|
|
|
"tag_name". Suppose "tag_name" has the value "get_cmd", then the command that
|
|
|
|
will be executed is: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
tag get_cmd
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The `execute` command can only execute Ex commands. The `normal` command
|
2004-06-13 20:20:40 +00:00
|
|
|
executes Normal mode commands. However, its argument is not an expression but
|
|
|
|
the literal command characters. Example: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
normal gg=G
|
|
|
|
|
|
|
|
This jumps to the first line with "gg" and formats all lines with the "="
|
|
|
|
operator and the "G" movement.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
To make `normal` work with an expression, combine `execute` with it.
|
2004-06-13 20:20:40 +00:00
|
|
|
Example: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
execute "normal " .. count .. "j"
|
|
|
|
|
|
|
|
This will move the cursor "count" lines down.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Make sure that the argument for `normal` is a complete command. Otherwise
|
2004-06-13 20:20:40 +00:00
|
|
|
Vim will run into the end of the argument and abort the command. For example,
|
2021-12-30 20:24:12 +00:00
|
|
|
if you start the delete operator, you must give the movement command also.
|
|
|
|
This works: >
|
|
|
|
|
|
|
|
normal d$
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
This does nothing: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
normal d
|
|
|
|
|
|
|
|
If you start Insert mode and do not end it with Esc, it will end anyway. This
|
|
|
|
works to insert "new text": >
|
|
|
|
|
|
|
|
execute "normal inew text"
|
|
|
|
|
|
|
|
If you want to do something after inserting text you do need to end Insert
|
|
|
|
mode: >
|
|
|
|
|
|
|
|
execute "normal inew text\<Esc>b"
|
|
|
|
|
|
|
|
This inserts "new text" and puts the cursor on the first letter of "text".
|
|
|
|
Notice the use of the special key "\<Esc>". This avoids having to enter a
|
|
|
|
real <Esc> character in your script. That is where `execute` with a
|
|
|
|
double-quote string comes in handy.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2005-02-07 22:01:03 +00:00
|
|
|
If you don't want to execute a string but evaluate it to get its expression
|
|
|
|
value, you can use the eval() function: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var optname = "path"
|
|
|
|
var optvalue = eval('&' .. optname)
|
2005-02-07 22:01:03 +00:00
|
|
|
|
|
|
|
A "&" character is prepended to "path", thus the argument to eval() is
|
|
|
|
"&path". The result will then be the value of the 'path' option.
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
==============================================================================
|
|
|
|
*41.6* Using functions
|
|
|
|
|
|
|
|
Vim defines many functions and provides a large amount of functionality that
|
|
|
|
way. A few examples will be given in this section. You can find the whole
|
2021-12-30 20:24:12 +00:00
|
|
|
list below: |function-list|.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
A function is called with the `call` command. The parameters are passed in
|
2011-02-25 14:42:19 +01:00
|
|
|
between parentheses separated by commas. Example: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
call search("Date: ", "W")
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
This calls the search() function, with arguments "Date: " and "W". The
|
|
|
|
search() function uses its first argument as a search pattern and the second
|
|
|
|
one as flags. The "W" flag means the search doesn't wrap around the end of
|
|
|
|
the file.
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Using `call` is optional in |Vim9| script, this works the same way: >
|
|
|
|
|
|
|
|
search("Date: ", "W")
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
A function can be called in an expression. Example: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var line = getline(".")
|
|
|
|
var repl = substitute(line, '\a', "*", "g")
|
|
|
|
setline(".", repl)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2005-02-07 22:01:03 +00:00
|
|
|
The getline() function obtains a line from the current buffer. Its argument
|
|
|
|
is a specification of the line number. In this case "." is used, which means
|
|
|
|
the line where the cursor is.
|
2021-12-30 20:24:12 +00:00
|
|
|
|
|
|
|
The substitute() function does something similar to the `substitute` command.
|
|
|
|
The first argument is the string on which to perform the substitution. The
|
|
|
|
second argument is the pattern, the third the replacement string. Finally,
|
|
|
|
the last arguments are the flags.
|
|
|
|
|
|
|
|
The setline() function sets the line, specified by the first argument, to a
|
2004-06-13 20:20:40 +00:00
|
|
|
new string, the second argument. In this example the line under the cursor is
|
|
|
|
replaced with the result of the substitute(). Thus the effect of the three
|
|
|
|
statements is equal to: >
|
|
|
|
|
|
|
|
:substitute/\a/*/g
|
|
|
|
|
|
|
|
Using the functions becomes more interesting when you do more work before and
|
|
|
|
after the substitute() call.
|
|
|
|
|
|
|
|
|
|
|
|
FUNCTIONS *function-list*
|
|
|
|
|
|
|
|
There are many functions. We will mention them here, grouped by what they are
|
2021-12-30 20:24:12 +00:00
|
|
|
used for. You can find an alphabetical list here: |builtin-function-list|.
|
|
|
|
Use CTRL-] on the function name to jump to detailed help on it.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
String manipulation: *string-functions*
|
2019-04-06 13:18:12 +02:00
|
|
|
nr2char() get a character by its number value
|
|
|
|
list2str() get a character string from a list of numbers
|
|
|
|
char2nr() get number value of a character
|
|
|
|
str2list() get list of numbers from a string
|
2008-06-24 21:16:56 +00:00
|
|
|
str2nr() convert a string to a Number
|
|
|
|
str2float() convert a string to a Float
|
2006-04-09 21:54:49 +00:00
|
|
|
printf() format a string according to % items
|
2004-06-13 20:20:40 +00:00
|
|
|
escape() escape characters in a string with a '\'
|
2008-06-24 21:16:56 +00:00
|
|
|
shellescape() escape a string for use with a shell command
|
|
|
|
fnameescape() escape a file name for use with a Vim command
|
2006-04-09 21:54:49 +00:00
|
|
|
tr() translate characters from one set to another
|
2004-06-13 20:20:40 +00:00
|
|
|
strtrans() translate a string to make it printable
|
|
|
|
tolower() turn a string to lowercase
|
|
|
|
toupper() turn a string to uppercase
|
2020-08-28 22:24:57 +02:00
|
|
|
charclass() class of a character
|
2004-06-13 20:20:40 +00:00
|
|
|
match() position where a pattern matches in a string
|
|
|
|
matchend() position where a pattern match ends in a string
|
2020-09-11 22:25:15 +02:00
|
|
|
matchfuzzy() fuzzy matches a string in a list of strings
|
2020-09-22 20:33:50 +02:00
|
|
|
matchfuzzypos() fuzzy matches a string in a list of strings
|
2004-06-13 20:20:40 +00:00
|
|
|
matchstr() match of a pattern in a string
|
2016-07-24 14:12:38 +02:00
|
|
|
matchstrpos() match and positions of a pattern in a string
|
2006-04-09 21:54:49 +00:00
|
|
|
matchlist() like matchstr() and also return submatches
|
2004-06-13 20:20:40 +00:00
|
|
|
stridx() first index of a short string in a long string
|
|
|
|
strridx() last index of a short string in a long string
|
2014-01-23 14:24:41 +01:00
|
|
|
strlen() length of a string in bytes
|
2021-03-14 19:02:09 +01:00
|
|
|
strcharlen() length of a string in characters
|
|
|
|
strchars() number of characters in a string
|
2014-01-23 14:24:41 +01:00
|
|
|
strwidth() size of string when displayed
|
|
|
|
strdisplaywidth() size of string when displayed, deals with tabs
|
2020-08-28 21:04:24 +02:00
|
|
|
setcellwidths() set character cell width overrides
|
2004-06-13 20:20:40 +00:00
|
|
|
substitute() substitute a pattern match with a string
|
2011-06-19 05:09:16 +02:00
|
|
|
submatch() get a specific match in ":s" and substitute()
|
2016-06-12 23:01:46 +02:00
|
|
|
strpart() get part of a string using byte index
|
|
|
|
strcharpart() get part of a string using char index
|
2021-01-13 21:47:15 +01:00
|
|
|
slice() take a slice of a string, using char index in
|
|
|
|
Vim9 script
|
2016-06-12 23:01:46 +02:00
|
|
|
strgetchar() get character from a string using char index
|
2004-06-13 20:20:40 +00:00
|
|
|
expand() expand special keywords
|
2019-06-09 17:22:31 +02:00
|
|
|
expandcmd() expand a command like done for `:edit`
|
2004-06-13 20:20:40 +00:00
|
|
|
iconv() convert text from one encoding to another
|
2006-04-09 21:54:49 +00:00
|
|
|
byteidx() byte index of a character in a string
|
2014-01-23 14:24:41 +01:00
|
|
|
byteidxcomp() like byteidx() but count composing characters
|
2020-12-28 12:56:58 +01:00
|
|
|
charidx() character index of a byte in a string
|
2006-04-09 21:54:49 +00:00
|
|
|
repeat() repeat a string multiple times
|
|
|
|
eval() evaluate a string expression
|
2016-07-09 20:21:48 +02:00
|
|
|
execute() execute an Ex command and get the output
|
2019-05-31 21:41:05 +02:00
|
|
|
win_execute() like execute() but in a specified window
|
2018-11-25 03:56:26 +01:00
|
|
|
trim() trim characters from a string
|
2020-08-30 15:52:10 +02:00
|
|
|
gettext() lookup message translation
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
List manipulation: *list-functions*
|
2005-01-17 22:11:23 +00:00
|
|
|
get() get an item without error for wrong index
|
|
|
|
len() number of items in a List
|
|
|
|
empty() check if List is empty
|
|
|
|
insert() insert an item somewhere in a List
|
|
|
|
add() append an item to a List
|
|
|
|
extend() append a List to a List
|
2021-01-12 20:23:40 +01:00
|
|
|
extendnew() make a new List and append items
|
2005-01-17 22:11:23 +00:00
|
|
|
remove() remove one or more items from a List
|
|
|
|
copy() make a shallow copy of a List
|
|
|
|
deepcopy() make a full copy of a List
|
|
|
|
filter() remove selected items from a List
|
|
|
|
map() change each List item
|
2020-11-09 18:31:39 +01:00
|
|
|
mapnew() make a new List with changed items
|
2020-06-04 15:22:21 +02:00
|
|
|
reduce() reduce a List to a value
|
2021-01-13 21:47:15 +01:00
|
|
|
slice() take a slice of a List
|
2005-01-17 22:11:23 +00:00
|
|
|
sort() sort a List
|
|
|
|
reverse() reverse the order of a List
|
2014-03-27 22:30:07 +01:00
|
|
|
uniq() remove copies of repeated adjacent items
|
2005-01-17 22:11:23 +00:00
|
|
|
split() split a String into a List
|
|
|
|
join() join List items into a String
|
2006-04-09 21:54:49 +00:00
|
|
|
range() return a List with a sequence of numbers
|
2005-01-17 22:11:23 +00:00
|
|
|
string() String representation of a List
|
|
|
|
call() call a function with List as arguments
|
2005-06-13 22:28:56 +00:00
|
|
|
index() index of a value in a List
|
2005-01-17 22:11:23 +00:00
|
|
|
max() maximum value in a List
|
|
|
|
min() minimum value in a List
|
|
|
|
count() count number of times a value appears in a List
|
2006-04-09 21:54:49 +00:00
|
|
|
repeat() repeat a List multiple times
|
2020-06-08 20:50:43 +02:00
|
|
|
flatten() flatten a List
|
2021-02-01 20:14:51 +01:00
|
|
|
flattennew() flatten a copy of a List
|
2005-01-17 22:11:23 +00:00
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
Dictionary manipulation: *dict-functions*
|
2005-06-13 22:28:56 +00:00
|
|
|
get() get an entry without an error for a wrong key
|
2005-01-17 22:11:23 +00:00
|
|
|
len() number of entries in a Dictionary
|
|
|
|
has_key() check whether a key appears in a Dictionary
|
|
|
|
empty() check if Dictionary is empty
|
|
|
|
remove() remove an entry from a Dictionary
|
|
|
|
extend() add entries from one Dictionary to another
|
2021-01-12 20:23:40 +01:00
|
|
|
extendnew() make a new Dictionary and append items
|
2005-01-17 22:11:23 +00:00
|
|
|
filter() remove selected entries from a Dictionary
|
|
|
|
map() change each Dictionary entry
|
2020-11-09 18:31:39 +01:00
|
|
|
mapnew() make a new Dictionary with changed items
|
2005-01-17 22:11:23 +00:00
|
|
|
keys() get List of Dictionary keys
|
|
|
|
values() get List of Dictionary values
|
|
|
|
items() get List of Dictionary key-value pairs
|
|
|
|
copy() make a shallow copy of a Dictionary
|
|
|
|
deepcopy() make a full copy of a Dictionary
|
|
|
|
string() String representation of a Dictionary
|
|
|
|
max() maximum value in a Dictionary
|
|
|
|
min() minimum value in a Dictionary
|
|
|
|
count() count number of times a value appears
|
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
Floating point computation: *float-functions*
|
2008-06-24 21:16:56 +00:00
|
|
|
float2nr() convert Float to Number
|
|
|
|
abs() absolute value (also works for Number)
|
|
|
|
round() round off
|
|
|
|
ceil() round up
|
|
|
|
floor() round down
|
|
|
|
trunc() remove value after decimal point
|
2014-01-23 14:24:41 +01:00
|
|
|
fmod() remainder of division
|
|
|
|
exp() exponential
|
|
|
|
log() natural logarithm (logarithm to base e)
|
2008-06-24 21:16:56 +00:00
|
|
|
log10() logarithm to base 10
|
|
|
|
pow() value of x to the exponent y
|
|
|
|
sqrt() square root
|
|
|
|
sin() sine
|
|
|
|
cos() cosine
|
2011-03-22 14:05:35 +01:00
|
|
|
tan() tangent
|
|
|
|
asin() arc sine
|
|
|
|
acos() arc cosine
|
2008-06-24 21:16:56 +00:00
|
|
|
atan() arc tangent
|
2011-03-22 14:05:35 +01:00
|
|
|
atan2() arc tangent
|
|
|
|
sinh() hyperbolic sine
|
|
|
|
cosh() hyperbolic cosine
|
|
|
|
tanh() hyperbolic tangent
|
2020-06-04 15:22:21 +02:00
|
|
|
isinf() check for infinity
|
2016-06-12 23:01:46 +02:00
|
|
|
isnan() check for not a number
|
2008-06-24 21:16:56 +00:00
|
|
|
|
2021-09-14 17:54:30 +02:00
|
|
|
Blob manipulation: *blob-functions*
|
|
|
|
blob2list() get a list of numbers from a blob
|
|
|
|
list2blob() get a blob from a list of numbers
|
|
|
|
|
2011-12-30 13:11:27 +01:00
|
|
|
Other computation: *bitwise-function*
|
|
|
|
and() bitwise AND
|
|
|
|
invert() bitwise invert
|
|
|
|
or() bitwise OR
|
|
|
|
xor() bitwise XOR
|
2014-01-23 14:24:41 +01:00
|
|
|
sha256() SHA-256 hash
|
2020-06-04 15:22:21 +02:00
|
|
|
rand() get a pseudo-random number
|
|
|
|
srand() initialize seed used by rand()
|
2011-12-30 13:11:27 +01:00
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
Variables: *var-functions*
|
2021-01-12 21:49:00 +01:00
|
|
|
type() type of a variable as a number
|
|
|
|
typename() type of a variable as text
|
2006-04-09 21:54:49 +00:00
|
|
|
islocked() check if a variable is locked
|
2017-03-05 17:04:09 +01:00
|
|
|
funcref() get a Funcref for a function reference
|
2006-04-09 21:54:49 +00:00
|
|
|
function() get a Funcref for a function name
|
|
|
|
getbufvar() get a variable value from a specific buffer
|
|
|
|
setbufvar() set a variable in a specific buffer
|
2006-04-15 20:25:09 +00:00
|
|
|
getwinvar() get a variable from specific window
|
2010-05-22 15:37:44 +02:00
|
|
|
gettabvar() get a variable from specific tab page
|
2006-04-15 20:25:09 +00:00
|
|
|
gettabwinvar() get a variable from specific window & tab page
|
2006-04-09 21:54:49 +00:00
|
|
|
setwinvar() set a variable in a specific window
|
2010-05-22 15:37:44 +02:00
|
|
|
settabvar() set a variable in a specific tab page
|
2006-04-15 20:25:09 +00:00
|
|
|
settabwinvar() set a variable in a specific window & tab page
|
2006-04-09 21:54:49 +00:00
|
|
|
garbagecollect() possibly free memory
|
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
Cursor and mark position: *cursor-functions* *mark-functions*
|
2004-06-13 20:20:40 +00:00
|
|
|
col() column number of the cursor or a mark
|
|
|
|
virtcol() screen column of the cursor or a mark
|
|
|
|
line() line number of the cursor or mark
|
|
|
|
wincol() window column number of the cursor
|
|
|
|
winline() window line number of the cursor
|
|
|
|
cursor() position the cursor at a line/column
|
2014-01-23 14:24:41 +01:00
|
|
|
screencol() get screen column of the cursor
|
|
|
|
screenrow() get screen row of the cursor
|
2019-07-07 18:28:14 +02:00
|
|
|
screenpos() screen row and col of a text character
|
2014-06-12 21:46:14 +02:00
|
|
|
getcurpos() get position of the cursor
|
2006-04-09 21:54:49 +00:00
|
|
|
getpos() get position of cursor, mark, etc.
|
|
|
|
setpos() set position of cursor, mark, etc.
|
2020-05-31 15:41:57 +02:00
|
|
|
getmarklist() list of global/local marks
|
2006-04-09 21:54:49 +00:00
|
|
|
byte2line() get line number at a specific byte count
|
|
|
|
line2byte() byte count at a specific line
|
|
|
|
diff_filler() get the number of filler lines above a line
|
2014-01-23 14:24:41 +01:00
|
|
|
screenattr() get attribute at a screen line/row
|
|
|
|
screenchar() get character code at a screen line/row
|
2019-03-29 14:16:42 +01:00
|
|
|
screenchars() get character codes at a screen line/row
|
|
|
|
screenstring() get string of characters at a screen line/row
|
2021-01-10 20:22:54 +01:00
|
|
|
charcol() character number of the cursor or a mark
|
|
|
|
getcharpos() get character position of cursor, mark, etc.
|
|
|
|
setcharpos() set character position of cursor, mark, etc.
|
|
|
|
getcursorcharpos() get character position of the cursor
|
|
|
|
setcursorcharpos() set character position of the cursor
|
2006-04-09 21:54:49 +00:00
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
Working with text in the current buffer: *text-functions*
|
2005-02-07 22:01:03 +00:00
|
|
|
getline() get a line or list of lines from the buffer
|
2004-06-13 20:20:40 +00:00
|
|
|
setline() replace a line in the buffer
|
2005-02-07 22:01:03 +00:00
|
|
|
append() append line or list of lines in the buffer
|
2004-06-13 20:20:40 +00:00
|
|
|
indent() indent of a specific line
|
|
|
|
cindent() indent according to C indenting
|
|
|
|
lispindent() indent according to Lisp indenting
|
|
|
|
nextnonblank() find next non-blank line
|
|
|
|
prevnonblank() find previous non-blank line
|
|
|
|
search() find a match for a pattern
|
2006-02-14 22:29:30 +00:00
|
|
|
searchpos() find a match for a pattern
|
2020-06-04 15:22:21 +02:00
|
|
|
searchcount() get number of matches before/after the cursor
|
2004-06-13 20:20:40 +00:00
|
|
|
searchpair() find the other end of a start/skip/end
|
2006-02-14 22:29:30 +00:00
|
|
|
searchpairpos() find the other end of a start/skip/end
|
2006-04-09 21:54:49 +00:00
|
|
|
searchdecl() search for the declaration of a name
|
2016-06-12 23:01:46 +02:00
|
|
|
getcharsearch() return character search information
|
|
|
|
setcharsearch() set character search information
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-07-04 16:54:54 +02:00
|
|
|
Working with text in another buffer:
|
|
|
|
getbufline() get a list of lines from the specified buffer
|
|
|
|
setbufline() replace a line in the specified buffer
|
|
|
|
appendbufline() append a list of lines in the specified buffer
|
|
|
|
deletebufline() delete lines from a specified buffer
|
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
*system-functions* *file-functions*
|
2004-06-13 20:20:40 +00:00
|
|
|
System functions and manipulation of files:
|
|
|
|
glob() expand wildcards
|
|
|
|
globpath() expand wildcards in a number of directories
|
2016-06-12 23:01:46 +02:00
|
|
|
glob2regpat() convert a glob pattern into a search pattern
|
2006-04-09 21:54:49 +00:00
|
|
|
findfile() find a file in a list of directories
|
|
|
|
finddir() find a directory in a list of directories
|
2004-06-13 20:20:40 +00:00
|
|
|
resolve() find out where a shortcut points to
|
|
|
|
fnamemodify() modify a file name
|
2006-04-09 21:54:49 +00:00
|
|
|
pathshorten() shorten directory names in a path
|
|
|
|
simplify() simplify a path without changing its meaning
|
2004-06-13 20:20:40 +00:00
|
|
|
executable() check if an executable program exists
|
2014-04-05 22:55:53 +02:00
|
|
|
exepath() full path of an executable program
|
2004-06-13 20:20:40 +00:00
|
|
|
filereadable() check if a file can be read
|
|
|
|
filewritable() check if a file can be written to
|
2006-04-09 21:54:49 +00:00
|
|
|
getfperm() get the permissions of a file
|
2016-06-12 23:01:46 +02:00
|
|
|
setfperm() set the permissions of a file
|
2006-04-09 21:54:49 +00:00
|
|
|
getftype() get the kind of a file
|
2004-06-13 20:20:40 +00:00
|
|
|
isdirectory() check if a directory exists
|
|
|
|
getfsize() get the size of a file
|
2006-04-09 21:54:49 +00:00
|
|
|
getcwd() get the current working directory
|
2019-04-27 20:37:57 +02:00
|
|
|
haslocaldir() check if current window used |:lcd| or |:tcd|
|
2004-06-13 20:20:40 +00:00
|
|
|
tempname() get the name of a temporary file
|
2006-04-09 21:54:49 +00:00
|
|
|
mkdir() create a new directory
|
2019-05-07 22:06:52 +02:00
|
|
|
chdir() change current working directory
|
2004-06-13 20:20:40 +00:00
|
|
|
delete() delete a file
|
|
|
|
rename() rename a file
|
2014-04-05 22:55:53 +02:00
|
|
|
system() get the result of a shell command as a string
|
|
|
|
systemlist() get the result of a shell command as a list
|
2019-05-09 14:52:41 +02:00
|
|
|
environ() get all environment variables
|
|
|
|
getenv() get one environment variable
|
|
|
|
setenv() set an environment variable
|
2004-06-13 20:20:40 +00:00
|
|
|
hostname() name of the system
|
2005-02-05 21:39:53 +00:00
|
|
|
readfile() read a file into a List of lines
|
2021-01-13 20:38:03 +01:00
|
|
|
readblob() read a file into a Blob
|
2019-04-08 16:25:07 +02:00
|
|
|
readdir() get a List of file names in a directory
|
2020-06-01 16:09:41 +02:00
|
|
|
readdirex() get a List of file information in a directory
|
2019-02-03 15:27:20 +01:00
|
|
|
writefile() write a List of lines or Blob into a file
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
Date and Time: *date-functions* *time-functions*
|
2006-04-09 21:54:49 +00:00
|
|
|
getftime() get last modification time of a file
|
|
|
|
localtime() get current time in seconds
|
|
|
|
strftime() convert time to a string
|
2019-11-21 15:36:18 +01:00
|
|
|
strptime() convert a date/time string to time
|
2006-04-09 21:54:49 +00:00
|
|
|
reltime() get the current or elapsed time accurately
|
|
|
|
reltimestr() convert reltime() result to a string
|
2016-04-12 21:07:15 +02:00
|
|
|
reltimefloat() convert reltime() result to a Float
|
2006-04-09 21:54:49 +00:00
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
*buffer-functions* *window-functions* *arg-functions*
|
2004-06-13 20:20:40 +00:00
|
|
|
Buffers, windows and the argument list:
|
|
|
|
argc() number of entries in the argument list
|
|
|
|
argidx() current position in the argument list
|
2014-05-28 18:22:57 +02:00
|
|
|
arglistid() get id of the argument list
|
2004-06-13 20:20:40 +00:00
|
|
|
argv() get one entry from the argument list
|
2019-07-04 16:54:54 +02:00
|
|
|
bufadd() add a file to the list of buffers
|
2004-06-13 20:20:40 +00:00
|
|
|
bufexists() check if a buffer exists
|
|
|
|
buflisted() check if a buffer exists and is listed
|
2019-07-04 16:54:54 +02:00
|
|
|
bufload() ensure a buffer is loaded
|
2004-06-13 20:20:40 +00:00
|
|
|
bufloaded() check if a buffer exists and is loaded
|
|
|
|
bufname() get the name of a specific buffer
|
|
|
|
bufnr() get the buffer number of a specific buffer
|
2006-04-09 21:54:49 +00:00
|
|
|
tabpagebuflist() return List of buffers in a tab page
|
|
|
|
tabpagenr() get the number of a tab page
|
|
|
|
tabpagewinnr() like winnr() for a specified tab page
|
2004-06-13 20:20:40 +00:00
|
|
|
winnr() get the window number for the current window
|
2016-06-04 20:20:29 +02:00
|
|
|
bufwinid() get the window ID of a specific buffer
|
2004-06-13 20:20:40 +00:00
|
|
|
bufwinnr() get the window number of a specific buffer
|
|
|
|
winbufnr() get the buffer number of a specific window
|
2019-05-11 21:14:24 +02:00
|
|
|
listener_add() add a callback to listen to changes
|
2019-05-26 21:33:31 +02:00
|
|
|
listener_flush() invoke listener callbacks
|
2019-05-11 21:14:24 +02:00
|
|
|
listener_remove() remove a listener callback
|
2016-06-12 23:01:46 +02:00
|
|
|
win_findbuf() find windows containing a buffer
|
|
|
|
win_getid() get window ID of a window
|
2020-06-04 15:22:21 +02:00
|
|
|
win_gettype() get type of window
|
2016-06-12 23:01:46 +02:00
|
|
|
win_gotoid() go to window with ID
|
|
|
|
win_id2tabwin() get tab and window nr from window ID
|
|
|
|
win_id2win() get window nr from window ID
|
2022-01-10 13:36:34 +00:00
|
|
|
win_move_separator() move window vertical separator
|
|
|
|
win_move_statusline() move window status line
|
2020-06-04 15:22:21 +02:00
|
|
|
win_splitmove() move window to a split of another window
|
2016-08-12 22:23:25 +02:00
|
|
|
getbufinfo() get a list with buffer information
|
|
|
|
gettabinfo() get a list with tab page information
|
|
|
|
getwininfo() get a list with window information
|
2018-02-13 13:59:59 +01:00
|
|
|
getchangelist() get a list of change list entries
|
2018-02-10 21:06:32 +01:00
|
|
|
getjumplist() get a list of jump list entries
|
2018-08-28 22:58:02 +02:00
|
|
|
swapinfo() information about a swap file
|
2018-11-25 03:56:26 +01:00
|
|
|
swapname() get the swap file path of a buffer
|
2006-04-09 21:54:49 +00:00
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
Command line: *command-line-functions*
|
2006-04-09 21:54:49 +00:00
|
|
|
getcmdline() get the current command line
|
|
|
|
getcmdpos() get position of the cursor in the command line
|
|
|
|
setcmdpos() set position of the cursor in the command line
|
|
|
|
getcmdtype() return the current command-line type
|
2014-08-22 19:21:47 +02:00
|
|
|
getcmdwintype() return the current command-line window type
|
2016-07-24 14:12:38 +02:00
|
|
|
getcompletion() list of command-line completion matches
|
2021-02-06 12:38:51 +01:00
|
|
|
fullcommand() get full command name
|
2006-04-09 21:54:49 +00:00
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
Quickfix and location lists: *quickfix-functions*
|
2006-04-09 21:54:49 +00:00
|
|
|
getqflist() list of quickfix errors
|
|
|
|
setqflist() modify a quickfix list
|
|
|
|
getloclist() list of location list items
|
|
|
|
setloclist() modify a location list
|
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
Insert mode completion: *completion-functions*
|
2006-04-09 21:54:49 +00:00
|
|
|
complete() set found matches
|
|
|
|
complete_add() add to found matches
|
|
|
|
complete_check() check if completion should be aborted
|
2019-03-29 12:20:27 +01:00
|
|
|
complete_info() get current completion information
|
2006-04-09 21:54:49 +00:00
|
|
|
pumvisible() check if the popup menu is displayed
|
2019-09-27 19:34:08 +02:00
|
|
|
pum_getpos() position and size of popup menu if visible
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
Folding: *folding-functions*
|
2004-06-13 20:20:40 +00:00
|
|
|
foldclosed() check for a closed fold at a specific line
|
|
|
|
foldclosedend() like foldclosed() but return the last line
|
|
|
|
foldlevel() check for the fold level at a specific line
|
|
|
|
foldtext() generate the line displayed for a closed fold
|
2006-04-09 21:54:49 +00:00
|
|
|
foldtextresult() get the text displayed for a closed fold
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
Syntax and highlighting: *syntax-functions* *highlighting-functions*
|
2007-07-26 20:58:42 +00:00
|
|
|
clearmatches() clear all matches defined by |matchadd()| and
|
|
|
|
the |:match| commands
|
|
|
|
getmatches() get all matches defined by |matchadd()| and
|
|
|
|
the |:match| commands
|
2004-06-13 20:20:40 +00:00
|
|
|
hlexists() check if a highlight group exists
|
2021-11-03 21:56:45 +00:00
|
|
|
hlget() get highlight group attributes
|
|
|
|
hlset() set highlight group attributes
|
2004-06-13 20:20:40 +00:00
|
|
|
hlID() get ID of a highlight group
|
|
|
|
synID() get syntax ID at a specific position
|
|
|
|
synIDattr() get a specific attribute of a syntax ID
|
|
|
|
synIDtrans() get translated syntax ID
|
2010-11-16 20:34:40 +01:00
|
|
|
synstack() get list of syntax IDs at a specific position
|
2010-12-10 20:35:50 +01:00
|
|
|
synconcealed() get info about concealing
|
2006-04-09 21:54:49 +00:00
|
|
|
diff_hlID() get highlight ID for diff mode at a position
|
2007-07-26 20:58:42 +00:00
|
|
|
matchadd() define a pattern to highlight (a "match")
|
2014-06-17 17:48:32 +02:00
|
|
|
matchaddpos() define a list of positions to highlight
|
2006-04-09 21:54:49 +00:00
|
|
|
matcharg() get info about |:match| arguments
|
2007-07-26 20:58:42 +00:00
|
|
|
matchdelete() delete a match defined by |matchadd()| or a
|
|
|
|
|:match| command
|
|
|
|
setmatches() restore a list of matches saved by
|
|
|
|
|getmatches()|
|
2006-04-09 21:54:49 +00:00
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
Spelling: *spell-functions*
|
2006-04-09 21:54:49 +00:00
|
|
|
spellbadword() locate badly spelled word at or after cursor
|
|
|
|
spellsuggest() return suggested spelling corrections
|
|
|
|
soundfold() return the sound-a-like equivalent of a word
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
History: *history-functions*
|
2004-06-13 20:20:40 +00:00
|
|
|
histadd() add an item to a history
|
|
|
|
histdel() delete an item from a history
|
|
|
|
histget() get an item from a history
|
|
|
|
histnr() get highest index of a history list
|
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
Interactive: *interactive-functions*
|
2006-04-09 21:54:49 +00:00
|
|
|
browse() put up a file requester
|
|
|
|
browsedir() put up a directory requester
|
2004-06-13 20:20:40 +00:00
|
|
|
confirm() let the user make a choice
|
|
|
|
getchar() get a character from the user
|
2021-06-07 18:50:01 +02:00
|
|
|
getcharstr() get a character from the user as a string
|
2004-06-13 20:20:40 +00:00
|
|
|
getcharmod() get modifiers for the last typed character
|
2019-11-17 15:55:14 +01:00
|
|
|
getmousepos() get last known mouse position
|
2020-06-04 15:22:21 +02:00
|
|
|
echoraw() output characters as-is
|
2008-06-24 21:16:56 +00:00
|
|
|
feedkeys() put characters in the typeahead queue
|
2004-06-13 20:20:40 +00:00
|
|
|
input() get a line from the user
|
2006-04-09 21:54:49 +00:00
|
|
|
inputlist() let the user pick an entry from a list
|
2004-06-13 20:20:40 +00:00
|
|
|
inputsecret() get a line from the user without showing it
|
|
|
|
inputdialog() get a line from the user in a dialog
|
2005-03-25 21:53:48 +00:00
|
|
|
inputsave() save and clear typeahead
|
2004-06-13 20:20:40 +00:00
|
|
|
inputrestore() restore typeahead
|
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
GUI: *gui-functions*
|
2006-04-09 21:54:49 +00:00
|
|
|
getfontname() get name of current font being used
|
2018-03-09 22:22:21 +01:00
|
|
|
getwinpos() position of the Vim window
|
|
|
|
getwinposx() X position of the Vim window
|
|
|
|
getwinposy() Y position of the Vim window
|
2017-03-05 17:04:09 +01:00
|
|
|
balloon_show() set the balloon content
|
2017-11-21 23:09:50 +01:00
|
|
|
balloon_split() split a message for a balloon
|
2019-05-09 14:52:41 +02:00
|
|
|
balloon_gettext() get the text in the balloon
|
2006-04-09 21:54:49 +00:00
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
Vim server: *server-functions*
|
2004-06-13 20:20:40 +00:00
|
|
|
serverlist() return the list of server names
|
2017-11-02 22:58:42 +01:00
|
|
|
remote_startserver() run a server
|
2004-06-13 20:20:40 +00:00
|
|
|
remote_send() send command characters to a Vim server
|
|
|
|
remote_expr() evaluate an expression in a Vim server
|
|
|
|
server2client() send a reply to a client of a Vim server
|
|
|
|
remote_peek() check if there is a reply from a Vim server
|
|
|
|
remote_read() read a reply from a Vim server
|
|
|
|
foreground() move the Vim window to the foreground
|
|
|
|
remote_foreground() move the Vim server window to the foreground
|
|
|
|
|
2010-07-11 19:01:06 +02:00
|
|
|
Window size and position: *window-size-functions*
|
2006-04-09 21:54:49 +00:00
|
|
|
winheight() get height of a specific window
|
|
|
|
winwidth() get width of a specific window
|
2017-12-17 17:17:07 +01:00
|
|
|
win_screenpos() get screen position of a window
|
2018-11-25 03:56:26 +01:00
|
|
|
winlayout() get layout of windows in a tab page
|
2006-04-09 21:54:49 +00:00
|
|
|
winrestcmd() return command to restore window sizes
|
|
|
|
winsaveview() get view of current window
|
|
|
|
winrestview() restore saved view of current window
|
|
|
|
|
2020-03-15 16:13:53 +01:00
|
|
|
Mappings and Menus: *mapping-functions*
|
2021-07-26 21:54:04 +02:00
|
|
|
digraph_get() get |digraph|
|
|
|
|
digraph_getlist() get all |digraph|s
|
|
|
|
digraph_set() register |digraph|
|
|
|
|
digraph_setlist() register multiple |digraph|s
|
2004-06-13 20:20:40 +00:00
|
|
|
hasmapto() check if a mapping exists
|
|
|
|
mapcheck() check if a matching mapping exists
|
|
|
|
maparg() get rhs of a mapping
|
2020-06-04 15:22:21 +02:00
|
|
|
mapset() restore a mapping
|
2020-03-15 16:13:53 +01:00
|
|
|
menu_info() get information about a menu item
|
2013-02-20 21:26:00 +01:00
|
|
|
wildmenumode() check if the wildmode is active
|
|
|
|
|
2015-11-30 21:38:24 +01:00
|
|
|
Testing: *test-functions*
|
2016-03-20 21:08:34 +01:00
|
|
|
assert_equal() assert that two expressions values are equal
|
2018-11-25 03:56:26 +01:00
|
|
|
assert_equalfile() assert that two file contents are equal
|
2016-04-12 21:07:15 +02:00
|
|
|
assert_notequal() assert that two expressions values are not equal
|
2016-07-24 14:12:38 +02:00
|
|
|
assert_inrange() assert that an expression is inside a range
|
2016-03-29 23:12:46 +02:00
|
|
|
assert_match() assert that a pattern matches the value
|
2016-04-12 21:07:15 +02:00
|
|
|
assert_notmatch() assert that a pattern does not match the value
|
2015-11-30 21:38:24 +01:00
|
|
|
assert_false() assert that an expression is false
|
|
|
|
assert_true() assert that an expression is true
|
2016-03-20 21:08:34 +01:00
|
|
|
assert_exception() assert that a command throws an exception
|
2018-02-27 14:53:30 +01:00
|
|
|
assert_beeps() assert that a command beeps
|
2021-04-03 15:15:47 +02:00
|
|
|
assert_nobeep() assert that a command does not cause a beep
|
2018-02-27 14:53:30 +01:00
|
|
|
assert_fails() assert that a command fails
|
2017-03-21 19:18:29 +01:00
|
|
|
assert_report() report a test failure
|
2022-01-29 13:06:40 +00:00
|
|
|
internal_get_nv_cmdchar() normal/visual command character at an index
|
2016-06-12 23:01:46 +02:00
|
|
|
test_alloc_fail() make memory allocation fail
|
2016-07-24 14:12:38 +02:00
|
|
|
test_autochdir() enable 'autochdir' during startup
|
2017-03-16 17:41:02 +01:00
|
|
|
test_override() test with Vim internal overrides
|
|
|
|
test_garbagecollect_now() free memory right now
|
2020-06-04 15:22:21 +02:00
|
|
|
test_garbagecollect_soon() set a flag to free memory soon
|
2019-05-26 21:33:31 +02:00
|
|
|
test_getvalue() get value of an internal variable
|
2021-06-23 20:46:52 +02:00
|
|
|
test_gui_drop_files() drop file(s) in a window
|
2021-06-21 18:44:26 +02:00
|
|
|
test_gui_mouse_event() add a GUI mouse event to the input buffer
|
2022-01-27 13:16:59 +00:00
|
|
|
test_gui_tabline_event() add a GUI tabline event to the input buffer
|
|
|
|
test_gui_tabmenu_event() add a GUI tabmenu event to the input buffer
|
2017-03-05 17:04:09 +01:00
|
|
|
test_ignore_error() ignore a specific error message
|
2019-02-03 15:27:20 +01:00
|
|
|
test_null_blob() return a null Blob
|
2016-06-12 23:01:46 +02:00
|
|
|
test_null_channel() return a null Channel
|
|
|
|
test_null_dict() return a null Dict
|
2020-06-04 15:22:21 +02:00
|
|
|
test_null_function() return a null Funcref
|
2016-06-12 23:01:46 +02:00
|
|
|
test_null_job() return a null Job
|
|
|
|
test_null_list() return a null List
|
|
|
|
test_null_partial() return a null Partial function
|
|
|
|
test_null_string() return a null String
|
2017-03-05 17:04:09 +01:00
|
|
|
test_settime() set the time Vim uses internally
|
2019-05-04 15:47:48 +02:00
|
|
|
test_setmouse() set the mouse position
|
2018-11-25 03:56:26 +01:00
|
|
|
test_feedinput() add key sequence to input buffer
|
|
|
|
test_option_not_set() reset flag indicating option was set
|
|
|
|
test_scrollbar() simulate scrollbar movement in the GUI
|
2020-06-04 15:22:21 +02:00
|
|
|
test_refcount() return an expression's reference count
|
|
|
|
test_srand_seed() set the seed value for srand()
|
|
|
|
test_unknown() return a value with unknown type
|
|
|
|
test_void() return a value with void type
|
2016-06-12 23:01:46 +02:00
|
|
|
|
|
|
|
Inter-process communication: *channel-functions*
|
2016-12-01 23:03:28 +01:00
|
|
|
ch_canread() check if there is something to read
|
2016-02-04 20:57:07 +01:00
|
|
|
ch_open() open a channel
|
|
|
|
ch_close() close a channel
|
2016-09-06 22:12:34 +02:00
|
|
|
ch_close_in() close the in part of a channel
|
2016-06-12 23:01:46 +02:00
|
|
|
ch_read() read a message from a channel
|
2019-01-17 16:07:22 +01:00
|
|
|
ch_readblob() read a Blob from a channel
|
2016-06-12 23:01:46 +02:00
|
|
|
ch_readraw() read a raw message from a channel
|
2016-02-04 20:57:07 +01:00
|
|
|
ch_sendexpr() send a JSON message over a channel
|
|
|
|
ch_sendraw() send a raw message over a channel
|
2020-06-04 15:22:21 +02:00
|
|
|
ch_evalexpr() evaluate an expression over channel
|
|
|
|
ch_evalraw() evaluate a raw string over channel
|
2016-06-12 23:01:46 +02:00
|
|
|
ch_status() get status of a channel
|
|
|
|
ch_getbufnr() get the buffer number of a channel
|
|
|
|
ch_getjob() get the job associated with a channel
|
|
|
|
ch_info() get channel information
|
|
|
|
ch_log() write a message in the channel log file
|
|
|
|
ch_logfile() set the channel log file
|
|
|
|
ch_setoptions() set the options for a channel
|
2016-06-17 12:48:11 +02:00
|
|
|
json_encode() encode an expression to a JSON string
|
|
|
|
json_decode() decode a JSON string to Vim types
|
2016-06-12 23:01:46 +02:00
|
|
|
js_encode() encode an expression to a JSON string
|
|
|
|
js_decode() decode a JSON string to Vim types
|
|
|
|
|
2020-06-04 15:22:21 +02:00
|
|
|
Jobs: *job-functions*
|
2016-06-12 23:01:46 +02:00
|
|
|
job_start() start a job
|
|
|
|
job_stop() stop a job
|
|
|
|
job_status() get the status of a job
|
|
|
|
job_getchannel() get the channel used by a job
|
|
|
|
job_info() get information about a job
|
|
|
|
job_setoptions() set options for a job
|
|
|
|
|
2018-12-21 15:17:36 +01:00
|
|
|
Signs: *sign-functions*
|
|
|
|
sign_define() define or update a sign
|
|
|
|
sign_getdefined() get a list of defined signs
|
|
|
|
sign_getplaced() get a list of placed signs
|
2019-01-11 13:42:41 +01:00
|
|
|
sign_jump() jump to a sign
|
2018-12-21 15:17:36 +01:00
|
|
|
sign_place() place a sign
|
2019-07-13 21:21:40 +02:00
|
|
|
sign_placelist() place a list of signs
|
2018-12-21 15:17:36 +01:00
|
|
|
sign_undefine() undefine a sign
|
|
|
|
sign_unplace() unplace a sign
|
2019-07-13 21:21:40 +02:00
|
|
|
sign_unplacelist() unplace a list of signs
|
2018-12-21 15:17:36 +01:00
|
|
|
|
2017-08-27 16:52:01 +02:00
|
|
|
Terminal window: *terminal-functions*
|
|
|
|
term_start() open a terminal window and run a job
|
|
|
|
term_list() get the list of terminal buffers
|
|
|
|
term_sendkeys() send keystrokes to a terminal
|
|
|
|
term_wait() wait for screen to be updated
|
|
|
|
term_getjob() get the job associated with a terminal
|
|
|
|
term_scrape() get row of a terminal screen
|
|
|
|
term_getline() get a line of text from a terminal
|
|
|
|
term_getattr() get the value of attribute {what}
|
|
|
|
term_getcursor() get the cursor position of a terminal
|
|
|
|
term_getscrolled() get the scroll count of a terminal
|
|
|
|
term_getaltscreen() get the alternate screen flag
|
|
|
|
term_getsize() get the size of a terminal
|
|
|
|
term_getstatus() get the status of a terminal
|
|
|
|
term_gettitle() get the title of a terminal
|
|
|
|
term_gettty() get the tty name of a terminal
|
2018-04-20 22:36:41 +02:00
|
|
|
term_setansicolors() set 16 ANSI colors, used for GUI
|
|
|
|
term_getansicolors() get 16 ANSI colors, used for GUI
|
2018-11-25 03:56:26 +01:00
|
|
|
term_dumpdiff() display difference between two screen dumps
|
|
|
|
term_dumpload() load a terminal screen dump in a window
|
|
|
|
term_dumpwrite() dump contents of a terminal screen to a file
|
|
|
|
term_setkill() set signal to stop job in a terminal
|
|
|
|
term_setrestore() set command to restore a terminal
|
|
|
|
term_setsize() set the size of a terminal
|
2020-06-04 15:22:21 +02:00
|
|
|
term_setapi() set terminal JSON API function name prefix
|
2017-08-27 16:52:01 +02:00
|
|
|
|
2019-07-04 16:54:54 +02:00
|
|
|
Popup window: *popup-window-functions*
|
|
|
|
popup_create() create popup centered in the screen
|
|
|
|
popup_atcursor() create popup just above the cursor position,
|
|
|
|
closes when the cursor moves away
|
2019-07-07 18:28:14 +02:00
|
|
|
popup_beval() at the position indicated by v:beval_
|
|
|
|
variables, closes when the mouse moves away
|
2019-07-04 16:54:54 +02:00
|
|
|
popup_notification() show a notification for three seconds
|
|
|
|
popup_dialog() create popup centered with padding and border
|
|
|
|
popup_menu() prompt for selecting an item from a list
|
|
|
|
popup_hide() hide a popup temporarily
|
|
|
|
popup_show() show a previously hidden popup
|
|
|
|
popup_move() change the position and size of a popup
|
|
|
|
popup_setoptions() override options of a popup
|
|
|
|
popup_settext() replace the popup buffer contents
|
|
|
|
popup_close() close one popup
|
|
|
|
popup_clear() close all popups
|
|
|
|
popup_filter_menu() select from a list of items
|
2020-06-04 15:22:21 +02:00
|
|
|
popup_filter_yesno() block until 'y' or 'n' is pressed
|
2019-07-04 16:54:54 +02:00
|
|
|
popup_getoptions() get current options for a popup
|
|
|
|
popup_getpos() get actual position and size of a popup
|
2020-06-04 15:22:21 +02:00
|
|
|
popup_findinfo() get window ID for popup info window
|
|
|
|
popup_findpreview() get window ID for popup preview window
|
|
|
|
popup_list() get list of all popup window IDs
|
|
|
|
popup_locate() get popup window ID from its screen position
|
2019-07-04 16:54:54 +02:00
|
|
|
|
2016-06-12 23:01:46 +02:00
|
|
|
Timers: *timer-functions*
|
|
|
|
timer_start() create a timer
|
2016-08-12 22:23:25 +02:00
|
|
|
timer_pause() pause or unpause a timer
|
2016-06-12 23:01:46 +02:00
|
|
|
timer_stop() stop a timer
|
2016-08-12 22:23:25 +02:00
|
|
|
timer_stopall() stop all timers
|
|
|
|
timer_info() get information about timers
|
2016-01-28 22:38:53 +01:00
|
|
|
|
2018-11-25 03:56:26 +01:00
|
|
|
Tags: *tag-functions*
|
|
|
|
taglist() get list of matching tags
|
|
|
|
tagfiles() get a list of tags files
|
|
|
|
gettagstack() get the tag stack of a window
|
|
|
|
settagstack() modify the tag stack of a window
|
|
|
|
|
|
|
|
Prompt Buffer: *promptbuffer-functions*
|
2020-09-04 16:35:35 +02:00
|
|
|
prompt_getprompt() get the effective prompt text for a buffer
|
2018-11-25 03:56:26 +01:00
|
|
|
prompt_setcallback() set prompt callback for a buffer
|
|
|
|
prompt_setinterrupt() set interrupt callback for a buffer
|
|
|
|
prompt_setprompt() set the prompt text for a buffer
|
|
|
|
|
2020-06-04 15:22:21 +02:00
|
|
|
Text Properties: *text-property-functions*
|
|
|
|
prop_add() attach a property at a position
|
2021-08-16 21:39:09 +02:00
|
|
|
prop_add_list() attach a property at multiple positions
|
2020-06-04 15:22:21 +02:00
|
|
|
prop_clear() remove all properties from a line or lines
|
|
|
|
prop_find() search for a property
|
|
|
|
prop_list() return a list of all properties in a line
|
|
|
|
prop_remove() remove a property from a line
|
|
|
|
prop_type_add() add/define a property type
|
|
|
|
prop_type_change() change properties of a type
|
|
|
|
prop_type_delete() remove a text property type
|
|
|
|
prop_type_get() return the properties of a type
|
|
|
|
prop_type_list() return a list of all property types
|
|
|
|
|
|
|
|
Sound: *sound-functions*
|
|
|
|
sound_clear() stop playing all sounds
|
|
|
|
sound_playevent() play an event's sound
|
|
|
|
sound_playfile() play a sound file
|
|
|
|
sound_stop() stop playing a sound
|
|
|
|
|
2013-02-20 21:26:00 +01:00
|
|
|
Various: *various-functions*
|
|
|
|
mode() get current editing mode
|
2020-06-04 15:22:21 +02:00
|
|
|
state() get current busy state
|
2013-02-20 21:26:00 +01:00
|
|
|
visualmode() last visual mode used
|
2004-06-13 20:20:40 +00:00
|
|
|
exists() check if a variable, function, etc. exists
|
2021-08-08 14:43:22 +02:00
|
|
|
exists_compiled() like exists() but check at compile time
|
2004-06-13 20:20:40 +00:00
|
|
|
has() check if a feature is supported in Vim
|
2006-04-09 21:54:49 +00:00
|
|
|
changenr() return number of most recent change
|
2004-06-13 20:20:40 +00:00
|
|
|
cscope_connection() check if a cscope connection exists
|
|
|
|
did_filetype() check if a FileType autocommand was used
|
|
|
|
eventhandler() check if invoked by an event handler
|
2008-06-24 21:16:56 +00:00
|
|
|
getpid() get process ID of Vim
|
2020-06-04 15:22:21 +02:00
|
|
|
getimstatus() check if IME status is active
|
|
|
|
interrupt() interrupt script execution
|
|
|
|
windowsversion() get MS-Windows version
|
2020-06-13 15:47:25 +02:00
|
|
|
terminalprops() properties of the terminal
|
2006-04-09 21:54:49 +00:00
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
libcall() call a function in an external library
|
|
|
|
libcallnr() idem, returning a number
|
2006-04-09 21:54:49 +00:00
|
|
|
|
2014-01-23 14:24:41 +01:00
|
|
|
undofile() get the name of the undo file
|
|
|
|
undotree() return the state of the undo tree
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
getreg() get contents of a register
|
2020-06-07 18:16:36 +02:00
|
|
|
getreginfo() get information about a register
|
2004-06-13 20:20:40 +00:00
|
|
|
getregtype() get type of a register
|
|
|
|
setreg() set contents and type of a register
|
2018-05-22 20:35:17 +02:00
|
|
|
reg_executing() return the name of the register being executed
|
|
|
|
reg_recording() return the name of the register being recorded
|
2006-04-09 21:54:49 +00:00
|
|
|
|
2014-01-23 14:24:41 +01:00
|
|
|
shiftwidth() effective value of 'shiftwidth'
|
|
|
|
|
2016-07-09 20:21:48 +02:00
|
|
|
wordcount() get byte/word/char count of buffer
|
|
|
|
|
2020-06-04 15:22:21 +02:00
|
|
|
luaeval() evaluate |Lua| expression
|
2010-01-19 15:55:06 +01:00
|
|
|
mzeval() evaluate |MzScheme| expression
|
2016-01-17 21:15:58 +01:00
|
|
|
perleval() evaluate Perl expression (|+perl|)
|
2014-01-23 14:24:41 +01:00
|
|
|
py3eval() evaluate Python expression (|+python3|)
|
|
|
|
pyeval() evaluate Python expression (|+python|)
|
2017-01-28 18:34:47 +01:00
|
|
|
pyxeval() evaluate |python_x| expression
|
2020-06-04 15:22:21 +02:00
|
|
|
rubyeval() evaluate |Ruby| expression
|
|
|
|
|
2018-12-18 21:41:50 +01:00
|
|
|
debugbreak() interrupt a program being debugged
|
2010-01-19 15:55:06 +01:00
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
==============================================================================
|
|
|
|
*41.7* Defining a function
|
|
|
|
|
|
|
|
Vim enables you to define your own functions. The basic function declaration
|
|
|
|
begins as follows: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
def {name}({var1}, {var2}, ...): return-type
|
|
|
|
{body}
|
|
|
|
enddef
|
2004-06-13 20:20:40 +00:00
|
|
|
<
|
|
|
|
Note:
|
|
|
|
Function names must begin with a capital letter.
|
|
|
|
|
|
|
|
Let's define a short function to return the smaller of two numbers. It starts
|
|
|
|
with this line: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
def Min(num1: number, num2: number): number
|
|
|
|
|
|
|
|
This tells Vim that the function is named "Min", it takes two arguments that
|
|
|
|
are numbers: "num1" and "num2" and returns a number.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The first thing you need to do is to check to see which number is smaller:
|
2004-06-13 20:20:40 +00:00
|
|
|
>
|
2021-12-30 20:24:12 +00:00
|
|
|
if num1 < num2
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
Let's assign the variable "smaller" the value of the smallest number: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var smaller: number
|
|
|
|
if num1 < num2
|
|
|
|
smaller = num1
|
|
|
|
else
|
|
|
|
smaller = num2
|
|
|
|
endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
The variable "smaller" is a local variable. Variables used inside a function
|
2021-12-30 20:24:12 +00:00
|
|
|
are local unless prefixed by something like "g:", "w:", or "s:".
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
Note:
|
|
|
|
To access a global variable from inside a function you must prepend
|
2008-06-24 21:16:56 +00:00
|
|
|
"g:" to it. Thus "g:today" inside a function is used for the global
|
|
|
|
variable "today", and "today" is another variable, local to the
|
2021-12-30 20:24:12 +00:00
|
|
|
function or the script.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
You now use the `return` statement to return the smallest number to the user.
|
2004-06-13 20:20:40 +00:00
|
|
|
Finally, you end the function: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
return smaller
|
|
|
|
enddef
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
The complete function definition is as follows: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
def Min(num1: number, num2: number): number
|
|
|
|
var smaller: number
|
|
|
|
if num1 < num2
|
|
|
|
smaller = num1
|
|
|
|
else
|
|
|
|
smaller = num2
|
|
|
|
endif
|
|
|
|
return smaller
|
|
|
|
enddef
|
|
|
|
|
|
|
|
Obviously this is a verbose example. You can make it shorter by using two
|
|
|
|
return commands: >
|
|
|
|
|
|
|
|
def Min(num1: number, num2: number): number
|
|
|
|
if num1 < num2
|
|
|
|
return num1
|
|
|
|
endif
|
|
|
|
return num2
|
|
|
|
enddef
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
And if you remember the conditional expression, you need only one line: >
|
2005-02-07 22:01:03 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
def Min(num1: number, num2: number): number
|
|
|
|
return num1 < num2 ? num1 : num2
|
|
|
|
enddef
|
2005-02-07 22:01:03 +00:00
|
|
|
|
2006-02-22 21:25:37 +00:00
|
|
|
A user defined function is called in exactly the same way as a built-in
|
2004-06-13 20:20:40 +00:00
|
|
|
function. Only the name is different. The Min function can be used like
|
|
|
|
this: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
echo Min(5, 8)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Only now will the function be executed and the lines be parsed by Vim.
|
2004-06-13 20:20:40 +00:00
|
|
|
If there are mistakes, like using an undefined variable or function, you will
|
|
|
|
now get an error message. When defining the function these errors are not
|
2021-12-30 20:24:12 +00:00
|
|
|
detected. To get the errors sooner you can tell Vim to compile all the
|
|
|
|
functions in the script: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
defcompile
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
For a function that does not return anything leave out the return type: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
def SayIt(text: string)
|
|
|
|
echo text
|
|
|
|
enddef
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
It is also possible to define a legacy function with `function` and
|
|
|
|
`endfunction`. These do not have types and are not compiled. They execute
|
|
|
|
much slower.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
USING A RANGE
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
A line range can be used with a function call. The function will be called
|
|
|
|
once for every line in the range, with the cursor in that line. Example: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
def Number()
|
|
|
|
echo "line " .. line(".") .. " contains: " .. getline(".")
|
|
|
|
enddef
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
If you call this function with: >
|
|
|
|
|
|
|
|
:10,15call Number()
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The function will be called six times, starting on line 10 and ending on line
|
|
|
|
15.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
VARIABLE NUMBER OF ARGUMENTS
|
|
|
|
|
|
|
|
Vim enables you to define functions that have a variable number of arguments.
|
|
|
|
The following command, for instance, defines a function that must have 1
|
|
|
|
argument (start) and can have up to 20 additional arguments: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
def Show(start: string, ...items: list<string>)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The variable "items" will be a list containing the extra arguments. You can
|
|
|
|
use it like any list, for example: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
def Show(start: string, ...items: list<string>)
|
|
|
|
echohl Title
|
|
|
|
echo "start is " .. start
|
|
|
|
echohl None
|
|
|
|
for index in range(len(items))
|
|
|
|
echon " Arg " .. index .. " is " .. items[index]
|
|
|
|
endfor
|
|
|
|
echo
|
|
|
|
enddef
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
You can call it like this: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Show('Title', 'one', 'two', 'three')
|
|
|
|
< start is Title Arg 0 is one Arg 1 is two Arg 2 is three ~
|
|
|
|
|
|
|
|
This uses the `echohl` command to specify the highlighting used for the
|
|
|
|
following `echo` command. `echohl None` stops it again. The `echon` command
|
|
|
|
works like `echo`, but doesn't output a line break.
|
|
|
|
|
|
|
|
If you call it with one argument the "items" list will be empty.
|
|
|
|
`range(len(items))` returns a list with the indexes, what `for` loops over,
|
|
|
|
we'll explain that further down.
|
2005-02-07 22:01:03 +00:00
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
LISTING FUNCTIONS
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The `function` command lists the names and arguments of all user-defined
|
2004-06-13 20:20:40 +00:00
|
|
|
functions: >
|
|
|
|
|
|
|
|
:function
|
2021-12-30 20:24:12 +00:00
|
|
|
< def <SNR>86_Show(start: string, ...items: list<string>) ~
|
2004-06-13 20:20:40 +00:00
|
|
|
function GetVimIndent() ~
|
|
|
|
function SetSyn(name) ~
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The "<SNR>" prefix means that a function is script-local. |Vim9| functions
|
|
|
|
wil start with "def" and include argument and return types. Legacy functions
|
|
|
|
are listed with "function".
|
|
|
|
|
|
|
|
To see what a function does, use its name as an argument for `function`: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
:function SetSyn
|
|
|
|
< 1 if &syntax == '' ~
|
|
|
|
2 let &syntax = a:name ~
|
|
|
|
3 endif ~
|
|
|
|
endfunction ~
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
To see the "Show" function you need to include the script prefix, since a
|
|
|
|
"Show" function can be defined multiple times in different scripts. To find
|
|
|
|
the exact name you can use `function`, but the result may be a very long list.
|
|
|
|
To only get the functions matching a pattern you can use the `filter` prefix:
|
|
|
|
>
|
|
|
|
|
|
|
|
:filter Show function
|
|
|
|
< def <SNR>86_Show(start: string, ...items: list<string>) ~
|
|
|
|
>
|
|
|
|
:function <SNR>86_Show
|
|
|
|
< 1 echohl Title ~
|
|
|
|
2 echo "start is " .. start ~
|
|
|
|
etc.
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
DEBUGGING
|
|
|
|
|
|
|
|
The line number is useful for when you get an error message or when debugging.
|
|
|
|
See |debug-scripts| about debugging mode.
|
2021-12-30 20:24:12 +00:00
|
|
|
|
|
|
|
You can also set the 'verbose' option to 12 or higher to see all function
|
2004-06-13 20:20:40 +00:00
|
|
|
calls. Set it to 15 or higher to see every executed line.
|
|
|
|
|
|
|
|
|
|
|
|
DELETING A FUNCTION
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
To delete the SetSyn() function: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
:delfunction SetSyn
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Deleting only works for global functions and functions in legacy script, not
|
|
|
|
for functions defined in a |Vim9| script.
|
|
|
|
|
|
|
|
You get an error when the function doesn't exist or cannot be deleted.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2005-02-07 22:01:03 +00:00
|
|
|
|
|
|
|
FUNCTION REFERENCES
|
|
|
|
|
|
|
|
Sometimes it can be useful to have a variable point to one function or
|
2021-12-30 20:24:12 +00:00
|
|
|
another. You can do it with function reference variable. Often shortened to
|
|
|
|
"funcref". Example: >
|
|
|
|
|
|
|
|
def Right()
|
|
|
|
return 'Right!'
|
|
|
|
enddef
|
|
|
|
def Wrong()
|
|
|
|
return 'Wrong!'
|
|
|
|
enddef
|
|
|
|
|
|
|
|
var Afunc = g:result == 1 ? Right : Wrong
|
|
|
|
Afunc()
|
2005-02-07 22:01:03 +00:00
|
|
|
< Wrong! ~
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
This assumes "g:result" is not one.
|
|
|
|
|
2005-02-07 22:01:03 +00:00
|
|
|
Note that the name of a variable that holds a function reference must start
|
|
|
|
with a capital. Otherwise it could be confused with the name of a builtin
|
|
|
|
function.
|
|
|
|
|
2021-09-14 17:54:30 +02:00
|
|
|
More information about defining your own functions here: |user-functions|.
|
|
|
|
|
2005-02-07 22:01:03 +00:00
|
|
|
==============================================================================
|
|
|
|
*41.8* Lists and Dictionaries
|
|
|
|
|
|
|
|
So far we have used the basic types String and Number. Vim also supports two
|
|
|
|
composite types: List and Dictionary.
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
A List is an ordered sequence of items. The items can be any kind of value,
|
2005-02-07 22:01:03 +00:00
|
|
|
thus you can make a List of numbers, a List of Lists and even a List of mixed
|
|
|
|
items. To create a List with three strings: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var alist = ['aap', 'mies', 'noot']
|
2005-02-07 22:01:03 +00:00
|
|
|
|
|
|
|
The List items are enclosed in square brackets and separated by commas. To
|
|
|
|
create an empty List: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var alist = []
|
2005-02-07 22:01:03 +00:00
|
|
|
|
|
|
|
You can add items to a List with the add() function: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var alist = []
|
|
|
|
add(alist, 'foo')
|
|
|
|
add(alist, 'bar')
|
|
|
|
echo alist
|
2005-02-07 22:01:03 +00:00
|
|
|
< ['foo', 'bar'] ~
|
|
|
|
|
|
|
|
List concatenation is done with +: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var alist = ['foo', 'bar']
|
|
|
|
alist = alist + ['and', 'more']
|
|
|
|
echo alist
|
|
|
|
< ['foo', 'bar', 'and', 'more'] ~
|
2005-02-07 22:01:03 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Or, if you want to extend a List with a function: >
|
2005-02-07 22:01:03 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var alist = ['one']
|
|
|
|
extend(alist, ['two', 'three'])
|
|
|
|
echo alist
|
2005-02-07 22:01:03 +00:00
|
|
|
< ['one', 'two', 'three'] ~
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Notice that using `add()` will have a different effect: >
|
2005-02-07 22:01:03 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var alist = ['one']
|
|
|
|
add(alist, ['two', 'three'])
|
|
|
|
echo alist
|
2005-02-07 22:01:03 +00:00
|
|
|
< ['one', ['two', 'three']] ~
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The second argument of add() is added as an item, now you have a nested list.
|
2005-02-07 22:01:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
FOR LOOP
|
|
|
|
|
|
|
|
One of the nice things you can do with a List is iterate over it: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var alist = ['one', 'two', 'three']
|
|
|
|
for n in alist
|
|
|
|
echo n
|
|
|
|
endfor
|
2005-02-07 22:01:03 +00:00
|
|
|
< one ~
|
|
|
|
two ~
|
|
|
|
three ~
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
This will loop over each element in List "alist", assigning each value to
|
2005-02-07 22:01:03 +00:00
|
|
|
variable "n". The generic form of a for loop is: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
for {varname} in {listexpression}
|
|
|
|
{commands}
|
|
|
|
endfor
|
2005-02-07 22:01:03 +00:00
|
|
|
|
|
|
|
To loop a certain number of times you need a List of a specific length. The
|
|
|
|
range() function creates one for you: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
for a in range(3)
|
|
|
|
echo a
|
|
|
|
endfor
|
2005-02-07 22:01:03 +00:00
|
|
|
< 0 ~
|
|
|
|
1 ~
|
|
|
|
2 ~
|
|
|
|
|
|
|
|
Notice that the first item of the List that range() produces is zero, thus the
|
|
|
|
last item is one less than the length of the list.
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
You can also specify the maximum value, the stride and even go backwards: >
|
|
|
|
|
|
|
|
for a in range(8, 4, -2)
|
|
|
|
echo a
|
|
|
|
endfor
|
2005-02-07 22:01:03 +00:00
|
|
|
< 8 ~
|
|
|
|
6 ~
|
|
|
|
4 ~
|
|
|
|
|
|
|
|
A more useful example, looping over lines in the buffer: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
for line in getline(1, 20)
|
|
|
|
if line =~ "Date: "
|
|
|
|
echo line
|
|
|
|
endif
|
|
|
|
endfor
|
2005-02-07 22:01:03 +00:00
|
|
|
|
|
|
|
This looks into lines 1 to 20 (inclusive) and echoes any date found in there.
|
|
|
|
|
|
|
|
|
|
|
|
DICTIONARIES
|
|
|
|
|
|
|
|
A Dictionary stores key-value pairs. You can quickly lookup a value if you
|
|
|
|
know the key. A Dictionary is created with curly braces: >
|
2006-04-30 18:54:39 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var uk2nl = {one: 'een', two: 'twee', three: 'drie'}
|
2005-02-07 22:01:03 +00:00
|
|
|
|
2005-02-12 14:29:27 +00:00
|
|
|
Now you can lookup words by putting the key in square brackets: >
|
2005-02-07 22:01:03 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
echo uk2nl['two']
|
|
|
|
< twee ~
|
|
|
|
|
|
|
|
If the key does not have special characters, you can use the dot notation: >
|
|
|
|
|
|
|
|
echo uk2nl.two
|
2005-02-07 22:01:03 +00:00
|
|
|
< twee ~
|
|
|
|
|
|
|
|
The generic form for defining a Dictionary is: >
|
|
|
|
|
|
|
|
{<key> : <value>, ...}
|
|
|
|
|
|
|
|
An empty Dictionary is one without any keys: >
|
|
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
The possibilities with Dictionaries are numerous. There are various functions
|
|
|
|
for them as well. For example, you can obtain a list of the keys and loop
|
|
|
|
over them: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
for key in keys(uk2nl)
|
|
|
|
echo key
|
|
|
|
endfor
|
2005-02-07 22:01:03 +00:00
|
|
|
< three ~
|
|
|
|
one ~
|
|
|
|
two ~
|
|
|
|
|
2008-06-24 21:16:56 +00:00
|
|
|
You will notice the keys are not ordered. You can sort the list to get a
|
2005-02-07 22:01:03 +00:00
|
|
|
specific order: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
for key in sort(keys(uk2nl))
|
|
|
|
echo key
|
|
|
|
endfor
|
2005-02-07 22:01:03 +00:00
|
|
|
< one ~
|
|
|
|
three ~
|
|
|
|
two ~
|
|
|
|
|
|
|
|
But you can never get back the order in which items are defined. For that you
|
|
|
|
need to use a List, it stores items in an ordered sequence.
|
|
|
|
|
|
|
|
|
|
|
|
For further reading see |Lists| and |Dictionaries|.
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
==============================================================================
|
2005-02-07 22:01:03 +00:00
|
|
|
*41.9* Exceptions
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
Let's start with an example: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
try
|
|
|
|
read ~/templates/pascal.tmpl
|
|
|
|
catch /E484:/
|
|
|
|
echo "Sorry, the Pascal template file cannot be found."
|
|
|
|
endtry
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The `read` command will fail if the file does not exist. Instead of
|
2004-06-13 20:20:40 +00:00
|
|
|
generating an error message, this code catches the error and gives the user a
|
2011-02-25 14:42:19 +01:00
|
|
|
nice message.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
For the commands in between `try` and `endtry` errors are turned into
|
2004-06-13 20:20:40 +00:00
|
|
|
exceptions. An exception is a string. In the case of an error the string
|
|
|
|
contains the error message. And every error message has a number. In this
|
|
|
|
case, the error we catch contains "E484:". This number is guaranteed to stay
|
|
|
|
the same (the text may change, e.g., it may be translated).
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Besides being able to give a nice error message, Vim will also continue
|
|
|
|
executing commands. Otherwise, once an uncaught error is encountered,
|
|
|
|
execution will be aborted.
|
|
|
|
|
|
|
|
When the `read` command causes another error, the pattern "E484:" will not
|
2004-06-13 20:20:40 +00:00
|
|
|
match in it. Thus this exception will not be caught and result in the usual
|
|
|
|
error message.
|
|
|
|
|
|
|
|
You might be tempted to do this: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
try
|
|
|
|
read ~/templates/pascal.tmpl
|
|
|
|
catch
|
|
|
|
echo "Sorry, the Pascal template file cannot be found."
|
|
|
|
endtry
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
This means all errors are caught. But then you will not see an error that
|
|
|
|
would indicate a completely different problem, such as "E21: Cannot make
|
|
|
|
changes, 'modifiable' is off".
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Another useful mechanism is the `finally` command: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var tmp = tempname()
|
|
|
|
try
|
|
|
|
exe ":.,$write " .. tmp
|
|
|
|
exe "!filter " .. tmp
|
|
|
|
:.,$delete
|
|
|
|
exe ":$read " .. tmp
|
|
|
|
finally
|
|
|
|
call delete(tmp)
|
|
|
|
endtry
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
This filters the lines from the cursor until the end of the file through the
|
|
|
|
"filter" command, which takes a file name argument. No matter if the
|
2021-12-30 20:24:12 +00:00
|
|
|
filtering works, something goes wrong in between `try` and `finally` or the
|
|
|
|
user cancels the filtering by pressing CTRL-C, the `call delete(tmp)` is
|
2004-06-13 20:20:40 +00:00
|
|
|
always executed. This makes sure you don't leave the temporary file behind.
|
|
|
|
|
|
|
|
More information about exception handling can be found in the reference
|
|
|
|
manual: |exception-handling|.
|
|
|
|
|
|
|
|
==============================================================================
|
2005-02-07 22:01:03 +00:00
|
|
|
*41.10* Various remarks
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Here is a summary of items that are useful to know when writing Vim scripts.
|
|
|
|
|
|
|
|
The end-of-line character depends on the system. For Vim scripts it is
|
|
|
|
recommended to always use the Unix fileformat, this also works on any other
|
|
|
|
system. That way you can copy your Vim scripts from MS-Windows to Unix and
|
|
|
|
they still work. See |:source_crnl|. To be sure it is set right, do this
|
|
|
|
before writing the file: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
:setlocal fileformat=unix
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
WHITE SPACE
|
|
|
|
|
|
|
|
Blank lines are allowed and ignored.
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Leading whitespace characters (blanks and TABs) are always ignored.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Trailing whitespace is often ignored, but not always. One command that
|
|
|
|
includes it is `map`.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
To include a whitespace character in the value of an option, it must be
|
|
|
|
escaped by a "\" (backslash) as in the following example: >
|
|
|
|
|
|
|
|
:set tags=my\ nice\ file
|
|
|
|
|
2011-02-25 14:42:19 +01:00
|
|
|
The same example written as: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
:set tags=my nice file
|
|
|
|
|
|
|
|
will issue an error, because it is interpreted as: >
|
|
|
|
|
|
|
|
:set tags=my
|
|
|
|
:set nice
|
|
|
|
:set file
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
|Vim9| script is very picky when it comes to white space. This was done
|
|
|
|
intentionally to make sure scripts are easy to read and to avoid mistakes.
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
COMMENTS
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
In |Vim9| script the character # starts a comment. Everything after
|
2004-06-13 20:20:40 +00:00
|
|
|
and including this character until the end-of-line is considered a comment and
|
|
|
|
is ignored, except for commands that don't consider comments, as shown in
|
2021-12-30 20:24:12 +00:00
|
|
|
examples below. A comment can start on any character position on the line,
|
|
|
|
but not when it is part of the command, e.g. in a string.
|
|
|
|
|
|
|
|
The character " (the double quote mark) starts a comment in legacy script.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
There is a little "catch" with comments for some commands. Examples: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
abbrev dev development # shorthand
|
|
|
|
map <F3> o#include # insert include
|
|
|
|
execute cmd # do it
|
|
|
|
!ls *.c # list C files
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The abbreviation 'dev' will be expanded to 'development # shorthand'. The
|
2004-06-13 20:20:40 +00:00
|
|
|
mapping of <F3> will actually be the whole line after the 'o# ....' including
|
2021-12-30 20:24:12 +00:00
|
|
|
the '# insert include'. The `execute` command will give an error. The `!`
|
|
|
|
command will send everything after it to the shell, most likely causing an
|
|
|
|
error.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
There can be no comment after `map`, `abbreviate`, `execute` and `!` commands
|
|
|
|
(there are a few more commands with this restriction). For the `map`,
|
|
|
|
`abbreviate` and `execute` commands there is a trick: >
|
|
|
|
|
|
|
|
abbrev dev development|# shorthand
|
|
|
|
map <F3> o#include|# insert include
|
|
|
|
execute '!ls *.c' |# do it
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
With the '|' character the command is separated from the next one. And that
|
2021-12-30 20:24:12 +00:00
|
|
|
next command is only a comment. The last command, using `execute` is a
|
|
|
|
general solution, it works for all commands that do not accept a comment or a
|
|
|
|
'|' to separate the next command.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
Notice that there is no white space before the '|' in the abbreviation and
|
|
|
|
mapping. For these commands, any character until the end-of-line or '|' is
|
|
|
|
included. As a consequence of this behavior, you don't always see that
|
|
|
|
trailing whitespace is included: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
map <F4> o#include
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
To spot these problems, you can highlight trailing spaces: >
|
|
|
|
match Search /\s\+$/
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2007-05-06 12:51:41 +00:00
|
|
|
For Unix there is one special way to comment a line, that allows making a Vim
|
2021-12-30 20:24:12 +00:00
|
|
|
script executable, and it also works in legacy script: >
|
2007-05-06 12:51:41 +00:00
|
|
|
#!/usr/bin/env vim -S
|
|
|
|
echo "this is a Vim script"
|
|
|
|
quit
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
PITFALLS
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
An even bigger problem arises in the following example: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
map ,ab o#include
|
|
|
|
unmap ,ab
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
Here the unmap command will not work, because it tries to unmap ",ab ". This
|
|
|
|
does not exist as a mapped sequence. An error will be issued, which is very
|
2021-12-30 20:24:12 +00:00
|
|
|
hard to identify, because the ending whitespace character in `unmap ,ab ` is
|
2004-06-13 20:20:40 +00:00
|
|
|
not visible.
|
|
|
|
|
|
|
|
And this is the same as what happens when one uses a comment after an 'unmap'
|
|
|
|
command: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
unmap ,ab # comment
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
Here the comment part will be ignored. However, Vim will try to unmap
|
|
|
|
',ab ', which does not exist. Rewrite it as: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
unmap ,ab| # comment
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
RESTORING THE VIEW
|
|
|
|
|
2012-10-21 03:02:54 +02:00
|
|
|
Sometimes you want to make a change and go back to where the cursor was.
|
2004-06-13 20:20:40 +00:00
|
|
|
Restoring the relative position would also be nice, so that the same line
|
|
|
|
appears at the top of the window.
|
2021-12-30 20:24:12 +00:00
|
|
|
|
|
|
|
This example yanks the current line, puts it above the first line in the file
|
|
|
|
and then restores the view: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
map ,p ma"aYHmbgg"aP`bzt`a
|
|
|
|
|
|
|
|
What this does: >
|
|
|
|
ma"aYHmbgg"aP`bzt`a
|
|
|
|
< ma set mark a at cursor position
|
|
|
|
"aY yank current line into register a
|
|
|
|
Hmb go to top line in window and set mark b there
|
|
|
|
gg go to first line in file
|
|
|
|
"aP put the yanked line above it
|
|
|
|
`b go back to top line in display
|
|
|
|
zt position the text in the window as before
|
|
|
|
`a go back to saved cursor position
|
|
|
|
|
|
|
|
|
|
|
|
PACKAGING
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Sometimes you will want to use global variables or functions, so that they can
|
|
|
|
be used anywhere. A good example is a global variable that passes a
|
|
|
|
preference to a plugin. To avoid other scripts using the same name, use a
|
|
|
|
prefix that is very unlikely to be used elsewhere. For example, if you have a
|
|
|
|
"mytags" plugin, you could use: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
g:mytags_location = '$HOME/project'
|
|
|
|
g:mytags_style = 'fast'
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
To minimize interference between plugins keep as much as possible local to the
|
|
|
|
script. |Vim9| script helps you with that, by default functions and variables
|
|
|
|
are script-local.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
If you split your plugin into parts, you can use `import` and `export` to
|
|
|
|
share items between those parts. See `:export` for the details.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
==============================================================================
|
2005-02-07 22:01:03 +00:00
|
|
|
*41.11* Writing a plugin *write-plugin*
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
You can write a Vim script in such a way that many people can use it. This is
|
|
|
|
called a plugin. Vim users can drop your script in their plugin directory and
|
|
|
|
use its features right away |add-plugin|.
|
|
|
|
|
|
|
|
There are actually two types of plugins:
|
|
|
|
|
|
|
|
global plugins: For all types of files.
|
|
|
|
filetype plugins: Only for files of a specific type.
|
|
|
|
|
|
|
|
In this section the first type is explained. Most items are also relevant for
|
|
|
|
writing filetype plugins. The specifics for filetype plugins are in the next
|
|
|
|
section |write-filetype-plugin|.
|
|
|
|
|
|
|
|
|
|
|
|
NAME
|
|
|
|
|
|
|
|
First of all you must choose a name for your plugin. The features provided
|
|
|
|
by the plugin should be clear from its name. And it should be unlikely that
|
|
|
|
someone else writes a plugin with the same name but which does something
|
2021-12-30 20:24:12 +00:00
|
|
|
different.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
A script that corrects typing mistakes could be called "typecorrect.vim". We
|
2004-06-13 20:20:40 +00:00
|
|
|
will use it here as an example.
|
|
|
|
|
|
|
|
For the plugin to work for everybody, it should follow a few guidelines. This
|
|
|
|
will be explained step-by-step. The complete example plugin is at the end.
|
|
|
|
|
|
|
|
|
|
|
|
BODY
|
|
|
|
|
|
|
|
Let's start with the body of the plugin, the lines that do the actual work: >
|
|
|
|
|
|
|
|
14 iabbrev teh the
|
|
|
|
15 iabbrev otehr other
|
|
|
|
16 iabbrev wnat want
|
|
|
|
17 iabbrev synchronisation
|
|
|
|
18 \ synchronization
|
|
|
|
|
|
|
|
The actual list should be much longer, of course.
|
|
|
|
|
|
|
|
The line numbers have only been added to explain a few things, don't put them
|
|
|
|
in your plugin file!
|
|
|
|
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
FIRST LINE
|
|
|
|
>
|
|
|
|
1 vim9script noclear
|
|
|
|
|
|
|
|
You need to use `vimscript` as the very first command. Best is to put it in
|
|
|
|
the very first line.
|
|
|
|
|
|
|
|
The script we are writing will have a `finish` command to bail out when it is
|
|
|
|
loaded a second time. To avoid the items defined in the script are lost the
|
|
|
|
"noclear" argument is used. More info about this at |vim9-reload|.
|
|
|
|
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
HEADER
|
|
|
|
|
|
|
|
You will probably add new corrections to the plugin and soon have several
|
2012-09-21 14:54:30 +02:00
|
|
|
versions lying around. And when distributing this file, people will want to
|
2004-06-13 20:20:40 +00:00
|
|
|
know who wrote this wonderful plugin and where they can send remarks.
|
|
|
|
Therefore, put a header at the top of your plugin: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
2 # Vim global plugin for correcting typing mistakes
|
|
|
|
3 # Last Change: 2021 Dec 30
|
|
|
|
4 # Maintainer: Bram Moolenaar <Bram@vim.org>
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
About copyright and licensing: Since plugins are very useful and it's hardly
|
|
|
|
worth restricting their distribution, please consider making your plugin
|
|
|
|
either public domain or use the Vim |license|. A short note about this near
|
|
|
|
the top of the plugin should be sufficient. Example: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
5 # License: This file is placed in the public domain.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
LINE CONTINUATION AND AVOIDING SIDE EFFECTS *use-cpo-save*
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
In line 18 above, the line-continuation mechanism is used |line-continuation|.
|
|
|
|
Users with 'compatible' set will run into trouble here, they will get an error
|
|
|
|
message. We can't just reset 'compatible', because that has a lot of side
|
2021-12-30 20:24:12 +00:00
|
|
|
effects. Instead, we will set the 'cpoptions' option to its Vim default
|
2004-06-13 20:20:40 +00:00
|
|
|
value and restore it later. That will allow the use of line-continuation and
|
|
|
|
make the script work for most people. It is done like this: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
11 var save_cpo = &cpo
|
2004-06-13 20:20:40 +00:00
|
|
|
12 set cpo&vim
|
|
|
|
..
|
2021-12-30 20:24:12 +00:00
|
|
|
42 &cpo = save_cpo
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
We first store the old value of 'cpoptions' in the "save_cpo" variable. At
|
2004-06-13 20:20:40 +00:00
|
|
|
the end of the plugin this value is restored.
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Notice that "save_cpo" is a script-local variable. A global variable could
|
2004-06-13 20:20:40 +00:00
|
|
|
already be in use for something else. Always use script-local variables for
|
|
|
|
things that are only used in the script.
|
|
|
|
|
|
|
|
|
|
|
|
NOT LOADING
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
It is possible that a user doesn't always want to load this plugin. Or the
|
2004-06-13 20:20:40 +00:00
|
|
|
system administrator has dropped it in the system-wide plugin directory, but a
|
|
|
|
user has his own plugin he wants to use. Then the user must have a chance to
|
2021-12-30 20:24:12 +00:00
|
|
|
disable loading this specific plugin. These lines will make it possible: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
7 if exists("g:loaded_typecorrect")
|
|
|
|
8 finish
|
|
|
|
9 endif
|
|
|
|
10 g:loaded_typecorrect = 1
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
This also avoids that when the script is loaded twice it would pointlessly
|
|
|
|
redefine functions and cause trouble for autocommands that are added twice.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The name is recommended to start with "g:loaded_" and then the file name of
|
|
|
|
the plugin, literally. The "g:" is prepended to make the variable global, so
|
|
|
|
that other places can check whether its functionality is available. Without
|
|
|
|
"g:" it would be local to the script.
|
2010-07-17 15:20:30 +02:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Using `finish` stops Vim from reading the rest of the file, it's much quicker
|
|
|
|
than using if-endif around the whole file, since Vim would still need to parse
|
|
|
|
the commands to find the `endif`.
|
2010-07-17 15:20:30 +02:00
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
MAPPING
|
|
|
|
|
|
|
|
Now let's make the plugin more interesting: We will add a mapping that adds a
|
|
|
|
correction for the word under the cursor. We could just pick a key sequence
|
|
|
|
for this mapping, but the user might already use it for something else. To
|
|
|
|
allow the user to define which keys a mapping in a plugin uses, the <Leader>
|
|
|
|
item can be used: >
|
|
|
|
|
2020-08-15 18:55:18 +02:00
|
|
|
22 map <unique> <Leader>a <Plug>TypecorrAdd;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2020-08-15 18:55:18 +02:00
|
|
|
The "<Plug>TypecorrAdd;" thing will do the work, more about that further on.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The user can set the "g:mapleader" variable to the key sequence that he wants
|
|
|
|
plugin mappings to start with. Thus if the user has done: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
g:mapleader = "_"
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
the mapping will define "_a". If the user didn't do this, the default value
|
|
|
|
will be used, which is a backslash. Then a map for "\a" will be defined.
|
|
|
|
|
|
|
|
Note that <unique> is used, this will cause an error message if the mapping
|
|
|
|
already happened to exist. |:map-<unique>|
|
|
|
|
|
|
|
|
But what if the user wants to define his own key sequence? We can allow that
|
|
|
|
with this mechanism: >
|
|
|
|
|
2020-08-15 18:55:18 +02:00
|
|
|
21 if !hasmapto('<Plug>TypecorrAdd;')
|
|
|
|
22 map <unique> <Leader>a <Plug>TypecorrAdd;
|
2004-06-13 20:20:40 +00:00
|
|
|
23 endif
|
|
|
|
|
2020-08-30 17:20:20 +02:00
|
|
|
This checks if a mapping to "<Plug>TypecorrAdd;" already exists, and only
|
2004-06-13 20:20:40 +00:00
|
|
|
defines the mapping from "<Leader>a" if it doesn't. The user then has a
|
|
|
|
chance of putting this in his vimrc file: >
|
|
|
|
|
2020-08-15 18:55:18 +02:00
|
|
|
map ,c <Plug>TypecorrAdd;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
Then the mapped key sequence will be ",c" instead of "_a" or "\a".
|
|
|
|
|
|
|
|
|
|
|
|
PIECES
|
|
|
|
|
|
|
|
If a script gets longer, you often want to break up the work in pieces. You
|
|
|
|
can use functions or mappings for this. But you don't want these functions
|
|
|
|
and mappings to interfere with the ones from other scripts. For example, you
|
|
|
|
could define a function Add(), but another script could try to define the same
|
2021-12-30 20:24:12 +00:00
|
|
|
function. To avoid this, we define the function local to the script.
|
|
|
|
Fortunately, in |Vim9| script this is the default. In a legacy script you
|
|
|
|
would need to prefix the name with "s:".
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
We will define a function that adds a new typing correction: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
30 def Add(from: string, correct: bool)
|
|
|
|
31 var to = input("type the correction for " .. from .. ": ")
|
|
|
|
32 exe ":iabbrev " .. from .. " " .. to
|
2004-06-13 20:20:40 +00:00
|
|
|
..
|
2021-12-30 20:24:12 +00:00
|
|
|
36 enddef
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Now we can call the function Add() from within this script. If another
|
|
|
|
script also defines Add(), it will be local to that script and can only
|
|
|
|
be called from that script. There can also be a global g:Add() function,
|
|
|
|
which is again another function.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
<SID> can be used with mappings. It generates a script ID, which identifies
|
|
|
|
the current script. In our typing correction plugin we use it like this: >
|
|
|
|
|
2020-08-15 18:55:18 +02:00
|
|
|
24 noremap <unique> <script> <Plug>TypecorrAdd; <SID>Add
|
2004-06-13 20:20:40 +00:00
|
|
|
..
|
2021-12-30 20:24:12 +00:00
|
|
|
28 noremap <SID>Add :call <SID>Add(expand("<cword>"), true)<CR>
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
Thus when a user types "\a", this sequence is invoked: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
\a -> <Plug>TypecorrAdd; -> <SID>Add -> :call <SID>Add(...)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2020-08-15 18:55:18 +02:00
|
|
|
If another script also maps <SID>Add, it will get another script ID and
|
2004-06-13 20:20:40 +00:00
|
|
|
thus define another mapping.
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Note that instead of Add() we use <SID>Add() here. That is because the
|
|
|
|
mapping is typed by the user, thus outside of the script context. The <SID>
|
|
|
|
is translated to the script ID, so that Vim knows in which script to look for
|
2004-06-13 20:20:40 +00:00
|
|
|
the Add() function.
|
|
|
|
|
|
|
|
This is a bit complicated, but it's required for the plugin to work together
|
|
|
|
with other plugins. The basic rule is that you use <SID>Add() in mappings and
|
2021-12-30 20:24:12 +00:00
|
|
|
Add() in other places (the script itself, autocommands, user commands).
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
We can also add a menu entry to do the same as the mapping: >
|
|
|
|
|
|
|
|
26 noremenu <script> Plugin.Add\ Correction <SID>Add
|
|
|
|
|
|
|
|
The "Plugin" menu is recommended for adding menu items for plugins. In this
|
|
|
|
case only one item is used. When adding more items, creating a submenu is
|
|
|
|
recommended. For example, "Plugin.CVS" could be used for a plugin that offers
|
|
|
|
CVS operations "Plugin.CVS.checkin", "Plugin.CVS.checkout", etc.
|
|
|
|
|
|
|
|
Note that in line 28 ":noremap" is used to avoid that any other mappings cause
|
|
|
|
trouble. Someone may have remapped ":call", for example. In line 24 we also
|
|
|
|
use ":noremap", but we do want "<SID>Add" to be remapped. This is why
|
|
|
|
"<script>" is used here. This only allows mappings which are local to the
|
|
|
|
script. |:map-<script>| The same is done in line 26 for ":noremenu".
|
|
|
|
|:menu-<script>|
|
|
|
|
|
|
|
|
|
|
|
|
<SID> AND <Plug> *using-<Plug>*
|
|
|
|
|
|
|
|
Both <SID> and <Plug> are used to avoid that mappings of typed keys interfere
|
|
|
|
with mappings that are only to be used from other mappings. Note the
|
|
|
|
difference between using <SID> and <Plug>:
|
|
|
|
|
|
|
|
<Plug> is visible outside of the script. It is used for mappings which the
|
|
|
|
user might want to map a key sequence to. <Plug> is a special code
|
|
|
|
that a typed key will never produce.
|
|
|
|
To make it very unlikely that other plugins use the same sequence of
|
|
|
|
characters, use this structure: <Plug> scriptname mapname
|
|
|
|
In our example the scriptname is "Typecorr" and the mapname is "Add".
|
2020-08-15 18:55:18 +02:00
|
|
|
We add a semicolon as the terminator. This results in
|
|
|
|
"<Plug>TypecorrAdd;". Only the first character of scriptname and
|
|
|
|
mapname is uppercase, so that we can see where mapname starts.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
<SID> is the script ID, a unique identifier for a script.
|
|
|
|
Internally Vim translates <SID> to "<SNR>123_", where "123" can be any
|
|
|
|
number. Thus a function "<SID>Add()" will have a name "<SNR>11_Add()"
|
|
|
|
in one script, and "<SNR>22_Add()" in another. You can see this if
|
|
|
|
you use the ":function" command to get a list of functions. The
|
|
|
|
translation of <SID> in mappings is exactly the same, that's how you
|
|
|
|
can call a script-local function from a mapping.
|
|
|
|
|
|
|
|
|
|
|
|
USER COMMAND
|
|
|
|
|
|
|
|
Now let's add a user command to add a correction: >
|
|
|
|
|
|
|
|
38 if !exists(":Correct")
|
2021-12-30 20:24:12 +00:00
|
|
|
39 command -nargs=1 Correct :call Add(<q-args>, false)
|
2004-06-13 20:20:40 +00:00
|
|
|
40 endif
|
|
|
|
|
|
|
|
The user command is defined only if no command with the same name already
|
|
|
|
exists. Otherwise we would get an error here. Overriding the existing user
|
|
|
|
command with ":command!" is not a good idea, this would probably make the user
|
|
|
|
wonder why the command he defined himself doesn't work. |:command|
|
2021-12-30 20:24:12 +00:00
|
|
|
If it did happen you can find out who to blame with: >
|
|
|
|
|
|
|
|
verbose command Correct
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
SCRIPT VARIABLES
|
|
|
|
|
|
|
|
When a variable starts with "s:" it is a script variable. It can only be used
|
|
|
|
inside a script. Outside the script it's not visible. This avoids trouble
|
|
|
|
with using the same variable name in different scripts. The variables will be
|
|
|
|
kept as long as Vim is running. And the same variables are used when sourcing
|
|
|
|
the same script again. |s:var|
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
The nice thing about |Vim9| script is that variables are local to the script
|
|
|
|
by default. You can prepend "s:" if you like, but you do not need to. And
|
|
|
|
functions in the script can also use the script variables without a prefix.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Script-local variables can also be used in functions, autocommands and user
|
|
|
|
commands that are defined in the script. Thus they are the perfect way to
|
|
|
|
share information between parts of your plugin, without it leaking out. In
|
|
|
|
our example we can add a few lines to count the number of corrections: >
|
|
|
|
|
|
|
|
19 var count = 4
|
2004-06-13 20:20:40 +00:00
|
|
|
..
|
2021-12-30 20:24:12 +00:00
|
|
|
30 def Add(from: string, correct: bool)
|
2004-06-13 20:20:40 +00:00
|
|
|
..
|
2021-12-30 20:24:12 +00:00
|
|
|
34 count += 1
|
|
|
|
35 echo "you now have " .. count .. " corrections"
|
|
|
|
36 enddef
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
"count" is declared and initialized to 4 in the script itself. When later
|
|
|
|
the Add() function is called, it increments "count". It doesn't matter from
|
2004-06-13 20:20:40 +00:00
|
|
|
where the function was called, since it has been defined in the script, it
|
|
|
|
will use the local variables from this script.
|
|
|
|
|
|
|
|
|
|
|
|
THE RESULT
|
|
|
|
|
|
|
|
Here is the resulting complete example: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
1 vim9script noclear
|
|
|
|
2 # Vim global plugin for correcting typing mistakes
|
|
|
|
3 # Last Change: 2021 Dec 30
|
|
|
|
4 # Maintainer: Bram Moolenaar <Bram@vim.org>
|
|
|
|
5 # License: This file is placed in the public domain.
|
|
|
|
6
|
|
|
|
7 if exists("g:loaded_typecorrect")
|
|
|
|
8 finish
|
|
|
|
9 endif
|
|
|
|
10 g:loaded_typecorrect = 1
|
|
|
|
11 var save_cpo = &cpo
|
2004-06-13 20:20:40 +00:00
|
|
|
12 set cpo&vim
|
|
|
|
13
|
|
|
|
14 iabbrev teh the
|
|
|
|
15 iabbrev otehr other
|
|
|
|
16 iabbrev wnat want
|
|
|
|
17 iabbrev synchronisation
|
|
|
|
18 \ synchronization
|
2021-12-30 20:24:12 +00:00
|
|
|
19 var count = 4
|
2004-06-13 20:20:40 +00:00
|
|
|
20
|
2020-08-15 18:55:18 +02:00
|
|
|
21 if !hasmapto('<Plug>TypecorrAdd;')
|
|
|
|
22 map <unique> <Leader>a <Plug>TypecorrAdd;
|
2004-06-13 20:20:40 +00:00
|
|
|
23 endif
|
2020-08-15 18:55:18 +02:00
|
|
|
24 noremap <unique> <script> <Plug>TypecorrAdd; <SID>Add
|
2004-06-13 20:20:40 +00:00
|
|
|
25
|
|
|
|
26 noremenu <script> Plugin.Add\ Correction <SID>Add
|
|
|
|
27
|
2021-12-30 20:24:12 +00:00
|
|
|
28 noremap <SID>Add :call <SID>Add(expand("<cword>"), true)<CR>
|
2004-06-13 20:20:40 +00:00
|
|
|
29
|
2021-12-30 20:24:12 +00:00
|
|
|
30 def Add(from: string, correct: bool)
|
|
|
|
31 var to = input("type the correction for " .. from .. ": ")
|
|
|
|
32 exe ":iabbrev " .. from .. " " .. to
|
|
|
|
33 if correct | exe "normal viws\<C-R>\" \b\e" | endif
|
|
|
|
34 count += 1
|
|
|
|
35 echo "you now have " .. count .. " corrections"
|
|
|
|
36 enddef
|
2004-06-13 20:20:40 +00:00
|
|
|
37
|
|
|
|
38 if !exists(":Correct")
|
2021-12-30 20:24:12 +00:00
|
|
|
39 command -nargs=1 Correct call Add(<q-args>, false)
|
2004-06-13 20:20:40 +00:00
|
|
|
40 endif
|
|
|
|
41
|
2021-12-30 20:24:12 +00:00
|
|
|
42 &cpo = save_cpo
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
Line 33 wasn't explained yet. It applies the new correction to the word under
|
|
|
|
the cursor. The |:normal| command is used to use the new abbreviation. Note
|
|
|
|
that mappings and abbreviations are expanded here, even though the function
|
|
|
|
was called from a mapping defined with ":noremap".
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION *write-local-help*
|
|
|
|
|
|
|
|
It's a good idea to also write some documentation for your plugin. Especially
|
|
|
|
when its behavior can be changed by the user. See |add-local-help| for how
|
|
|
|
they are installed.
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Here is a simple example for a plugin help file, called "typecorrect.txt": >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
1 *typecorrect.txt* Plugin for correcting typing mistakes
|
2004-06-13 20:20:40 +00:00
|
|
|
2
|
|
|
|
3 If you make typing mistakes, this plugin will have them corrected
|
|
|
|
4 automatically.
|
|
|
|
5
|
|
|
|
6 There are currently only a few corrections. Add your own if you like.
|
|
|
|
7
|
|
|
|
8 Mappings:
|
2020-08-15 18:55:18 +02:00
|
|
|
9 <Leader>a or <Plug>TypecorrAdd;
|
2004-06-13 20:20:40 +00:00
|
|
|
10 Add a correction for the word under the cursor.
|
|
|
|
11
|
|
|
|
12 Commands:
|
|
|
|
13 :Correct {word}
|
|
|
|
14 Add a correction for {word}.
|
|
|
|
15
|
2021-12-30 20:24:12 +00:00
|
|
|
16 *typecorrect-settings*
|
2004-06-13 20:20:40 +00:00
|
|
|
17 This plugin doesn't have any settings.
|
|
|
|
|
|
|
|
The first line is actually the only one for which the format matters. It will
|
|
|
|
be extracted from the help file to be put in the "LOCAL ADDITIONS:" section of
|
|
|
|
help.txt |local-additions|. The first "*" must be in the first column of the
|
|
|
|
first line. After adding your help file do ":help" and check that the entries
|
|
|
|
line up nicely.
|
|
|
|
|
|
|
|
You can add more tags inside ** in your help file. But be careful not to use
|
|
|
|
existing help tags. You would probably use the name of your plugin in most of
|
2021-12-30 20:24:12 +00:00
|
|
|
them, like "typecorrect-settings" in the example.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
Using references to other parts of the help in || is recommended. This makes
|
|
|
|
it easy for the user to find associated help.
|
|
|
|
|
|
|
|
|
|
|
|
FILETYPE DETECTION *plugin-filetype*
|
|
|
|
|
|
|
|
If your filetype is not already detected by Vim, you should create a filetype
|
|
|
|
detection snippet in a separate file. It is usually in the form of an
|
|
|
|
autocommand that sets the filetype when the file name matches a pattern.
|
|
|
|
Example: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
au BufNewFile,BufRead *.foo setlocal filetype=foofoo
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
Write this single-line file as "ftdetect/foofoo.vim" in the first directory
|
|
|
|
that appears in 'runtimepath'. For Unix that would be
|
|
|
|
"~/.vim/ftdetect/foofoo.vim". The convention is to use the name of the
|
|
|
|
filetype for the script name.
|
|
|
|
|
|
|
|
You can make more complicated checks if you like, for example to inspect the
|
|
|
|
contents of the file to recognize the language. Also see |new-filetype|.
|
|
|
|
|
|
|
|
|
|
|
|
SUMMARY *plugin-special*
|
|
|
|
|
|
|
|
Summary of special things to use in a plugin:
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var name Variable local to the script.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
<SID> Script-ID, used for mappings and functions local to
|
|
|
|
the script.
|
|
|
|
|
|
|
|
hasmapto() Function to test if the user already defined a mapping
|
|
|
|
for functionality the script offers.
|
|
|
|
|
|
|
|
<Leader> Value of "mapleader", which the user defines as the
|
|
|
|
keys that plugin mappings start with.
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
map <unique> Give a warning if a mapping already exists.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
noremap <script> Use only mappings local to the script, not global
|
2004-06-13 20:20:40 +00:00
|
|
|
mappings.
|
|
|
|
|
|
|
|
exists(":Cmd") Check if a user command already exists.
|
|
|
|
|
|
|
|
==============================================================================
|
2005-02-07 22:01:03 +00:00
|
|
|
*41.12* Writing a filetype plugin *write-filetype-plugin* *ftplugin*
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
A filetype plugin is like a global plugin, except that it sets options and
|
|
|
|
defines mappings for the current buffer only. See |add-filetype-plugin| for
|
|
|
|
how this type of plugin is used.
|
|
|
|
|
2005-02-07 22:01:03 +00:00
|
|
|
First read the section on global plugins above |41.11|. All that is said there
|
2004-06-13 20:20:40 +00:00
|
|
|
also applies to filetype plugins. There are a few extras, which are explained
|
|
|
|
here. The essential thing is that a filetype plugin should only have an
|
|
|
|
effect on the current buffer.
|
|
|
|
|
|
|
|
|
|
|
|
DISABLING
|
|
|
|
|
|
|
|
If you are writing a filetype plugin to be used by many people, they need a
|
|
|
|
chance to disable loading it. Put this at the top of the plugin: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
# Only do this when not done yet for this buffer
|
2004-06-13 20:20:40 +00:00
|
|
|
if exists("b:did_ftplugin")
|
|
|
|
finish
|
|
|
|
endif
|
2021-12-30 20:24:12 +00:00
|
|
|
b:did_ftplugin = 1
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
This also needs to be used to avoid that the same plugin is executed twice for
|
|
|
|
the same buffer (happens when using an ":edit" command without arguments).
|
|
|
|
|
|
|
|
Now users can disable loading the default plugin completely by making a
|
2021-12-30 20:24:12 +00:00
|
|
|
filetype plugin with only these lines: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
vim9script
|
|
|
|
b:did_ftplugin = 1
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
This does require that the filetype plugin directory comes before $VIMRUNTIME
|
|
|
|
in 'runtimepath'!
|
|
|
|
|
|
|
|
If you do want to use the default plugin, but overrule one of the settings,
|
|
|
|
you can write the different setting in a script: >
|
|
|
|
|
|
|
|
setlocal textwidth=70
|
|
|
|
|
|
|
|
Now write this in the "after" directory, so that it gets sourced after the
|
|
|
|
distributed "vim.vim" ftplugin |after-directory|. For Unix this would be
|
|
|
|
"~/.vim/after/ftplugin/vim.vim". Note that the default plugin will have set
|
|
|
|
"b:did_ftplugin", but it is ignored here.
|
|
|
|
|
|
|
|
|
|
|
|
OPTIONS
|
|
|
|
|
|
|
|
To make sure the filetype plugin only affects the current buffer use the >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
setlocal
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
command to set options. And only set options which are local to a buffer (see
|
2021-12-30 20:24:12 +00:00
|
|
|
the help for the option to check that). When using `:setlocal` for global
|
2004-06-13 20:20:40 +00:00
|
|
|
options or options local to a window, the value will change for many buffers,
|
|
|
|
and that is not what a filetype plugin should do.
|
|
|
|
|
|
|
|
When an option has a value that is a list of flags or items, consider using
|
|
|
|
"+=" and "-=" to keep the existing value. Be aware that the user may have
|
|
|
|
changed an option value already. First resetting to the default value and
|
2011-02-09 17:07:58 +01:00
|
|
|
then changing it is often a good idea. Example: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
setlocal formatoptions& formatoptions+=ro
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
MAPPINGS
|
|
|
|
|
|
|
|
To make sure mappings will only work in the current buffer use the >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
map <buffer>
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
command. This needs to be combined with the two-step mapping explained above.
|
|
|
|
An example of how to define functionality in a filetype plugin: >
|
|
|
|
|
2020-08-15 18:55:18 +02:00
|
|
|
if !hasmapto('<Plug>JavaImport;')
|
|
|
|
map <buffer> <unique> <LocalLeader>i <Plug>JavaImport;
|
2004-06-13 20:20:40 +00:00
|
|
|
endif
|
2020-08-15 18:55:18 +02:00
|
|
|
noremap <buffer> <unique> <Plug>JavaImport; oimport ""<Left><Esc>
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|hasmapto()| is used to check if the user has already defined a map to
|
2020-08-15 18:55:18 +02:00
|
|
|
<Plug>JavaImport;. If not, then the filetype plugin defines the default
|
2004-06-13 20:20:40 +00:00
|
|
|
mapping. This starts with |<LocalLeader>|, which allows the user to select
|
|
|
|
the key(s) he wants filetype plugin mappings to start with. The default is a
|
|
|
|
backslash.
|
|
|
|
"<unique>" is used to give an error message if the mapping already exists or
|
|
|
|
overlaps with an existing mapping.
|
|
|
|
|:noremap| is used to avoid that any other mappings that the user has defined
|
|
|
|
interferes. You might want to use ":noremap <script>" to allow remapping
|
|
|
|
mappings defined in this script that start with <SID>.
|
|
|
|
|
|
|
|
The user must have a chance to disable the mappings in a filetype plugin,
|
|
|
|
without disabling everything. Here is an example of how this is done for a
|
|
|
|
plugin for the mail filetype: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
# Add mappings, unless the user didn't want this.
|
|
|
|
if !exists("g:no_plugin_maps") && !exists("g:no_mail_maps")
|
|
|
|
# Quote text by inserting "> "
|
2020-08-15 18:55:18 +02:00
|
|
|
if !hasmapto('<Plug>MailQuote;')
|
|
|
|
vmap <buffer> <LocalLeader>q <Plug>MailQuote;
|
|
|
|
nmap <buffer> <LocalLeader>q <Plug>MailQuote;
|
2004-06-13 20:20:40 +00:00
|
|
|
endif
|
2020-08-15 18:55:18 +02:00
|
|
|
vnoremap <buffer> <Plug>MailQuote; :s/^/> /<CR>
|
|
|
|
nnoremap <buffer> <Plug>MailQuote; :.,$s/^/> /<CR>
|
2004-06-13 20:20:40 +00:00
|
|
|
endif
|
|
|
|
|
|
|
|
Two global variables are used:
|
2021-12-30 20:24:12 +00:00
|
|
|
|g:no_plugin_maps| disables mappings for all filetype plugins
|
|
|
|
|g:no_mail_maps| disables mappings for the "mail" filetype
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
USER COMMANDS
|
|
|
|
|
|
|
|
To add a user command for a specific file type, so that it can only be used in
|
|
|
|
one buffer, use the "-buffer" argument to |:command|. Example: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
command -buffer Make make %:r.s
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
VARIABLES
|
|
|
|
|
|
|
|
A filetype plugin will be sourced for each buffer of the type it's for. Local
|
2021-12-30 20:24:12 +00:00
|
|
|
script variables will be shared between all invocations. Use local buffer
|
|
|
|
variables |b:var| if you want a variable specifically for one buffer.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
FUNCTIONS
|
|
|
|
|
|
|
|
When defining a function, this only needs to be done once. But the filetype
|
|
|
|
plugin will be sourced every time a file with this filetype will be opened.
|
2010-05-22 15:37:44 +02:00
|
|
|
This construct makes sure the function is only defined once: >
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
if !exists("*Func")
|
|
|
|
def Func(arg)
|
|
|
|
...
|
|
|
|
enddef
|
|
|
|
endif
|
2004-06-13 20:20:40 +00:00
|
|
|
<
|
|
|
|
|
2016-02-15 22:07:32 +01:00
|
|
|
UNDO *undo_indent* *undo_ftplugin*
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
When the user does ":setfiletype xyz" the effect of the previous filetype
|
|
|
|
should be undone. Set the b:undo_ftplugin variable to the commands that will
|
|
|
|
undo the settings in your filetype plugin. Example: >
|
|
|
|
|
2022-01-29 22:20:48 +00:00
|
|
|
let b:undo_ftplugin = "setlocal fo< com< tw< commentstring<"
|
2020-09-07 22:18:52 +02:00
|
|
|
\ .. "| unlet b:match_ignorecase b:match_words b:match_skip"
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
Using ":setlocal" with "<" after the option name resets the option to its
|
|
|
|
global value. That is mostly the best way to reset the option value.
|
|
|
|
|
|
|
|
This does require removing the "C" flag from 'cpoptions' to allow line
|
|
|
|
continuation, as mentioned above |use-cpo-save|.
|
|
|
|
|
2016-02-15 22:07:32 +01:00
|
|
|
For undoing the effect of an indent script, the b:undo_indent variable should
|
|
|
|
be set accordingly.
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
FILE NAME
|
|
|
|
|
|
|
|
The filetype must be included in the file name |ftplugin-name|. Use one of
|
|
|
|
these three forms:
|
|
|
|
|
|
|
|
.../ftplugin/stuff.vim
|
|
|
|
.../ftplugin/stuff_foo.vim
|
|
|
|
.../ftplugin/stuff/bar.vim
|
|
|
|
|
|
|
|
"stuff" is the filetype, "foo" and "bar" are arbitrary names.
|
|
|
|
|
|
|
|
|
|
|
|
SUMMARY *ftplugin-special*
|
|
|
|
|
|
|
|
Summary of special things to use in a filetype plugin:
|
|
|
|
|
|
|
|
<LocalLeader> Value of "maplocalleader", which the user defines as
|
|
|
|
the keys that filetype plugin mappings start with.
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
map <buffer> Define a mapping local to the buffer.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
noremap <script> Only remap mappings defined in this script that start
|
2004-06-13 20:20:40 +00:00
|
|
|
with <SID>.
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
setlocal Set an option for the current buffer only.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
command -buffer Define a user command local to the buffer.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
exists("*s:Func") Check if a function was already defined.
|
|
|
|
|
|
|
|
Also see |plugin-special|, the special things used for all plugins.
|
|
|
|
|
|
|
|
==============================================================================
|
2005-02-07 22:01:03 +00:00
|
|
|
*41.13* Writing a compiler plugin *write-compiler-plugin*
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
A compiler plugin sets options for use with a specific compiler. The user can
|
|
|
|
load it with the |:compiler| command. The main use is to set the
|
|
|
|
'errorformat' and 'makeprg' options.
|
|
|
|
|
|
|
|
Easiest is to have a look at examples. This command will edit all the default
|
|
|
|
compiler plugins: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
next $VIMRUNTIME/compiler/*.vim
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Type `:next` to go to the next plugin file.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
There are two special items about these files. First is a mechanism to allow
|
|
|
|
a user to overrule or add to the default file. The default files start with: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
if exists("g:current_compiler")
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
g:current_compiler = "mine"
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
When you write a compiler file and put it in your personal runtime directory
|
|
|
|
(e.g., ~/.vim/compiler for Unix), you set the "current_compiler" variable to
|
|
|
|
make the default file skip the settings.
|
2005-12-02 00:44:04 +00:00
|
|
|
*:CompilerSet*
|
2004-06-13 20:20:40 +00:00
|
|
|
The second mechanism is to use ":set" for ":compiler!" and ":setlocal" for
|
|
|
|
":compiler". Vim defines the ":CompilerSet" user command for this. However,
|
|
|
|
older Vim versions don't, thus your plugin should define it then. This is an
|
|
|
|
example: >
|
|
|
|
|
|
|
|
if exists(":CompilerSet") != 2
|
|
|
|
command -nargs=* CompilerSet setlocal <args>
|
|
|
|
endif
|
|
|
|
CompilerSet errorformat& " use the default 'errorformat'
|
|
|
|
CompilerSet makeprg=nmake
|
|
|
|
|
|
|
|
When you write a compiler plugin for the Vim distribution or for a system-wide
|
|
|
|
runtime directory, use the mechanism mentioned above. When
|
|
|
|
"current_compiler" was already set by a user plugin nothing will be done.
|
|
|
|
|
|
|
|
When you write a compiler plugin to overrule settings from a default plugin,
|
|
|
|
don't check "current_compiler". This plugin is supposed to be loaded
|
|
|
|
last, thus it should be in a directory at the end of 'runtimepath'. For Unix
|
|
|
|
that could be ~/.vim/after/compiler.
|
|
|
|
|
2005-02-26 23:04:13 +00:00
|
|
|
==============================================================================
|
|
|
|
*41.14* Writing a plugin that loads quickly *write-plugin-quickload*
|
|
|
|
|
|
|
|
A plugin may grow and become quite long. The startup delay may become
|
2008-06-24 21:16:56 +00:00
|
|
|
noticeable, while you hardly ever use the plugin. Then it's time for a
|
2005-02-26 23:04:13 +00:00
|
|
|
quickload plugin.
|
|
|
|
|
|
|
|
The basic idea is that the plugin is loaded twice. The first time user
|
|
|
|
commands and mappings are defined that offer the functionality. The second
|
|
|
|
time the functions that implement the functionality are defined.
|
|
|
|
|
|
|
|
It may sound surprising that quickload means loading a script twice. What we
|
|
|
|
mean is that it loads quickly the first time, postponing the bulk of the
|
|
|
|
script to the second time, which only happens when you actually use it. When
|
|
|
|
you always use the functionality it actually gets slower!
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
This uses a FuncUndefined autocommand. Since Vim 7 there is an alternative:
|
|
|
|
use the |autoload| functionality |41.15|. That will also use |Vim9| script
|
|
|
|
instead of legacy script that is used here.
|
2006-03-21 21:23:25 +00:00
|
|
|
|
2005-02-26 23:04:13 +00:00
|
|
|
The following example shows how it's done: >
|
|
|
|
|
|
|
|
" Vim global plugin for demonstrating quick loading
|
|
|
|
" Last Change: 2005 Feb 25
|
|
|
|
" Maintainer: Bram Moolenaar <Bram@vim.org>
|
|
|
|
" License: This file is placed in the public domain.
|
|
|
|
|
|
|
|
if !exists("s:did_load")
|
|
|
|
command -nargs=* BNRead call BufNetRead(<f-args>)
|
|
|
|
map <F19> :call BufNetWrite('something')<CR>
|
|
|
|
|
|
|
|
let s:did_load = 1
|
2020-09-07 22:18:52 +02:00
|
|
|
exe 'au FuncUndefined BufNet* source ' .. expand('<sfile>')
|
2005-02-26 23:04:13 +00:00
|
|
|
finish
|
|
|
|
endif
|
|
|
|
|
|
|
|
function BufNetRead(...)
|
2020-09-07 22:18:52 +02:00
|
|
|
echo 'BufNetRead(' .. string(a:000) .. ')'
|
2005-02-26 23:04:13 +00:00
|
|
|
" read functionality here
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function BufNetWrite(...)
|
2020-09-07 22:18:52 +02:00
|
|
|
echo 'BufNetWrite(' .. string(a:000) .. ')'
|
2005-02-26 23:04:13 +00:00
|
|
|
" write functionality here
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
When the script is first loaded "s:did_load" is not set. The commands between
|
|
|
|
the "if" and "endif" will be executed. This ends in a |:finish| command, thus
|
|
|
|
the rest of the script is not executed.
|
|
|
|
|
|
|
|
The second time the script is loaded "s:did_load" exists and the commands
|
|
|
|
after the "endif" are executed. This defines the (possible long)
|
|
|
|
BufNetRead() and BufNetWrite() functions.
|
|
|
|
|
|
|
|
If you drop this script in your plugin directory Vim will execute it on
|
|
|
|
startup. This is the sequence of events that happens:
|
|
|
|
|
|
|
|
1. The "BNRead" command is defined and the <F19> key is mapped when the script
|
|
|
|
is sourced at startup. A |FuncUndefined| autocommand is defined. The
|
|
|
|
":finish" command causes the script to terminate early.
|
|
|
|
|
|
|
|
2. The user types the BNRead command or presses the <F19> key. The
|
|
|
|
BufNetRead() or BufNetWrite() function will be called.
|
2006-04-30 18:54:39 +00:00
|
|
|
|
2005-02-26 23:04:13 +00:00
|
|
|
3. Vim can't find the function and triggers the |FuncUndefined| autocommand
|
|
|
|
event. Since the pattern "BufNet*" matches the invoked function, the
|
|
|
|
command "source fname" will be executed. "fname" will be equal to the name
|
|
|
|
of the script, no matter where it is located, because it comes from
|
|
|
|
expanding "<sfile>" (see |expand()|).
|
|
|
|
|
|
|
|
4. The script is sourced again, the "s:did_load" variable exists and the
|
|
|
|
functions are defined.
|
|
|
|
|
|
|
|
Notice that the functions that are loaded afterwards match the pattern in the
|
|
|
|
|FuncUndefined| autocommand. You must make sure that no other plugin defines
|
|
|
|
functions that match this pattern.
|
|
|
|
|
|
|
|
==============================================================================
|
|
|
|
*41.15* Writing library scripts *write-library-script*
|
|
|
|
|
|
|
|
Some functionality will be required in several places. When this becomes more
|
|
|
|
than a few lines you will want to put it in one script and use it from many
|
|
|
|
scripts. We will call that one script a library script.
|
|
|
|
|
|
|
|
Manually loading a library script is possible, so long as you avoid loading it
|
|
|
|
when it's already done. You can do this with the |exists()| function.
|
|
|
|
Example: >
|
|
|
|
|
|
|
|
if !exists('*MyLibFunction')
|
|
|
|
runtime library/mylibscript.vim
|
|
|
|
endif
|
2021-12-30 20:24:12 +00:00
|
|
|
MyLibFunction(arg)
|
2005-02-26 23:04:13 +00:00
|
|
|
|
|
|
|
Here you need to know that MyLibFunction() is defined in a script
|
|
|
|
"library/mylibscript.vim" in one of the directories in 'runtimepath'.
|
|
|
|
|
|
|
|
To make this a bit simpler Vim offers the autoload mechanism. Then the
|
|
|
|
example looks like this: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
mylib#myfunction(arg)
|
2005-02-26 23:04:13 +00:00
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
That's a lot simpler, isn't it? Vim will recognize the function name by the
|
|
|
|
embedded "#" character and when it's not defined search for the script
|
|
|
|
"autoload/mylib.vim" in 'runtimepath'. That script must define the
|
|
|
|
"mylib#myfunction()" function.
|
2005-02-26 23:04:13 +00:00
|
|
|
|
|
|
|
You can put many other functions in the mylib.vim script, you are free to
|
|
|
|
organize your functions in library scripts. But you must use function names
|
2005-06-13 22:28:56 +00:00
|
|
|
where the part before the '#' matches the script name. Otherwise Vim would
|
|
|
|
not know what script to load.
|
2005-02-26 23:04:13 +00:00
|
|
|
|
2006-02-22 21:25:37 +00:00
|
|
|
If you get really enthusiastic and write lots of library scripts, you may
|
2005-02-26 23:04:13 +00:00
|
|
|
want to use subdirectories. Example: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
netlib#ftp#read('somefile')
|
2005-02-26 23:04:13 +00:00
|
|
|
|
|
|
|
For Unix the library script used for this could be:
|
|
|
|
|
|
|
|
~/.vim/autoload/netlib/ftp.vim
|
|
|
|
|
|
|
|
Where the function is defined like this: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
def netlib#ftp#read(fname: string)
|
|
|
|
# Read the file fname through ftp
|
|
|
|
enddef
|
2005-02-26 23:04:13 +00:00
|
|
|
|
|
|
|
Notice that the name the function is defined with is exactly the same as the
|
2005-06-13 22:28:56 +00:00
|
|
|
name used for calling the function. And the part before the last '#'
|
2005-02-26 23:04:13 +00:00
|
|
|
exactly matches the subdirectory and script name.
|
|
|
|
|
|
|
|
You can use the same mechanism for variables: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var weekdays = dutch#weekdays
|
2005-02-26 23:04:13 +00:00
|
|
|
|
|
|
|
This will load the script "autoload/dutch.vim", which should contain something
|
|
|
|
like: >
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
var dutch#weekdays = ['zondag', 'maandag', 'dinsdag', 'woensdag',
|
2005-02-26 23:04:13 +00:00
|
|
|
\ 'donderdag', 'vrijdag', 'zaterdag']
|
|
|
|
|
|
|
|
Further reading: |autoload|.
|
|
|
|
|
2006-03-21 21:23:25 +00:00
|
|
|
==============================================================================
|
|
|
|
*41.16* Distributing Vim scripts *distribute-script*
|
|
|
|
|
|
|
|
Vim users will look for scripts on the Vim website: http://www.vim.org.
|
|
|
|
If you made something that is useful for others, share it!
|
|
|
|
|
2021-12-30 20:24:12 +00:00
|
|
|
Another place is github. But there you need to know where to find it! The
|
|
|
|
advantage is that most plugin managers fetch plugins from github. You'll have
|
|
|
|
to use your favorite search engine to find them.
|
|
|
|
|
|
|
|
Vim scripts can be used on any system. However, there might not be a tar or
|
|
|
|
gzip command. If you want to pack files together and/or compress them the
|
|
|
|
"zip" utility is recommended.
|
2006-03-21 21:23:25 +00:00
|
|
|
|
|
|
|
For utmost portability use Vim itself to pack scripts together. This can be
|
|
|
|
done with the Vimball utility. See |vimball|.
|
|
|
|
|
2006-03-24 22:21:52 +00:00
|
|
|
It's good if you add a line to allow automatic updating. See |glvs-plugins|.
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
==============================================================================
|
|
|
|
|
|
|
|
Next chapter: |usr_42.txt| Add new menus
|
|
|
|
|
2018-08-11 18:00:22 +02:00
|
|
|
Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl:
|