1*repeat.txt* For Vim version 7.0aa. Last change: 2004 Jun 22 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. Debugging scripts |debug-scripts| 16 17============================================================================== 181. Single repeats *single-repeat* 19 20 *.* 21. Repeat last change, with count replaced with [count]. 22 Also repeat a yank command, when the 'y' flag is 23 included in 'cpoptions'. 24 25Simple changes can be repeated with the "." command. Without a count, the 26count of the last change is used. If you enter a count, it will replace the 27last one. If the last change included a specification of a numbered register, 28the register number will be incremented. See |redo-register| for an example 29how to use this. Note that when repeating a command that used a Visual 30selection, the same SIZE of area is used, see |visual-repeat|. 31 32 *@:* 33@: Repeat last command-line [count] times. 34 {not available when compiled without the 35 |+cmdline_hist| feature} 36 37 38============================================================================== 392. Multiple repeats *multi-repeat* 40 41 *:g* *:global* *E147* *E148* 42:[range]g[lobal]/{pattern}/[cmd] 43 Execute the Ex command [cmd] (default ":p") on the 44 lines within [range] where {pattern} matches. 45 46:[range]g[lobal]!/{pattern}/[cmd] 47 Execute the Ex command [cmd] (default ":p") on the 48 lines within [range] where {pattern} does NOT match. 49 50 *:v* *:vglobal* 51:[range]v[global]/{pattern}/[cmd] 52 Same as :g!. 53 54The global commands work by first scanning through the [range] lines and 55marking each line where a match occurs (for a multi-line pattern, only the 56start of the match matters). 57In a second scan the [cmd] is executed for each marked line with its line 58number prepended. For ":v" and ":g!" the command is executed for each not 59marked line. If a line is deleted its mark disappears. 60The default for [range] is the whole buffer (1,$). Use "CTRL-C" to interrupt 61the command. If an error message is given for a line, the command for that 62line is aborted and the global command continues with the next marked or 63unmarked line. 64 65To repeat a non-Ex command, you can use the ":normal" command: > 66 :g/pat/normal {commands} 67Make sure that {commands} ends with a whole command, otherwise Vim will wait 68for you to type the rest of the command for each match. The screen will not 69have been updated, so you don't know what you are doing. See |:normal|. 70 71The undo/redo command will undo/redo the whole global command at once. 72The previous context mark will only be set once (with "''" you go back to 73where the cursor was before the global command). 74 75The global command sets both the last used search pattern and the last used 76substitute pattern (this is vi compatible). This makes it easy to globally 77replace a string: 78 :g/pat/s//PAT/g 79This replaces all occurrences of "pat" with "PAT". The same can be done with: 80 :%s/pat/PAT/g 81Which is two characters shorter! 82 83============================================================================== 843. Complex repeats *complex-repeat* 85 86 *q* *recording* 87q{0-9a-zA-Z"} Record typed characters into register {0-9a-zA-Z"} 88 (uppercase to append). The 'q' command is disabled 89 while executing a register, and it doesn't work inside 90 a mapping. {Vi: no recording} 91 92q Stops recording. (Implementation note: The 'q' that 93 stops recording is not stored in the register, unless 94 it was the result of a mapping) {Vi: no recording} 95 96 *@* 97@{0-9a-z".=*} Execute the contents of register {0-9a-z".=*} [count] 98 times. Note that register '%' (name of the current 99 file) and '#' (name of the alternate file) cannot be 100 used. For "@=" you are prompted to enter an 101 expression. The result of the expression is then 102 executed. See also |@:|. {Vi: only named registers} 103 104 *@@* 105@@ Repeat the previous @{0-9a-z":*} [count] times. 106 107:[addr]*{0-9a-z".=} *:@* *:star* 108:[addr]@{0-9a-z".=*} Execute the contents of register {0-9a-z".=*} as an Ex 109 command. First set cursor at line [addr] (default is 110 current line). When the last line in the register does 111 not have a <CR> it will be added automatically when 112 the 'e' flag is present in 'cpoptions'. 113 Note that the ":*" command is only recognized when the 114 '*' flag is present in 'cpoptions'. This is NOT the 115 default when 'nocompatible' is used. 116 For ":@=" the last used expression is used. The 117 result of evaluating the expression is executed as an 118 Ex command. 119 Mappings are not recognized in these commands. 120 {Vi: only in some versions} Future: Will execute the 121 register for each line in the address range. 122 123 *:@:* 124:[addr]@: Repeat last command-line. First set cursor at line 125 [addr] (default is current line). {not in Vi} 126 127 *:@@* 128:[addr]@@ Repeat the previous :@{0-9a-z"}. First set cursor at 129 line [addr] (default is current line). {Vi: only in 130 some versions} 131 132============================================================================== 1334. Using Vim scripts *using-scripts* 134 135For writing a Vim script, see chapter 41 of the user manual |usr_41.txt|. 136 137 *:so* *:source* *load-vim-script* 138:so[urce] {file} Read Ex commands from {file}. These are commands that 139 start with a ":". 140 141:so[urce]! {file} Read Vim commands from {file}. These are commands 142 that are executed from Normal mode, like you type 143 them. 144 When used after |:global|, |:argdo|, |:windo|, 145 |:bufdo|, in a loop or when another command follows 146 the display won't be updated while executing the 147 commands. 148 {not in Vi} 149 150 *:ru* *:runtime* 151:ru[ntime][!] {file} .. 152 Read Ex commands from {file} in each directory given 153 by 'runtimepath'. There is no error for non-existing 154 files. Example: > 155 :runtime syntax/c.vim 156 157< There can be multiple {file} arguments, separated by 158 spaces. Each {file} is searched for in the first 159 directory from 'runtimepath', then in the second 160 directory, etc. Use a backslash to include a space 161 inside {file} (although it's better not to use spaces 162 in file names, it causes trouble). 163 164 When [!] is included, all found files are sourced. 165 When it is not included only the first found file is 166 sourced. 167 168 When {file} contains wildcards it is expanded to all 169 matching files. Example: > 170 :runtime! plugin/*.vim 171< This is what Vim uses to load the plugin files when 172 starting up. This similar command: > 173 :runtime plugin/*.vim 174< would source the first file only. 175 176 When 'verbose' is one or higher, there is a message 177 when no file could be found. 178 When 'verbose' is two or higher, there is a message 179 about each searched file. 180 {not in Vi} 181 182:scripte[ncoding] [encoding] *:scripte* *:scriptencoding* *E167* 183 Specify the character encoding used in the script. 184 The following lines will be converted from [encoding] 185 to the value of the 'encoding' option, if they are 186 different. Examples: > 187 scriptencoding iso-8859-5 188 scriptencoding cp932 189< 190 When [encoding] is empty, no conversion is done. This 191 can be used to restrict conversion to a sequence of 192 lines: > 193 scriptencoding euc-jp 194 ... lines to be converted ... 195 scriptencoding 196 ... not converted ... 197 198< When conversion isn't supported by the system, there 199 is no error message and no conversion is done. 200 201 Don't use "ucs-2" or "ucs-4", scripts cannot be in 202 these encodings (they would contain NUL bytes). 203 When a sourced script starts with a BOM (Byte Order 204 Mark) in utf-8 format Vim will recognized it, no need 205 to use ":scriptencoding utf-8" then. 206 207 When compiled without the |+multi_byte| feature this 208 command is ignored. 209 {not in Vi} 210 211 *:scrip* *:scriptnames* 212:scrip[tnames] List all sourced script names, in the order they were 213 first sourced. The number is used for the script ID 214 |<SID>|. 215 {not in Vi} {not available when compiled without the 216 |+eval| feature} 217 218 *:fini* *:finish* *E168* 219:fini[sh] Stop sourcing a script. Can only be used in a Vim 220 script file. This is a quick way to skip the rest of 221 the file. If it is used after a |:try| but before the 222 matching |:finally| (if present), the commands 223 following the ":finally" up to the matching |:endtry| 224 are executed first. This process applies to all 225 nested ":try"s in the script. The outermost ":endtry" 226 then stops sourcing the script. {not in Vi} 227 228All commands and command sequences can be repeated by putting them in a named 229register and then executing it. There are two ways to get the commands in the 230register: 231- Use the record command "q". You type the commands once, and while they are 232 being executed they are stored in a register. Easy, because you can see 233 what you are doing. If you make a mistake, "p"ut the register into the 234 file, edit the command sequence, and then delete it into the register 235 again. You can continue recording by appending to the register (use an 236 uppercase letter). 237- Delete or yank the command sequence into the register. 238 239Often used command sequences can be put under a function key with the ':map' 240command. 241 242An alternative is to put the commands in a file, and execute them with the 243':source!' command. Useful for long command sequences. Can be combined with 244the ':map' command to put complicated commands under a function key. 245 246The ':source' command reads Ex commands from a file line by line. You will 247have to type any needed keyboard input. The ':source!' command reads from a 248script file character by character, interpreting each character as if you 249typed it. 250 251Example: When you give the ":!ls" command you get the |hit-enter| prompt. If 252you ':source' a file with the line "!ls" in it, you will have to type the 253<Enter> yourself. But if you ':source!' a file with the line ":!ls" in it, 254the next characters from that file are read until a <CR> is found. You will 255not have to type <CR> yourself, unless ":!ls" was the last line in the file. 256 257It is possible to put ':source[!]' commands in the script file, so you can 258make a top-down hierarchy of script files. The ':source' command can be 259nested as deep as the number of files that can be opened at one time (about 26015). The ':source!' command can be nested up to 15 levels deep. 261 262You can use the "<sfile>" string (literally, this is not a special key) inside 263of the sourced file, in places where a file name is expected. It will be 264replaced by the file name of the sourced file. For example, if you have a 265"other.vimrc" file in the same directory as your ".vimrc" file, you can source 266it from your ".vimrc" file with this command: > 267 :source <sfile>:h/other.vimrc 268 269In script files terminal-dependent key codes are represented by 270terminal-independent two character codes. This means that they can be used 271in the same way on different kinds of terminals. The first character of a 272key code is 0x80 or 128, shown on the screen as "~@". The second one can be 273found in the list |key-notation|. Any of these codes can also be entered 274with CTRL-V followed by the three digit decimal code. This does NOT work for 275the <t_xx> termcap codes, these can only be used in mappings. 276 277 *:source_crnl* *W15* 278MS-DOS, Win32 and OS/2: Files that are read with ":source" normally have 279<CR><NL> <EOL>s. These always work. If you are using a file with <NL> <EOL>s 280(for example, a file made on Unix), this will be recognized if 'fileformats' 281is not empty and the first line does not end in a <CR>. This fails if the 282first line has something like ":map <F1> :help^M", where "^M" is a <CR>. If 283the first line ends in a <CR>, but following ones don't, you will get an error 284message, because the <CR> from the first lines will be lost. 285 286Macintosh: Files that are read with ":source" normally have <CR> <EOL>s. 287These always work. If you are using a file with <NL> <EOL>s (for example, a 288file made on Unix), this will be recognized if 'fileformats' is not empty and 289the first line does not end in a <CR>. Be careful not to use a file with <NL> 290linebreaks which has a <CR> in first line. 291 292On other systems, Vim expects ":source"ed files to end in a <NL>. These 293always work. If you are using a file with <CR><NL> <EOL>s (for example, a 294file made on MS-DOS), all lines will have a trailing <CR>. This may cause 295problems for some commands (e.g., mappings). There is no automatic <EOL> 296detection, because it's common to start with a line that defines a mapping 297that ends in a <CR>, which will confuse the automaton. 298 299 *line-continuation* 300Long lines in a ":source"d Ex command script file can be split by inserting 301a line continuation symbol "\" (backslash) at the start of the next line. 302There can be white space before the backslash, which is ignored. 303 304Example: the lines > 305 :set comments=sr:/*,mb:*,el:*/, 306 \://, 307 \b:#, 308 \:%, 309 \n:>, 310 \fb:- 311are interpreted as if they were given in one line: 312 :set comments=sr:/*,mb:*,el:*/,://,b:#,:%,n:>,fb:- 313 314All leading whitespace characters in the line before a backslash are ignored. 315Note however that trailing whitespace in the line before it cannot be 316inserted freely; it depends on the position where a command is split up 317whether additional whitespace is allowed or not. 318 319There is a problem with the ":append" and ":insert" commands: > 320 :1append 321 \asdf 322 . 323The backslash is seen as a line-continuation symbol, thus this results in the 324command: > 325 :1appendasdf 326 . 327To avoid this, add the 'C' flag to the 'cpoptions' option: > 328 :set cpo+=C 329 :1append 330 \asdf 331 . 332 :set cpo-=C 333 334Note that when the commands are inside a function, you need to add the 'C' 335flag when defining the function, it is not relevant when executing it. > 336 :set cpo+=C 337 :function Foo() 338 :1append 339 \asdf 340 . 341 :endfunction 342 :set cpo-=C 343 344Rationale: 345 Most programs work with a trailing backslash to indicate line 346 continuation. Using this in Vim would cause incompatibility with Vi. 347 For example for this Vi mapping: > 348 :map xx asdf\ 349< Therefore the unusual leading backslash is used. 350 351============================================================================== 3525. Debugging scripts *debug-scripts* 353 354Besides the obvious messages that you can add to your scripts to find out what 355they are doing, Vim offers a debug mode. This allows you to step through a 356sourced file or user function and set breakpoints. 357 358NOTE: The debugging mode is far from perfect. Debugging will have side 359effects on how Vim works. You cannot use it to debug everything. For 360example, the display is messed up by the debugging messages. 361{Vi does not have a debug mode} 362 363An alternative to debug mode is setting the 'verbose' option. With a bigger 364number it will give more verbose messages about what Vim is doing. 365 366 367STARTING DEBUG MODE *debug-mode* 368 369To enter debugging mode use one of these methods: 3701. Start Vim with the |-D| argument: > 371 vim -D file.txt 372< Debugging will start as soon as the first vimrc file is sourced. This is 373 useful to find out what is happening when Vim is starting up. A side 374 effect is that Vim will switch the terminal mode before initialisations 375 have finished, with unpredictable results. 376 For a GUI-only version (Windows, Macintosh) the debugging will start as 377 soon as the GUI window has been opened. To make this happen early, add a 378 ":gui" command in the vimrc file. 379 *:debug* 3802. Run a command with ":debug" prepended. Debugging will only be done while 381 this command executes. Useful for debugging a specific script or user 382 function. And for scripts and functions used by autocommands. Example: > 383 :debug edit test.txt.gz 384 3853. Set a breakpoint in a sourced file or user function. You could do this in 386 the command line: > 387 vim -c "breakadd file */explorer.vim" . 388< This will run Vim and stop in the first line of the "explorer.vim" script. 389 Breakpoints can also be set while in debugging mode. 390 391In debugging mode every executed command is displayed before it is executed. 392Comment lines, empty lines and lines that are not executed are skipped. When 393a line contains two commands, separated by "|", each command will be displayed 394separately. 395 396 397DEBUG MODE 398 399Once in debugging mode, the usual Ex commands can be used. For example, to 400inspect the value of a variable: > 401 echo idx 402When inside a user function, this will print the value of the local variable 403"idx". Prepend "g:" to get the value of a global variable: > 404 echo g:idx 405All commands are executed in the context of the current function or script. 406You can also set options, for example setting or resetting 'verbose' will show 407what happens, but you might want to set it just before executing the lines you 408are interested in: > 409 :set verbose=20 410 411Commands that require updating the screen should be avoided, because their 412effect won't be noticed until after leaving debug mode. For example: > 413 :help 414won't be very helpful. 415 416There is a separate command-line history for debug mode. 417 418The line number for a function line is relative to the start of the function. 419If you have trouble figuring out where you are, edit the file that defines 420the function in another Vim, search for the start of the function and do 421"99j". Replace "99" with the line number. 422 423Additionally, these commands can be used: 424 *>cont* 425 cont Continue execution until the next breakpoint is hit. 426 *>quit* 427 quit Abort execution. This is like using CTRL-C, some 428 things might still be executed, doesn't abort 429 everything. Still stops at the next breakpoint. 430 *>next* 431 next Execute the command and come back to debug mode when 432 it's finished. This steps over user function calls 433 and sourced files. 434 *>step* 435 step Execute the command and come back to debug mode for 436 the next command. This steps into called user 437 functions and sourced files. 438 *>interrupt* 439 interrupt This is like using CTRL-C, but unlike ">quit" comes 440 back to debug mode for the next command that is 441 executed. Useful for testing |:finally| and |:catch| 442 on interrupt exceptions. 443 *>finish* 444 finish Finish the current script or user function and come 445 back to debug mode for the command after the one that 446 sourced or called it. 447 448About the additional commands in debug mode: 449- There is no command-line completion for them, you get the completion for the 450 normal Ex commands only. 451- You can shorten them, up to a single character: "c", "n", "s" and "f". 452- Hitting <CR> will repeat the previous one. When doing another command, this 453 is reset (because it's not clear what you want to repeat). 454- When you want to use the Ex command with the same name, prepend a colon: 455 ":cont", ":next", ":finish" (or shorter). 456 457 458DEFINING BREAKPOINTS 459 *:breaka* *:breakadd* 460:breaka[dd] func [lnum] {name} 461 Set a breakpoint in a function. Example: > 462 :breakadd func Explore 463< Doesn't check for a valid function name, thus the breakpoint 464 can be set before the function is defined. 465 466:breaka[dd] file [lnum] {name} 467 Set a breakpoint in a sourced file. Example: > 468 :breakadd file 43 .vimrc 469 470:breaka[dd] here 471 Set a breakpoint in the current line of the current file. 472 Like doing: > 473 :breakadd file <cursor-line> <current-file> 474< Note that this only works for commands that are executed when 475 sourcing the file, not for a function defined in that file. 476 477The [lnum] is the line number of the breakpoint. Vim will stop at or after 478this line. When omitted line 1 is used. 479 480{name} is a pattern that is matched with the file or function name. The 481pattern is like what is used for autocommands. There must be a full match (as 482if the pattern starts with "^" and ends in "$"). A "*" matches any sequence 483of characters. 'ignorecase' is not used, but "\c" can be used in the pattern 484to ignore case |/\c|. Don't include the () for the function name! 485 486The match for sourced scripts is done against the full file name. Examples: > 487 breakadd file explorer 488won't match, the path is missing. > 489 breakadd file *explorer.vim 490matches ".../plugin/explorer.vim" and ".../plugin/iexplorer.vim". > 491 breakadd file */explorer.vim 492matches ".../plugin/explorer.vim" only. 493 494The match for functions is done against the name as it's shown in the output 495of ":function". For local functions this means that something like "<SNR>99_" 496is prepended. 497 498 499DELETING BREAKPOINTS 500 *:breakd* *:breakdel* *E161* 501:breakd[el] {nr} 502 Delete breakpoint {nr}. Use |:breaklist| to see the number of 503 each breakpoint. 504 505:breakd[el] func [lnum] {name} 506 Delete a breakpoint in a function. 507 508:breakd[el] file [lnum] {name} 509 Delete a breakpoint in a sourced file. 510 511:breakd[el] here 512 Delete a breakpoint at the current line of the current file. 513 514When [lnum] is omitted, the first breakpoint in the function or file is 515deleted. 516The {name} must be exactly the same as what was typed for the ":breakadd" 517command. "explorer", "*explorer.vim" and "*explorer*" are different. 518 519 520LISTING BREAKPOINTS 521 *:breakl* *:breaklist* 522:breakl[ist] 523 List all breakpoints. 524 525 526OBSCURE 527 528 *:debugg* *:debuggreedy* 529:debugg[reedy] 530 Read debug mode commands from the normal input stream, instead 531 of getting them directly from the user. Only useful for test 532 scripts. Example: > 533 echo 'q^Mq' | vim -e -s -c debuggreedy -c 'breakadd file script.vim' -S script.vim 534 535:0debugg[reedy] 536 Undo ":debuggreedy": get debug mode commands directly from the 537 user, don't use typeahead for debug commands. 538 539 vim:tw=78:ts=8:ft=help:norl: 540