Mercurial > vim
comparison runtime/doc/repeat.txt @ 7:3fc0f57ecb91 v7.0001
updated for version 7.0001
author | vimboss |
---|---|
date | Sun, 13 Jun 2004 20:20:40 +0000 |
parents | |
children | 4e2284e71352 |
comparison
equal
deleted
inserted
replaced
6:c2daee826b8f | 7:3fc0f57ecb91 |
---|---|
1 *repeat.txt* For Vim version 7.0aa. Last change: 2004 Apr 02 | |
2 | |
3 | |
4 VIM REFERENCE MANUAL by Bram Moolenaar | |
5 | |
6 | |
7 Repeating commands, Vim scripts and debugging *repeating* | |
8 | |
9 Chapter 26 of the user manual introduces repeating |usr_26.txt|. | |
10 | |
11 1. Single repeats |single-repeat| | |
12 2. Multiple repeats |multi-repeat| | |
13 3. Complex repeats |complex-repeat| | |
14 4. Using Vim scripts |using-scripts| | |
15 5. Debugging scripts |debug-scripts| | |
16 | |
17 ============================================================================== | |
18 1. 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 | |
25 Simple changes can be repeated with the "." command. Without a count, the | |
26 count of the last change is used. If you enter a count, it will replace the | |
27 last one. If the last change included a specification of a numbered register, | |
28 the register number will be incremented. See |redo-register| for an example | |
29 how to use this. Note that when repeating a command that used a Visual | |
30 selection, 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 ============================================================================== | |
39 2. 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 | |
54 The global commands work by first scanning through the [range] lines and | |
55 marking each line where a match occurs (for a multi-line pattern, only the | |
56 start of the match matters). | |
57 In a second scan the [cmd] is executed for each marked line with its line | |
58 number prepended. For ":v" and ":g!" the command is executed for each not | |
59 marked line. If a line is deleted its mark disappears. | |
60 The default for [range] is the whole buffer (1,$). Use "CTRL-C" to interrupt | |
61 the command. If an error message is given for a line, the command for that | |
62 line is aborted and the global command continues with the next marked or | |
63 unmarked line. | |
64 | |
65 To repeat a non-Ex command, you can use the ":normal" command: > | |
66 :g/pat/normal {commands} | |
67 Make sure that {commands} ends with a whole command, otherwise Vim will wait | |
68 for you to type the rest of the command for each match. The screen will not | |
69 have been updated, so you don't know what you are doing. See |:normal|. | |
70 | |
71 The undo/redo command will undo/redo the whole global command at once. | |
72 The previous context mark will only be set once (with "''" you go back to | |
73 where the cursor was before the global command). | |
74 | |
75 The global command sets both the last used search pattern and the last used | |
76 substitute pattern (this is vi compatible). This makes it easy to globally | |
77 replace a string: | |
78 :g/pat/s//PAT/g | |
79 This replaces all occurrences of "pat" with "PAT". The same can be done with: | |
80 :%s/pat/PAT/g | |
81 Which is two characters shorter! | |
82 | |
83 ============================================================================== | |
84 3. Complex repeats *complex-repeat* | |
85 | |
86 *q* *recording* | |
87 q{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 | |
92 q 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 ============================================================================== | |
133 4. Using Vim scripts *using-scripts* | |
134 | |
135 For 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 | |
228 All commands and command sequences can be repeated by putting them in a named | |
229 register and then executing it. There are two ways to get the commands in the | |
230 register: | |
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 | |
239 Often used command sequences can be put under a function key with the ':map' | |
240 command. | |
241 | |
242 An 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 | |
244 the ':map' command to put complicated commands under a function key. | |
245 | |
246 The ':source' command reads Ex commands from a file line by line. You will | |
247 have to type any needed keyboard input. The ':source!' command reads from a | |
248 script file character by character, interpreting each character as if you | |
249 typed it. | |
250 | |
251 Example: When you give the ":!ls" command you get the |hit-enter| prompt. If | |
252 you ':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, | |
254 the next characters from that file are read until a <CR> is found. You will | |
255 not have to type <CR> yourself, unless ":!ls" was the last line in the file. | |
256 | |
257 It is possible to put ':source[!]' commands in the script file, so you can | |
258 make a top-down hierarchy of script files. The ':source' command can be | |
259 nested as deep as the number of files that can be opened at one time (about | |
260 15). The ':source!' command can be nested up to 15 levels deep. | |
261 | |
262 You can use the "<sfile>" string (literally, this is not a special key) inside | |
263 of the sourced file, in places where a file name is expected. It will be | |
264 replaced 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 | |
266 it from your ".vimrc" file with this command: > | |
267 :source <sfile>:h/other.vimrc | |
268 | |
269 In script files terminal-dependent key codes are represented by | |
270 terminal-independent two character codes. This means that they can be used | |
271 in the same way on different kinds of terminals. The first character of a | |
272 key code is 0x80 or 128, shown on the screen as "~@". The second one can be | |
273 found in the list |key-notation|. Any of these codes can also be entered | |
274 with CTRL-V followed by the three digit decimal code. This does NOT work for | |
275 the <t_xx> termcap codes, these can only be used in mappings. | |
276 | |
277 *:source_crnl* *W15* | |
278 MS-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' | |
281 is not empty and the first line does not end in a <CR>. This fails if the | |
282 first line has something like ":map <F1> :help^M", where "^M" is a <CR>. If | |
283 the first line ends in a <CR>, but following ones don't, you will get an error | |
284 message, because the <CR> from the first lines will be lost. | |
285 | |
286 Macintosh: Files that are read with ":source" normally have <CR> <EOL>s. | |
287 These always work. If you are using a file with <NL> <EOL>s (for example, a | |
288 file made on Unix), this will be recognized if 'fileformats' is not empty and | |
289 the first line does not end in a <CR>. Be careful not to use a file with <NL> | |
290 linebreaks which has a <CR> in first line. | |
291 | |
292 On other systems, Vim expects ":source"ed files to end in a <NL>. These | |
293 always work. If you are using a file with <CR><NL> <EOL>s (for example, a | |
294 file made on MS-DOS), all lines will have a trailing <CR>. This may cause | |
295 problems for some commands (e.g., mappings). There is no automatic <EOL> | |
296 detection, because it's common to start with a line that defines a mapping | |
297 that ends in a <CR>, which will confuse the automaton. | |
298 | |
299 *line-continuation* | |
300 Long lines in a ":source"d Ex command script file can be split by inserting | |
301 a line continuation symbol "\" (backslash) at the start of the next line. | |
302 There can be white space before the backslash, which is ignored. | |
303 | |
304 Example: the lines > | |
305 :set comments=sr:/*,mb:*,el:*/, | |
306 \://, | |
307 \b:#, | |
308 \:%, | |
309 \n:>, | |
310 \fb:- | |
311 are interpreted as if they were given in one line: | |
312 :set comments=sr:/*,mb:*,el:*/,://,b:#,:%,n:>,fb:- | |
313 | |
314 All leading whitespace characters in the line before a backslash are ignored. | |
315 Note however that trailing whitespace in the line before it cannot be | |
316 inserted freely; it depends on the position where a command is split up | |
317 whether additional whitespace is allowed or not. | |
318 | |
319 There is a problem with the ":append" and ":insert" commands: > | |
320 :1append | |
321 \asdf | |
322 . | |
323 The backslash is seen as a line-continuation symbol, thus this results in the | |
324 command: > | |
325 :1appendasdf | |
326 . | |
327 To avoid this, add the 'C' flag to the 'cpoptions' option: > | |
328 :set cpo+=C | |
329 :1append | |
330 \asdf | |
331 . | |
332 :set cpo-=C | |
333 | |
334 Note that when the commands are inside a function, you need to add the 'C' | |
335 flag 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 | |
344 Rationale: | |
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 ============================================================================== | |
352 5. Debugging scripts *debug-scripts* | |
353 | |
354 Besides the obvious messages that you can add to your scripts to find out what | |
355 they are doing, Vim offers a debug mode. This allows you to step through a | |
356 sourced file or user function and set breakpoints. | |
357 | |
358 NOTE: The debugging mode is far from perfect. Debugging will have side | |
359 effects on how Vim works. You cannot use it to debug everything. For | |
360 example, the display is messed up by the debugging messages. | |
361 {Vi does not have a debug mode} | |
362 | |
363 An alternative to debug mode is setting the 'verbose' option. With a bigger | |
364 number it will give more verbose messages about what Vim is doing. | |
365 | |
366 | |
367 STARTING DEBUG MODE *debug-mode* | |
368 | |
369 To enter debugging mode use one of these methods: | |
370 1. 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* | |
380 2. 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 | |
385 3. 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 | |
391 In debugging mode every executed command is displayed before it is executed. | |
392 Comment lines, empty lines and lines that are not executed are skipped. When | |
393 a line contains two commands, separated by "|", each command will be displayed | |
394 separately. | |
395 | |
396 | |
397 DEBUG MODE | |
398 | |
399 Once in debugging mode, the usual Ex commands can be used. For example, to | |
400 inspect the value of a variable: > | |
401 echo idx | |
402 When 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 | |
405 All commands are executed in the context of the current function or script. | |
406 You can also set options, for example setting or resetting 'verbose' will show | |
407 what happens, but you might want to set it just before executing the lines you | |
408 are interested in: > | |
409 :set verbose=20 | |
410 | |
411 Commands that require updating the screen should be avoided, because their | |
412 effect won't be noticed until after leaving debug mode. For example: > | |
413 :help | |
414 won't be very helpful. | |
415 | |
416 There is a separate command-line history for debug mode. | |
417 | |
418 The line number for a function line is relative to the start of the function. | |
419 If you have trouble figuring out where you are, edit the file that defines | |
420 the function in another Vim, search for the start of the function and do | |
421 "99j". Replace "99" with the line number. | |
422 | |
423 Additionally, 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 | |
448 About 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 | |
458 DEFINING 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 The [lnum] is the line number of the breakpoint. Vim will stop at or after | |
471 this line. When omitted line 1 is used. | |
472 | |
473 {name} is a pattern that is matched with the file or function name. The | |
474 pattern is like what is used for autocommands. There must be a full match (as | |
475 if the pattern starts with "^" and ends in "$"). A "*" matches any sequence | |
476 of characters. 'ignorecase' is not used, but "\c" can be used in the pattern | |
477 to ignore case |/\c|. Don't include the () for the function name! | |
478 | |
479 The match for sourced scripts is done against the full file name. Examples: > | |
480 breakadd file explorer | |
481 won't match, the path is missing. > | |
482 breakadd file *explorer.vim | |
483 matches ".../plugin/explorer.vim" and ".../plugin/iexplorer.vim". > | |
484 breakadd file */explorer.vim | |
485 matches ".../plugin/explorer.vim" only. | |
486 | |
487 The match for functions is done against the name as it's shown in the output | |
488 of ":function". For local functions this means that something like "<SNR>99_" | |
489 is prepended. | |
490 | |
491 | |
492 DELETING BREAKPOINTS | |
493 *:breakd* *:breakdel* *E161* | |
494 :breakd[el] {nr} | |
495 Delete breakpoint {nr}. Use |:breaklist| to see the number of | |
496 each breakpoint. | |
497 | |
498 :breakd[el] func [lnum] {name} | |
499 Delete a breakpoint in a function. | |
500 | |
501 :breakd[el] file [lnum] {name} | |
502 Delete a breakpoint in a sourced file. | |
503 | |
504 When [lnum] is omitted, the first breakpoint in the function or file is | |
505 deleted. | |
506 The {name} must be exactly the same as what was typed for the ":breakadd" | |
507 command. "explorer", "*explorer.vim" and "*explorer*" are different. | |
508 | |
509 | |
510 LISTING BREAKPOINTS | |
511 *:breakl* *:breaklist* | |
512 :breakl[ist] | |
513 List all breakpoints. | |
514 | |
515 | |
516 OBSCURE | |
517 | |
518 *:debugg* *:debuggreedy* | |
519 :debugg[reedy] | |
520 Read debug mode commands from the normal input stream, instead | |
521 of getting them directly from the user. Only useful for test | |
522 scripts. Example: > | |
523 echo 'q^Mq' | vim -e -s -c debuggreedy -c 'breakadd file script.vim' -S script.vim | |
524 | |
525 :0debugg[reedy] | |
526 Undo ":debuggreedy": get debug mode commands directly from the | |
527 user, don't use typeahead for debug commands. | |
528 | |
529 vim:tw=78:ts=8:ft=help:norl: |