1*repeat.txt* For Vim version 8.2. Last change: 2021 Sep 09 2 3 4 VIM REFERENCE MANUAL by Bram Moolenaar 5 6 7Repeating commands, Vim scripts and debugging *repeating* 8 9Chapter 26 of the user manual introduces repeating |usr_26.txt|. 10 111. Single repeats |single-repeat| 122. Multiple repeats |multi-repeat| 133. Complex repeats |complex-repeat| 144. Using Vim scripts |using-scripts| 155. Using Vim packages |packages| 166. Creating Vim packages |package-create| 177. Debugging scripts |debug-scripts| 188. Profiling |profiling| 19 20============================================================================== 211. Single repeats *single-repeat* 22 23 *.* 24. Repeat last change, with count replaced with [count]. 25 Also repeat a yank command, when the 'y' flag is 26 included in 'cpoptions'. Does not repeat a 27 command-line command. 28 29Simple changes can be repeated with the "." command. Without a count, the 30count of the last change is used. If you enter a count, it will replace the 31last one. |v:count| and |v:count1| will be set. 32 33If the last change included a specification of a numbered register, the 34register number will be incremented. See |redo-register| for an example how 35to use this. 36 37Note that when repeating a command that used a Visual selection, the same SIZE 38of area is used, see |visual-repeat|. 39 40 *@:* 41@: Repeat last command-line [count] times. 42 {not available when compiled without the 43 |+cmdline_hist| feature} 44 45 46============================================================================== 472. Multiple repeats *multi-repeat* 48 49 *:g* *:global* *E148* 50:[range]g[lobal]/{pattern}/[cmd] 51 Execute the Ex command [cmd] (default ":p") on the 52 lines within [range] where {pattern} matches. 53 54:[range]g[lobal]!/{pattern}/[cmd] 55 Execute the Ex command [cmd] (default ":p") on the 56 lines within [range] where {pattern} does NOT match. 57 58 *:v* *:vglobal* 59:[range]v[global]/{pattern}/[cmd] 60 Same as :g!. 61 62Example: > 63 :g/^Obsolete/d _ 64Using the underscore after `:d` avoids clobbering registers or the clipboard. 65This also makes it faster. 66 67Instead of the '/' which surrounds the {pattern}, you can use any other 68single byte character, but not an alphabetic character, '\', '"' or '|'. 69This is useful if you want to include a '/' in the search pattern or 70replacement string. 71 72For the definition of a pattern, see |pattern|. 73 74NOTE [cmd] may contain a range; see |collapse| and |edit-paragraph-join| for 75examples. 76 77The global commands work by first scanning through the [range] lines and 78marking each line where a match occurs (for a multi-line pattern, only the 79start of the match matters). 80In a second scan the [cmd] is executed for each marked line, as if the cursor 81was in that line. For ":v" and ":g!" the command is executed for each not 82marked line. If a line is deleted its mark disappears. 83The default for [range] is the whole buffer (1,$). Use "CTRL-C" to interrupt 84the command. If an error message is given for a line, the command for that 85line is aborted and the global command continues with the next marked or 86unmarked line. 87 *E147* 88When the command is used recursively, it only works on one line. Giving a 89range is then not allowed. This is useful to find all lines that match a 90pattern and do not match another pattern: > 91 :g/found/v/notfound/{cmd} 92This first finds all lines containing "found", but only executes {cmd} when 93there is no match for "notfound". 94 95To execute a non-Ex command, you can use the `:normal` command: > 96 :g/pat/normal {commands} 97Make sure that {commands} ends with a whole command, otherwise Vim will wait 98for you to type the rest of the command for each match. The screen will not 99have been updated, so you don't know what you are doing. See |:normal|. 100 101The undo/redo command will undo/redo the whole global command at once. 102The previous context mark will only be set once (with "''" you go back to 103where the cursor was before the global command). 104 105The global command sets both the last used search pattern and the last used 106substitute pattern (this is vi compatible). This makes it easy to globally 107replace a string: 108 :g/pat/s//PAT/g 109This replaces all occurrences of "pat" with "PAT". The same can be done with: 110 :%s/pat/PAT/g 111Which is two characters shorter! 112 113When using "global" in Ex mode, a special case is using ":visual" as a 114command. This will move to a matching line, go to Normal mode to let you 115execute commands there until you use |Q| to return to Ex mode. This will be 116repeated for each matching line. While doing this you cannot use ":global". 117To abort this type CTRL-C twice. 118 119============================================================================== 1203. Complex repeats *complex-repeat* 121 122 *q* *recording* 123q{0-9a-zA-Z"} Record typed characters into register {0-9a-zA-Z"} 124 (uppercase to append). The 'q' command is disabled 125 while executing a register, and it doesn't work inside 126 a mapping and |:normal|. 127 128 Note: If the register being used for recording is also 129 used for |y| and |p| the result is most likely not 130 what is expected, because the put will paste the 131 recorded macro and the yank will overwrite the 132 recorded macro. 133 134 Note: The recording happens while you type, replaying 135 the register happens as if the keys come from a 136 mapping. This matters, for example, for undo, which 137 only syncs when commands were typed. 138 139q Stops recording. (Implementation note: The 'q' that 140 stops recording is not stored in the register, unless 141 it was the result of a mapping) 142 143 *@* 144@{0-9a-z".=*+} Execute the contents of register {0-9a-z".=*+} [count] 145 times. Note that register '%' (name of the current 146 file) and '#' (name of the alternate file) cannot be 147 used. 148 The register is executed like a mapping, that means 149 that the difference between 'wildchar' and 'wildcharm' 150 applies, and undo might not be synced in the same way. 151 For "@=" you are prompted to enter an expression. The 152 result of the expression is then executed. 153 See also |@:|. 154 155 *@@* *E748* 156@@ Repeat the previous @{0-9a-z":*} [count] times. 157 158 *:@* 159:[addr]@{0-9a-z".=*+} Execute the contents of register {0-9a-z".=*+} as an Ex 160 command. First set cursor at line [addr] (default is 161 current line). When the last line in the register does 162 not have a <CR> it will be added automatically when 163 the 'e' flag is present in 'cpoptions'. 164 For ":@=" the last used expression is used. The 165 result of evaluating the expression is executed as an 166 Ex command. 167 Mappings are not recognized in these commands. 168 When the |line-continuation| character (\) is present 169 at the beginning of a line in a linewise register, 170 then it is combined with the previous line. This is 171 useful for yanking and executing parts of a Vim 172 script. 173 Future: Will execute the register for each line in the 174 address range. 175 176:[addr]*{0-9a-z".=+} *:star-compatible* 177 When '*' is present in 'cpoptions' |cpo-star|, use 178 ":*" in the same way as ":@". This is NOT the default 179 when 'nocompatible' is used. When the '*' flag is not 180 present in 'cpoptions', ":*" is an alias for ":'<,'>", 181 select the Visual area |:star|. 182 183 *:@:* 184:[addr]@: Repeat last command-line. First set cursor at line 185 [addr] (default is current line). 186 187:[addr]@ *:@@* 188:[addr]@@ Repeat the previous :@{register}. First set cursor at 189 line [addr] (default is current line). 190 191============================================================================== 1924. Using Vim scripts *using-scripts* 193 194For writing a Vim script, see chapter 41 of the user manual |usr_41.txt|. 195 196 *:so* *:source* *load-vim-script* 197:so[urce] {file} Read Ex commands from {file}. These are commands that 198 start with a ":". 199 Triggers the |SourcePre| autocommand. 200 *:source!* 201:so[urce]! {file} Read Vim commands from {file}. These are commands 202 that are executed from Normal mode, like you type 203 them. 204 When used after |:global|, |:argdo|, |:windo|, 205 |:bufdo|, in a loop or when another command follows 206 the display won't be updated while executing the 207 commands. 208 Cannot be used in the |sandbox|. 209 210 *:ru* *:runtime* 211:ru[ntime][!] [where] {file} .. 212 Read Ex commands from {file} in each directory given 213 by 'runtimepath' and/or 'packpath'. There is no error 214 for non-existing files. 215 216 Example: > 217 :runtime syntax/c.vim 218 219< There can be multiple {file} arguments, separated by 220 spaces. Each {file} is searched for in the first 221 directory from 'runtimepath', then in the second 222 directory, etc. Use a backslash to include a space 223 inside {file} (although it's better not to use spaces 224 in file names, it causes trouble). 225 226 When [!] is included, all found files are sourced. 227 When it is not included only the first found file is 228 sourced. 229 230 When [where] is omitted only 'runtimepath' is used. 231 Other values: 232 START search under "start" in 'packpath' 233 OPT search under "opt" in 'packpath' 234 PACK search under "start" and "opt" in 235 'packpath' 236 ALL first use 'runtimepath', then search 237 under "start" and "opt" in 'packpath' 238 239 When {file} contains wildcards it is expanded to all 240 matching files. Example: > 241 :runtime! plugin/**/*.vim 242< This is what Vim uses to load the plugin files when 243 starting up. This similar command: > 244 :runtime plugin/**/*.vim 245< would source the first file only. 246 247 When 'verbose' is one or higher, there is a message 248 when no file could be found. 249 When 'verbose' is two or higher, there is a message 250 about each searched file. 251 252 *:pa* *:packadd* *E919* 253:pa[ckadd][!] {name} Search for an optional plugin directory in 'packpath' 254 and source any plugin files found. The directory must 255 match: 256 pack/*/opt/{name} ~ 257 The directory is added to 'runtimepath' if it wasn't 258 there yet. 259 If the directory pack/*/opt/{name}/after exists it is 260 added at the end of 'runtimepath'. 261 262 If loading packages from "pack/*/start" was skipped, 263 then this directory is searched first: 264 pack/*/start/{name} ~ 265 266 Note that {name} is the directory name, not the name 267 of the .vim file. All the files matching the pattern 268 pack/*/opt/{name}/plugin/**/*.vim ~ 269 will be sourced. This allows for using subdirectories 270 below "plugin", just like with plugins in 271 'runtimepath'. 272 273 If the filetype detection was not enabled yet (this 274 is usually done with a "syntax enable" or "filetype 275 on" command in your .vimrc file), this will also look 276 for "{name}/ftdetect/*.vim" files. 277 278 When the optional ! is added no plugin files or 279 ftdetect scripts are loaded, only the matching 280 directories are added to 'runtimepath'. This is 281 useful in your .vimrc. The plugins will then be 282 loaded during initialization, see |load-plugins| (note 283 that the loading order will be reversed, because each 284 directory is inserted before others). 285 Note that for ftdetect scripts to be loaded 286 you will need to write `filetype plugin indent on` 287 AFTER all `packadd!` commands. 288 289 Also see |pack-add|. 290 {only available when compiled with |+eval|} 291 292 *:packl* *:packloadall* 293:packl[oadall][!] Load all packages in the "start" directory under each 294 entry in 'packpath'. 295 296 First all the directories found are added to 297 'runtimepath', then the plugins found in the 298 directories are sourced. This allows for a plugin to 299 depend on something of another plugin, e.g. an 300 "autoload" directory. See |packload-two-steps| for 301 how this can be useful. 302 303 This is normally done automatically during startup, 304 after loading your .vimrc file. With this command it 305 can be done earlier. 306 307 Packages will be loaded only once. Using 308 `:packloadall` a second time will have no effect. 309 When the optional ! is added this command will load 310 packages even when done before. 311 312 Note that when using `:packloadall` in the |vimrc| 313 file, the 'runtimepath' option is updated, and later 314 all plugins in 'runtimepath' will be loaded, which 315 means they are loaded again. Plugins are expected to 316 handle that. 317 318 An error only causes sourcing the script where it 319 happens to be aborted, further plugins will be loaded. 320 See |packages|. 321 {only available when compiled with |+eval|} 322 323:scripte[ncoding] [encoding] *:scripte* *:scriptencoding* *E167* 324 Specify the character encoding used in the script. 325 The following lines will be converted from [encoding] 326 to the value of the 'encoding' option, if they are 327 different. Examples: > 328 scriptencoding iso-8859-5 329 scriptencoding cp932 330< 331 When [encoding] is empty, no conversion is done. This 332 can be used to restrict conversion to a sequence of 333 lines: > 334 scriptencoding euc-jp 335 ... lines to be converted ... 336 scriptencoding 337 ... not converted ... 338 339< When conversion isn't supported by the system, there 340 is no error message and no conversion is done. When a 341 line can't be converted there is no error and the 342 original line is kept. 343 344 Don't use "ucs-2" or "ucs-4", scripts cannot be in 345 these encodings (they would contain NUL bytes). 346 When a sourced script starts with a BOM (Byte Order 347 Mark) in utf-8 format Vim will recognize it, no need 348 to use ":scriptencoding utf-8" then. 349 350 If you set the 'encoding' option in your |.vimrc|, 351 `:scriptencoding` must be placed after that. E.g.: > 352 set encoding=utf-8 353 scriptencoding utf-8 354< 355 356:scriptv[ersion] {version} *:scriptv* *:scriptversion* 357 *E999* *E984* 358 Specify the version of Vim for the lines that follow 359 in the same file. Only applies at the toplevel of 360 sourced scripts, not inside functions. 361 362 If {version} is higher than what the current Vim 363 version supports E999 will be given. You either need 364 to rewrite the script to make it work with an older 365 Vim version, or update Vim to a newer version. See 366 |vimscript-version| for what changed between versions. 367 368:vim9s[cript] [noclear] *:vim9s* *:vim9script* 369 Marks a script file as containing |Vim9-script| 370 commands. Also see |vim9-namespace|. 371 Must be the first command in the file. 372 For [noclear] see |vim9-reload|. 373 Without the |+eval| feature this changes the syntax 374 for some commands. 375 See |:vim9cmd| for executing one command with Vim9 376 syntax and semantics. 377 378 *:scr* *:scriptnames* 379:scr[iptnames] List all sourced script names, in the order they were 380 first sourced. The number is used for the script ID 381 |<SID>|. 382 {not available when compiled without the |+eval| 383 feature} 384 385:scr[iptnames][!] {scriptId} *:script* 386 Edit script {scriptId}. Although ":scriptnames name" 387 works, using ":script name" is recommended. 388 When the current buffer can't be |abandon|ed and the ! 389 is not present, the command fails. 390 391 *:fini* *:finish* *E168* 392:fini[sh] Stop sourcing a script. Can only be used in a Vim 393 script file. This is a quick way to skip the rest of 394 the file. If it is used after a |:try| but before the 395 matching |:finally| (if present), the commands 396 following the ":finally" up to the matching |:endtry| 397 are executed first. This process applies to all 398 nested ":try"s in the script. The outermost ":endtry" 399 then stops sourcing the script. 400 401All commands and command sequences can be repeated by putting them in a named 402register and then executing it. There are two ways to get the commands in the 403register: 404- Use the record command "q". You type the commands once, and while they are 405 being executed they are stored in a register. Easy, because you can see 406 what you are doing. If you make a mistake, "p"ut the register into the 407 file, edit the command sequence, and then delete it into the register 408 again. You can continue recording by appending to the register (use an 409 uppercase letter). 410- Delete or yank the command sequence into the register. 411 412Often used command sequences can be put under a function key with the ':map' 413command. 414 415An alternative is to put the commands in a file, and execute them with the 416':source!' command. Useful for long command sequences. Can be combined with 417the ':map' command to put complicated commands under a function key. 418 419The ':source' command reads Ex commands from a file line by line. You will 420have to type any needed keyboard input. The ':source!' command reads from a 421script file character by character, interpreting each character as if you 422typed it. 423 424Example: When you give the ":!ls" command you get the |hit-enter| prompt. If 425you ':source' a file with the line "!ls" in it, you will have to type the 426<Enter> yourself. But if you ':source!' a file with the line ":!ls" in it, 427the next characters from that file are read until a <CR> is found. You will 428not have to type <CR> yourself, unless ":!ls" was the last line in the file. 429 430It is possible to put ':source[!]' commands in the script file, so you can 431make a top-down hierarchy of script files. The ':source' command can be 432nested as deep as the number of files that can be opened at one time (about 43315). The ':source!' command can be nested up to 15 levels deep. 434 435You can use the "<sfile>" string (literally, this is not a special key) inside 436of the sourced file, in places where a file name is expected. It will be 437replaced by the file name of the sourced file. For example, if you have a 438"other.vimrc" file in the same directory as your ".vimrc" file, you can source 439it from your ".vimrc" file with this command: > 440 :source <sfile>:h/other.vimrc 441 442In script files terminal-dependent key codes are represented by 443terminal-independent two character codes. This means that they can be used 444in the same way on different kinds of terminals. The first character of a 445key code is 0x80 or 128, shown on the screen as "~@". The second one can be 446found in the list |key-notation|. Any of these codes can also be entered 447with CTRL-V followed by the three digit decimal code. This does NOT work for 448the <t_xx> termcap codes, these can only be used in mappings. 449 450 *:source_crnl* *W15* 451Win32: Files that are read with ":source" normally have <CR><NL> <EOL>s. 452These always work. If you are using a file with <NL> <EOL>s (for example, a 453file made on Unix), this will be recognized if 'fileformats' is not empty and 454the first line does not end in a <CR>. This fails if the first line has 455something like ":map <F1> :help^M", where "^M" is a <CR>. If the first line 456ends in a <CR>, but following ones don't, you will get an error message, 457because the <CR> from the first lines will be lost. 458 459Mac Classic: Files that are read with ":source" normally have <CR> <EOL>s. 460These always work. If you are using a file with <NL> <EOL>s (for example, a 461file made on Unix), this will be recognized if 'fileformats' is not empty and 462the first line does not end in a <CR>. Be careful not to use a file with <NL> 463linebreaks which has a <CR> in first line. 464 465On other systems, Vim expects ":source"ed files to end in a <NL>. These 466always work. If you are using a file with <CR><NL> <EOL>s (for example, a 467file made on MS-Windows), all lines will have a trailing <CR>. This may cause 468problems for some commands (e.g., mappings). There is no automatic <EOL> 469detection, because it's common to start with a line that defines a mapping 470that ends in a <CR>, which will confuse the automaton. 471 472 *line-continuation* 473Long lines in a ":source"d Ex command script file can be split by inserting 474a line continuation symbol "\" (backslash) at the start of the next line. 475There can be white space before the backslash, which is ignored. 476 477Example: the lines > 478 :set comments=sr:/*,mb:*,el:*/, 479 \://, 480 \b:#, 481 \:%, 482 \n:>, 483 \fb:- 484are interpreted as if they were given in one line: 485 :set comments=sr:/*,mb:*,el:*/,://,b:#,:%,n:>,fb:- 486 487All leading whitespace characters in the line before a backslash are ignored. 488Note however that trailing whitespace in the line before it cannot be 489inserted freely; it depends on the position where a command is split up 490whether additional whitespace is allowed or not. 491 492When a space is required it's best to put it right after the backslash. A 493space at the end of a line is hard to see and may be accidentally deleted. > 494 :syn match Comment 495 \ "very long regexp" 496 \ keepend 497 498There is a problem with the ":append" and ":insert" commands: > 499 :1append 500 \asdf 501 . 502The backslash is seen as a line-continuation symbol, thus this results in the 503command: > 504 :1appendasdf 505 . 506To avoid this, add the 'C' flag to the 'cpoptions' option: > 507 :set cpo+=C 508 :1append 509 \asdf 510 . 511 :set cpo-=C 512 513Note that when the commands are inside a function, you need to add the 'C' 514flag when defining the function, it is not relevant when executing it. > 515 :set cpo+=C 516 :function Foo() 517 :1append 518 \asdf 519 . 520 :endfunction 521 :set cpo-=C 522< 523 *line-continuation-comment* 524To add a comment in between the lines start with '"\ '. Notice the space 525after the backslash. Example: > 526 let array = [ 527 "\ first entry comment 528 \ 'first', 529 "\ second entry comment 530 \ 'second', 531 \ ] 532 533Rationale: 534 Most programs work with a trailing backslash to indicate line 535 continuation. Using this in Vim would cause incompatibility with Vi. 536 For example for this Vi mapping: > 537 :map xx asdf\ 538< Therefore the unusual leading backslash is used. 539 540 Starting a comment in a continuation line results in all following 541 continuation lines to be part of the comment. Since it was like this 542 for a long time, when making it possible to add a comment halfway a 543 sequence of continuation lines, it was not possible to use \", since 544 that was a valid continuation line. Using '"\ ' comes closest, even 545 though it may look a bit weird. Requiring the space after the 546 backslash is to make it very unlikely this is a normal comment line. 547 548============================================================================== 5495. Using Vim packages *packages* 550 551A Vim package is a directory that contains one or more plugins. The 552advantages over normal plugins: 553- A package can be downloaded as an archive and unpacked in its own directory. 554 Thus the files are not mixed with files of other plugins. That makes it 555 easy to update and remove. 556- A package can be a git, mercurial, etc. repository. That makes it really 557 easy to update. 558- A package can contain multiple plugins that depend on each other. 559- A package can contain plugins that are automatically loaded on startup and 560 ones that are only loaded when needed with `:packadd`. 561 562 563Using a package and loading automatically ~ 564 565Let's assume your Vim files are in the "~/.vim" directory and you want to add a 566package from a zip archive "/tmp/foopack.zip": 567 % mkdir -p ~/.vim/pack/foo 568 % cd ~/.vim/pack/foo 569 % unzip /tmp/foopack.zip 570 571The directory name "foo" is arbitrary, you can pick anything you like. 572 573You would now have these files under ~/.vim: 574 pack/foo/README.txt 575 pack/foo/start/foobar/plugin/foo.vim 576 pack/foo/start/foobar/syntax/some.vim 577 pack/foo/opt/foodebug/plugin/debugger.vim 578 579When Vim starts up, after processing your .vimrc, it scans all directories in 580'packpath' for plugins under the "pack/*/start" directory. First all those 581directories are added to 'runtimepath'. Then all the plugins are loaded. 582See |packload-two-steps| for how these two steps can be useful. 583 584In the example Vim will find "pack/foo/start/foobar/plugin/foo.vim" and adds 585"~/.vim/pack/foo/start/foobar" to 'runtimepath'. 586 587If the "foobar" plugin kicks in and sets the 'filetype' to "some", Vim will 588find the syntax/some.vim file, because its directory is in 'runtimepath'. 589 590Vim will also load ftdetect files, if there are any. 591 592Note that the files under "pack/foo/opt" are not loaded automatically, only the 593ones under "pack/foo/start". See |pack-add| below for how the "opt" directory 594is used. 595 596Loading packages automatically will not happen if loading plugins is disabled, 597see |load-plugins|. 598 599To load packages earlier, so that 'runtimepath' gets updated: > 600 :packloadall 601This also works when loading plugins is disabled. The automatic loading will 602only happen once. 603 604If the package has an "after" directory, that directory is added to the end of 605'runtimepath', so that anything there will be loaded later. 606 607 608Using a single plugin and loading it automatically ~ 609 610If you don't have a package but a single plugin, you need to create the extra 611directory level: 612 % mkdir -p ~/.vim/pack/foo/start/foobar 613 % cd ~/.vim/pack/foo/start/foobar 614 % unzip /tmp/someplugin.zip 615 616You would now have these files: 617 pack/foo/start/foobar/plugin/foo.vim 618 pack/foo/start/foobar/syntax/some.vim 619 620From here it works like above. 621 622 623Optional plugins ~ 624 *pack-add* 625To load an optional plugin from a pack use the `:packadd` command: > 626 :packadd foodebug 627This searches for "pack/*/opt/foodebug" in 'packpath' and will find 628~/.vim/pack/foo/opt/foodebug/plugin/debugger.vim and source it. 629 630This could be done if some conditions are met. For example, depending on 631whether Vim supports a feature or a dependency is missing. 632 633You can also load an optional plugin at startup, by putting this command in 634your |.vimrc|: > 635 :packadd! foodebug 636The extra "!" is so that the plugin isn't loaded if Vim was started with 637|--noplugin|. 638 639It is perfectly normal for a package to only have files in the "opt" 640directory. You then need to load each plugin when you want to use it. 641 642 643Where to put what ~ 644 645Since color schemes, loaded with `:colorscheme`, are found below 646"pack/*/start" and "pack/*/opt", you could put them anywhere. We recommend 647you put them below "pack/*/opt", for example 648".vim/pack/mycolors/opt/dark/colors/very_dark.vim". 649 650Filetype plugins should go under "pack/*/start", so that they are always 651found. Unless you have more than one plugin for a file type and want to 652select which one to load with `:packadd`. E.g. depending on the compiler 653version: > 654 if foo_compiler_version > 34 655 packadd foo_new 656 else 657 packadd foo_old 658 endif 659 660The "after" directory is most likely not useful in a package. It's not 661disallowed though. 662 663============================================================================== 6646. Creating Vim packages *package-create* 665 666This assumes you write one or more plugins that you distribute as a package. 667 668If you have two unrelated plugins you would use two packages, so that Vim 669users can choose what they include or not. Or you can decide to use one 670package with optional plugins, and tell the user to add the preferred ones with 671`:packadd`. 672 673Decide how you want to distribute the package. You can create an archive or 674you could use a repository. An archive can be used by more users, but is a 675bit harder to update to a new version. A repository can usually be kept 676up-to-date easily, but it requires a program like "git" to be available. 677You can do both, github can automatically create an archive for a release. 678 679Your directory layout would be like this: 680 start/foobar/plugin/foo.vim " always loaded, defines commands 681 start/foobar/plugin/bar.vim " always loaded, defines commands 682 start/foobar/autoload/foo.vim " loaded when foo command used 683 start/foobar/doc/foo.txt " help for foo.vim 684 start/foobar/doc/tags " help tags 685 opt/fooextra/plugin/extra.vim " optional plugin, defines commands 686 opt/fooextra/autoload/extra.vim " loaded when extra command used 687 opt/fooextra/doc/extra.txt " help for extra.vim 688 opt/fooextra/doc/tags " help tags 689 690This allows for the user to do: > 691 mkdir ~/.vim/pack 692 cd ~/.vim/pack 693 git clone https://github.com/you/foobar.git myfoobar 694 695Here "myfoobar" is a name that the user can choose, the only condition is that 696it differs from other packages. 697 698In your documentation you explain what the plugins do, and tell the user how 699to load the optional plugin: > 700 :packadd! fooextra 701 702You could add this packadd command in one of your plugins, to be executed when 703the optional plugin is needed. 704 705Run the `:helptags` command to generate the doc/tags file. Including this 706generated file in the package means that the user can drop the package in the 707pack directory and the help command works right away. Don't forget to re-run 708the command after changing the plugin help: > 709 :helptags path/start/foobar/doc 710 :helptags path/opt/fooextra/doc 711 712 713Dependencies between plugins ~ 714 *packload-two-steps* 715Suppose you have two plugins that depend on the same functionality. You can 716put the common functionality in an autoload directory, so that it will be 717found automatically. Your package would have these files: 718 719 pack/foo/start/one/plugin/one.vim > 720 call foolib#getit() 721< pack/foo/start/two/plugin/two.vim > 722 call foolib#getit() 723< pack/foo/start/lib/autoload/foolib.vim > 724 func foolib#getit() 725 726This works, because loading packages will first add all found directories to 727'runtimepath' before sourcing the plugins. 728 729============================================================================== 7307. Debugging scripts *debug-scripts* 731 732Besides the obvious messages that you can add to your scripts to find out what 733they are doing, Vim offers a debug mode. This allows you to step through a 734sourced file or user function and set breakpoints. 735 736NOTE: The debugging mode is far from perfect. Debugging will have side 737effects on how Vim works. You cannot use it to debug everything. For 738example, the display is messed up by the debugging messages. 739 740An alternative to debug mode is setting the 'verbose' option. With a bigger 741number it will give more verbose messages about what Vim is doing. 742 743 744STARTING DEBUG MODE *debug-mode* 745 746To enter debugging mode use one of these methods: 7471. Start Vim with the |-D| argument: > 748 vim -D file.txt 749< Debugging will start as soon as the first vimrc file is sourced. This is 750 useful to find out what is happening when Vim is starting up. A side 751 effect is that Vim will switch the terminal mode before initialisations 752 have finished, with unpredictable results. 753 For a GUI-only version (Windows, Macintosh) the debugging will start as 754 soon as the GUI window has been opened. To make this happen early, add a 755 ":gui" command in the vimrc file. 756 *:debug* 7572. Run a command with ":debug" prepended. Debugging will only be done while 758 this command executes. Useful for debugging a specific script or user 759 function. And for scripts and functions used by autocommands. Example: > 760 :debug edit test.txt.gz 761 7623. Set a breakpoint in a sourced file or user function. You could do this in 763 the command line: > 764 vim -c "breakadd file */explorer.vim" . 765< This will run Vim and stop in the first line of the "explorer.vim" script. 766 Breakpoints can also be set while in debugging mode. 767 768In debugging mode every executed command is displayed before it is executed. 769Comment lines, empty lines and lines that are not executed are skipped. When 770a line contains two commands, separated by "|", each command will be displayed 771separately. 772 773 774DEBUG MODE 775 776Once in debugging mode, the usual Ex commands can be used. For example, to 777inspect the value of a variable: > 778 echo idx 779When inside a user function, this will print the value of the local variable 780"idx". Prepend "g:" to get the value of a global variable: > 781 echo g:idx 782All commands are executed in the context of the current function or script. 783You can also set options, for example setting or resetting 'verbose' will show 784what happens, but you might want to set it just before executing the lines you 785are interested in: > 786 :set verbose=20 787 788Commands that require updating the screen should be avoided, because their 789effect won't be noticed until after leaving debug mode. For example: > 790 :help 791won't be very helpful. 792 793There is a separate command-line history for debug mode. 794 795The line number for a function line is relative to the start of the function. 796If you have trouble figuring out where you are, edit the file that defines 797the function in another Vim, search for the start of the function and do 798"99j". Replace "99" with the line number. 799 800Additionally, these commands can be used: 801 *>cont* 802 cont Continue execution until the next breakpoint is hit. 803 *>quit* 804 quit Abort execution. This is like using CTRL-C, some 805 things might still be executed, doesn't abort 806 everything. Still stops at the next breakpoint. 807 *>next* 808 next Execute the command and come back to debug mode when 809 it's finished. This steps over user function calls 810 and sourced files. 811 *>step* 812 step Execute the command and come back to debug mode for 813 the next command. This steps into called user 814 functions and sourced files. 815 *>interrupt* 816 interrupt This is like using CTRL-C, but unlike ">quit" comes 817 back to debug mode for the next command that is 818 executed. Useful for testing |:finally| and |:catch| 819 on interrupt exceptions. 820 *>finish* 821 finish Finish the current script or user function and come 822 back to debug mode for the command after the one that 823 sourced or called it. 824 *>bt* 825 *>backtrace* 826 *>where* 827 backtrace Show the call stacktrace for current debugging session. 828 bt 829 where 830 *>frame* 831 frame N Goes to N backtrace level. + and - signs make movement 832 relative. E.g., ":frame +3" goes three frames up. 833 *>up* 834 up Goes one level up from call stacktrace. 835 *>down* 836 down Goes one level down from call stacktrace. 837 838About the additional commands in debug mode: 839- There is no command-line completion for them, you get the completion for the 840 normal Ex commands only. 841- You can shorten them, up to a single character, unless more than one command 842 starts with the same letter. "f" stands for "finish", use "fr" for "frame". 843- Hitting <CR> will repeat the previous one. When doing another command, this 844 is reset (because it's not clear what you want to repeat). 845- When you want to use the Ex command with the same name, prepend a colon: 846 ":cont", ":next", ":finish" (or shorter). 847 *vim9-debug* 848When debugging a compiled :def function, "step" will stop before every 849executed line, not every single instruction. Thus it works mostly like a not 850compiled function. Access to local variables is limited you can use: > 851 echo varname 852But not much else. 853When executing a command that is not a specific bytecode instruction but 854executed like a normal Ex command, "step" will stop once in the compiled 855context, where local variables can be inspected, and once just before 856executing the command. 857 858The backtrace shows the hierarchy of function calls, e.g.: 859 >bt ~ 860 3 function One[3] ~ 861 2 Two[3] ~ 862 ->1 Three[3] ~ 863 0 Four ~ 864 line 1: let four = 4 ~ 865 866The "->" points to the current frame. Use "up", "down" and "frame N" to 867select another frame. 868 869In the current frame you can evaluate the local function variables. There is 870no way to see the command at the current line yet. 871 872 873DEFINING BREAKPOINTS 874 *:breaka* *:breakadd* 875:breaka[dd] func [lnum] {name} 876 Set a breakpoint in a function. Example: > 877 :breakadd func Explore 878< Doesn't check for a valid function name, thus the breakpoint 879 can be set before the function is defined. 880 881:breaka[dd] file [lnum] {name} 882 Set a breakpoint in a sourced file. Example: > 883 :breakadd file 43 .vimrc 884 885:breaka[dd] here 886 Set a breakpoint in the current line of the current file. 887 Like doing: > 888 :breakadd file <cursor-line> <current-file> 889< Note that this only works for commands that are executed when 890 sourcing the file, not for a function defined in that file. 891 892:breaka[dd] expr {expression} 893 Sets a breakpoint, that will break whenever the {expression} 894 evaluates to a different value. Example: > 895 :breakadd expr g:lnum 896< Will break, whenever the global variable lnum changes. 897 898 Errors in evaluation are suppressed, you can use the name of a 899 variable that does not exist yet. This also means you will 900 not notice anything if the expression has a mistake. 901 902 Note if you watch a |script-variable| this will break 903 when switching scripts, since the script variable is only 904 valid in the script where it has been defined and if that 905 script is called from several other scripts, this will stop 906 whenever that particular variable will become visible or 907 inaccessible again. 908 909The [lnum] is the line number of the breakpoint. Vim will stop at or after 910this line. When omitted line 1 is used. 911 912 *:debug-name* 913{name} is a pattern that is matched with the file or function name. The 914pattern is like what is used for autocommands. There must be a full match (as 915if the pattern starts with "^" and ends in "$"). A "*" matches any sequence 916of characters. 'ignorecase' is not used, but "\c" can be used in the pattern 917to ignore case |/\c|. Don't include the () for the function name! 918 919The match for sourced scripts is done against the full file name. If no path 920is specified the current directory is used. Examples: > 921 breakadd file explorer.vim 922matches "explorer.vim" in the current directory. > 923 breakadd file *explorer.vim 924matches ".../plugin/explorer.vim", ".../plugin/iexplorer.vim", etc. > 925 breakadd file */explorer.vim 926matches ".../plugin/explorer.vim" and "explorer.vim" in any other directory. 927 928The match for functions is done against the name as it's shown in the output 929of ":function". However, for local functions the script-specific prefix such 930as "<SNR>99_" is ignored to make it easier to match script-local functions 931without knowing the ID of the script. 932 933Note that functions are first loaded and later executed. When they are loaded 934the "file" breakpoints are checked, when they are executed the "func" 935breakpoints. 936 937 938DELETING BREAKPOINTS 939 *:breakd* *:breakdel* *E161* 940:breakd[el] {nr} 941 Delete breakpoint {nr}. Use |:breaklist| to see the number of 942 each breakpoint. 943 944:breakd[el] * 945 Delete all breakpoints. 946 947:breakd[el] func [lnum] {name} 948 Delete a breakpoint in a function. 949 950:breakd[el] file [lnum] {name} 951 Delete a breakpoint in a sourced file. 952 953:breakd[el] here 954 Delete a breakpoint at the current line of the current file. 955 956When [lnum] is omitted, the first breakpoint in the function or file is 957deleted. 958The {name} must be exactly the same as what was typed for the ":breakadd" 959command. "explorer", "*explorer.vim" and "*explorer*" are different. 960 961 962LISTING BREAKPOINTS 963 *:breakl* *:breaklist* 964:breakl[ist] 965 List all breakpoints. 966 967 968OBSCURE 969 970 *:debugg* *:debuggreedy* 971:debugg[reedy] 972 Read debug mode commands from the normal input stream, instead 973 of getting them directly from the user. Only useful for test 974 scripts. Example: > 975 echo 'q^Mq' | vim -e -s -c debuggreedy -c 'breakadd file script.vim' -S script.vim 976 977:0debugg[reedy] 978 Undo ":debuggreedy": get debug mode commands directly from the 979 user, don't use typeahead for debug commands. 980 981============================================================================== 9828. Profiling *profile* *profiling* 983 984Profiling means that Vim measures the time that is spent on executing 985functions and/or scripts. The |+profile| feature is required for this. 986It is included when Vim was compiled with "huge" features. 987 988You can also use the |reltime()| function to measure time. This only requires 989the |+reltime| feature, which is present in more builds. 990 991For profiling syntax highlighting see |:syntime|. 992 993For example, to profile the one_script.vim script file: > 994 :profile start /tmp/one_script_profile 995 :profile file one_script.vim 996 :source one_script.vim 997 :exit 998 999 1000:prof[ile] start {fname} *:prof* *:profile* *E750* 1001 Start profiling, write the output in {fname} upon exit. 1002 "~/" and environment variables in {fname} will be expanded. 1003 If {fname} already exists it will be silently overwritten. 1004 The variable |v:profiling| is set to one. 1005 1006:prof[ile] pause 1007 Don't profile until the following ":profile continue". Can be 1008 used when doing something that should not be counted (e.g., an 1009 external command). Does not nest. 1010 1011:prof[ile] continue 1012 Continue profiling after ":profile pause". 1013 1014:prof[ile] func {pattern} 1015 Profile function that matches the pattern {pattern}. 1016 See |:debug-name| for how {pattern} is used. 1017 1018:prof[ile][!] file {pattern} 1019 Profile script file that matches the pattern {pattern}. 1020 See |:debug-name| for how {pattern} is used. 1021 This only profiles the script itself, not the functions 1022 defined in it. 1023 When the [!] is added then all functions defined in the script 1024 will also be profiled. 1025 Note that profiling only starts when the script is loaded 1026 after this command. A :profile command in the script itself 1027 won't work. 1028 1029 1030:profd[el] ... *:profd* *:profdel* 1031 Stop profiling for the arguments specified. See |:breakdel| 1032 for the arguments. 1033 1034 1035You must always start with a ":profile start fname" command. The resulting 1036file is written when Vim exits. For example, to profile one specific 1037function: > 1038 profile start /tmp/vimprofile 1039 profile func MyFunc 1040 1041Here is an example of the output, with line 1042numbers prepended for the explanation: 1043 1044 1 FUNCTION Test2() ~ 1045 2 Called 1 time ~ 1046 3 Total time: 0.155251 ~ 1047 4 Self time: 0.002006 ~ 1048 5 ~ 1049 6 count total (s) self (s) ~ 1050 7 9 0.000096 for i in range(8) ~ 1051 8 8 0.153655 0.000410 call Test3() ~ 1052 9 8 0.000070 endfor ~ 1053 10 " Ask a question ~ 1054 11 1 0.001341 echo input("give me an answer: ") ~ 1055 1056The header (lines 1-4) gives the time for the whole function. The "Total" 1057time is the time passed while the function was executing. The "Self" time is 1058the "Total" time reduced by time spent in: 1059- other user defined functions 1060- sourced scripts 1061- executed autocommands 1062- external (shell) commands 1063 1064Lines 7-11 show the time spent in each executed line. Lines that are not 1065executed do not count. Thus a comment line is never counted. 1066 1067The Count column shows how many times a line was executed. Note that the 1068"for" command in line 7 is executed one more time as the following lines. 1069That is because the line is also executed to detect the end of the loop. 1070 1071The time Vim spends waiting for user input isn't counted at all. Thus how 1072long you take to respond to the input() prompt is irrelevant. 1073 1074Profiling should give a good indication of where time is spent, but keep in 1075mind there are various things that may clobber the results: 1076 1077- The accuracy of the time measured depends on the gettimeofday() system 1078 function. It may only be as accurate as 1/100 second, even though the times 1079 are displayed in micro seconds. 1080 1081- Real elapsed time is measured, if other processes are busy they may cause 1082 delays at unpredictable moments. You may want to run the profiling several 1083 times and use the lowest results. 1084 1085- If you have several commands in one line you only get one time. Split the 1086 line to see the time for the individual commands. 1087 1088- The time of the lines added up is mostly less than the time of the whole 1089 function. There is some overhead in between. 1090 1091- Functions that are deleted before Vim exits will not produce profiling 1092 information. You can check the |v:profiling| variable if needed: > 1093 :if !v:profiling 1094 : delfunc MyFunc 1095 :endif 1096< 1097- Profiling may give weird results on multi-processor systems, when sleep 1098 mode kicks in or the processor frequency is reduced to save power. 1099 1100- The "self" time is wrong when a function is used recursively. 1101 1102 1103 vim:tw=78:ts=8:noet:ft=help:norl: 1104