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