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