Bram Moolenaar | f10911e | 2022-01-29 22:20:48 +0000 | [diff] [blame] | 1 | *usr_41.txt* For Vim version 8.2. Last change: 2022 Jan 28 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | |
| 3 | VIM USER MANUAL - by Bram Moolenaar |
| 4 | |
| 5 | Write a Vim script |
| 6 | |
| 7 | |
| 8 | The Vim script language is used for the startup vimrc file, syntax files, and |
| 9 | many other things. This chapter explains the items that can be used in a Vim |
| 10 | script. There are a lot of them, thus this is a long chapter. |
| 11 | |
| 12 | |41.1| Introduction |
| 13 | |41.2| Variables |
| 14 | |41.3| Expressions |
| 15 | |41.4| Conditionals |
| 16 | |41.5| Executing an expression |
| 17 | |41.6| Using functions |
| 18 | |41.7| Defining a function |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 19 | |41.8| Lists and Dictionaries |
| 20 | |41.9| Exceptions |
| 21 | |41.10| Various remarks |
| 22 | |41.11| Writing a plugin |
| 23 | |41.12| Writing a filetype plugin |
| 24 | |41.13| Writing a compiler plugin |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 25 | |41.14| Writing a plugin that loads quickly |
| 26 | |41.15| Writing library scripts |
Bram Moolenaar | 76916e6 | 2006-03-21 21:23:25 +0000 | [diff] [blame] | 27 | |41.16| Distributing Vim scripts |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 28 | |
| 29 | Next chapter: |usr_42.txt| Add new menus |
| 30 | Previous chapter: |usr_40.txt| Make new commands |
| 31 | Table of contents: |usr_toc.txt| |
| 32 | |
| 33 | ============================================================================== |
Bram Moolenaar | 9d75c83 | 2005-01-25 21:57:23 +0000 | [diff] [blame] | 34 | *41.1* Introduction *vim-script-intro* *script* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 35 | |
| 36 | Your first experience with Vim scripts is the vimrc file. Vim reads it when |
| 37 | it starts up and executes the commands. You can set options to values you |
| 38 | prefer. And you can use any colon command in it (commands that start with a |
| 39 | ":"; these are sometimes referred to as Ex commands or command-line commands). |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 40 | |
| 41 | Syntax files are also Vim scripts. As are files that set options for a |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 42 | specific file type. A complicated macro can be defined by a separate Vim |
| 43 | script file. You can think of other uses yourself. |
| 44 | |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 45 | If you are familiar with Python, you can find a comparison between |
| 46 | Python and Vim script here, with pointers to other documents: |
| 47 | https://gist.github.com/yegappan/16d964a37ead0979b05e655aa036cad0 |
Bram Moolenaar | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [diff] [blame] | 48 | And if you are familiar with JavaScript: |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 49 | https://w0rp.com/blog/post/vim-script-for-the-javascripter/ |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 50 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 51 | Vim script comes in two flavors: legacy and |Vim9|. Since this help file is |
| 52 | for new users, we'll teach you the newer and more convenient |Vim9| syntax. |
| 53 | |
| 54 | To try out Vim script the best way is to edit a script file and source it. |
| 55 | Basically: > |
| 56 | :edit test.vim |
| 57 | [insert the script lines you want] |
| 58 | :w |
| 59 | :source % |
| 60 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 61 | Let's start with a simple example: > |
| 62 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 63 | vim9script |
| 64 | var i = 1 |
| 65 | while i < 5 |
| 66 | echo "count is" i |
| 67 | i += 1 |
| 68 | endwhile |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 69 | < |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 70 | The output of the example code is: |
| 71 | |
| 72 | count is 1 ~ |
| 73 | count is 2 ~ |
| 74 | count is 3 ~ |
| 75 | count is 4 ~ |
| 76 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 77 | In the first line the `vim9script` command makes clear this is a new, |Vim9| |
| 78 | script file. That matters for how the rest of the file is used. |
| 79 | |
| 80 | The `var i = 1` command declares the "i" variable and initializes it. The |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 81 | generic form is: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 82 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 83 | var {name} = {expression} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 84 | |
| 85 | In this case the variable name is "i" and the expression is a simple value, |
| 86 | the number one. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 87 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 88 | The `while` command starts a loop. The generic form is: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 89 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 90 | while {condition} |
| 91 | {statements} |
| 92 | endwhile |
| 93 | |
| 94 | The statements until the matching `endwhile` are executed for as long as the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 95 | condition is true. The condition used here is the expression "i < 5". This |
| 96 | is true when the variable i is smaller than five. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 97 | Note: |
| 98 | If you happen to write a while loop that keeps on running, you can |
| 99 | interrupt it by pressing CTRL-C (CTRL-Break on MS-Windows). |
| 100 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 101 | The `echo` command prints its arguments. In this case the string "count is" |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 102 | and the value of the variable i. Since i is one, this will print: |
| 103 | |
| 104 | count is 1 ~ |
| 105 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 106 | Then there is the `i += 1` command. This does the same thing as "i = i + 1", |
| 107 | it adds one to the variable i and assigns the new value to the same variable. |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 108 | |
| 109 | The example was given to explain the commands, but would you really want to |
Bram Moolenaar | 214641f | 2017-03-05 17:04:09 +0100 | [diff] [blame] | 110 | make such a loop, it can be written much more compact: > |
Bram Moolenaar | af7f641 | 2005-01-17 22:11:23 +0000 | [diff] [blame] | 111 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 112 | for i in range(1, 4) |
| 113 | echo "count is" i |
| 114 | endfor |
Bram Moolenaar | af7f641 | 2005-01-17 22:11:23 +0000 | [diff] [blame] | 115 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 116 | We won't explain how `for` and `range()` work until later. Follow the links |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 117 | if you are impatient. |
Bram Moolenaar | af7f641 | 2005-01-17 22:11:23 +0000 | [diff] [blame] | 118 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 119 | |
Bram Moolenaar | 7dd64a3 | 2019-05-31 21:41:05 +0200 | [diff] [blame] | 120 | FOUR KINDS OF NUMBERS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 121 | |
Bram Moolenaar | 11e3c5b | 2021-04-21 18:09:37 +0200 | [diff] [blame] | 122 | Numbers can be decimal, hexadecimal, octal or binary. |
| 123 | |
| 124 | A hexadecimal number starts with "0x" or "0X". For example "0x1f" is decimal |
| 125 | 31. |
| 126 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 127 | An octal number starts with "0o", "0O". "0o17" is decimal 15. |
Bram Moolenaar | 11e3c5b | 2021-04-21 18:09:37 +0200 | [diff] [blame] | 128 | |
| 129 | A binary number starts with "0b" or "0B". For example "0b101" is decimal 5. |
| 130 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 131 | A decimal number is just digits. Careful: In legacy script don't put a zero |
| 132 | before a decimal number, it will be interpreted as an octal number! |
Bram Moolenaar | 11e3c5b | 2021-04-21 18:09:37 +0200 | [diff] [blame] | 133 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 134 | The `echo` command evaluates its argument and always prints decimal numbers. |
| 135 | Example: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 136 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 137 | echo 0x7f 0o36 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 138 | < 127 30 ~ |
| 139 | |
Bram Moolenaar | 7dd64a3 | 2019-05-31 21:41:05 +0200 | [diff] [blame] | 140 | A number is made negative with a minus sign. This also works for hexadecimal, |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 141 | octal and binary numbers: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 142 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 143 | echo -0x7f |
| 144 | < -127 ~ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 145 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 146 | A minus sign is also used for subtraction. This can sometimes lead to |
| 147 | confusion. If we put a minus sign before both numbers we get an error: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 148 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 149 | echo -0x7f -0o36 |
| 150 | < E1004: White space required before and after '-' at "-0o36" ~ |
| 151 | |
| 152 | Note: if you are not using a |Vim9| script to try out these commands but type |
| 153 | them directly, they will be executed as legacy script. Then the echo command |
| 154 | sees the second minus sign as subtraction. To get the error, prefix the |
| 155 | command with `vim9cmd`: > |
| 156 | |
| 157 | vim9cmd echo -0x7f -0o36 |
| 158 | < E1004: White space required before and after '-' at "-0o36" ~ |
| 159 | |
| 160 | White space in an expression is often required to make sure it is easy to read |
| 161 | and avoid errors. Such as thinking that the "-0o36" above makes the number |
| 162 | negative, while it is actually seen as a subtraction. |
| 163 | |
| 164 | To actually have the minus sign be used for negation, you can put the second |
| 165 | expression in parenthesis: > |
| 166 | |
| 167 | echo -0x7f (-0o36) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 168 | |
| 169 | ============================================================================== |
| 170 | *41.2* Variables |
| 171 | |
| 172 | A variable name consists of ASCII letters, digits and the underscore. It |
| 173 | cannot start with a digit. Valid variable names are: |
| 174 | |
| 175 | counter |
| 176 | _aap3 |
| 177 | very_long_variable_name_with_underscores |
| 178 | FuncLength |
| 179 | LENGTH |
| 180 | |
| 181 | Invalid names are "foo+bar" and "6var". |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 182 | |
| 183 | Some variables are global. To see a list of currently defined global |
| 184 | variables type this command: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 185 | |
| 186 | :let |
| 187 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 188 | You can use global variables everywhere. However, it is easy to use the same |
| 189 | name in two unrelated scripts. Therefore variables declared in a script are |
| 190 | local to that script. For example, if you have this in "script1.vim": > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 191 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 192 | vim9script |
| 193 | var counter = 5 |
| 194 | echo counter |
| 195 | < 5 ~ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 196 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 197 | And you try to use the variable in "script2.vim": > |
| 198 | |
| 199 | vim9script |
| 200 | echo counter |
| 201 | < E121: Undefined variable: counter ~ |
| 202 | |
| 203 | Using a script-local variable means you can be sure that it is only changed in |
| 204 | that script and not elsewhere. |
| 205 | |
| 206 | If you do want to share variables between scripts, use the "g:" prefix and |
| 207 | assign the value directly, do not use `var`. Thus in "script1.vim": > |
| 208 | |
| 209 | vim9script |
| 210 | g:counter = 5 |
| 211 | echo g:counter |
| 212 | < 5 ~ |
| 213 | |
| 214 | And then in "script2.vim": > |
| 215 | |
| 216 | vim9script |
| 217 | echo g:counter |
| 218 | < 5 ~ |
| 219 | |
| 220 | More about script-local variables here: |script-variable|. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 221 | |
| 222 | There are more kinds of variables, see |internal-variables|. The most often |
| 223 | used ones are: |
| 224 | |
| 225 | b:name variable local to a buffer |
| 226 | w:name variable local to a window |
| 227 | g:name global variable (also in a function) |
| 228 | v:name variable predefined by Vim |
| 229 | |
| 230 | |
| 231 | DELETING VARIABLES |
| 232 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 233 | Variables take up memory and show up in the output of the `let` command. To |
| 234 | delete a global variable use the `unlet` command. Example: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 235 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 236 | unlet g:counter |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 237 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 238 | This deletes the global variable "g:counter" to free up the memory it uses. |
| 239 | If you are not sure if the variable exists, and don't want an error message |
| 240 | when it doesn't, append !: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 241 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 242 | unlet! g:counter |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 243 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 244 | You cannot `unlet` script-local variables in |Vim9| script. You can in legacy |
| 245 | script. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 246 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 247 | When a script finishes, the local variables declared there will not be |
| 248 | deleted. Functions defined in the script can use them. Example: |
| 249 | > |
| 250 | vim9script |
| 251 | var counter = 0 |
| 252 | def g:GetCount(): number |
| 253 | s:counter += 1 |
| 254 | return s:counter |
| 255 | enddef |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 256 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 257 | Every time you call the function it will return the next count: > |
| 258 | :echo g:GetCount() |
| 259 | < 1 ~ |
| 260 | > |
| 261 | :echo g:GetCount() |
| 262 | < 2 ~ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 263 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 264 | If you are worried a script-local variable is consuming too much |
| 265 | memory, set it to an empty value after you no longer need it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 266 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 267 | Note: below we'll leave out the `vim9script` line, so we can concentrate on |
| 268 | the relevant commands, but you'll still need to put it at the top of your |
| 269 | script file. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 270 | |
| 271 | |
| 272 | STRING VARIABLES AND CONSTANTS |
| 273 | |
| 274 | So far only numbers were used for the variable value. Strings can be used as |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 275 | well. Numbers and strings are the basic types of variables that Vim supports. |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 276 | Example: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 277 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 278 | var name = "Peter" |
| 279 | echo name |
Bram Moolenaar | 2f0936c | 2022-01-08 21:51:59 +0000 | [diff] [blame] | 280 | < Peter ~ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 281 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 282 | Every variable has a type. Very often, as in this example, the type is |
| 283 | defined by assigning a value. This is called type inference. If you do not |
| 284 | want to give the variable a value yet, you need to specify the type: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 285 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 286 | var name: string |
| 287 | var age: number |
| 288 | ... |
| 289 | name = "Peter" |
| 290 | age = 42 |
| 291 | |
| 292 | If you make a mistake and try to assign the wrong type of value you'll get an |
| 293 | error: > |
| 294 | |
| 295 | age = "Peter" |
| 296 | < E1012: Type mismatch; expected number but got string ~ |
| 297 | |
| 298 | More about types in |41.8|. |
| 299 | |
| 300 | To assign a string value to a variable, you need to use a string constant. |
| 301 | There are two types of these. First the string in double quotes, as we used |
| 302 | already. If you want to include a double quote inside the string, put a |
| 303 | backslash in front of it: > |
| 304 | |
| 305 | var name = "he is \"Peter\"" |
| 306 | echo name |
| 307 | < he is "Peter" ~ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 308 | |
| 309 | To avoid the need for a backslash, you can use a string in single quotes: > |
| 310 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 311 | var name = 'he is "Peter"' |
| 312 | echo name |
| 313 | < he is "Peter" ~ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 314 | |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 315 | Inside a single-quote string all the characters are as they are. Only the |
| 316 | single quote itself is special: you need to use two to get one. A backslash |
| 317 | is taken literally, thus you can't use it to change the meaning of the |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 318 | character after it: > |
| 319 | |
| 320 | var name = 'P\e''ter''' |
| 321 | echo name |
| 322 | < P\e'ter' ~ |
| 323 | |
| 324 | In double-quote strings it is possible to use special characters. Here are a |
| 325 | few useful ones: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 326 | |
| 327 | \t <Tab> |
| 328 | \n <NL>, line break |
| 329 | \r <CR>, <Enter> |
| 330 | \e <Esc> |
| 331 | \b <BS>, backspace |
| 332 | \" " |
| 333 | \\ \, backslash |
| 334 | \<Esc> <Esc> |
| 335 | \<C-W> CTRL-W |
| 336 | |
| 337 | The last two are just examples. The "\<name>" form can be used to include |
| 338 | the special key "name". |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 339 | |
| 340 | See |expr-quote| for the full list of special items in a string. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 341 | |
| 342 | ============================================================================== |
| 343 | *41.3* Expressions |
| 344 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 345 | Vim has a fairly standard way to handle expressions. You can read the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 346 | definition here: |expression-syntax|. Here we will show the most common |
| 347 | items. |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 348 | |
| 349 | The numbers, strings and variables mentioned above are expressions by |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 350 | themselves. Thus everywhere an expression is expected, you can use a number, |
| 351 | string or variable. Other basic items in an expression are: |
| 352 | |
| 353 | $NAME environment variable |
| 354 | &name option |
| 355 | @r register |
| 356 | |
| 357 | Examples: > |
| 358 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 359 | echo "The value of 'tabstop' is" &ts |
| 360 | echo "Your home directory is" $HOME |
| 361 | if @a == 'text' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 362 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 363 | The &name form can also be used to set an option value, do something and |
| 364 | restore the old value. Example: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 365 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 366 | var save_ic = &ic |
| 367 | set noic |
| 368 | s/The Start/The Beginning/ |
| 369 | &ic = save_ic |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 370 | |
| 371 | This makes sure the "The Start" pattern is used with the 'ignorecase' option |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 372 | off. Still, it keeps the value that the user had set. (Another way to do |
| 373 | this would be to add "\C" to the pattern, see |/\C|.) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 374 | |
| 375 | |
| 376 | MATHEMATICS |
| 377 | |
| 378 | It becomes more interesting if we combine these basic items. Let's start with |
| 379 | mathematics on numbers: |
| 380 | |
| 381 | a + b add |
| 382 | a - b subtract |
| 383 | a * b multiply |
| 384 | a / b divide |
| 385 | a % b modulo |
| 386 | |
| 387 | The usual precedence is used. Example: > |
| 388 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 389 | echo 10 + 5 * 2 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 390 | < 20 ~ |
| 391 | |
Bram Moolenaar | 0065402 | 2011-02-25 14:42:19 +0100 | [diff] [blame] | 392 | Grouping is done with parentheses. No surprises here. Example: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 393 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 394 | echo (10 + 5) * 2 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 395 | < 30 ~ |
| 396 | |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 397 | Strings can be concatenated with ".." (see |expr6|). Example: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 398 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 399 | echo "foo" .. "bar" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 400 | < foobar ~ |
| 401 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 402 | When the "echo" command gets multiple arguments, it separates them with a |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 403 | space. In the example the argument is a single expression, thus no space is |
| 404 | inserted. |
| 405 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 406 | Borrowed from the C language is the conditional expression: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 407 | |
| 408 | a ? b : c |
| 409 | |
| 410 | If "a" evaluates to true "b" is used, otherwise "c" is used. Example: > |
| 411 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 412 | var nr = 4 |
| 413 | echo nr > 5 ? "nr is big" : "nr is small" |
| 414 | < nr is small ~ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 415 | |
| 416 | The three parts of the constructs are always evaluated first, thus you could |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 417 | see it works as: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 418 | |
| 419 | (a) ? (b) : (c) |
| 420 | |
| 421 | ============================================================================== |
| 422 | *41.4* Conditionals |
| 423 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 424 | The `if` commands executes the following statements, until the matching |
| 425 | `endif`, only when a condition is met. The generic form is: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 426 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 427 | if {condition} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 428 | {statements} |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 429 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 430 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 431 | Only when the expression {condition} evaluates to true or one will the |
| 432 | {statements} be executed. If they are not executed they must still be valid |
| 433 | commands. If they contain garbage, Vim won't be able to find the matching |
| 434 | `endif`. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 435 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 436 | You can also use `else`. The generic form for this is: |
| 437 | |
| 438 | if {condition} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 439 | {statements} |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 440 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 441 | {statements} |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 442 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 443 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 444 | The second {statements} block is only executed if the first one isn't. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 445 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 446 | Finally, there is `elseif` |
| 447 | |
| 448 | if {condition} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 449 | {statements} |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 450 | elseif {condition} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 451 | {statements} |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 452 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 453 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 454 | This works just like using `else` and then `if`, but without the need for an |
| 455 | extra `endif`. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 456 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 457 | A useful example for your vimrc file is checking the 'term' option and doing |
| 458 | something depending upon its value: > |
| 459 | |
| 460 | if &term == "xterm" |
| 461 | # Do stuff for xterm |
| 462 | elseif &term == "vt100" |
| 463 | # Do stuff for a vt100 terminal |
| 464 | else |
| 465 | # Do something for other terminals |
| 466 | endif |
| 467 | |
| 468 | This uses "#" to start a comment, more about that later. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 469 | |
| 470 | |
| 471 | LOGIC OPERATIONS |
| 472 | |
| 473 | We already used some of them in the examples. These are the most often used |
| 474 | ones: |
| 475 | |
| 476 | a == b equal to |
| 477 | a != b not equal to |
| 478 | a > b greater than |
| 479 | a >= b greater than or equal to |
| 480 | a < b less than |
| 481 | a <= b less than or equal to |
| 482 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 483 | The result is true if the condition is met and false otherwise. An example: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 484 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 485 | if v:version >= 700 |
| 486 | echo "congratulations" |
| 487 | else |
| 488 | echo "you are using an old version, upgrade!" |
| 489 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 490 | |
| 491 | Here "v:version" is a variable defined by Vim, which has the value of the Vim |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 492 | version. 600 is for version 6.0, version 6.1 has the value 601. This is |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 493 | very useful to write a script that works with multiple versions of Vim. |
| 494 | |v:version| |
| 495 | |
| 496 | The logic operators work both for numbers and strings. When comparing two |
| 497 | strings, the mathematical difference is used. This compares byte values, |
| 498 | which may not be right for some languages. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 499 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 500 | If you try to compare a string with a number you will get an error. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 501 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 502 | For strings there are two more useful items: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 503 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 504 | str =~ pat matches with |
| 505 | str !~ pat does not match with |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 506 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 507 | The left item "str" is used as a string. The right item "pat" is used as a |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 508 | pattern, like what's used for searching. Example: > |
| 509 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 510 | if str =~ " " |
| 511 | echo "str contains a space" |
| 512 | endif |
| 513 | if str !~ '\.$' |
| 514 | echo "str does not end in a full stop" |
| 515 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 516 | |
| 517 | Notice the use of a single-quote string for the pattern. This is useful, |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 518 | because backslashes would need to be doubled in a double-quote string and |
| 519 | patterns tend to contain many backslashes. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 520 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 521 | The match is not anchored, if you want to match the whole string start with |
| 522 | "^" and end with "$". |
| 523 | |
| 524 | The 'ignorecase' option is not used when comparing strings. When you do want |
| 525 | to ignore case append "?". Thus "==?" compares two strings to be equal while |
| 526 | ignoring case. For the full table see |expr-==|. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 527 | |
| 528 | |
| 529 | MORE LOOPING |
| 530 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 531 | The `while` command was already mentioned. Two more statements can be used in |
| 532 | between the `while` and the `endwhile`: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 533 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 534 | continue Jump back to the start of the while loop; the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 535 | loop continues. |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 536 | break Jump forward to the `endwhile`; the loop is |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 537 | discontinued. |
| 538 | |
| 539 | Example: > |
| 540 | |
Bram Moolenaar | 2f0936c | 2022-01-08 21:51:59 +0000 | [diff] [blame] | 541 | var counter = 1 |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 542 | while counter < 40 |
Bram Moolenaar | 2f0936c | 2022-01-08 21:51:59 +0000 | [diff] [blame] | 543 | if skip_number(counter) |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 544 | continue |
| 545 | endif |
Bram Moolenaar | 2f0936c | 2022-01-08 21:51:59 +0000 | [diff] [blame] | 546 | if last_number(counter) |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 547 | break |
| 548 | endif |
| 549 | sleep 50m |
Bram Moolenaar | 2f0936c | 2022-01-08 21:51:59 +0000 | [diff] [blame] | 550 | ++counter |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 551 | endwhile |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 552 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 553 | The `sleep` command makes Vim take a nap. The "50m" specifies fifty |
| 554 | milliseconds. Another example is `sleep 4`, which sleeps for four seconds. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 555 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 556 | Even more looping can be done with the `for` command, see below in |41.8|. |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 557 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 558 | ============================================================================== |
| 559 | *41.5* Executing an expression |
| 560 | |
| 561 | So far the commands in the script were executed by Vim directly. The |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 562 | `execute` command allows executing the result of an expression. This is a |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 563 | very powerful way to build commands and execute them. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 564 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 565 | An example is to jump to a tag, which is contained in a variable: > |
| 566 | |
| 567 | execute "tag " .. tag_name |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 568 | |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 569 | The ".." is used to concatenate the string "tag " with the value of variable |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 570 | "tag_name". Suppose "tag_name" has the value "get_cmd", then the command that |
| 571 | will be executed is: > |
| 572 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 573 | tag get_cmd |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 574 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 575 | The `execute` command can only execute Ex commands. The `normal` command |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 576 | executes Normal mode commands. However, its argument is not an expression but |
| 577 | the literal command characters. Example: > |
| 578 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 579 | normal gg=G |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 580 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 581 | This jumps to the first line with "gg" and formats all lines with the "=" |
| 582 | operator and the "G" movement. |
| 583 | |
| 584 | To make `normal` work with an expression, combine `execute` with it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 585 | Example: > |
| 586 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 587 | execute "normal " .. count .. "j" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 588 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 589 | This will move the cursor "count" lines down. |
| 590 | |
| 591 | Make sure that the argument for `normal` is a complete command. Otherwise |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 592 | Vim will run into the end of the argument and abort the command. For example, |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 593 | if you start the delete operator, you must give the movement command also. |
| 594 | This works: > |
| 595 | |
| 596 | normal d$ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 597 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 598 | This does nothing: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 599 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 600 | normal d |
| 601 | |
| 602 | If you start Insert mode and do not end it with Esc, it will end anyway. This |
| 603 | works to insert "new text": > |
| 604 | |
| 605 | execute "normal inew text" |
| 606 | |
| 607 | If you want to do something after inserting text you do need to end Insert |
| 608 | mode: > |
| 609 | |
| 610 | execute "normal inew text\<Esc>b" |
| 611 | |
| 612 | This inserts "new text" and puts the cursor on the first letter of "text". |
| 613 | Notice the use of the special key "\<Esc>". This avoids having to enter a |
| 614 | real <Esc> character in your script. That is where `execute` with a |
| 615 | double-quote string comes in handy. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 616 | |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 617 | If you don't want to execute a string but evaluate it to get its expression |
| 618 | value, you can use the eval() function: > |
| 619 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 620 | var optname = "path" |
| 621 | var optvalue = eval('&' .. optname) |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 622 | |
| 623 | A "&" character is prepended to "path", thus the argument to eval() is |
| 624 | "&path". The result will then be the value of the 'path' option. |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 625 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 626 | ============================================================================== |
| 627 | *41.6* Using functions |
| 628 | |
| 629 | Vim defines many functions and provides a large amount of functionality that |
| 630 | way. A few examples will be given in this section. You can find the whole |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 631 | list below: |function-list|. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 632 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 633 | A function is called with the `call` command. The parameters are passed in |
Bram Moolenaar | 0065402 | 2011-02-25 14:42:19 +0100 | [diff] [blame] | 634 | between parentheses separated by commas. Example: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 635 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 636 | call search("Date: ", "W") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 637 | |
| 638 | This calls the search() function, with arguments "Date: " and "W". The |
| 639 | search() function uses its first argument as a search pattern and the second |
| 640 | one as flags. The "W" flag means the search doesn't wrap around the end of |
| 641 | the file. |
| 642 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 643 | Using `call` is optional in |Vim9| script, this works the same way: > |
| 644 | |
| 645 | search("Date: ", "W") |
| 646 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 647 | A function can be called in an expression. Example: > |
| 648 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 649 | var line = getline(".") |
| 650 | var repl = substitute(line, '\a', "*", "g") |
| 651 | setline(".", repl) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 652 | |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 653 | The getline() function obtains a line from the current buffer. Its argument |
| 654 | is a specification of the line number. In this case "." is used, which means |
| 655 | the line where the cursor is. |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 656 | |
| 657 | The substitute() function does something similar to the `substitute` command. |
| 658 | The first argument is the string on which to perform the substitution. The |
| 659 | second argument is the pattern, the third the replacement string. Finally, |
| 660 | the last arguments are the flags. |
| 661 | |
| 662 | The setline() function sets the line, specified by the first argument, to a |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 663 | new string, the second argument. In this example the line under the cursor is |
| 664 | replaced with the result of the substitute(). Thus the effect of the three |
| 665 | statements is equal to: > |
| 666 | |
| 667 | :substitute/\a/*/g |
| 668 | |
| 669 | Using the functions becomes more interesting when you do more work before and |
| 670 | after the substitute() call. |
| 671 | |
| 672 | |
| 673 | FUNCTIONS *function-list* |
| 674 | |
| 675 | There are many functions. We will mention them here, grouped by what they are |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 676 | used for. You can find an alphabetical list here: |builtin-function-list|. |
| 677 | Use CTRL-] on the function name to jump to detailed help on it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 678 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 679 | String manipulation: *string-functions* |
Bram Moolenaar | 9d40128 | 2019-04-06 13:18:12 +0200 | [diff] [blame] | 680 | nr2char() get a character by its number value |
| 681 | list2str() get a character string from a list of numbers |
| 682 | char2nr() get number value of a character |
| 683 | str2list() get list of numbers from a string |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 684 | str2nr() convert a string to a Number |
| 685 | str2float() convert a string to a Float |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 686 | printf() format a string according to % items |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 687 | escape() escape characters in a string with a '\' |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 688 | shellescape() escape a string for use with a shell command |
| 689 | fnameescape() escape a file name for use with a Vim command |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 690 | tr() translate characters from one set to another |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 691 | strtrans() translate a string to make it printable |
| 692 | tolower() turn a string to lowercase |
| 693 | toupper() turn a string to uppercase |
Bram Moolenaar | 4e4473c | 2020-08-28 22:24:57 +0200 | [diff] [blame] | 694 | charclass() class of a character |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 695 | match() position where a pattern matches in a string |
| 696 | matchend() position where a pattern match ends in a string |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 697 | matchfuzzy() fuzzy matches a string in a list of strings |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 698 | matchfuzzypos() fuzzy matches a string in a list of strings |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 699 | matchstr() match of a pattern in a string |
Bram Moolenaar | 6f1d9a0 | 2016-07-24 14:12:38 +0200 | [diff] [blame] | 700 | matchstrpos() match and positions of a pattern in a string |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 701 | matchlist() like matchstr() and also return submatches |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 702 | stridx() first index of a short string in a long string |
| 703 | strridx() last index of a short string in a long string |
Bram Moolenaar | 8d04317 | 2014-01-23 14:24:41 +0100 | [diff] [blame] | 704 | strlen() length of a string in bytes |
Bram Moolenaar | 70ce8a1 | 2021-03-14 19:02:09 +0100 | [diff] [blame] | 705 | strcharlen() length of a string in characters |
| 706 | strchars() number of characters in a string |
Bram Moolenaar | 8d04317 | 2014-01-23 14:24:41 +0100 | [diff] [blame] | 707 | strwidth() size of string when displayed |
| 708 | strdisplaywidth() size of string when displayed, deals with tabs |
Bram Moolenaar | 08aac3c | 2020-08-28 21:04:24 +0200 | [diff] [blame] | 709 | setcellwidths() set character cell width overrides |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 710 | substitute() substitute a pattern match with a string |
Bram Moolenaar | 251e191 | 2011-06-19 05:09:16 +0200 | [diff] [blame] | 711 | submatch() get a specific match in ":s" and substitute() |
Bram Moolenaar | c95a302 | 2016-06-12 23:01:46 +0200 | [diff] [blame] | 712 | strpart() get part of a string using byte index |
| 713 | strcharpart() get part of a string using char index |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 714 | slice() take a slice of a string, using char index in |
| 715 | Vim9 script |
Bram Moolenaar | c95a302 | 2016-06-12 23:01:46 +0200 | [diff] [blame] | 716 | strgetchar() get character from a string using char index |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 717 | expand() expand special keywords |
Bram Moolenaar | 80dad48 | 2019-06-09 17:22:31 +0200 | [diff] [blame] | 718 | expandcmd() expand a command like done for `:edit` |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 719 | iconv() convert text from one encoding to another |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 720 | byteidx() byte index of a character in a string |
Bram Moolenaar | 8d04317 | 2014-01-23 14:24:41 +0100 | [diff] [blame] | 721 | byteidxcomp() like byteidx() but count composing characters |
Bram Moolenaar | 17793ef | 2020-12-28 12:56:58 +0100 | [diff] [blame] | 722 | charidx() character index of a byte in a string |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 723 | repeat() repeat a string multiple times |
| 724 | eval() evaluate a string expression |
Bram Moolenaar | 063b9d1 | 2016-07-09 20:21:48 +0200 | [diff] [blame] | 725 | execute() execute an Ex command and get the output |
Bram Moolenaar | 7dd64a3 | 2019-05-31 21:41:05 +0200 | [diff] [blame] | 726 | win_execute() like execute() but in a specified window |
Bram Moolenaar | b730f0c | 2018-11-25 03:56:26 +0100 | [diff] [blame] | 727 | trim() trim characters from a string |
Bram Moolenaar | 0b39c3f | 2020-08-30 15:52:10 +0200 | [diff] [blame] | 728 | gettext() lookup message translation |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 729 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 730 | List manipulation: *list-functions* |
Bram Moolenaar | af7f641 | 2005-01-17 22:11:23 +0000 | [diff] [blame] | 731 | get() get an item without error for wrong index |
| 732 | len() number of items in a List |
| 733 | empty() check if List is empty |
| 734 | insert() insert an item somewhere in a List |
| 735 | add() append an item to a List |
| 736 | extend() append a List to a List |
Bram Moolenaar | b0e6b51 | 2021-01-12 20:23:40 +0100 | [diff] [blame] | 737 | extendnew() make a new List and append items |
Bram Moolenaar | af7f641 | 2005-01-17 22:11:23 +0000 | [diff] [blame] | 738 | remove() remove one or more items from a List |
| 739 | copy() make a shallow copy of a List |
| 740 | deepcopy() make a full copy of a List |
| 741 | filter() remove selected items from a List |
| 742 | map() change each List item |
Bram Moolenaar | ea69685 | 2020-11-09 18:31:39 +0100 | [diff] [blame] | 743 | mapnew() make a new List with changed items |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 744 | reduce() reduce a List to a value |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 745 | slice() take a slice of a List |
Bram Moolenaar | af7f641 | 2005-01-17 22:11:23 +0000 | [diff] [blame] | 746 | sort() sort a List |
| 747 | reverse() reverse the order of a List |
Bram Moolenaar | 76f3b1a | 2014-03-27 22:30:07 +0100 | [diff] [blame] | 748 | uniq() remove copies of repeated adjacent items |
Bram Moolenaar | af7f641 | 2005-01-17 22:11:23 +0000 | [diff] [blame] | 749 | split() split a String into a List |
| 750 | join() join List items into a String |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 751 | range() return a List with a sequence of numbers |
Bram Moolenaar | af7f641 | 2005-01-17 22:11:23 +0000 | [diff] [blame] | 752 | string() String representation of a List |
| 753 | call() call a function with List as arguments |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 754 | index() index of a value in a List |
Bram Moolenaar | af7f641 | 2005-01-17 22:11:23 +0000 | [diff] [blame] | 755 | max() maximum value in a List |
| 756 | min() minimum value in a List |
| 757 | count() count number of times a value appears in a List |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 758 | repeat() repeat a List multiple times |
Bram Moolenaar | 077a1e6 | 2020-06-08 20:50:43 +0200 | [diff] [blame] | 759 | flatten() flatten a List |
Bram Moolenaar | 3b69006 | 2021-02-01 20:14:51 +0100 | [diff] [blame] | 760 | flattennew() flatten a copy of a List |
Bram Moolenaar | af7f641 | 2005-01-17 22:11:23 +0000 | [diff] [blame] | 761 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 762 | Dictionary manipulation: *dict-functions* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 763 | get() get an entry without an error for a wrong key |
Bram Moolenaar | af7f641 | 2005-01-17 22:11:23 +0000 | [diff] [blame] | 764 | len() number of entries in a Dictionary |
| 765 | has_key() check whether a key appears in a Dictionary |
| 766 | empty() check if Dictionary is empty |
| 767 | remove() remove an entry from a Dictionary |
| 768 | extend() add entries from one Dictionary to another |
Bram Moolenaar | b0e6b51 | 2021-01-12 20:23:40 +0100 | [diff] [blame] | 769 | extendnew() make a new Dictionary and append items |
Bram Moolenaar | af7f641 | 2005-01-17 22:11:23 +0000 | [diff] [blame] | 770 | filter() remove selected entries from a Dictionary |
| 771 | map() change each Dictionary entry |
Bram Moolenaar | ea69685 | 2020-11-09 18:31:39 +0100 | [diff] [blame] | 772 | mapnew() make a new Dictionary with changed items |
Bram Moolenaar | af7f641 | 2005-01-17 22:11:23 +0000 | [diff] [blame] | 773 | keys() get List of Dictionary keys |
| 774 | values() get List of Dictionary values |
| 775 | items() get List of Dictionary key-value pairs |
| 776 | copy() make a shallow copy of a Dictionary |
| 777 | deepcopy() make a full copy of a Dictionary |
| 778 | string() String representation of a Dictionary |
| 779 | max() maximum value in a Dictionary |
| 780 | min() minimum value in a Dictionary |
| 781 | count() count number of times a value appears |
| 782 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 783 | Floating point computation: *float-functions* |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 784 | float2nr() convert Float to Number |
| 785 | abs() absolute value (also works for Number) |
| 786 | round() round off |
| 787 | ceil() round up |
| 788 | floor() round down |
| 789 | trunc() remove value after decimal point |
Bram Moolenaar | 8d04317 | 2014-01-23 14:24:41 +0100 | [diff] [blame] | 790 | fmod() remainder of division |
| 791 | exp() exponential |
| 792 | log() natural logarithm (logarithm to base e) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 793 | log10() logarithm to base 10 |
| 794 | pow() value of x to the exponent y |
| 795 | sqrt() square root |
| 796 | sin() sine |
| 797 | cos() cosine |
Bram Moolenaar | 662db67 | 2011-03-22 14:05:35 +0100 | [diff] [blame] | 798 | tan() tangent |
| 799 | asin() arc sine |
| 800 | acos() arc cosine |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 801 | atan() arc tangent |
Bram Moolenaar | 662db67 | 2011-03-22 14:05:35 +0100 | [diff] [blame] | 802 | atan2() arc tangent |
| 803 | sinh() hyperbolic sine |
| 804 | cosh() hyperbolic cosine |
| 805 | tanh() hyperbolic tangent |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 806 | isinf() check for infinity |
Bram Moolenaar | c95a302 | 2016-06-12 23:01:46 +0200 | [diff] [blame] | 807 | isnan() check for not a number |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 808 | |
Yegappan Lakshmanan | 5dfe467 | 2021-09-14 17:54:30 +0200 | [diff] [blame] | 809 | Blob manipulation: *blob-functions* |
| 810 | blob2list() get a list of numbers from a blob |
| 811 | list2blob() get a blob from a list of numbers |
| 812 | |
Bram Moolenaar | b6b046b | 2011-12-30 13:11:27 +0100 | [diff] [blame] | 813 | Other computation: *bitwise-function* |
| 814 | and() bitwise AND |
| 815 | invert() bitwise invert |
| 816 | or() bitwise OR |
| 817 | xor() bitwise XOR |
Bram Moolenaar | 8d04317 | 2014-01-23 14:24:41 +0100 | [diff] [blame] | 818 | sha256() SHA-256 hash |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 819 | rand() get a pseudo-random number |
| 820 | srand() initialize seed used by rand() |
Bram Moolenaar | b6b046b | 2011-12-30 13:11:27 +0100 | [diff] [blame] | 821 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 822 | Variables: *var-functions* |
Bram Moolenaar | a47e05f | 2021-01-12 21:49:00 +0100 | [diff] [blame] | 823 | type() type of a variable as a number |
| 824 | typename() type of a variable as text |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 825 | islocked() check if a variable is locked |
Bram Moolenaar | 214641f | 2017-03-05 17:04:09 +0100 | [diff] [blame] | 826 | funcref() get a Funcref for a function reference |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 827 | function() get a Funcref for a function name |
| 828 | getbufvar() get a variable value from a specific buffer |
| 829 | setbufvar() set a variable in a specific buffer |
Bram Moolenaar | c6249bb | 2006-04-15 20:25:09 +0000 | [diff] [blame] | 830 | getwinvar() get a variable from specific window |
Bram Moolenaar | 06b5d51 | 2010-05-22 15:37:44 +0200 | [diff] [blame] | 831 | gettabvar() get a variable from specific tab page |
Bram Moolenaar | c6249bb | 2006-04-15 20:25:09 +0000 | [diff] [blame] | 832 | gettabwinvar() get a variable from specific window & tab page |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 833 | setwinvar() set a variable in a specific window |
Bram Moolenaar | 06b5d51 | 2010-05-22 15:37:44 +0200 | [diff] [blame] | 834 | settabvar() set a variable in a specific tab page |
Bram Moolenaar | c6249bb | 2006-04-15 20:25:09 +0000 | [diff] [blame] | 835 | settabwinvar() set a variable in a specific window & tab page |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 836 | garbagecollect() possibly free memory |
| 837 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 838 | Cursor and mark position: *cursor-functions* *mark-functions* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 839 | col() column number of the cursor or a mark |
| 840 | virtcol() screen column of the cursor or a mark |
| 841 | line() line number of the cursor or mark |
| 842 | wincol() window column number of the cursor |
| 843 | winline() window line number of the cursor |
| 844 | cursor() position the cursor at a line/column |
Bram Moolenaar | 8d04317 | 2014-01-23 14:24:41 +0100 | [diff] [blame] | 845 | screencol() get screen column of the cursor |
| 846 | screenrow() get screen row of the cursor |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 847 | screenpos() screen row and col of a text character |
Bram Moolenaar | 822ff86 | 2014-06-12 21:46:14 +0200 | [diff] [blame] | 848 | getcurpos() get position of the cursor |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 849 | getpos() get position of cursor, mark, etc. |
| 850 | setpos() set position of cursor, mark, etc. |
Bram Moolenaar | cfb4b47 | 2020-05-31 15:41:57 +0200 | [diff] [blame] | 851 | getmarklist() list of global/local marks |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 852 | byte2line() get line number at a specific byte count |
| 853 | line2byte() byte count at a specific line |
| 854 | diff_filler() get the number of filler lines above a line |
Bram Moolenaar | 8d04317 | 2014-01-23 14:24:41 +0100 | [diff] [blame] | 855 | screenattr() get attribute at a screen line/row |
| 856 | screenchar() get character code at a screen line/row |
Bram Moolenaar | 2912abb | 2019-03-29 14:16:42 +0100 | [diff] [blame] | 857 | screenchars() get character codes at a screen line/row |
| 858 | screenstring() get string of characters at a screen line/row |
Bram Moolenaar | 6f02b00 | 2021-01-10 20:22:54 +0100 | [diff] [blame] | 859 | charcol() character number of the cursor or a mark |
| 860 | getcharpos() get character position of cursor, mark, etc. |
| 861 | setcharpos() set character position of cursor, mark, etc. |
| 862 | getcursorcharpos() get character position of the cursor |
| 863 | setcursorcharpos() set character position of the cursor |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 864 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 865 | Working with text in the current buffer: *text-functions* |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 866 | getline() get a line or list of lines from the buffer |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 867 | setline() replace a line in the buffer |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 868 | append() append line or list of lines in the buffer |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 869 | indent() indent of a specific line |
| 870 | cindent() indent according to C indenting |
| 871 | lispindent() indent according to Lisp indenting |
| 872 | nextnonblank() find next non-blank line |
| 873 | prevnonblank() find previous non-blank line |
| 874 | search() find a match for a pattern |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 875 | searchpos() find a match for a pattern |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 876 | searchcount() get number of matches before/after the cursor |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 877 | searchpair() find the other end of a start/skip/end |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 878 | searchpairpos() find the other end of a start/skip/end |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 879 | searchdecl() search for the declaration of a name |
Bram Moolenaar | c95a302 | 2016-06-12 23:01:46 +0200 | [diff] [blame] | 880 | getcharsearch() return character search information |
| 881 | setcharsearch() set character search information |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 882 | |
Bram Moolenaar | 931a277 | 2019-07-04 16:54:54 +0200 | [diff] [blame] | 883 | Working with text in another buffer: |
| 884 | getbufline() get a list of lines from the specified buffer |
| 885 | setbufline() replace a line in the specified buffer |
| 886 | appendbufline() append a list of lines in the specified buffer |
| 887 | deletebufline() delete lines from a specified buffer |
| 888 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 889 | *system-functions* *file-functions* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 890 | System functions and manipulation of files: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 891 | glob() expand wildcards |
| 892 | globpath() expand wildcards in a number of directories |
Bram Moolenaar | c95a302 | 2016-06-12 23:01:46 +0200 | [diff] [blame] | 893 | glob2regpat() convert a glob pattern into a search pattern |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 894 | findfile() find a file in a list of directories |
| 895 | finddir() find a directory in a list of directories |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 896 | resolve() find out where a shortcut points to |
| 897 | fnamemodify() modify a file name |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 898 | pathshorten() shorten directory names in a path |
| 899 | simplify() simplify a path without changing its meaning |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 900 | executable() check if an executable program exists |
Bram Moolenaar | 7e38ea2 | 2014-04-05 22:55:53 +0200 | [diff] [blame] | 901 | exepath() full path of an executable program |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 902 | filereadable() check if a file can be read |
| 903 | filewritable() check if a file can be written to |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 904 | getfperm() get the permissions of a file |
Bram Moolenaar | c95a302 | 2016-06-12 23:01:46 +0200 | [diff] [blame] | 905 | setfperm() set the permissions of a file |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 906 | getftype() get the kind of a file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 907 | isdirectory() check if a directory exists |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 908 | getfsize() get the size of a file |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 909 | getcwd() get the current working directory |
Bram Moolenaar | 00aa069 | 2019-04-27 20:37:57 +0200 | [diff] [blame] | 910 | haslocaldir() check if current window used |:lcd| or |:tcd| |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 911 | tempname() get the name of a temporary file |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 912 | mkdir() create a new directory |
Bram Moolenaar | 1063f3d | 2019-05-07 22:06:52 +0200 | [diff] [blame] | 913 | chdir() change current working directory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 914 | delete() delete a file |
| 915 | rename() rename a file |
Bram Moolenaar | 7e38ea2 | 2014-04-05 22:55:53 +0200 | [diff] [blame] | 916 | system() get the result of a shell command as a string |
| 917 | systemlist() get the result of a shell command as a list |
Bram Moolenaar | 691ddee | 2019-05-09 14:52:41 +0200 | [diff] [blame] | 918 | environ() get all environment variables |
| 919 | getenv() get one environment variable |
| 920 | setenv() set an environment variable |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 921 | hostname() name of the system |
Bram Moolenaar | 3a7c85b | 2005-02-05 21:39:53 +0000 | [diff] [blame] | 922 | readfile() read a file into a List of lines |
Bram Moolenaar | c423ad7 | 2021-01-13 20:38:03 +0100 | [diff] [blame] | 923 | readblob() read a file into a Blob |
Bram Moolenaar | 62e1bb4 | 2019-04-08 16:25:07 +0200 | [diff] [blame] | 924 | readdir() get a List of file names in a directory |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 925 | readdirex() get a List of file information in a directory |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 926 | writefile() write a List of lines or Blob into a file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 927 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 928 | Date and Time: *date-functions* *time-functions* |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 929 | getftime() get last modification time of a file |
| 930 | localtime() get current time in seconds |
| 931 | strftime() convert time to a string |
Bram Moolenaar | 10455d4 | 2019-11-21 15:36:18 +0100 | [diff] [blame] | 932 | strptime() convert a date/time string to time |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 933 | reltime() get the current or elapsed time accurately |
| 934 | reltimestr() convert reltime() result to a string |
Bram Moolenaar | 03413f4 | 2016-04-12 21:07:15 +0200 | [diff] [blame] | 935 | reltimefloat() convert reltime() result to a Float |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 936 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 937 | *buffer-functions* *window-functions* *arg-functions* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 938 | Buffers, windows and the argument list: |
| 939 | argc() number of entries in the argument list |
| 940 | argidx() current position in the argument list |
Bram Moolenaar | 2d1fe05 | 2014-05-28 18:22:57 +0200 | [diff] [blame] | 941 | arglistid() get id of the argument list |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 942 | argv() get one entry from the argument list |
Bram Moolenaar | 931a277 | 2019-07-04 16:54:54 +0200 | [diff] [blame] | 943 | bufadd() add a file to the list of buffers |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 944 | bufexists() check if a buffer exists |
| 945 | buflisted() check if a buffer exists and is listed |
Bram Moolenaar | 931a277 | 2019-07-04 16:54:54 +0200 | [diff] [blame] | 946 | bufload() ensure a buffer is loaded |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 947 | bufloaded() check if a buffer exists and is loaded |
| 948 | bufname() get the name of a specific buffer |
| 949 | bufnr() get the buffer number of a specific buffer |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 950 | tabpagebuflist() return List of buffers in a tab page |
| 951 | tabpagenr() get the number of a tab page |
| 952 | tabpagewinnr() like winnr() for a specified tab page |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 953 | winnr() get the window number for the current window |
Bram Moolenaar | 82af871 | 2016-06-04 20:20:29 +0200 | [diff] [blame] | 954 | bufwinid() get the window ID of a specific buffer |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 955 | bufwinnr() get the window number of a specific buffer |
| 956 | winbufnr() get the buffer number of a specific window |
Bram Moolenaar | a334772 | 2019-05-11 21:14:24 +0200 | [diff] [blame] | 957 | listener_add() add a callback to listen to changes |
Bram Moolenaar | 68e6560 | 2019-05-26 21:33:31 +0200 | [diff] [blame] | 958 | listener_flush() invoke listener callbacks |
Bram Moolenaar | a334772 | 2019-05-11 21:14:24 +0200 | [diff] [blame] | 959 | listener_remove() remove a listener callback |
Bram Moolenaar | c95a302 | 2016-06-12 23:01:46 +0200 | [diff] [blame] | 960 | win_findbuf() find windows containing a buffer |
| 961 | win_getid() get window ID of a window |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 962 | win_gettype() get type of window |
Bram Moolenaar | c95a302 | 2016-06-12 23:01:46 +0200 | [diff] [blame] | 963 | win_gotoid() go to window with ID |
| 964 | win_id2tabwin() get tab and window nr from window ID |
| 965 | win_id2win() get window nr from window ID |
Daniel Steinberg | ee63031 | 2022-01-10 13:36:34 +0000 | [diff] [blame] | 966 | win_move_separator() move window vertical separator |
| 967 | win_move_statusline() move window status line |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 968 | win_splitmove() move window to a split of another window |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 969 | getbufinfo() get a list with buffer information |
| 970 | gettabinfo() get a list with tab page information |
| 971 | getwininfo() get a list with window information |
Bram Moolenaar | 07ad816 | 2018-02-13 13:59:59 +0100 | [diff] [blame] | 972 | getchangelist() get a list of change list entries |
Bram Moolenaar | 4f50588 | 2018-02-10 21:06:32 +0100 | [diff] [blame] | 973 | getjumplist() get a list of jump list entries |
Bram Moolenaar | fc65cab | 2018-08-28 22:58:02 +0200 | [diff] [blame] | 974 | swapinfo() information about a swap file |
Bram Moolenaar | b730f0c | 2018-11-25 03:56:26 +0100 | [diff] [blame] | 975 | swapname() get the swap file path of a buffer |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 976 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 977 | Command line: *command-line-functions* |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 978 | getcmdline() get the current command line |
| 979 | getcmdpos() get position of the cursor in the command line |
| 980 | setcmdpos() set position of the cursor in the command line |
| 981 | getcmdtype() return the current command-line type |
Bram Moolenaar | fb53927 | 2014-08-22 19:21:47 +0200 | [diff] [blame] | 982 | getcmdwintype() return the current command-line window type |
Bram Moolenaar | 6f1d9a0 | 2016-07-24 14:12:38 +0200 | [diff] [blame] | 983 | getcompletion() list of command-line completion matches |
Bram Moolenaar | 038e09e | 2021-02-06 12:38:51 +0100 | [diff] [blame] | 984 | fullcommand() get full command name |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 985 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 986 | Quickfix and location lists: *quickfix-functions* |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 987 | getqflist() list of quickfix errors |
| 988 | setqflist() modify a quickfix list |
| 989 | getloclist() list of location list items |
| 990 | setloclist() modify a location list |
| 991 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 992 | Insert mode completion: *completion-functions* |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 993 | complete() set found matches |
| 994 | complete_add() add to found matches |
| 995 | complete_check() check if completion should be aborted |
Bram Moolenaar | fd13332 | 2019-03-29 12:20:27 +0100 | [diff] [blame] | 996 | complete_info() get current completion information |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 997 | pumvisible() check if the popup menu is displayed |
Bram Moolenaar | 5be4cee | 2019-09-27 19:34:08 +0200 | [diff] [blame] | 998 | pum_getpos() position and size of popup menu if visible |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 999 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 1000 | Folding: *folding-functions* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1001 | foldclosed() check for a closed fold at a specific line |
| 1002 | foldclosedend() like foldclosed() but return the last line |
| 1003 | foldlevel() check for the fold level at a specific line |
| 1004 | foldtext() generate the line displayed for a closed fold |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 1005 | foldtextresult() get the text displayed for a closed fold |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1006 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 1007 | Syntax and highlighting: *syntax-functions* *highlighting-functions* |
Bram Moolenaar | 6ee1016 | 2007-07-26 20:58:42 +0000 | [diff] [blame] | 1008 | clearmatches() clear all matches defined by |matchadd()| and |
| 1009 | the |:match| commands |
| 1010 | getmatches() get all matches defined by |matchadd()| and |
| 1011 | the |:match| commands |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1012 | hlexists() check if a highlight group exists |
Yegappan Lakshmanan | d1a8d65 | 2021-11-03 21:56:45 +0000 | [diff] [blame] | 1013 | hlget() get highlight group attributes |
| 1014 | hlset() set highlight group attributes |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1015 | hlID() get ID of a highlight group |
| 1016 | synID() get syntax ID at a specific position |
| 1017 | synIDattr() get a specific attribute of a syntax ID |
| 1018 | synIDtrans() get translated syntax ID |
Bram Moolenaar | 166af9b | 2010-11-16 20:34:40 +0100 | [diff] [blame] | 1019 | synstack() get list of syntax IDs at a specific position |
Bram Moolenaar | 81af925 | 2010-12-10 20:35:50 +0100 | [diff] [blame] | 1020 | synconcealed() get info about concealing |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 1021 | diff_hlID() get highlight ID for diff mode at a position |
Bram Moolenaar | 6ee1016 | 2007-07-26 20:58:42 +0000 | [diff] [blame] | 1022 | matchadd() define a pattern to highlight (a "match") |
Bram Moolenaar | b341459 | 2014-06-17 17:48:32 +0200 | [diff] [blame] | 1023 | matchaddpos() define a list of positions to highlight |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 1024 | matcharg() get info about |:match| arguments |
Bram Moolenaar | 6ee1016 | 2007-07-26 20:58:42 +0000 | [diff] [blame] | 1025 | matchdelete() delete a match defined by |matchadd()| or a |
| 1026 | |:match| command |
| 1027 | setmatches() restore a list of matches saved by |
| 1028 | |getmatches()| |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 1029 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 1030 | Spelling: *spell-functions* |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 1031 | spellbadword() locate badly spelled word at or after cursor |
| 1032 | spellsuggest() return suggested spelling corrections |
| 1033 | soundfold() return the sound-a-like equivalent of a word |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1034 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 1035 | History: *history-functions* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1036 | histadd() add an item to a history |
| 1037 | histdel() delete an item from a history |
| 1038 | histget() get an item from a history |
| 1039 | histnr() get highest index of a history list |
| 1040 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 1041 | Interactive: *interactive-functions* |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 1042 | browse() put up a file requester |
| 1043 | browsedir() put up a directory requester |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1044 | confirm() let the user make a choice |
| 1045 | getchar() get a character from the user |
Bram Moolenaar | f7a023e | 2021-06-07 18:50:01 +0200 | [diff] [blame] | 1046 | getcharstr() get a character from the user as a string |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1047 | getcharmod() get modifiers for the last typed character |
Bram Moolenaar | 09c6f26 | 2019-11-17 15:55:14 +0100 | [diff] [blame] | 1048 | getmousepos() get last known mouse position |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 1049 | echoraw() output characters as-is |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 1050 | feedkeys() put characters in the typeahead queue |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1051 | input() get a line from the user |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 1052 | inputlist() let the user pick an entry from a list |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1053 | inputsecret() get a line from the user without showing it |
| 1054 | inputdialog() get a line from the user in a dialog |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1055 | inputsave() save and clear typeahead |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1056 | inputrestore() restore typeahead |
| 1057 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 1058 | GUI: *gui-functions* |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 1059 | getfontname() get name of current font being used |
Bram Moolenaar | b5b7562 | 2018-03-09 22:22:21 +0100 | [diff] [blame] | 1060 | getwinpos() position of the Vim window |
| 1061 | getwinposx() X position of the Vim window |
| 1062 | getwinposy() Y position of the Vim window |
Bram Moolenaar | 214641f | 2017-03-05 17:04:09 +0100 | [diff] [blame] | 1063 | balloon_show() set the balloon content |
Bram Moolenaar | a2a8016 | 2017-11-21 23:09:50 +0100 | [diff] [blame] | 1064 | balloon_split() split a message for a balloon |
Bram Moolenaar | 691ddee | 2019-05-09 14:52:41 +0200 | [diff] [blame] | 1065 | balloon_gettext() get the text in the balloon |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 1066 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 1067 | Vim server: *server-functions* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1068 | serverlist() return the list of server names |
Bram Moolenaar | 01164a6 | 2017-11-02 22:58:42 +0100 | [diff] [blame] | 1069 | remote_startserver() run a server |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1070 | remote_send() send command characters to a Vim server |
| 1071 | remote_expr() evaluate an expression in a Vim server |
| 1072 | server2client() send a reply to a client of a Vim server |
| 1073 | remote_peek() check if there is a reply from a Vim server |
| 1074 | remote_read() read a reply from a Vim server |
| 1075 | foreground() move the Vim window to the foreground |
| 1076 | remote_foreground() move the Vim server window to the foreground |
| 1077 | |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 1078 | Window size and position: *window-size-functions* |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 1079 | winheight() get height of a specific window |
| 1080 | winwidth() get width of a specific window |
Bram Moolenaar | f0b03c4 | 2017-12-17 17:17:07 +0100 | [diff] [blame] | 1081 | win_screenpos() get screen position of a window |
Bram Moolenaar | b730f0c | 2018-11-25 03:56:26 +0100 | [diff] [blame] | 1082 | winlayout() get layout of windows in a tab page |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 1083 | winrestcmd() return command to restore window sizes |
| 1084 | winsaveview() get view of current window |
| 1085 | winrestview() restore saved view of current window |
| 1086 | |
Bram Moolenaar | 0eabd4d | 2020-03-15 16:13:53 +0100 | [diff] [blame] | 1087 | Mappings and Menus: *mapping-functions* |
h-east | 29b8571 | 2021-07-26 21:54:04 +0200 | [diff] [blame] | 1088 | digraph_get() get |digraph| |
| 1089 | digraph_getlist() get all |digraph|s |
| 1090 | digraph_set() register |digraph| |
| 1091 | digraph_setlist() register multiple |digraph|s |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1092 | hasmapto() check if a mapping exists |
| 1093 | mapcheck() check if a matching mapping exists |
| 1094 | maparg() get rhs of a mapping |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 1095 | mapset() restore a mapping |
Bram Moolenaar | 0eabd4d | 2020-03-15 16:13:53 +0100 | [diff] [blame] | 1096 | menu_info() get information about a menu item |
Bram Moolenaar | 26402cb | 2013-02-20 21:26:00 +0100 | [diff] [blame] | 1097 | wildmenumode() check if the wildmode is active |
| 1098 | |
Bram Moolenaar | 683fa18 | 2015-11-30 21:38:24 +0100 | [diff] [blame] | 1099 | Testing: *test-functions* |
Bram Moolenaar | e18c0b3 | 2016-03-20 21:08:34 +0100 | [diff] [blame] | 1100 | assert_equal() assert that two expressions values are equal |
Bram Moolenaar | b730f0c | 2018-11-25 03:56:26 +0100 | [diff] [blame] | 1101 | assert_equalfile() assert that two file contents are equal |
Bram Moolenaar | 03413f4 | 2016-04-12 21:07:15 +0200 | [diff] [blame] | 1102 | assert_notequal() assert that two expressions values are not equal |
Bram Moolenaar | 6f1d9a0 | 2016-07-24 14:12:38 +0200 | [diff] [blame] | 1103 | assert_inrange() assert that an expression is inside a range |
Bram Moolenaar | 7db8f6f | 2016-03-29 23:12:46 +0200 | [diff] [blame] | 1104 | assert_match() assert that a pattern matches the value |
Bram Moolenaar | 03413f4 | 2016-04-12 21:07:15 +0200 | [diff] [blame] | 1105 | assert_notmatch() assert that a pattern does not match the value |
Bram Moolenaar | 683fa18 | 2015-11-30 21:38:24 +0100 | [diff] [blame] | 1106 | assert_false() assert that an expression is false |
| 1107 | assert_true() assert that an expression is true |
Bram Moolenaar | e18c0b3 | 2016-03-20 21:08:34 +0100 | [diff] [blame] | 1108 | assert_exception() assert that a command throws an exception |
Bram Moolenaar | 22f1d0e | 2018-02-27 14:53:30 +0100 | [diff] [blame] | 1109 | assert_beeps() assert that a command beeps |
Bram Moolenaar | 0df6030 | 2021-04-03 15:15:47 +0200 | [diff] [blame] | 1110 | assert_nobeep() assert that a command does not cause a beep |
Bram Moolenaar | 22f1d0e | 2018-02-27 14:53:30 +0100 | [diff] [blame] | 1111 | assert_fails() assert that a command fails |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 1112 | assert_report() report a test failure |
Yegappan Lakshmanan | 4dc0dd8 | 2022-01-29 13:06:40 +0000 | [diff] [blame] | 1113 | internal_get_nv_cmdchar() normal/visual command character at an index |
Bram Moolenaar | c95a302 | 2016-06-12 23:01:46 +0200 | [diff] [blame] | 1114 | test_alloc_fail() make memory allocation fail |
Bram Moolenaar | 6f1d9a0 | 2016-07-24 14:12:38 +0200 | [diff] [blame] | 1115 | test_autochdir() enable 'autochdir' during startup |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 1116 | test_override() test with Vim internal overrides |
| 1117 | test_garbagecollect_now() free memory right now |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 1118 | test_garbagecollect_soon() set a flag to free memory soon |
Bram Moolenaar | 68e6560 | 2019-05-26 21:33:31 +0200 | [diff] [blame] | 1119 | test_getvalue() get value of an internal variable |
Yegappan Lakshmanan | 18d4658 | 2021-06-23 20:46:52 +0200 | [diff] [blame] | 1120 | test_gui_drop_files() drop file(s) in a window |
Yegappan Lakshmanan | f1e7449 | 2021-06-21 18:44:26 +0200 | [diff] [blame] | 1121 | test_gui_mouse_event() add a GUI mouse event to the input buffer |
Yegappan Lakshmanan | b0ad2d9 | 2022-01-27 13:16:59 +0000 | [diff] [blame] | 1122 | test_gui_tabline_event() add a GUI tabline event to the input buffer |
| 1123 | test_gui_tabmenu_event() add a GUI tabmenu event to the input buffer |
Bram Moolenaar | 214641f | 2017-03-05 17:04:09 +0100 | [diff] [blame] | 1124 | test_ignore_error() ignore a specific error message |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 1125 | test_null_blob() return a null Blob |
Bram Moolenaar | c95a302 | 2016-06-12 23:01:46 +0200 | [diff] [blame] | 1126 | test_null_channel() return a null Channel |
| 1127 | test_null_dict() return a null Dict |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 1128 | test_null_function() return a null Funcref |
Bram Moolenaar | c95a302 | 2016-06-12 23:01:46 +0200 | [diff] [blame] | 1129 | test_null_job() return a null Job |
| 1130 | test_null_list() return a null List |
| 1131 | test_null_partial() return a null Partial function |
| 1132 | test_null_string() return a null String |
Bram Moolenaar | 214641f | 2017-03-05 17:04:09 +0100 | [diff] [blame] | 1133 | test_settime() set the time Vim uses internally |
Bram Moolenaar | bb8476b | 2019-05-04 15:47:48 +0200 | [diff] [blame] | 1134 | test_setmouse() set the mouse position |
Bram Moolenaar | b730f0c | 2018-11-25 03:56:26 +0100 | [diff] [blame] | 1135 | test_feedinput() add key sequence to input buffer |
| 1136 | test_option_not_set() reset flag indicating option was set |
| 1137 | test_scrollbar() simulate scrollbar movement in the GUI |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 1138 | test_refcount() return an expression's reference count |
| 1139 | test_srand_seed() set the seed value for srand() |
| 1140 | test_unknown() return a value with unknown type |
| 1141 | test_void() return a value with void type |
Bram Moolenaar | 683fa18 | 2015-11-30 21:38:24 +0100 | [diff] [blame] | 1142 | |
Bram Moolenaar | c95a302 | 2016-06-12 23:01:46 +0200 | [diff] [blame] | 1143 | Inter-process communication: *channel-functions* |
Bram Moolenaar | 5162822 | 2016-12-01 23:03:28 +0100 | [diff] [blame] | 1144 | ch_canread() check if there is something to read |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 1145 | ch_open() open a channel |
| 1146 | ch_close() close a channel |
Bram Moolenaar | 64d8e25 | 2016-09-06 22:12:34 +0200 | [diff] [blame] | 1147 | ch_close_in() close the in part of a channel |
Bram Moolenaar | c95a302 | 2016-06-12 23:01:46 +0200 | [diff] [blame] | 1148 | ch_read() read a message from a channel |
Bram Moolenaar | d09091d | 2019-01-17 16:07:22 +0100 | [diff] [blame] | 1149 | ch_readblob() read a Blob from a channel |
Bram Moolenaar | c95a302 | 2016-06-12 23:01:46 +0200 | [diff] [blame] | 1150 | ch_readraw() read a raw message from a channel |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 1151 | ch_sendexpr() send a JSON message over a channel |
| 1152 | ch_sendraw() send a raw message over a channel |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 1153 | ch_evalexpr() evaluate an expression over channel |
| 1154 | ch_evalraw() evaluate a raw string over channel |
Bram Moolenaar | c95a302 | 2016-06-12 23:01:46 +0200 | [diff] [blame] | 1155 | ch_status() get status of a channel |
| 1156 | ch_getbufnr() get the buffer number of a channel |
| 1157 | ch_getjob() get the job associated with a channel |
| 1158 | ch_info() get channel information |
| 1159 | ch_log() write a message in the channel log file |
| 1160 | ch_logfile() set the channel log file |
| 1161 | ch_setoptions() set the options for a channel |
Bram Moolenaar | a02a551 | 2016-06-17 12:48:11 +0200 | [diff] [blame] | 1162 | json_encode() encode an expression to a JSON string |
| 1163 | json_decode() decode a JSON string to Vim types |
Bram Moolenaar | c95a302 | 2016-06-12 23:01:46 +0200 | [diff] [blame] | 1164 | js_encode() encode an expression to a JSON string |
| 1165 | js_decode() decode a JSON string to Vim types |
| 1166 | |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 1167 | Jobs: *job-functions* |
Bram Moolenaar | c95a302 | 2016-06-12 23:01:46 +0200 | [diff] [blame] | 1168 | job_start() start a job |
| 1169 | job_stop() stop a job |
| 1170 | job_status() get the status of a job |
| 1171 | job_getchannel() get the channel used by a job |
| 1172 | job_info() get information about a job |
| 1173 | job_setoptions() set options for a job |
| 1174 | |
Bram Moolenaar | 162b714 | 2018-12-21 15:17:36 +0100 | [diff] [blame] | 1175 | Signs: *sign-functions* |
| 1176 | sign_define() define or update a sign |
| 1177 | sign_getdefined() get a list of defined signs |
| 1178 | sign_getplaced() get a list of placed signs |
Bram Moolenaar | 6b7b719 | 2019-01-11 13:42:41 +0100 | [diff] [blame] | 1179 | sign_jump() jump to a sign |
Bram Moolenaar | 162b714 | 2018-12-21 15:17:36 +0100 | [diff] [blame] | 1180 | sign_place() place a sign |
Bram Moolenaar | 809ce4d | 2019-07-13 21:21:40 +0200 | [diff] [blame] | 1181 | sign_placelist() place a list of signs |
Bram Moolenaar | 162b714 | 2018-12-21 15:17:36 +0100 | [diff] [blame] | 1182 | sign_undefine() undefine a sign |
| 1183 | sign_unplace() unplace a sign |
Bram Moolenaar | 809ce4d | 2019-07-13 21:21:40 +0200 | [diff] [blame] | 1184 | sign_unplacelist() unplace a list of signs |
Bram Moolenaar | 162b714 | 2018-12-21 15:17:36 +0100 | [diff] [blame] | 1185 | |
Bram Moolenaar | c572da5 | 2017-08-27 16:52:01 +0200 | [diff] [blame] | 1186 | Terminal window: *terminal-functions* |
| 1187 | term_start() open a terminal window and run a job |
| 1188 | term_list() get the list of terminal buffers |
| 1189 | term_sendkeys() send keystrokes to a terminal |
| 1190 | term_wait() wait for screen to be updated |
| 1191 | term_getjob() get the job associated with a terminal |
| 1192 | term_scrape() get row of a terminal screen |
| 1193 | term_getline() get a line of text from a terminal |
| 1194 | term_getattr() get the value of attribute {what} |
| 1195 | term_getcursor() get the cursor position of a terminal |
| 1196 | term_getscrolled() get the scroll count of a terminal |
| 1197 | term_getaltscreen() get the alternate screen flag |
| 1198 | term_getsize() get the size of a terminal |
| 1199 | term_getstatus() get the status of a terminal |
| 1200 | term_gettitle() get the title of a terminal |
| 1201 | term_gettty() get the tty name of a terminal |
Bram Moolenaar | 7dda86f | 2018-04-20 22:36:41 +0200 | [diff] [blame] | 1202 | term_setansicolors() set 16 ANSI colors, used for GUI |
| 1203 | term_getansicolors() get 16 ANSI colors, used for GUI |
Bram Moolenaar | b730f0c | 2018-11-25 03:56:26 +0100 | [diff] [blame] | 1204 | term_dumpdiff() display difference between two screen dumps |
| 1205 | term_dumpload() load a terminal screen dump in a window |
| 1206 | term_dumpwrite() dump contents of a terminal screen to a file |
| 1207 | term_setkill() set signal to stop job in a terminal |
| 1208 | term_setrestore() set command to restore a terminal |
| 1209 | term_setsize() set the size of a terminal |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 1210 | term_setapi() set terminal JSON API function name prefix |
Bram Moolenaar | c572da5 | 2017-08-27 16:52:01 +0200 | [diff] [blame] | 1211 | |
Bram Moolenaar | 931a277 | 2019-07-04 16:54:54 +0200 | [diff] [blame] | 1212 | Popup window: *popup-window-functions* |
| 1213 | popup_create() create popup centered in the screen |
| 1214 | popup_atcursor() create popup just above the cursor position, |
| 1215 | closes when the cursor moves away |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 1216 | popup_beval() at the position indicated by v:beval_ |
| 1217 | variables, closes when the mouse moves away |
Bram Moolenaar | 931a277 | 2019-07-04 16:54:54 +0200 | [diff] [blame] | 1218 | popup_notification() show a notification for three seconds |
| 1219 | popup_dialog() create popup centered with padding and border |
| 1220 | popup_menu() prompt for selecting an item from a list |
| 1221 | popup_hide() hide a popup temporarily |
| 1222 | popup_show() show a previously hidden popup |
| 1223 | popup_move() change the position and size of a popup |
| 1224 | popup_setoptions() override options of a popup |
| 1225 | popup_settext() replace the popup buffer contents |
| 1226 | popup_close() close one popup |
| 1227 | popup_clear() close all popups |
| 1228 | popup_filter_menu() select from a list of items |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 1229 | popup_filter_yesno() block until 'y' or 'n' is pressed |
Bram Moolenaar | 931a277 | 2019-07-04 16:54:54 +0200 | [diff] [blame] | 1230 | popup_getoptions() get current options for a popup |
| 1231 | popup_getpos() get actual position and size of a popup |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 1232 | popup_findinfo() get window ID for popup info window |
| 1233 | popup_findpreview() get window ID for popup preview window |
| 1234 | popup_list() get list of all popup window IDs |
| 1235 | popup_locate() get popup window ID from its screen position |
Bram Moolenaar | 931a277 | 2019-07-04 16:54:54 +0200 | [diff] [blame] | 1236 | |
Bram Moolenaar | c95a302 | 2016-06-12 23:01:46 +0200 | [diff] [blame] | 1237 | Timers: *timer-functions* |
| 1238 | timer_start() create a timer |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 1239 | timer_pause() pause or unpause a timer |
Bram Moolenaar | c95a302 | 2016-06-12 23:01:46 +0200 | [diff] [blame] | 1240 | timer_stop() stop a timer |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 1241 | timer_stopall() stop all timers |
| 1242 | timer_info() get information about timers |
Bram Moolenaar | 298b440 | 2016-01-28 22:38:53 +0100 | [diff] [blame] | 1243 | |
Bram Moolenaar | b730f0c | 2018-11-25 03:56:26 +0100 | [diff] [blame] | 1244 | Tags: *tag-functions* |
| 1245 | taglist() get list of matching tags |
| 1246 | tagfiles() get a list of tags files |
| 1247 | gettagstack() get the tag stack of a window |
| 1248 | settagstack() modify the tag stack of a window |
| 1249 | |
| 1250 | Prompt Buffer: *promptbuffer-functions* |
Bram Moolenaar | 077cc7a | 2020-09-04 16:35:35 +0200 | [diff] [blame] | 1251 | prompt_getprompt() get the effective prompt text for a buffer |
Bram Moolenaar | b730f0c | 2018-11-25 03:56:26 +0100 | [diff] [blame] | 1252 | prompt_setcallback() set prompt callback for a buffer |
| 1253 | prompt_setinterrupt() set interrupt callback for a buffer |
| 1254 | prompt_setprompt() set the prompt text for a buffer |
| 1255 | |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 1256 | Text Properties: *text-property-functions* |
| 1257 | prop_add() attach a property at a position |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 1258 | prop_add_list() attach a property at multiple positions |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 1259 | prop_clear() remove all properties from a line or lines |
| 1260 | prop_find() search for a property |
| 1261 | prop_list() return a list of all properties in a line |
| 1262 | prop_remove() remove a property from a line |
| 1263 | prop_type_add() add/define a property type |
| 1264 | prop_type_change() change properties of a type |
| 1265 | prop_type_delete() remove a text property type |
| 1266 | prop_type_get() return the properties of a type |
| 1267 | prop_type_list() return a list of all property types |
| 1268 | |
| 1269 | Sound: *sound-functions* |
| 1270 | sound_clear() stop playing all sounds |
| 1271 | sound_playevent() play an event's sound |
| 1272 | sound_playfile() play a sound file |
| 1273 | sound_stop() stop playing a sound |
| 1274 | |
Bram Moolenaar | 26402cb | 2013-02-20 21:26:00 +0100 | [diff] [blame] | 1275 | Various: *various-functions* |
| 1276 | mode() get current editing mode |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 1277 | state() get current busy state |
Bram Moolenaar | 26402cb | 2013-02-20 21:26:00 +0100 | [diff] [blame] | 1278 | visualmode() last visual mode used |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1279 | exists() check if a variable, function, etc. exists |
Bram Moolenaar | 2673599 | 2021-08-08 14:43:22 +0200 | [diff] [blame] | 1280 | exists_compiled() like exists() but check at compile time |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1281 | has() check if a feature is supported in Vim |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 1282 | changenr() return number of most recent change |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1283 | cscope_connection() check if a cscope connection exists |
| 1284 | did_filetype() check if a FileType autocommand was used |
| 1285 | eventhandler() check if invoked by an event handler |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 1286 | getpid() get process ID of Vim |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 1287 | getimstatus() check if IME status is active |
| 1288 | interrupt() interrupt script execution |
| 1289 | windowsversion() get MS-Windows version |
Bram Moolenaar | 0c0eddd | 2020-06-13 15:47:25 +0200 | [diff] [blame] | 1290 | terminalprops() properties of the terminal |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 1291 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1292 | libcall() call a function in an external library |
| 1293 | libcallnr() idem, returning a number |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 1294 | |
Bram Moolenaar | 8d04317 | 2014-01-23 14:24:41 +0100 | [diff] [blame] | 1295 | undofile() get the name of the undo file |
| 1296 | undotree() return the state of the undo tree |
| 1297 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1298 | getreg() get contents of a register |
Bram Moolenaar | bb861e2 | 2020-06-07 18:16:36 +0200 | [diff] [blame] | 1299 | getreginfo() get information about a register |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1300 | getregtype() get type of a register |
| 1301 | setreg() set contents and type of a register |
Bram Moolenaar | 0b6d911 | 2018-05-22 20:35:17 +0200 | [diff] [blame] | 1302 | reg_executing() return the name of the register being executed |
| 1303 | reg_recording() return the name of the register being recorded |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 1304 | |
Bram Moolenaar | 8d04317 | 2014-01-23 14:24:41 +0100 | [diff] [blame] | 1305 | shiftwidth() effective value of 'shiftwidth' |
| 1306 | |
Bram Moolenaar | 063b9d1 | 2016-07-09 20:21:48 +0200 | [diff] [blame] | 1307 | wordcount() get byte/word/char count of buffer |
| 1308 | |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 1309 | luaeval() evaluate |Lua| expression |
Bram Moolenaar | 7e506b6 | 2010-01-19 15:55:06 +0100 | [diff] [blame] | 1310 | mzeval() evaluate |MzScheme| expression |
Bram Moolenaar | e9b892e | 2016-01-17 21:15:58 +0100 | [diff] [blame] | 1311 | perleval() evaluate Perl expression (|+perl|) |
Bram Moolenaar | 8d04317 | 2014-01-23 14:24:41 +0100 | [diff] [blame] | 1312 | py3eval() evaluate Python expression (|+python3|) |
| 1313 | pyeval() evaluate Python expression (|+python|) |
Bram Moolenaar | 690afe1 | 2017-01-28 18:34:47 +0100 | [diff] [blame] | 1314 | pyxeval() evaluate |python_x| expression |
Bram Moolenaar | ebacddb | 2020-06-04 15:22:21 +0200 | [diff] [blame] | 1315 | rubyeval() evaluate |Ruby| expression |
| 1316 | |
Bram Moolenaar | 9d87a37 | 2018-12-18 21:41:50 +0100 | [diff] [blame] | 1317 | debugbreak() interrupt a program being debugged |
Bram Moolenaar | 7e506b6 | 2010-01-19 15:55:06 +0100 | [diff] [blame] | 1318 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1319 | ============================================================================== |
| 1320 | *41.7* Defining a function |
| 1321 | |
| 1322 | Vim enables you to define your own functions. The basic function declaration |
| 1323 | begins as follows: > |
| 1324 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1325 | def {name}({var1}, {var2}, ...): return-type |
| 1326 | {body} |
| 1327 | enddef |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1328 | < |
| 1329 | Note: |
| 1330 | Function names must begin with a capital letter. |
| 1331 | |
| 1332 | Let's define a short function to return the smaller of two numbers. It starts |
| 1333 | with this line: > |
| 1334 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1335 | def Min(num1: number, num2: number): number |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1336 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1337 | This tells Vim that the function is named "Min", it takes two arguments that |
| 1338 | are numbers: "num1" and "num2" and returns a number. |
| 1339 | |
| 1340 | The first thing you need to do is to check to see which number is smaller: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1341 | > |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1342 | if num1 < num2 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1343 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1344 | Let's assign the variable "smaller" the value of the smallest number: > |
| 1345 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1346 | var smaller: number |
| 1347 | if num1 < num2 |
| 1348 | smaller = num1 |
| 1349 | else |
| 1350 | smaller = num2 |
| 1351 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1352 | |
| 1353 | The variable "smaller" is a local variable. Variables used inside a function |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1354 | are local unless prefixed by something like "g:", "w:", or "s:". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1355 | |
| 1356 | Note: |
| 1357 | To access a global variable from inside a function you must prepend |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 1358 | "g:" to it. Thus "g:today" inside a function is used for the global |
| 1359 | variable "today", and "today" is another variable, local to the |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1360 | function or the script. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1361 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1362 | You now use the `return` statement to return the smallest number to the user. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1363 | Finally, you end the function: > |
| 1364 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1365 | return smaller |
| 1366 | enddef |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1367 | |
| 1368 | The complete function definition is as follows: > |
| 1369 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1370 | def Min(num1: number, num2: number): number |
| 1371 | var smaller: number |
| 1372 | if num1 < num2 |
| 1373 | smaller = num1 |
| 1374 | else |
| 1375 | smaller = num2 |
| 1376 | endif |
| 1377 | return smaller |
| 1378 | enddef |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1379 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1380 | Obviously this is a verbose example. You can make it shorter by using two |
| 1381 | return commands: > |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1382 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1383 | def Min(num1: number, num2: number): number |
| 1384 | if num1 < num2 |
| 1385 | return num1 |
| 1386 | endif |
| 1387 | return num2 |
| 1388 | enddef |
| 1389 | |
| 1390 | And if you remember the conditional expression, you need only one line: > |
| 1391 | |
| 1392 | def Min(num1: number, num2: number): number |
| 1393 | return num1 < num2 ? num1 : num2 |
| 1394 | enddef |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1395 | |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 1396 | A user defined function is called in exactly the same way as a built-in |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1397 | function. Only the name is different. The Min function can be used like |
| 1398 | this: > |
| 1399 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1400 | echo Min(5, 8) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1401 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1402 | Only now will the function be executed and the lines be parsed by Vim. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1403 | If there are mistakes, like using an undefined variable or function, you will |
| 1404 | now get an error message. When defining the function these errors are not |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1405 | detected. To get the errors sooner you can tell Vim to compile all the |
| 1406 | functions in the script: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1407 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1408 | defcompile |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1409 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1410 | For a function that does not return anything leave out the return type: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1411 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1412 | def SayIt(text: string) |
| 1413 | echo text |
| 1414 | enddef |
| 1415 | |
| 1416 | It is also possible to define a legacy function with `function` and |
| 1417 | `endfunction`. These do not have types and are not compiled. They execute |
| 1418 | much slower. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1419 | |
| 1420 | |
| 1421 | USING A RANGE |
| 1422 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1423 | A line range can be used with a function call. The function will be called |
| 1424 | once for every line in the range, with the cursor in that line. Example: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1425 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1426 | def Number() |
| 1427 | echo "line " .. line(".") .. " contains: " .. getline(".") |
| 1428 | enddef |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1429 | |
| 1430 | If you call this function with: > |
| 1431 | |
| 1432 | :10,15call Number() |
| 1433 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1434 | The function will be called six times, starting on line 10 and ending on line |
| 1435 | 15. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1436 | |
| 1437 | |
| 1438 | VARIABLE NUMBER OF ARGUMENTS |
| 1439 | |
| 1440 | Vim enables you to define functions that have a variable number of arguments. |
| 1441 | The following command, for instance, defines a function that must have 1 |
| 1442 | argument (start) and can have up to 20 additional arguments: > |
| 1443 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1444 | def Show(start: string, ...items: list<string>) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1445 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1446 | The variable "items" will be a list containing the extra arguments. You can |
| 1447 | use it like any list, for example: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1448 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1449 | def Show(start: string, ...items: list<string>) |
| 1450 | echohl Title |
| 1451 | echo "start is " .. start |
| 1452 | echohl None |
| 1453 | for index in range(len(items)) |
| 1454 | echon " Arg " .. index .. " is " .. items[index] |
| 1455 | endfor |
| 1456 | echo |
| 1457 | enddef |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1458 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1459 | You can call it like this: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1460 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1461 | Show('Title', 'one', 'two', 'three') |
| 1462 | < start is Title Arg 0 is one Arg 1 is two Arg 2 is three ~ |
| 1463 | |
| 1464 | This uses the `echohl` command to specify the highlighting used for the |
| 1465 | following `echo` command. `echohl None` stops it again. The `echon` command |
| 1466 | works like `echo`, but doesn't output a line break. |
| 1467 | |
| 1468 | If you call it with one argument the "items" list will be empty. |
| 1469 | `range(len(items))` returns a list with the indexes, what `for` loops over, |
| 1470 | we'll explain that further down. |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1471 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1472 | |
| 1473 | LISTING FUNCTIONS |
| 1474 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1475 | The `function` command lists the names and arguments of all user-defined |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1476 | functions: > |
| 1477 | |
| 1478 | :function |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1479 | < def <SNR>86_Show(start: string, ...items: list<string>) ~ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1480 | function GetVimIndent() ~ |
| 1481 | function SetSyn(name) ~ |
| 1482 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1483 | The "<SNR>" prefix means that a function is script-local. |Vim9| functions |
| 1484 | wil start with "def" and include argument and return types. Legacy functions |
| 1485 | are listed with "function". |
| 1486 | |
| 1487 | To see what a function does, use its name as an argument for `function`: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1488 | |
| 1489 | :function SetSyn |
| 1490 | < 1 if &syntax == '' ~ |
| 1491 | 2 let &syntax = a:name ~ |
| 1492 | 3 endif ~ |
| 1493 | endfunction ~ |
| 1494 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1495 | To see the "Show" function you need to include the script prefix, since a |
| 1496 | "Show" function can be defined multiple times in different scripts. To find |
| 1497 | the exact name you can use `function`, but the result may be a very long list. |
| 1498 | To only get the functions matching a pattern you can use the `filter` prefix: |
| 1499 | > |
| 1500 | |
| 1501 | :filter Show function |
| 1502 | < def <SNR>86_Show(start: string, ...items: list<string>) ~ |
| 1503 | > |
| 1504 | :function <SNR>86_Show |
| 1505 | < 1 echohl Title ~ |
| 1506 | 2 echo "start is " .. start ~ |
| 1507 | etc. |
| 1508 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1509 | |
| 1510 | DEBUGGING |
| 1511 | |
| 1512 | The line number is useful for when you get an error message or when debugging. |
| 1513 | See |debug-scripts| about debugging mode. |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1514 | |
| 1515 | You can also set the 'verbose' option to 12 or higher to see all function |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1516 | calls. Set it to 15 or higher to see every executed line. |
| 1517 | |
| 1518 | |
| 1519 | DELETING A FUNCTION |
| 1520 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1521 | To delete the SetSyn() function: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1522 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1523 | :delfunction SetSyn |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1524 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1525 | Deleting only works for global functions and functions in legacy script, not |
| 1526 | for functions defined in a |Vim9| script. |
| 1527 | |
| 1528 | You get an error when the function doesn't exist or cannot be deleted. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1529 | |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1530 | |
| 1531 | FUNCTION REFERENCES |
| 1532 | |
| 1533 | Sometimes it can be useful to have a variable point to one function or |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1534 | another. You can do it with function reference variable. Often shortened to |
| 1535 | "funcref". Example: > |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1536 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1537 | def Right() |
| 1538 | return 'Right!' |
| 1539 | enddef |
| 1540 | def Wrong() |
| 1541 | return 'Wrong!' |
| 1542 | enddef |
| 1543 | |
| 1544 | var Afunc = g:result == 1 ? Right : Wrong |
| 1545 | Afunc() |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1546 | < Wrong! ~ |
| 1547 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1548 | This assumes "g:result" is not one. |
| 1549 | |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1550 | Note that the name of a variable that holds a function reference must start |
| 1551 | with a capital. Otherwise it could be confused with the name of a builtin |
| 1552 | function. |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1553 | |
Yegappan Lakshmanan | 5dfe467 | 2021-09-14 17:54:30 +0200 | [diff] [blame] | 1554 | More information about defining your own functions here: |user-functions|. |
| 1555 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1556 | ============================================================================== |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1557 | *41.8* Lists and Dictionaries |
| 1558 | |
| 1559 | So far we have used the basic types String and Number. Vim also supports two |
| 1560 | composite types: List and Dictionary. |
| 1561 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1562 | A List is an ordered sequence of items. The items can be any kind of value, |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1563 | thus you can make a List of numbers, a List of Lists and even a List of mixed |
| 1564 | items. To create a List with three strings: > |
| 1565 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1566 | var alist = ['aap', 'mies', 'noot'] |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1567 | |
| 1568 | The List items are enclosed in square brackets and separated by commas. To |
| 1569 | create an empty List: > |
| 1570 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1571 | var alist = [] |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1572 | |
| 1573 | You can add items to a List with the add() function: > |
| 1574 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1575 | var alist = [] |
| 1576 | add(alist, 'foo') |
| 1577 | add(alist, 'bar') |
| 1578 | echo alist |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1579 | < ['foo', 'bar'] ~ |
| 1580 | |
| 1581 | List concatenation is done with +: > |
| 1582 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1583 | var alist = ['foo', 'bar'] |
| 1584 | alist = alist + ['and', 'more'] |
| 1585 | echo alist |
| 1586 | < ['foo', 'bar', 'and', 'more'] ~ |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1587 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1588 | Or, if you want to extend a List with a function: > |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1589 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1590 | var alist = ['one'] |
| 1591 | extend(alist, ['two', 'three']) |
| 1592 | echo alist |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1593 | < ['one', 'two', 'three'] ~ |
| 1594 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1595 | Notice that using `add()` will have a different effect: > |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1596 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1597 | var alist = ['one'] |
| 1598 | add(alist, ['two', 'three']) |
| 1599 | echo alist |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1600 | < ['one', ['two', 'three']] ~ |
| 1601 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1602 | The second argument of add() is added as an item, now you have a nested list. |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1603 | |
| 1604 | |
| 1605 | FOR LOOP |
| 1606 | |
| 1607 | One of the nice things you can do with a List is iterate over it: > |
| 1608 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1609 | var alist = ['one', 'two', 'three'] |
| 1610 | for n in alist |
| 1611 | echo n |
| 1612 | endfor |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1613 | < one ~ |
| 1614 | two ~ |
| 1615 | three ~ |
| 1616 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1617 | This will loop over each element in List "alist", assigning each value to |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1618 | variable "n". The generic form of a for loop is: > |
| 1619 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1620 | for {varname} in {listexpression} |
| 1621 | {commands} |
| 1622 | endfor |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1623 | |
| 1624 | To loop a certain number of times you need a List of a specific length. The |
| 1625 | range() function creates one for you: > |
| 1626 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1627 | for a in range(3) |
| 1628 | echo a |
| 1629 | endfor |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1630 | < 0 ~ |
| 1631 | 1 ~ |
| 1632 | 2 ~ |
| 1633 | |
| 1634 | Notice that the first item of the List that range() produces is zero, thus the |
| 1635 | last item is one less than the length of the list. |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1636 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1637 | You can also specify the maximum value, the stride and even go backwards: > |
| 1638 | |
| 1639 | for a in range(8, 4, -2) |
| 1640 | echo a |
| 1641 | endfor |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1642 | < 8 ~ |
| 1643 | 6 ~ |
| 1644 | 4 ~ |
| 1645 | |
| 1646 | A more useful example, looping over lines in the buffer: > |
| 1647 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1648 | for line in getline(1, 20) |
| 1649 | if line =~ "Date: " |
| 1650 | echo line |
| 1651 | endif |
| 1652 | endfor |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1653 | |
| 1654 | This looks into lines 1 to 20 (inclusive) and echoes any date found in there. |
| 1655 | |
| 1656 | |
| 1657 | DICTIONARIES |
| 1658 | |
| 1659 | A Dictionary stores key-value pairs. You can quickly lookup a value if you |
| 1660 | know the key. A Dictionary is created with curly braces: > |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 1661 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1662 | var uk2nl = {one: 'een', two: 'twee', three: 'drie'} |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1663 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 1664 | Now you can lookup words by putting the key in square brackets: > |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1665 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1666 | echo uk2nl['two'] |
| 1667 | < twee ~ |
| 1668 | |
| 1669 | If the key does not have special characters, you can use the dot notation: > |
| 1670 | |
| 1671 | echo uk2nl.two |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1672 | < twee ~ |
| 1673 | |
| 1674 | The generic form for defining a Dictionary is: > |
| 1675 | |
| 1676 | {<key> : <value>, ...} |
| 1677 | |
| 1678 | An empty Dictionary is one without any keys: > |
| 1679 | |
| 1680 | {} |
| 1681 | |
| 1682 | The possibilities with Dictionaries are numerous. There are various functions |
| 1683 | for them as well. For example, you can obtain a list of the keys and loop |
| 1684 | over them: > |
| 1685 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1686 | for key in keys(uk2nl) |
| 1687 | echo key |
| 1688 | endfor |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1689 | < three ~ |
| 1690 | one ~ |
| 1691 | two ~ |
| 1692 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 1693 | You will notice the keys are not ordered. You can sort the list to get a |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1694 | specific order: > |
| 1695 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1696 | for key in sort(keys(uk2nl)) |
| 1697 | echo key |
| 1698 | endfor |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1699 | < one ~ |
| 1700 | three ~ |
| 1701 | two ~ |
| 1702 | |
| 1703 | But you can never get back the order in which items are defined. For that you |
| 1704 | need to use a List, it stores items in an ordered sequence. |
| 1705 | |
| 1706 | |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1707 | For further reading see |Lists| and |Dictionaries|. |
| 1708 | |
| 1709 | ============================================================================== |
| 1710 | *41.9* Exceptions |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1711 | |
| 1712 | Let's start with an example: > |
| 1713 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1714 | try |
| 1715 | read ~/templates/pascal.tmpl |
| 1716 | catch /E484:/ |
| 1717 | echo "Sorry, the Pascal template file cannot be found." |
| 1718 | endtry |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1719 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1720 | The `read` command will fail if the file does not exist. Instead of |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1721 | generating an error message, this code catches the error and gives the user a |
Bram Moolenaar | 0065402 | 2011-02-25 14:42:19 +0100 | [diff] [blame] | 1722 | nice message. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1723 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1724 | For the commands in between `try` and `endtry` errors are turned into |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1725 | exceptions. An exception is a string. In the case of an error the string |
| 1726 | contains the error message. And every error message has a number. In this |
| 1727 | case, the error we catch contains "E484:". This number is guaranteed to stay |
| 1728 | the same (the text may change, e.g., it may be translated). |
| 1729 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1730 | Besides being able to give a nice error message, Vim will also continue |
| 1731 | executing commands. Otherwise, once an uncaught error is encountered, |
| 1732 | execution will be aborted. |
| 1733 | |
| 1734 | When the `read` command causes another error, the pattern "E484:" will not |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1735 | match in it. Thus this exception will not be caught and result in the usual |
| 1736 | error message. |
| 1737 | |
| 1738 | You might be tempted to do this: > |
| 1739 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1740 | try |
| 1741 | read ~/templates/pascal.tmpl |
| 1742 | catch |
| 1743 | echo "Sorry, the Pascal template file cannot be found." |
| 1744 | endtry |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1745 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1746 | This means all errors are caught. But then you will not see an error that |
| 1747 | would indicate a completely different problem, such as "E21: Cannot make |
| 1748 | changes, 'modifiable' is off". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1749 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1750 | Another useful mechanism is the `finally` command: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1751 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1752 | var tmp = tempname() |
| 1753 | try |
| 1754 | exe ":.,$write " .. tmp |
| 1755 | exe "!filter " .. tmp |
| 1756 | :.,$delete |
| 1757 | exe ":$read " .. tmp |
| 1758 | finally |
| 1759 | call delete(tmp) |
| 1760 | endtry |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1761 | |
| 1762 | This filters the lines from the cursor until the end of the file through the |
| 1763 | "filter" command, which takes a file name argument. No matter if the |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1764 | filtering works, something goes wrong in between `try` and `finally` or the |
| 1765 | user cancels the filtering by pressing CTRL-C, the `call delete(tmp)` is |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1766 | always executed. This makes sure you don't leave the temporary file behind. |
| 1767 | |
| 1768 | More information about exception handling can be found in the reference |
| 1769 | manual: |exception-handling|. |
| 1770 | |
| 1771 | ============================================================================== |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1772 | *41.10* Various remarks |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1773 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1774 | Here is a summary of items that are useful to know when writing Vim scripts. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1775 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1776 | The end-of-line character depends on the system. For Vim scripts it is |
| 1777 | recommended to always use the Unix fileformat, this also works on any other |
| 1778 | system. That way you can copy your Vim scripts from MS-Windows to Unix and |
| 1779 | they still work. See |:source_crnl|. To be sure it is set right, do this |
| 1780 | before writing the file: > |
| 1781 | |
| 1782 | :setlocal fileformat=unix |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1783 | |
| 1784 | |
| 1785 | WHITE SPACE |
| 1786 | |
| 1787 | Blank lines are allowed and ignored. |
| 1788 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1789 | Leading whitespace characters (blanks and TABs) are always ignored. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1790 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1791 | Trailing whitespace is often ignored, but not always. One command that |
| 1792 | includes it is `map`. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1793 | |
| 1794 | To include a whitespace character in the value of an option, it must be |
| 1795 | escaped by a "\" (backslash) as in the following example: > |
| 1796 | |
| 1797 | :set tags=my\ nice\ file |
| 1798 | |
Bram Moolenaar | 0065402 | 2011-02-25 14:42:19 +0100 | [diff] [blame] | 1799 | The same example written as: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1800 | |
| 1801 | :set tags=my nice file |
| 1802 | |
| 1803 | will issue an error, because it is interpreted as: > |
| 1804 | |
| 1805 | :set tags=my |
| 1806 | :set nice |
| 1807 | :set file |
| 1808 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1809 | |Vim9| script is very picky when it comes to white space. This was done |
| 1810 | intentionally to make sure scripts are easy to read and to avoid mistakes. |
| 1811 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1812 | |
| 1813 | COMMENTS |
| 1814 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1815 | In |Vim9| script the character # starts a comment. Everything after |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1816 | and including this character until the end-of-line is considered a comment and |
| 1817 | is ignored, except for commands that don't consider comments, as shown in |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1818 | examples below. A comment can start on any character position on the line, |
| 1819 | but not when it is part of the command, e.g. in a string. |
| 1820 | |
| 1821 | The character " (the double quote mark) starts a comment in legacy script. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1822 | |
| 1823 | There is a little "catch" with comments for some commands. Examples: > |
| 1824 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1825 | abbrev dev development # shorthand |
| 1826 | map <F3> o#include # insert include |
| 1827 | execute cmd # do it |
| 1828 | !ls *.c # list C files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1829 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1830 | The abbreviation 'dev' will be expanded to 'development # shorthand'. The |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1831 | mapping of <F3> will actually be the whole line after the 'o# ....' including |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1832 | the '# insert include'. The `execute` command will give an error. The `!` |
| 1833 | command will send everything after it to the shell, most likely causing an |
| 1834 | error. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1835 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1836 | There can be no comment after `map`, `abbreviate`, `execute` and `!` commands |
| 1837 | (there are a few more commands with this restriction). For the `map`, |
| 1838 | `abbreviate` and `execute` commands there is a trick: > |
| 1839 | |
| 1840 | abbrev dev development|# shorthand |
| 1841 | map <F3> o#include|# insert include |
| 1842 | execute '!ls *.c' |# do it |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1843 | |
| 1844 | With the '|' character the command is separated from the next one. And that |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1845 | next command is only a comment. The last command, using `execute` is a |
| 1846 | general solution, it works for all commands that do not accept a comment or a |
| 1847 | '|' to separate the next command. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1848 | |
| 1849 | Notice that there is no white space before the '|' in the abbreviation and |
| 1850 | mapping. For these commands, any character until the end-of-line or '|' is |
| 1851 | included. As a consequence of this behavior, you don't always see that |
| 1852 | trailing whitespace is included: > |
| 1853 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1854 | map <F4> o#include |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1855 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1856 | To spot these problems, you can highlight trailing spaces: > |
| 1857 | match Search /\s\+$/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1858 | |
Bram Moolenaar | 9e1d283 | 2007-05-06 12:51:41 +0000 | [diff] [blame] | 1859 | For Unix there is one special way to comment a line, that allows making a Vim |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1860 | script executable, and it also works in legacy script: > |
Bram Moolenaar | 9e1d283 | 2007-05-06 12:51:41 +0000 | [diff] [blame] | 1861 | #!/usr/bin/env vim -S |
| 1862 | echo "this is a Vim script" |
| 1863 | quit |
| 1864 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1865 | |
| 1866 | PITFALLS |
| 1867 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1868 | An even bigger problem arises in the following example: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1869 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1870 | map ,ab o#include |
| 1871 | unmap ,ab |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1872 | |
| 1873 | Here the unmap command will not work, because it tries to unmap ",ab ". This |
| 1874 | does not exist as a mapped sequence. An error will be issued, which is very |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1875 | hard to identify, because the ending whitespace character in `unmap ,ab ` is |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1876 | not visible. |
| 1877 | |
| 1878 | And this is the same as what happens when one uses a comment after an 'unmap' |
| 1879 | command: > |
| 1880 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1881 | unmap ,ab # comment |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1882 | |
| 1883 | Here the comment part will be ignored. However, Vim will try to unmap |
| 1884 | ',ab ', which does not exist. Rewrite it as: > |
| 1885 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1886 | unmap ,ab| # comment |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1887 | |
| 1888 | |
| 1889 | RESTORING THE VIEW |
| 1890 | |
Bram Moolenaar | 3a0d809 | 2012-10-21 03:02:54 +0200 | [diff] [blame] | 1891 | Sometimes you want to make a change and go back to where the cursor was. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1892 | Restoring the relative position would also be nice, so that the same line |
| 1893 | appears at the top of the window. |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1894 | |
| 1895 | This example yanks the current line, puts it above the first line in the file |
| 1896 | and then restores the view: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1897 | |
| 1898 | map ,p ma"aYHmbgg"aP`bzt`a |
| 1899 | |
| 1900 | What this does: > |
| 1901 | ma"aYHmbgg"aP`bzt`a |
| 1902 | < ma set mark a at cursor position |
| 1903 | "aY yank current line into register a |
| 1904 | Hmb go to top line in window and set mark b there |
| 1905 | gg go to first line in file |
| 1906 | "aP put the yanked line above it |
| 1907 | `b go back to top line in display |
| 1908 | zt position the text in the window as before |
| 1909 | `a go back to saved cursor position |
| 1910 | |
| 1911 | |
| 1912 | PACKAGING |
| 1913 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1914 | Sometimes you will want to use global variables or functions, so that they can |
| 1915 | be used anywhere. A good example is a global variable that passes a |
| 1916 | preference to a plugin. To avoid other scripts using the same name, use a |
| 1917 | prefix that is very unlikely to be used elsewhere. For example, if you have a |
| 1918 | "mytags" plugin, you could use: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1919 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1920 | g:mytags_location = '$HOME/project' |
| 1921 | g:mytags_style = 'fast' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1922 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1923 | To minimize interference between plugins keep as much as possible local to the |
| 1924 | script. |Vim9| script helps you with that, by default functions and variables |
| 1925 | are script-local. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1926 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1927 | If you split your plugin into parts, you can use `import` and `export` to |
| 1928 | share items between those parts. See `:export` for the details. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1929 | |
| 1930 | ============================================================================== |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1931 | *41.11* Writing a plugin *write-plugin* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1932 | |
| 1933 | You can write a Vim script in such a way that many people can use it. This is |
| 1934 | called a plugin. Vim users can drop your script in their plugin directory and |
| 1935 | use its features right away |add-plugin|. |
| 1936 | |
| 1937 | There are actually two types of plugins: |
| 1938 | |
| 1939 | global plugins: For all types of files. |
| 1940 | filetype plugins: Only for files of a specific type. |
| 1941 | |
| 1942 | In this section the first type is explained. Most items are also relevant for |
| 1943 | writing filetype plugins. The specifics for filetype plugins are in the next |
| 1944 | section |write-filetype-plugin|. |
| 1945 | |
| 1946 | |
| 1947 | NAME |
| 1948 | |
| 1949 | First of all you must choose a name for your plugin. The features provided |
| 1950 | by the plugin should be clear from its name. And it should be unlikely that |
| 1951 | someone else writes a plugin with the same name but which does something |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1952 | different. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1953 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1954 | A script that corrects typing mistakes could be called "typecorrect.vim". We |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1955 | will use it here as an example. |
| 1956 | |
| 1957 | For the plugin to work for everybody, it should follow a few guidelines. This |
| 1958 | will be explained step-by-step. The complete example plugin is at the end. |
| 1959 | |
| 1960 | |
| 1961 | BODY |
| 1962 | |
| 1963 | Let's start with the body of the plugin, the lines that do the actual work: > |
| 1964 | |
| 1965 | 14 iabbrev teh the |
| 1966 | 15 iabbrev otehr other |
| 1967 | 16 iabbrev wnat want |
| 1968 | 17 iabbrev synchronisation |
| 1969 | 18 \ synchronization |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1970 | |
| 1971 | The actual list should be much longer, of course. |
| 1972 | |
| 1973 | The line numbers have only been added to explain a few things, don't put them |
| 1974 | in your plugin file! |
| 1975 | |
| 1976 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1977 | FIRST LINE |
| 1978 | > |
| 1979 | 1 vim9script noclear |
| 1980 | |
| 1981 | You need to use `vimscript` as the very first command. Best is to put it in |
| 1982 | the very first line. |
| 1983 | |
| 1984 | The script we are writing will have a `finish` command to bail out when it is |
| 1985 | loaded a second time. To avoid the items defined in the script are lost the |
| 1986 | "noclear" argument is used. More info about this at |vim9-reload|. |
| 1987 | |
| 1988 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1989 | HEADER |
| 1990 | |
| 1991 | You will probably add new corrections to the plugin and soon have several |
Bram Moolenaar | d09acef | 2012-09-21 14:54:30 +0200 | [diff] [blame] | 1992 | versions lying around. And when distributing this file, people will want to |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1993 | know who wrote this wonderful plugin and where they can send remarks. |
| 1994 | Therefore, put a header at the top of your plugin: > |
| 1995 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 1996 | 2 # Vim global plugin for correcting typing mistakes |
| 1997 | 3 # Last Change: 2021 Dec 30 |
| 1998 | 4 # Maintainer: Bram Moolenaar <Bram@vim.org> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1999 | |
| 2000 | About copyright and licensing: Since plugins are very useful and it's hardly |
| 2001 | worth restricting their distribution, please consider making your plugin |
| 2002 | either public domain or use the Vim |license|. A short note about this near |
| 2003 | the top of the plugin should be sufficient. Example: > |
| 2004 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2005 | 5 # License: This file is placed in the public domain. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2006 | |
| 2007 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2008 | LINE CONTINUATION AND AVOIDING SIDE EFFECTS *use-cpo-save* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2009 | |
| 2010 | In line 18 above, the line-continuation mechanism is used |line-continuation|. |
| 2011 | Users with 'compatible' set will run into trouble here, they will get an error |
| 2012 | message. We can't just reset 'compatible', because that has a lot of side |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2013 | effects. Instead, we will set the 'cpoptions' option to its Vim default |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2014 | value and restore it later. That will allow the use of line-continuation and |
| 2015 | make the script work for most people. It is done like this: > |
| 2016 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2017 | 11 var save_cpo = &cpo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2018 | 12 set cpo&vim |
| 2019 | .. |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2020 | 42 &cpo = save_cpo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2021 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2022 | We first store the old value of 'cpoptions' in the "save_cpo" variable. At |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2023 | the end of the plugin this value is restored. |
| 2024 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2025 | Notice that "save_cpo" is a script-local variable. A global variable could |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2026 | already be in use for something else. Always use script-local variables for |
| 2027 | things that are only used in the script. |
| 2028 | |
| 2029 | |
| 2030 | NOT LOADING |
| 2031 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2032 | It is possible that a user doesn't always want to load this plugin. Or the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2033 | system administrator has dropped it in the system-wide plugin directory, but a |
| 2034 | user has his own plugin he wants to use. Then the user must have a chance to |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2035 | disable loading this specific plugin. These lines will make it possible: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2036 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2037 | 7 if exists("g:loaded_typecorrect") |
| 2038 | 8 finish |
| 2039 | 9 endif |
| 2040 | 10 g:loaded_typecorrect = 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2041 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2042 | This also avoids that when the script is loaded twice it would pointlessly |
| 2043 | redefine functions and cause trouble for autocommands that are added twice. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2044 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2045 | The name is recommended to start with "g:loaded_" and then the file name of |
| 2046 | the plugin, literally. The "g:" is prepended to make the variable global, so |
| 2047 | that other places can check whether its functionality is available. Without |
| 2048 | "g:" it would be local to the script. |
Bram Moolenaar | c5604bc | 2010-07-17 15:20:30 +0200 | [diff] [blame] | 2049 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2050 | Using `finish` stops Vim from reading the rest of the file, it's much quicker |
| 2051 | than using if-endif around the whole file, since Vim would still need to parse |
| 2052 | the commands to find the `endif`. |
Bram Moolenaar | c5604bc | 2010-07-17 15:20:30 +0200 | [diff] [blame] | 2053 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2054 | |
| 2055 | MAPPING |
| 2056 | |
| 2057 | Now let's make the plugin more interesting: We will add a mapping that adds a |
| 2058 | correction for the word under the cursor. We could just pick a key sequence |
| 2059 | for this mapping, but the user might already use it for something else. To |
| 2060 | allow the user to define which keys a mapping in a plugin uses, the <Leader> |
| 2061 | item can be used: > |
| 2062 | |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 2063 | 22 map <unique> <Leader>a <Plug>TypecorrAdd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2064 | |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 2065 | The "<Plug>TypecorrAdd;" thing will do the work, more about that further on. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2066 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2067 | The user can set the "g:mapleader" variable to the key sequence that he wants |
| 2068 | plugin mappings to start with. Thus if the user has done: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2069 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2070 | g:mapleader = "_" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2071 | |
| 2072 | the mapping will define "_a". If the user didn't do this, the default value |
| 2073 | will be used, which is a backslash. Then a map for "\a" will be defined. |
| 2074 | |
| 2075 | Note that <unique> is used, this will cause an error message if the mapping |
| 2076 | already happened to exist. |:map-<unique>| |
| 2077 | |
| 2078 | But what if the user wants to define his own key sequence? We can allow that |
| 2079 | with this mechanism: > |
| 2080 | |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 2081 | 21 if !hasmapto('<Plug>TypecorrAdd;') |
| 2082 | 22 map <unique> <Leader>a <Plug>TypecorrAdd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2083 | 23 endif |
| 2084 | |
Bram Moolenaar | 207f009 | 2020-08-30 17:20:20 +0200 | [diff] [blame] | 2085 | This checks if a mapping to "<Plug>TypecorrAdd;" already exists, and only |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2086 | defines the mapping from "<Leader>a" if it doesn't. The user then has a |
| 2087 | chance of putting this in his vimrc file: > |
| 2088 | |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 2089 | map ,c <Plug>TypecorrAdd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2090 | |
| 2091 | Then the mapped key sequence will be ",c" instead of "_a" or "\a". |
| 2092 | |
| 2093 | |
| 2094 | PIECES |
| 2095 | |
| 2096 | If a script gets longer, you often want to break up the work in pieces. You |
| 2097 | can use functions or mappings for this. But you don't want these functions |
| 2098 | and mappings to interfere with the ones from other scripts. For example, you |
| 2099 | could define a function Add(), but another script could try to define the same |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2100 | function. To avoid this, we define the function local to the script. |
| 2101 | Fortunately, in |Vim9| script this is the default. In a legacy script you |
| 2102 | would need to prefix the name with "s:". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2103 | |
| 2104 | We will define a function that adds a new typing correction: > |
| 2105 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2106 | 30 def Add(from: string, correct: bool) |
| 2107 | 31 var to = input("type the correction for " .. from .. ": ") |
| 2108 | 32 exe ":iabbrev " .. from .. " " .. to |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2109 | .. |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2110 | 36 enddef |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2111 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2112 | Now we can call the function Add() from within this script. If another |
| 2113 | script also defines Add(), it will be local to that script and can only |
| 2114 | be called from that script. There can also be a global g:Add() function, |
| 2115 | which is again another function. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2116 | |
| 2117 | <SID> can be used with mappings. It generates a script ID, which identifies |
| 2118 | the current script. In our typing correction plugin we use it like this: > |
| 2119 | |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 2120 | 24 noremap <unique> <script> <Plug>TypecorrAdd; <SID>Add |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2121 | .. |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2122 | 28 noremap <SID>Add :call <SID>Add(expand("<cword>"), true)<CR> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2123 | |
| 2124 | Thus when a user types "\a", this sequence is invoked: > |
| 2125 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2126 | \a -> <Plug>TypecorrAdd; -> <SID>Add -> :call <SID>Add(...) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2127 | |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 2128 | If another script also maps <SID>Add, it will get another script ID and |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2129 | thus define another mapping. |
| 2130 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2131 | Note that instead of Add() we use <SID>Add() here. That is because the |
| 2132 | mapping is typed by the user, thus outside of the script context. The <SID> |
| 2133 | is translated to the script ID, so that Vim knows in which script to look for |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2134 | the Add() function. |
| 2135 | |
| 2136 | This is a bit complicated, but it's required for the plugin to work together |
| 2137 | with other plugins. The basic rule is that you use <SID>Add() in mappings and |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2138 | Add() in other places (the script itself, autocommands, user commands). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2139 | |
| 2140 | We can also add a menu entry to do the same as the mapping: > |
| 2141 | |
| 2142 | 26 noremenu <script> Plugin.Add\ Correction <SID>Add |
| 2143 | |
| 2144 | The "Plugin" menu is recommended for adding menu items for plugins. In this |
| 2145 | case only one item is used. When adding more items, creating a submenu is |
| 2146 | recommended. For example, "Plugin.CVS" could be used for a plugin that offers |
| 2147 | CVS operations "Plugin.CVS.checkin", "Plugin.CVS.checkout", etc. |
| 2148 | |
| 2149 | Note that in line 28 ":noremap" is used to avoid that any other mappings cause |
| 2150 | trouble. Someone may have remapped ":call", for example. In line 24 we also |
| 2151 | use ":noremap", but we do want "<SID>Add" to be remapped. This is why |
| 2152 | "<script>" is used here. This only allows mappings which are local to the |
| 2153 | script. |:map-<script>| The same is done in line 26 for ":noremenu". |
| 2154 | |:menu-<script>| |
| 2155 | |
| 2156 | |
| 2157 | <SID> AND <Plug> *using-<Plug>* |
| 2158 | |
| 2159 | Both <SID> and <Plug> are used to avoid that mappings of typed keys interfere |
| 2160 | with mappings that are only to be used from other mappings. Note the |
| 2161 | difference between using <SID> and <Plug>: |
| 2162 | |
| 2163 | <Plug> is visible outside of the script. It is used for mappings which the |
| 2164 | user might want to map a key sequence to. <Plug> is a special code |
| 2165 | that a typed key will never produce. |
| 2166 | To make it very unlikely that other plugins use the same sequence of |
| 2167 | characters, use this structure: <Plug> scriptname mapname |
| 2168 | In our example the scriptname is "Typecorr" and the mapname is "Add". |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 2169 | We add a semicolon as the terminator. This results in |
| 2170 | "<Plug>TypecorrAdd;". Only the first character of scriptname and |
| 2171 | mapname is uppercase, so that we can see where mapname starts. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2172 | |
| 2173 | <SID> is the script ID, a unique identifier for a script. |
| 2174 | Internally Vim translates <SID> to "<SNR>123_", where "123" can be any |
| 2175 | number. Thus a function "<SID>Add()" will have a name "<SNR>11_Add()" |
| 2176 | in one script, and "<SNR>22_Add()" in another. You can see this if |
| 2177 | you use the ":function" command to get a list of functions. The |
| 2178 | translation of <SID> in mappings is exactly the same, that's how you |
| 2179 | can call a script-local function from a mapping. |
| 2180 | |
| 2181 | |
| 2182 | USER COMMAND |
| 2183 | |
| 2184 | Now let's add a user command to add a correction: > |
| 2185 | |
| 2186 | 38 if !exists(":Correct") |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2187 | 39 command -nargs=1 Correct :call Add(<q-args>, false) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2188 | 40 endif |
| 2189 | |
| 2190 | The user command is defined only if no command with the same name already |
| 2191 | exists. Otherwise we would get an error here. Overriding the existing user |
| 2192 | command with ":command!" is not a good idea, this would probably make the user |
| 2193 | wonder why the command he defined himself doesn't work. |:command| |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2194 | If it did happen you can find out who to blame with: > |
| 2195 | |
| 2196 | verbose command Correct |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2197 | |
| 2198 | |
| 2199 | SCRIPT VARIABLES |
| 2200 | |
| 2201 | When a variable starts with "s:" it is a script variable. It can only be used |
| 2202 | inside a script. Outside the script it's not visible. This avoids trouble |
| 2203 | with using the same variable name in different scripts. The variables will be |
| 2204 | kept as long as Vim is running. And the same variables are used when sourcing |
| 2205 | the same script again. |s:var| |
| 2206 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2207 | The nice thing about |Vim9| script is that variables are local to the script |
| 2208 | by default. You can prepend "s:" if you like, but you do not need to. And |
| 2209 | functions in the script can also use the script variables without a prefix. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2210 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2211 | Script-local variables can also be used in functions, autocommands and user |
| 2212 | commands that are defined in the script. Thus they are the perfect way to |
| 2213 | share information between parts of your plugin, without it leaking out. In |
| 2214 | our example we can add a few lines to count the number of corrections: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2215 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2216 | 19 var count = 4 |
| 2217 | .. |
| 2218 | 30 def Add(from: string, correct: bool) |
| 2219 | .. |
| 2220 | 34 count += 1 |
| 2221 | 35 echo "you now have " .. count .. " corrections" |
| 2222 | 36 enddef |
| 2223 | |
| 2224 | "count" is declared and initialized to 4 in the script itself. When later |
| 2225 | the Add() function is called, it increments "count". It doesn't matter from |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2226 | where the function was called, since it has been defined in the script, it |
| 2227 | will use the local variables from this script. |
| 2228 | |
| 2229 | |
| 2230 | THE RESULT |
| 2231 | |
| 2232 | Here is the resulting complete example: > |
| 2233 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2234 | 1 vim9script noclear |
| 2235 | 2 # Vim global plugin for correcting typing mistakes |
| 2236 | 3 # Last Change: 2021 Dec 30 |
| 2237 | 4 # Maintainer: Bram Moolenaar <Bram@vim.org> |
| 2238 | 5 # License: This file is placed in the public domain. |
| 2239 | 6 |
| 2240 | 7 if exists("g:loaded_typecorrect") |
| 2241 | 8 finish |
| 2242 | 9 endif |
| 2243 | 10 g:loaded_typecorrect = 1 |
| 2244 | 11 var save_cpo = &cpo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2245 | 12 set cpo&vim |
| 2246 | 13 |
| 2247 | 14 iabbrev teh the |
| 2248 | 15 iabbrev otehr other |
| 2249 | 16 iabbrev wnat want |
| 2250 | 17 iabbrev synchronisation |
| 2251 | 18 \ synchronization |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2252 | 19 var count = 4 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2253 | 20 |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 2254 | 21 if !hasmapto('<Plug>TypecorrAdd;') |
| 2255 | 22 map <unique> <Leader>a <Plug>TypecorrAdd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2256 | 23 endif |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 2257 | 24 noremap <unique> <script> <Plug>TypecorrAdd; <SID>Add |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2258 | 25 |
| 2259 | 26 noremenu <script> Plugin.Add\ Correction <SID>Add |
| 2260 | 27 |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2261 | 28 noremap <SID>Add :call <SID>Add(expand("<cword>"), true)<CR> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2262 | 29 |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2263 | 30 def Add(from: string, correct: bool) |
| 2264 | 31 var to = input("type the correction for " .. from .. ": ") |
| 2265 | 32 exe ":iabbrev " .. from .. " " .. to |
| 2266 | 33 if correct | exe "normal viws\<C-R>\" \b\e" | endif |
| 2267 | 34 count += 1 |
| 2268 | 35 echo "you now have " .. count .. " corrections" |
| 2269 | 36 enddef |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2270 | 37 |
| 2271 | 38 if !exists(":Correct") |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2272 | 39 command -nargs=1 Correct call Add(<q-args>, false) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2273 | 40 endif |
| 2274 | 41 |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2275 | 42 &cpo = save_cpo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2276 | |
| 2277 | Line 33 wasn't explained yet. It applies the new correction to the word under |
| 2278 | the cursor. The |:normal| command is used to use the new abbreviation. Note |
| 2279 | that mappings and abbreviations are expanded here, even though the function |
| 2280 | was called from a mapping defined with ":noremap". |
| 2281 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2282 | |
| 2283 | DOCUMENTATION *write-local-help* |
| 2284 | |
| 2285 | It's a good idea to also write some documentation for your plugin. Especially |
| 2286 | when its behavior can be changed by the user. See |add-local-help| for how |
| 2287 | they are installed. |
| 2288 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2289 | Here is a simple example for a plugin help file, called "typecorrect.txt": > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2290 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2291 | 1 *typecorrect.txt* Plugin for correcting typing mistakes |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2292 | 2 |
| 2293 | 3 If you make typing mistakes, this plugin will have them corrected |
| 2294 | 4 automatically. |
| 2295 | 5 |
| 2296 | 6 There are currently only a few corrections. Add your own if you like. |
| 2297 | 7 |
| 2298 | 8 Mappings: |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 2299 | 9 <Leader>a or <Plug>TypecorrAdd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2300 | 10 Add a correction for the word under the cursor. |
| 2301 | 11 |
| 2302 | 12 Commands: |
| 2303 | 13 :Correct {word} |
| 2304 | 14 Add a correction for {word}. |
| 2305 | 15 |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2306 | 16 *typecorrect-settings* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2307 | 17 This plugin doesn't have any settings. |
| 2308 | |
| 2309 | The first line is actually the only one for which the format matters. It will |
| 2310 | be extracted from the help file to be put in the "LOCAL ADDITIONS:" section of |
| 2311 | help.txt |local-additions|. The first "*" must be in the first column of the |
| 2312 | first line. After adding your help file do ":help" and check that the entries |
| 2313 | line up nicely. |
| 2314 | |
| 2315 | You can add more tags inside ** in your help file. But be careful not to use |
| 2316 | existing help tags. You would probably use the name of your plugin in most of |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2317 | them, like "typecorrect-settings" in the example. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2318 | |
| 2319 | Using references to other parts of the help in || is recommended. This makes |
| 2320 | it easy for the user to find associated help. |
| 2321 | |
| 2322 | |
| 2323 | FILETYPE DETECTION *plugin-filetype* |
| 2324 | |
| 2325 | If your filetype is not already detected by Vim, you should create a filetype |
| 2326 | detection snippet in a separate file. It is usually in the form of an |
| 2327 | autocommand that sets the filetype when the file name matches a pattern. |
| 2328 | Example: > |
| 2329 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2330 | au BufNewFile,BufRead *.foo setlocal filetype=foofoo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2331 | |
| 2332 | Write this single-line file as "ftdetect/foofoo.vim" in the first directory |
| 2333 | that appears in 'runtimepath'. For Unix that would be |
| 2334 | "~/.vim/ftdetect/foofoo.vim". The convention is to use the name of the |
| 2335 | filetype for the script name. |
| 2336 | |
| 2337 | You can make more complicated checks if you like, for example to inspect the |
| 2338 | contents of the file to recognize the language. Also see |new-filetype|. |
| 2339 | |
| 2340 | |
| 2341 | SUMMARY *plugin-special* |
| 2342 | |
| 2343 | Summary of special things to use in a plugin: |
| 2344 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2345 | var name Variable local to the script. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2346 | |
| 2347 | <SID> Script-ID, used for mappings and functions local to |
| 2348 | the script. |
| 2349 | |
| 2350 | hasmapto() Function to test if the user already defined a mapping |
| 2351 | for functionality the script offers. |
| 2352 | |
| 2353 | <Leader> Value of "mapleader", which the user defines as the |
| 2354 | keys that plugin mappings start with. |
| 2355 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2356 | map <unique> Give a warning if a mapping already exists. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2357 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2358 | noremap <script> Use only mappings local to the script, not global |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2359 | mappings. |
| 2360 | |
| 2361 | exists(":Cmd") Check if a user command already exists. |
| 2362 | |
| 2363 | ============================================================================== |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 2364 | *41.12* Writing a filetype plugin *write-filetype-plugin* *ftplugin* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2365 | |
| 2366 | A filetype plugin is like a global plugin, except that it sets options and |
| 2367 | defines mappings for the current buffer only. See |add-filetype-plugin| for |
| 2368 | how this type of plugin is used. |
| 2369 | |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 2370 | First read the section on global plugins above |41.11|. All that is said there |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2371 | also applies to filetype plugins. There are a few extras, which are explained |
| 2372 | here. The essential thing is that a filetype plugin should only have an |
| 2373 | effect on the current buffer. |
| 2374 | |
| 2375 | |
| 2376 | DISABLING |
| 2377 | |
| 2378 | If you are writing a filetype plugin to be used by many people, they need a |
| 2379 | chance to disable loading it. Put this at the top of the plugin: > |
| 2380 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2381 | # Only do this when not done yet for this buffer |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2382 | if exists("b:did_ftplugin") |
| 2383 | finish |
| 2384 | endif |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2385 | b:did_ftplugin = 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2386 | |
| 2387 | This also needs to be used to avoid that the same plugin is executed twice for |
| 2388 | the same buffer (happens when using an ":edit" command without arguments). |
| 2389 | |
| 2390 | Now users can disable loading the default plugin completely by making a |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2391 | filetype plugin with only these lines: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2392 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2393 | vim9script |
| 2394 | b:did_ftplugin = 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2395 | |
| 2396 | This does require that the filetype plugin directory comes before $VIMRUNTIME |
| 2397 | in 'runtimepath'! |
| 2398 | |
| 2399 | If you do want to use the default plugin, but overrule one of the settings, |
| 2400 | you can write the different setting in a script: > |
| 2401 | |
| 2402 | setlocal textwidth=70 |
| 2403 | |
| 2404 | Now write this in the "after" directory, so that it gets sourced after the |
| 2405 | distributed "vim.vim" ftplugin |after-directory|. For Unix this would be |
| 2406 | "~/.vim/after/ftplugin/vim.vim". Note that the default plugin will have set |
| 2407 | "b:did_ftplugin", but it is ignored here. |
| 2408 | |
| 2409 | |
| 2410 | OPTIONS |
| 2411 | |
| 2412 | To make sure the filetype plugin only affects the current buffer use the > |
| 2413 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2414 | setlocal |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2415 | |
| 2416 | command to set options. And only set options which are local to a buffer (see |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2417 | the help for the option to check that). When using `:setlocal` for global |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2418 | options or options local to a window, the value will change for many buffers, |
| 2419 | and that is not what a filetype plugin should do. |
| 2420 | |
| 2421 | When an option has a value that is a list of flags or items, consider using |
| 2422 | "+=" and "-=" to keep the existing value. Be aware that the user may have |
| 2423 | changed an option value already. First resetting to the default value and |
Bram Moolenaar | d58e929 | 2011-02-09 17:07:58 +0100 | [diff] [blame] | 2424 | then changing it is often a good idea. Example: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2425 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2426 | setlocal formatoptions& formatoptions+=ro |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2427 | |
| 2428 | |
| 2429 | MAPPINGS |
| 2430 | |
| 2431 | To make sure mappings will only work in the current buffer use the > |
| 2432 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2433 | map <buffer> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2434 | |
| 2435 | command. This needs to be combined with the two-step mapping explained above. |
| 2436 | An example of how to define functionality in a filetype plugin: > |
| 2437 | |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 2438 | if !hasmapto('<Plug>JavaImport;') |
| 2439 | map <buffer> <unique> <LocalLeader>i <Plug>JavaImport; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2440 | endif |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 2441 | noremap <buffer> <unique> <Plug>JavaImport; oimport ""<Left><Esc> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2442 | |
| 2443 | |hasmapto()| is used to check if the user has already defined a map to |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 2444 | <Plug>JavaImport;. If not, then the filetype plugin defines the default |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2445 | mapping. This starts with |<LocalLeader>|, which allows the user to select |
| 2446 | the key(s) he wants filetype plugin mappings to start with. The default is a |
| 2447 | backslash. |
| 2448 | "<unique>" is used to give an error message if the mapping already exists or |
| 2449 | overlaps with an existing mapping. |
| 2450 | |:noremap| is used to avoid that any other mappings that the user has defined |
| 2451 | interferes. You might want to use ":noremap <script>" to allow remapping |
| 2452 | mappings defined in this script that start with <SID>. |
| 2453 | |
| 2454 | The user must have a chance to disable the mappings in a filetype plugin, |
| 2455 | without disabling everything. Here is an example of how this is done for a |
| 2456 | plugin for the mail filetype: > |
| 2457 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2458 | # Add mappings, unless the user didn't want this. |
| 2459 | if !exists("g:no_plugin_maps") && !exists("g:no_mail_maps") |
| 2460 | # Quote text by inserting "> " |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 2461 | if !hasmapto('<Plug>MailQuote;') |
| 2462 | vmap <buffer> <LocalLeader>q <Plug>MailQuote; |
| 2463 | nmap <buffer> <LocalLeader>q <Plug>MailQuote; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2464 | endif |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 2465 | vnoremap <buffer> <Plug>MailQuote; :s/^/> /<CR> |
| 2466 | nnoremap <buffer> <Plug>MailQuote; :.,$s/^/> /<CR> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2467 | endif |
| 2468 | |
| 2469 | Two global variables are used: |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2470 | |g:no_plugin_maps| disables mappings for all filetype plugins |
| 2471 | |g:no_mail_maps| disables mappings for the "mail" filetype |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2472 | |
| 2473 | |
| 2474 | USER COMMANDS |
| 2475 | |
| 2476 | To add a user command for a specific file type, so that it can only be used in |
| 2477 | one buffer, use the "-buffer" argument to |:command|. Example: > |
| 2478 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2479 | command -buffer Make make %:r.s |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2480 | |
| 2481 | |
| 2482 | VARIABLES |
| 2483 | |
| 2484 | A filetype plugin will be sourced for each buffer of the type it's for. Local |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2485 | script variables will be shared between all invocations. Use local buffer |
| 2486 | variables |b:var| if you want a variable specifically for one buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2487 | |
| 2488 | |
| 2489 | FUNCTIONS |
| 2490 | |
| 2491 | When defining a function, this only needs to be done once. But the filetype |
| 2492 | plugin will be sourced every time a file with this filetype will be opened. |
Bram Moolenaar | 06b5d51 | 2010-05-22 15:37:44 +0200 | [diff] [blame] | 2493 | This construct makes sure the function is only defined once: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2494 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2495 | if !exists("*Func") |
| 2496 | def Func(arg) |
| 2497 | ... |
| 2498 | enddef |
| 2499 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2500 | < |
| 2501 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 2502 | UNDO *undo_indent* *undo_ftplugin* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2503 | |
| 2504 | When the user does ":setfiletype xyz" the effect of the previous filetype |
| 2505 | should be undone. Set the b:undo_ftplugin variable to the commands that will |
| 2506 | undo the settings in your filetype plugin. Example: > |
| 2507 | |
Bram Moolenaar | f10911e | 2022-01-29 22:20:48 +0000 | [diff] [blame] | 2508 | let b:undo_ftplugin = "setlocal fo< com< tw< commentstring<" |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 2509 | \ .. "| unlet b:match_ignorecase b:match_words b:match_skip" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2510 | |
| 2511 | Using ":setlocal" with "<" after the option name resets the option to its |
| 2512 | global value. That is mostly the best way to reset the option value. |
| 2513 | |
| 2514 | This does require removing the "C" flag from 'cpoptions' to allow line |
| 2515 | continuation, as mentioned above |use-cpo-save|. |
| 2516 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 2517 | For undoing the effect of an indent script, the b:undo_indent variable should |
| 2518 | be set accordingly. |
| 2519 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2520 | |
| 2521 | FILE NAME |
| 2522 | |
| 2523 | The filetype must be included in the file name |ftplugin-name|. Use one of |
| 2524 | these three forms: |
| 2525 | |
| 2526 | .../ftplugin/stuff.vim |
| 2527 | .../ftplugin/stuff_foo.vim |
| 2528 | .../ftplugin/stuff/bar.vim |
| 2529 | |
| 2530 | "stuff" is the filetype, "foo" and "bar" are arbitrary names. |
| 2531 | |
| 2532 | |
| 2533 | SUMMARY *ftplugin-special* |
| 2534 | |
| 2535 | Summary of special things to use in a filetype plugin: |
| 2536 | |
| 2537 | <LocalLeader> Value of "maplocalleader", which the user defines as |
| 2538 | the keys that filetype plugin mappings start with. |
| 2539 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2540 | map <buffer> Define a mapping local to the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2541 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2542 | noremap <script> Only remap mappings defined in this script that start |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2543 | with <SID>. |
| 2544 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2545 | setlocal Set an option for the current buffer only. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2546 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2547 | command -buffer Define a user command local to the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2548 | |
| 2549 | exists("*s:Func") Check if a function was already defined. |
| 2550 | |
| 2551 | Also see |plugin-special|, the special things used for all plugins. |
| 2552 | |
| 2553 | ============================================================================== |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 2554 | *41.13* Writing a compiler plugin *write-compiler-plugin* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2555 | |
| 2556 | A compiler plugin sets options for use with a specific compiler. The user can |
| 2557 | load it with the |:compiler| command. The main use is to set the |
| 2558 | 'errorformat' and 'makeprg' options. |
| 2559 | |
| 2560 | Easiest is to have a look at examples. This command will edit all the default |
| 2561 | compiler plugins: > |
| 2562 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2563 | next $VIMRUNTIME/compiler/*.vim |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2564 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2565 | Type `:next` to go to the next plugin file. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2566 | |
| 2567 | There are two special items about these files. First is a mechanism to allow |
| 2568 | a user to overrule or add to the default file. The default files start with: > |
| 2569 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2570 | if exists("g:current_compiler") |
| 2571 | finish |
| 2572 | endif |
| 2573 | g:current_compiler = "mine" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2574 | |
| 2575 | When you write a compiler file and put it in your personal runtime directory |
| 2576 | (e.g., ~/.vim/compiler for Unix), you set the "current_compiler" variable to |
| 2577 | make the default file skip the settings. |
Bram Moolenaar | c6039d8 | 2005-12-02 00:44:04 +0000 | [diff] [blame] | 2578 | *:CompilerSet* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2579 | The second mechanism is to use ":set" for ":compiler!" and ":setlocal" for |
| 2580 | ":compiler". Vim defines the ":CompilerSet" user command for this. However, |
| 2581 | older Vim versions don't, thus your plugin should define it then. This is an |
| 2582 | example: > |
| 2583 | |
| 2584 | if exists(":CompilerSet") != 2 |
| 2585 | command -nargs=* CompilerSet setlocal <args> |
| 2586 | endif |
| 2587 | CompilerSet errorformat& " use the default 'errorformat' |
| 2588 | CompilerSet makeprg=nmake |
| 2589 | |
| 2590 | When you write a compiler plugin for the Vim distribution or for a system-wide |
| 2591 | runtime directory, use the mechanism mentioned above. When |
| 2592 | "current_compiler" was already set by a user plugin nothing will be done. |
| 2593 | |
| 2594 | When you write a compiler plugin to overrule settings from a default plugin, |
| 2595 | don't check "current_compiler". This plugin is supposed to be loaded |
| 2596 | last, thus it should be in a directory at the end of 'runtimepath'. For Unix |
| 2597 | that could be ~/.vim/after/compiler. |
| 2598 | |
| 2599 | ============================================================================== |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 2600 | *41.14* Writing a plugin that loads quickly *write-plugin-quickload* |
| 2601 | |
| 2602 | A plugin may grow and become quite long. The startup delay may become |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 2603 | noticeable, while you hardly ever use the plugin. Then it's time for a |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 2604 | quickload plugin. |
| 2605 | |
| 2606 | The basic idea is that the plugin is loaded twice. The first time user |
| 2607 | commands and mappings are defined that offer the functionality. The second |
| 2608 | time the functions that implement the functionality are defined. |
| 2609 | |
| 2610 | It may sound surprising that quickload means loading a script twice. What we |
| 2611 | mean is that it loads quickly the first time, postponing the bulk of the |
| 2612 | script to the second time, which only happens when you actually use it. When |
| 2613 | you always use the functionality it actually gets slower! |
| 2614 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2615 | This uses a FuncUndefined autocommand. Since Vim 7 there is an alternative: |
| 2616 | use the |autoload| functionality |41.15|. That will also use |Vim9| script |
| 2617 | instead of legacy script that is used here. |
Bram Moolenaar | 76916e6 | 2006-03-21 21:23:25 +0000 | [diff] [blame] | 2618 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 2619 | The following example shows how it's done: > |
| 2620 | |
| 2621 | " Vim global plugin for demonstrating quick loading |
| 2622 | " Last Change: 2005 Feb 25 |
| 2623 | " Maintainer: Bram Moolenaar <Bram@vim.org> |
| 2624 | " License: This file is placed in the public domain. |
| 2625 | |
| 2626 | if !exists("s:did_load") |
| 2627 | command -nargs=* BNRead call BufNetRead(<f-args>) |
| 2628 | map <F19> :call BufNetWrite('something')<CR> |
| 2629 | |
| 2630 | let s:did_load = 1 |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 2631 | exe 'au FuncUndefined BufNet* source ' .. expand('<sfile>') |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 2632 | finish |
| 2633 | endif |
| 2634 | |
| 2635 | function BufNetRead(...) |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 2636 | echo 'BufNetRead(' .. string(a:000) .. ')' |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 2637 | " read functionality here |
| 2638 | endfunction |
| 2639 | |
| 2640 | function BufNetWrite(...) |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 2641 | echo 'BufNetWrite(' .. string(a:000) .. ')' |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 2642 | " write functionality here |
| 2643 | endfunction |
| 2644 | |
| 2645 | When the script is first loaded "s:did_load" is not set. The commands between |
| 2646 | the "if" and "endif" will be executed. This ends in a |:finish| command, thus |
| 2647 | the rest of the script is not executed. |
| 2648 | |
| 2649 | The second time the script is loaded "s:did_load" exists and the commands |
| 2650 | after the "endif" are executed. This defines the (possible long) |
| 2651 | BufNetRead() and BufNetWrite() functions. |
| 2652 | |
| 2653 | If you drop this script in your plugin directory Vim will execute it on |
| 2654 | startup. This is the sequence of events that happens: |
| 2655 | |
| 2656 | 1. The "BNRead" command is defined and the <F19> key is mapped when the script |
| 2657 | is sourced at startup. A |FuncUndefined| autocommand is defined. The |
| 2658 | ":finish" command causes the script to terminate early. |
| 2659 | |
| 2660 | 2. The user types the BNRead command or presses the <F19> key. The |
| 2661 | BufNetRead() or BufNetWrite() function will be called. |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 2662 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 2663 | 3. Vim can't find the function and triggers the |FuncUndefined| autocommand |
| 2664 | event. Since the pattern "BufNet*" matches the invoked function, the |
| 2665 | command "source fname" will be executed. "fname" will be equal to the name |
| 2666 | of the script, no matter where it is located, because it comes from |
| 2667 | expanding "<sfile>" (see |expand()|). |
| 2668 | |
| 2669 | 4. The script is sourced again, the "s:did_load" variable exists and the |
| 2670 | functions are defined. |
| 2671 | |
| 2672 | Notice that the functions that are loaded afterwards match the pattern in the |
| 2673 | |FuncUndefined| autocommand. You must make sure that no other plugin defines |
| 2674 | functions that match this pattern. |
| 2675 | |
| 2676 | ============================================================================== |
| 2677 | *41.15* Writing library scripts *write-library-script* |
| 2678 | |
| 2679 | Some functionality will be required in several places. When this becomes more |
| 2680 | than a few lines you will want to put it in one script and use it from many |
| 2681 | scripts. We will call that one script a library script. |
| 2682 | |
| 2683 | Manually loading a library script is possible, so long as you avoid loading it |
| 2684 | when it's already done. You can do this with the |exists()| function. |
| 2685 | Example: > |
| 2686 | |
| 2687 | if !exists('*MyLibFunction') |
| 2688 | runtime library/mylibscript.vim |
| 2689 | endif |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2690 | MyLibFunction(arg) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 2691 | |
| 2692 | Here you need to know that MyLibFunction() is defined in a script |
| 2693 | "library/mylibscript.vim" in one of the directories in 'runtimepath'. |
| 2694 | |
| 2695 | To make this a bit simpler Vim offers the autoload mechanism. Then the |
| 2696 | example looks like this: > |
| 2697 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2698 | mylib#myfunction(arg) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 2699 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2700 | That's a lot simpler, isn't it? Vim will recognize the function name by the |
| 2701 | embedded "#" character and when it's not defined search for the script |
| 2702 | "autoload/mylib.vim" in 'runtimepath'. That script must define the |
| 2703 | "mylib#myfunction()" function. |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 2704 | |
| 2705 | You can put many other functions in the mylib.vim script, you are free to |
| 2706 | organize your functions in library scripts. But you must use function names |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2707 | where the part before the '#' matches the script name. Otherwise Vim would |
| 2708 | not know what script to load. |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 2709 | |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2710 | If you get really enthusiastic and write lots of library scripts, you may |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 2711 | want to use subdirectories. Example: > |
| 2712 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2713 | netlib#ftp#read('somefile') |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 2714 | |
| 2715 | For Unix the library script used for this could be: |
| 2716 | |
| 2717 | ~/.vim/autoload/netlib/ftp.vim |
| 2718 | |
| 2719 | Where the function is defined like this: > |
| 2720 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2721 | def netlib#ftp#read(fname: string) |
| 2722 | # Read the file fname through ftp |
| 2723 | enddef |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 2724 | |
| 2725 | Notice that the name the function is defined with is exactly the same as the |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2726 | name used for calling the function. And the part before the last '#' |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 2727 | exactly matches the subdirectory and script name. |
| 2728 | |
| 2729 | You can use the same mechanism for variables: > |
| 2730 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2731 | var weekdays = dutch#weekdays |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 2732 | |
| 2733 | This will load the script "autoload/dutch.vim", which should contain something |
| 2734 | like: > |
| 2735 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2736 | var dutch#weekdays = ['zondag', 'maandag', 'dinsdag', 'woensdag', |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 2737 | \ 'donderdag', 'vrijdag', 'zaterdag'] |
| 2738 | |
| 2739 | Further reading: |autoload|. |
| 2740 | |
| 2741 | ============================================================================== |
Bram Moolenaar | 76916e6 | 2006-03-21 21:23:25 +0000 | [diff] [blame] | 2742 | *41.16* Distributing Vim scripts *distribute-script* |
| 2743 | |
| 2744 | Vim users will look for scripts on the Vim website: http://www.vim.org. |
| 2745 | If you made something that is useful for others, share it! |
| 2746 | |
Bram Moolenaar | 04fb916 | 2021-12-30 20:24:12 +0000 | [diff] [blame] | 2747 | Another place is github. But there you need to know where to find it! The |
| 2748 | advantage is that most plugin managers fetch plugins from github. You'll have |
| 2749 | to use your favorite search engine to find them. |
| 2750 | |
| 2751 | Vim scripts can be used on any system. However, there might not be a tar or |
| 2752 | gzip command. If you want to pack files together and/or compress them the |
| 2753 | "zip" utility is recommended. |
Bram Moolenaar | 76916e6 | 2006-03-21 21:23:25 +0000 | [diff] [blame] | 2754 | |
| 2755 | For utmost portability use Vim itself to pack scripts together. This can be |
| 2756 | done with the Vimball utility. See |vimball|. |
| 2757 | |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 2758 | It's good if you add a line to allow automatic updating. See |glvs-plugins|. |
| 2759 | |
Bram Moolenaar | 76916e6 | 2006-03-21 21:23:25 +0000 | [diff] [blame] | 2760 | ============================================================================== |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2761 | |
| 2762 | Next chapter: |usr_42.txt| Add new menus |
| 2763 | |
Bram Moolenaar | d473c8c | 2018-08-11 18:00:22 +0200 | [diff] [blame] | 2764 | Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: |