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