blob: 38a485753db62c59e1cc59274b1ae9d4b6476aab [file] [log] [blame]
Bram Moolenaar864207d2008-06-24 22:14:38 +00001*repeat.txt* For Vim version 7.2a. Last change: 2007 Aug 12
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|
Bram Moolenaar05159a02005-02-26 23:04:13 +0000166. Profiling |profiling|
Bram Moolenaar071d4272004-06-13 20:20:40 +000017
18==============================================================================
191. Single repeats *single-repeat*
20
21 *.*
22. Repeat last change, with count replaced with [count].
23 Also repeat a yank command, when the 'y' flag is
Bram Moolenaard4755bb2004-09-02 19:12:26 +000024 included in 'cpoptions'. Does not repeat a
25 command-line command.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
27Simple changes can be repeated with the "." command. Without a count, the
28count of the last change is used. If you enter a count, it will replace the
29last one. If the last change included a specification of a numbered register,
30the register number will be incremented. See |redo-register| for an example
31how to use this. Note that when repeating a command that used a Visual
32selection, the same SIZE of area is used, see |visual-repeat|.
33
34 *@:*
35@: Repeat last command-line [count] times.
36 {not available when compiled without the
37 |+cmdline_hist| feature}
38
39
40==============================================================================
412. Multiple repeats *multi-repeat*
42
43 *:g* *:global* *E147* *E148*
44:[range]g[lobal]/{pattern}/[cmd]
45 Execute the Ex command [cmd] (default ":p") on the
46 lines within [range] where {pattern} matches.
47
48:[range]g[lobal]!/{pattern}/[cmd]
49 Execute the Ex command [cmd] (default ":p") on the
50 lines within [range] where {pattern} does NOT match.
51
52 *:v* *:vglobal*
53:[range]v[global]/{pattern}/[cmd]
54 Same as :g!.
55
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000056Instead of the '/' which surrounds the {pattern}, you can use any other
57single byte character, but not an alphanumeric character, '\', '"' or '|'.
58This is useful if you want to include a '/' in the search pattern or
59replacement string.
60
61For the definition of a pattern, see |pattern|.
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063The global commands work by first scanning through the [range] lines and
64marking each line where a match occurs (for a multi-line pattern, only the
65start of the match matters).
66In a second scan the [cmd] is executed for each marked line with its line
67number prepended. For ":v" and ":g!" the command is executed for each not
68marked line. If a line is deleted its mark disappears.
69The default for [range] is the whole buffer (1,$). Use "CTRL-C" to interrupt
70the command. If an error message is given for a line, the command for that
71line is aborted and the global command continues with the next marked or
72unmarked line.
73
74To repeat a non-Ex command, you can use the ":normal" command: >
75 :g/pat/normal {commands}
76Make sure that {commands} ends with a whole command, otherwise Vim will wait
77for you to type the rest of the command for each match. The screen will not
78have been updated, so you don't know what you are doing. See |:normal|.
79
80The undo/redo command will undo/redo the whole global command at once.
81The previous context mark will only be set once (with "''" you go back to
82where the cursor was before the global command).
83
84The global command sets both the last used search pattern and the last used
85substitute pattern (this is vi compatible). This makes it easy to globally
86replace a string:
87 :g/pat/s//PAT/g
88This replaces all occurrences of "pat" with "PAT". The same can be done with:
89 :%s/pat/PAT/g
90Which is two characters shorter!
91
Bram Moolenaar864207d2008-06-24 22:14:38 +000092When using "global" in Ex mode, a special case is using ":visual" as a
93command. This will move to a matching line, go to Normal mode to let you
94execute commands there until you use |Q| to return to Ex mode. This will be
95repeated for each matching line. While doing this you cannot use ":global".
96To abort this type CTRL-C twice.
Bram Moolenaar26a60b42005-02-22 08:49:11 +000097
Bram Moolenaar071d4272004-06-13 20:20:40 +000098==============================================================================
993. Complex repeats *complex-repeat*
100
101 *q* *recording*
102q{0-9a-zA-Z"} Record typed characters into register {0-9a-zA-Z"}
103 (uppercase to append). The 'q' command is disabled
104 while executing a register, and it doesn't work inside
105 a mapping. {Vi: no recording}
106
107q Stops recording. (Implementation note: The 'q' that
108 stops recording is not stored in the register, unless
109 it was the result of a mapping) {Vi: no recording}
110
111 *@*
112@{0-9a-z".=*} Execute the contents of register {0-9a-z".=*} [count]
113 times. Note that register '%' (name of the current
114 file) and '#' (name of the alternate file) cannot be
115 used. For "@=" you are prompted to enter an
116 expression. The result of the expression is then
117 executed. See also |@:|. {Vi: only named registers}
118
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000119 *@@* *E748*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120@@ Repeat the previous @{0-9a-z":*} [count] times.
121
122:[addr]*{0-9a-z".=} *:@* *:star*
123:[addr]@{0-9a-z".=*} Execute the contents of register {0-9a-z".=*} as an Ex
124 command. First set cursor at line [addr] (default is
125 current line). When the last line in the register does
126 not have a <CR> it will be added automatically when
127 the 'e' flag is present in 'cpoptions'.
128 Note that the ":*" command is only recognized when the
129 '*' flag is present in 'cpoptions'. This is NOT the
130 default when 'nocompatible' is used.
131 For ":@=" the last used expression is used. The
132 result of evaluating the expression is executed as an
133 Ex command.
134 Mappings are not recognized in these commands.
135 {Vi: only in some versions} Future: Will execute the
136 register for each line in the address range.
137
138 *:@:*
139:[addr]@: Repeat last command-line. First set cursor at line
140 [addr] (default is current line). {not in Vi}
141
142 *:@@*
143:[addr]@@ Repeat the previous :@{0-9a-z"}. First set cursor at
144 line [addr] (default is current line). {Vi: only in
145 some versions}
146
147==============================================================================
1484. Using Vim scripts *using-scripts*
149
150For writing a Vim script, see chapter 41 of the user manual |usr_41.txt|.
151
152 *:so* *:source* *load-vim-script*
153:so[urce] {file} Read Ex commands from {file}. These are commands that
154 start with a ":".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000155 Triggers the |SourcePre| autocommand.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156
157:so[urce]! {file} Read Vim commands from {file}. These are commands
158 that are executed from Normal mode, like you type
159 them.
160 When used after |:global|, |:argdo|, |:windo|,
161 |:bufdo|, in a loop or when another command follows
162 the display won't be updated while executing the
163 commands.
164 {not in Vi}
165
166 *:ru* *:runtime*
167:ru[ntime][!] {file} ..
168 Read Ex commands from {file} in each directory given
169 by 'runtimepath'. There is no error for non-existing
170 files. Example: >
171 :runtime syntax/c.vim
172
173< There can be multiple {file} arguments, separated by
174 spaces. Each {file} is searched for in the first
175 directory from 'runtimepath', then in the second
176 directory, etc. Use a backslash to include a space
177 inside {file} (although it's better not to use spaces
178 in file names, it causes trouble).
179
180 When [!] is included, all found files are sourced.
181 When it is not included only the first found file is
182 sourced.
183
184 When {file} contains wildcards it is expanded to all
185 matching files. Example: >
186 :runtime! plugin/*.vim
187< This is what Vim uses to load the plugin files when
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000188 starting up. This similar command: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 :runtime plugin/*.vim
190< would source the first file only.
191
192 When 'verbose' is one or higher, there is a message
193 when no file could be found.
194 When 'verbose' is two or higher, there is a message
195 about each searched file.
196 {not in Vi}
197
198:scripte[ncoding] [encoding] *:scripte* *:scriptencoding* *E167*
199 Specify the character encoding used in the script.
200 The following lines will be converted from [encoding]
201 to the value of the 'encoding' option, if they are
202 different. Examples: >
203 scriptencoding iso-8859-5
204 scriptencoding cp932
205<
206 When [encoding] is empty, no conversion is done. This
207 can be used to restrict conversion to a sequence of
208 lines: >
209 scriptencoding euc-jp
210 ... lines to be converted ...
211 scriptencoding
212 ... not converted ...
213
214< When conversion isn't supported by the system, there
215 is no error message and no conversion is done.
216
217 Don't use "ucs-2" or "ucs-4", scripts cannot be in
218 these encodings (they would contain NUL bytes).
219 When a sourced script starts with a BOM (Byte Order
220 Mark) in utf-8 format Vim will recognized it, no need
221 to use ":scriptencoding utf-8" then.
222
223 When compiled without the |+multi_byte| feature this
224 command is ignored.
225 {not in Vi}
226
227 *:scrip* *:scriptnames*
228:scrip[tnames] List all sourced script names, in the order they were
229 first sourced. The number is used for the script ID
230 |<SID>|.
231 {not in Vi} {not available when compiled without the
232 |+eval| feature}
233
234 *:fini* *:finish* *E168*
235:fini[sh] Stop sourcing a script. Can only be used in a Vim
236 script file. This is a quick way to skip the rest of
237 the file. If it is used after a |:try| but before the
238 matching |:finally| (if present), the commands
239 following the ":finally" up to the matching |:endtry|
240 are executed first. This process applies to all
241 nested ":try"s in the script. The outermost ":endtry"
242 then stops sourcing the script. {not in Vi}
243
244All commands and command sequences can be repeated by putting them in a named
245register and then executing it. There are two ways to get the commands in the
246register:
247- Use the record command "q". You type the commands once, and while they are
248 being executed they are stored in a register. Easy, because you can see
249 what you are doing. If you make a mistake, "p"ut the register into the
250 file, edit the command sequence, and then delete it into the register
251 again. You can continue recording by appending to the register (use an
252 uppercase letter).
253- Delete or yank the command sequence into the register.
254
255Often used command sequences can be put under a function key with the ':map'
256command.
257
258An alternative is to put the commands in a file, and execute them with the
259':source!' command. Useful for long command sequences. Can be combined with
260the ':map' command to put complicated commands under a function key.
261
262The ':source' command reads Ex commands from a file line by line. You will
263have to type any needed keyboard input. The ':source!' command reads from a
264script file character by character, interpreting each character as if you
265typed it.
266
267Example: When you give the ":!ls" command you get the |hit-enter| prompt. If
268you ':source' a file with the line "!ls" in it, you will have to type the
269<Enter> yourself. But if you ':source!' a file with the line ":!ls" in it,
270the next characters from that file are read until a <CR> is found. You will
271not have to type <CR> yourself, unless ":!ls" was the last line in the file.
272
273It is possible to put ':source[!]' commands in the script file, so you can
274make a top-down hierarchy of script files. The ':source' command can be
275nested as deep as the number of files that can be opened at one time (about
27615). The ':source!' command can be nested up to 15 levels deep.
277
278You can use the "<sfile>" string (literally, this is not a special key) inside
279of the sourced file, in places where a file name is expected. It will be
280replaced by the file name of the sourced file. For example, if you have a
281"other.vimrc" file in the same directory as your ".vimrc" file, you can source
282it from your ".vimrc" file with this command: >
283 :source <sfile>:h/other.vimrc
284
285In script files terminal-dependent key codes are represented by
286terminal-independent two character codes. This means that they can be used
287in the same way on different kinds of terminals. The first character of a
288key code is 0x80 or 128, shown on the screen as "~@". The second one can be
289found in the list |key-notation|. Any of these codes can also be entered
290with CTRL-V followed by the three digit decimal code. This does NOT work for
291the <t_xx> termcap codes, these can only be used in mappings.
292
293 *:source_crnl* *W15*
294MS-DOS, Win32 and OS/2: Files that are read with ":source" normally have
295<CR><NL> <EOL>s. These always work. If you are using a file with <NL> <EOL>s
296(for example, a file made on Unix), this will be recognized if 'fileformats'
297is not empty and the first line does not end in a <CR>. This fails if the
298first line has something like ":map <F1> :help^M", where "^M" is a <CR>. If
299the first line ends in a <CR>, but following ones don't, you will get an error
300message, because the <CR> from the first lines will be lost.
301
Bram Moolenaar520470a2005-06-16 21:59:56 +0000302Mac Classic: Files that are read with ":source" normally have <CR> <EOL>s.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303These always work. If you are using a file with <NL> <EOL>s (for example, a
304file made on Unix), this will be recognized if 'fileformats' is not empty and
305the first line does not end in a <CR>. Be careful not to use a file with <NL>
306linebreaks which has a <CR> in first line.
307
308On other systems, Vim expects ":source"ed files to end in a <NL>. These
309always work. If you are using a file with <CR><NL> <EOL>s (for example, a
310file made on MS-DOS), all lines will have a trailing <CR>. This may cause
311problems for some commands (e.g., mappings). There is no automatic <EOL>
312detection, because it's common to start with a line that defines a mapping
313that ends in a <CR>, which will confuse the automaton.
314
315 *line-continuation*
316Long lines in a ":source"d Ex command script file can be split by inserting
317a line continuation symbol "\" (backslash) at the start of the next line.
318There can be white space before the backslash, which is ignored.
319
320Example: the lines >
321 :set comments=sr:/*,mb:*,el:*/,
322 \://,
323 \b:#,
324 \:%,
325 \n:>,
326 \fb:-
327are interpreted as if they were given in one line:
328 :set comments=sr:/*,mb:*,el:*/,://,b:#,:%,n:>,fb:-
329
330All leading whitespace characters in the line before a backslash are ignored.
331Note however that trailing whitespace in the line before it cannot be
332inserted freely; it depends on the position where a command is split up
333whether additional whitespace is allowed or not.
334
335There is a problem with the ":append" and ":insert" commands: >
336 :1append
337 \asdf
338 .
339The backslash is seen as a line-continuation symbol, thus this results in the
340command: >
341 :1appendasdf
342 .
343To avoid this, add the 'C' flag to the 'cpoptions' option: >
344 :set cpo+=C
345 :1append
346 \asdf
347 .
348 :set cpo-=C
349
350Note that when the commands are inside a function, you need to add the 'C'
351flag when defining the function, it is not relevant when executing it. >
352 :set cpo+=C
353 :function Foo()
354 :1append
355 \asdf
356 .
357 :endfunction
358 :set cpo-=C
359
360Rationale:
361 Most programs work with a trailing backslash to indicate line
362 continuation. Using this in Vim would cause incompatibility with Vi.
363 For example for this Vi mapping: >
364 :map xx asdf\
365< Therefore the unusual leading backslash is used.
366
367==============================================================================
3685. Debugging scripts *debug-scripts*
369
370Besides the obvious messages that you can add to your scripts to find out what
371they are doing, Vim offers a debug mode. This allows you to step through a
372sourced file or user function and set breakpoints.
373
374NOTE: The debugging mode is far from perfect. Debugging will have side
375effects on how Vim works. You cannot use it to debug everything. For
376example, the display is messed up by the debugging messages.
377{Vi does not have a debug mode}
378
379An alternative to debug mode is setting the 'verbose' option. With a bigger
380number it will give more verbose messages about what Vim is doing.
381
382
383STARTING DEBUG MODE *debug-mode*
384
385To enter debugging mode use one of these methods:
3861. Start Vim with the |-D| argument: >
387 vim -D file.txt
388< Debugging will start as soon as the first vimrc file is sourced. This is
389 useful to find out what is happening when Vim is starting up. A side
390 effect is that Vim will switch the terminal mode before initialisations
391 have finished, with unpredictable results.
392 For a GUI-only version (Windows, Macintosh) the debugging will start as
393 soon as the GUI window has been opened. To make this happen early, add a
394 ":gui" command in the vimrc file.
395 *:debug*
3962. Run a command with ":debug" prepended. Debugging will only be done while
397 this command executes. Useful for debugging a specific script or user
398 function. And for scripts and functions used by autocommands. Example: >
399 :debug edit test.txt.gz
400
4013. Set a breakpoint in a sourced file or user function. You could do this in
402 the command line: >
403 vim -c "breakadd file */explorer.vim" .
404< This will run Vim and stop in the first line of the "explorer.vim" script.
405 Breakpoints can also be set while in debugging mode.
406
407In debugging mode every executed command is displayed before it is executed.
408Comment lines, empty lines and lines that are not executed are skipped. When
409a line contains two commands, separated by "|", each command will be displayed
410separately.
411
412
413DEBUG MODE
414
415Once in debugging mode, the usual Ex commands can be used. For example, to
416inspect the value of a variable: >
417 echo idx
418When inside a user function, this will print the value of the local variable
419"idx". Prepend "g:" to get the value of a global variable: >
420 echo g:idx
421All commands are executed in the context of the current function or script.
422You can also set options, for example setting or resetting 'verbose' will show
423what happens, but you might want to set it just before executing the lines you
424are interested in: >
425 :set verbose=20
426
427Commands that require updating the screen should be avoided, because their
428effect won't be noticed until after leaving debug mode. For example: >
429 :help
430won't be very helpful.
431
432There is a separate command-line history for debug mode.
433
434The line number for a function line is relative to the start of the function.
435If you have trouble figuring out where you are, edit the file that defines
436the function in another Vim, search for the start of the function and do
437"99j". Replace "99" with the line number.
438
439Additionally, these commands can be used:
440 *>cont*
441 cont Continue execution until the next breakpoint is hit.
442 *>quit*
443 quit Abort execution. This is like using CTRL-C, some
444 things might still be executed, doesn't abort
445 everything. Still stops at the next breakpoint.
446 *>next*
447 next Execute the command and come back to debug mode when
448 it's finished. This steps over user function calls
449 and sourced files.
450 *>step*
451 step Execute the command and come back to debug mode for
452 the next command. This steps into called user
453 functions and sourced files.
454 *>interrupt*
455 interrupt This is like using CTRL-C, but unlike ">quit" comes
456 back to debug mode for the next command that is
457 executed. Useful for testing |:finally| and |:catch|
458 on interrupt exceptions.
459 *>finish*
460 finish Finish the current script or user function and come
461 back to debug mode for the command after the one that
462 sourced or called it.
463
464About the additional commands in debug mode:
465- There is no command-line completion for them, you get the completion for the
466 normal Ex commands only.
467- You can shorten them, up to a single character: "c", "n", "s" and "f".
468- Hitting <CR> will repeat the previous one. When doing another command, this
469 is reset (because it's not clear what you want to repeat).
470- When you want to use the Ex command with the same name, prepend a colon:
471 ":cont", ":next", ":finish" (or shorter).
472
473
474DEFINING BREAKPOINTS
475 *:breaka* *:breakadd*
476:breaka[dd] func [lnum] {name}
477 Set a breakpoint in a function. Example: >
478 :breakadd func Explore
479< Doesn't check for a valid function name, thus the breakpoint
480 can be set before the function is defined.
481
482:breaka[dd] file [lnum] {name}
483 Set a breakpoint in a sourced file. Example: >
484 :breakadd file 43 .vimrc
485
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000486:breaka[dd] here
487 Set a breakpoint in the current line of the current file.
488 Like doing: >
489 :breakadd file <cursor-line> <current-file>
490< Note that this only works for commands that are executed when
491 sourcing the file, not for a function defined in that file.
492
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493The [lnum] is the line number of the breakpoint. Vim will stop at or after
494this line. When omitted line 1 is used.
495
Bram Moolenaar05159a02005-02-26 23:04:13 +0000496 *:debug-name*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497{name} is a pattern that is matched with the file or function name. The
498pattern is like what is used for autocommands. There must be a full match (as
499if the pattern starts with "^" and ends in "$"). A "*" matches any sequence
500of characters. 'ignorecase' is not used, but "\c" can be used in the pattern
501to ignore case |/\c|. Don't include the () for the function name!
502
Bram Moolenaar843ee412004-06-30 16:16:41 +0000503The match for sourced scripts is done against the full file name. If no path
504is specified the current directory is used. Examples: >
505 breakadd file explorer.vim
506matches "explorer.vim" in the current directory. >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 breakadd file *explorer.vim
Bram Moolenaar843ee412004-06-30 16:16:41 +0000508matches ".../plugin/explorer.vim", ".../plugin/iexplorer.vim", etc. >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 breakadd file */explorer.vim
Bram Moolenaar843ee412004-06-30 16:16:41 +0000510matches ".../plugin/explorer.vim" and "explorer.vim" in any other directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511
512The match for functions is done against the name as it's shown in the output
513of ":function". For local functions this means that something like "<SNR>99_"
514is prepended.
515
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000516Note that functions are first loaded and later executed. When they are loaded
517the "file" breakpoints are checked, when they are executed the "func"
518breakpoints.
519
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520
521DELETING BREAKPOINTS
522 *:breakd* *:breakdel* *E161*
523:breakd[el] {nr}
524 Delete breakpoint {nr}. Use |:breaklist| to see the number of
525 each breakpoint.
526
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000527:breakd[el] *
528 Delete all breakpoints.
529
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530:breakd[el] func [lnum] {name}
531 Delete a breakpoint in a function.
532
533:breakd[el] file [lnum] {name}
534 Delete a breakpoint in a sourced file.
535
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000536:breakd[el] here
537 Delete a breakpoint at the current line of the current file.
538
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539When [lnum] is omitted, the first breakpoint in the function or file is
540deleted.
541The {name} must be exactly the same as what was typed for the ":breakadd"
542command. "explorer", "*explorer.vim" and "*explorer*" are different.
543
544
545LISTING BREAKPOINTS
546 *:breakl* *:breaklist*
547:breakl[ist]
548 List all breakpoints.
549
550
551OBSCURE
552
553 *:debugg* *:debuggreedy*
554:debugg[reedy]
555 Read debug mode commands from the normal input stream, instead
556 of getting them directly from the user. Only useful for test
557 scripts. Example: >
558 echo 'q^Mq' | vim -e -s -c debuggreedy -c 'breakadd file script.vim' -S script.vim
559
560:0debugg[reedy]
561 Undo ":debuggreedy": get debug mode commands directly from the
562 user, don't use typeahead for debug commands.
563
Bram Moolenaar05159a02005-02-26 23:04:13 +0000564==============================================================================
5656. Profiling *profile* *profiling*
566
567Profiling means that Vim measures the time that is spend on executing
568functions and/or scripts. The |+profile| feature is required for this.
569It is only included when Vim was compiled with "huge" features.
570{Vi does not have profiling}
571
Bram Moolenaar433f7c82006-03-21 21:29:36 +0000572You can also use the |reltime()| function to measure time. This only requires
573the |+reltime| feature, which is present more often.
574
Bram Moolenaar05159a02005-02-26 23:04:13 +0000575:prof[ile] start {fname} *:prof* *:profile* *E750*
576 Start profiling, write the output in {fname} upon exit.
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000577 If {fname} already exists it will be silently overwritten.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000578 The variable |v:profiling| is set to one.
579
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000580:prof[ile] pause
581 Don't profile until the following ":profile continue". Can be
582 used when doing something that should not be counted (e.g., an
583 external command). Does not nest.
584
585:prof[ile] continue
586 Continue profiling after ":profile pause".
587
Bram Moolenaar05159a02005-02-26 23:04:13 +0000588:prof[ile] func {pattern}
589 Profile function that matches the pattern {pattern}.
590 See |:debug-name| for how {pattern} is used.
591
592:prof[ile][!] file {pattern}
593 Profile script file that matches the pattern {pattern}.
594 See |:debug-name| for how {pattern} is used.
595 This only profiles the script itself, not the functions
596 defined in it.
597 When the [!] is added then all functions defined in the script
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000598 will also be profiled. But only if the script is loaded after
599 this command.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000600
601
Bram Moolenaard9fba312005-06-26 22:34:35 +0000602:profd[el] ... *:profd* *:profdel*
603 Stop profiling for the arguments specified. See |:breakdel|
604 for the arguments.
605
606
Bram Moolenaar05159a02005-02-26 23:04:13 +0000607You must always start with a ":profile start fname" command. The resulting
608file is written when Vim exits. Here is an example of the output, with line
609numbers prepended for the explanation:
610
611 1 FUNCTION Test2() ~
612 2 Called 1 time ~
613 3 Total time: 0.155251 ~
614 4 Self time: 0.002006 ~
615 5 ~
616 6 count total (s) self (s) ~
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000617 7 9 0.000096 for i in range(8) ~
618 8 8 0.153655 0.000410 call Test3() ~
619 9 8 0.000070 endfor ~
620 10 " Ask a question ~
621 11 1 0.001341 echo input("give me an answer: ") ~
Bram Moolenaar05159a02005-02-26 23:04:13 +0000622
623The header (lines 1-4) gives the time for the whole function. The "Total"
624time is the time passed while the function was executing. The "Self" time is
625the "Total" time reduced by time spent in:
626- other user defined functions
627- sourced scripts
628- executed autocommands
629- external (shell) commands
630
631Lines 7-11 show the time spent in each executed line. Lines that are not
632executed do not count. Thus a comment line is never counted.
633
634The Count column shows how many times a line was executed. Note that the
635"for" command in line 7 is executed one more time as the following lines.
636That is because the line is also executed to detect the end of the loop.
637
638The time Vim spends waiting for user input isn't counted at all. Thus how
639long you take to respond to the input() prompt is irrelevant.
640
641Profiling should give a good indication of where time is spent, but keep in
642mind there are various things that may clobber the results:
643
644- The accuracy of the time measured depends on the gettimeofday() system
645 function. It may only be as accurate as 1/100 second, even though the times
646 are displayed in micro seconds.
647
648- Real elapsed time is measured, if other processes are busy they may cause
649 delays at unpredictable moments. You may want to run the profiling several
650 times and use the lowest results.
651
652- If you have several commands in one line you only get one time. Split the
653 line to see the time for the individual commands.
654
655- The time of the lines added up is mostly less than the time of the whole
656 function. There is some overhead in between.
657
658- Functions that are deleted before Vim exits will not produce profiling
659 information. You can check the |v:profiling| variable if needed: >
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000660 :if !v:profiling
Bram Moolenaar05159a02005-02-26 23:04:13 +0000661 : delfunc MyFunc
662 :endif
663<
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000664- Profiling may give weird results on multi-processor systems, when sleep
665 mode kicks in or the processor frequency is reduced to save power.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000666
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000667- The "self" time is wrong when a function is used recursively.
668
669
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 vim:tw=78:ts=8:ft=help:norl: