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