blob: a6ebf711859d5613dcde166cbd5655178dc57740 [file] [log] [blame]
Bram Moolenaardae8d212016-02-27 22:40:16 +01001*repeat.txt* For Vim version 7.4. Last change: 2016 Feb 26
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|
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100155. Using Vim packages |packages|
166. Debugging scripts |debug-scripts|
177. Profiling |profiling|
Bram Moolenaar071d4272004-06-13 20:20:40 +000018
19==============================================================================
201. Single repeats *single-repeat*
21
22 *.*
23. Repeat last change, with count replaced with [count].
24 Also repeat a yank command, when the 'y' flag is
Bram Moolenaard4755bb2004-09-02 19:12:26 +000025 included in 'cpoptions'. Does not repeat a
26 command-line command.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027
28Simple changes can be repeated with the "." command. Without a count, the
29count of the last change is used. If you enter a count, it will replace the
Bram Moolenaar92dff182014-02-11 19:15:50 +010030last one. |v:count| and |v:count1| will be set.
31
32If the last change included a specification of a numbered register, the
33register number will be incremented. See |redo-register| for an example how
34to use this.
35
36Note that when repeating a command that used a Visual selection, the same SIZE
37of area is used, see |visual-repeat|.
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
39 *@:*
40@: Repeat last command-line [count] times.
41 {not available when compiled without the
42 |+cmdline_hist| feature}
43
44
45==============================================================================
462. Multiple repeats *multi-repeat*
47
48 *:g* *:global* *E147* *E148*
49:[range]g[lobal]/{pattern}/[cmd]
50 Execute the Ex command [cmd] (default ":p") on the
51 lines within [range] where {pattern} matches.
52
53:[range]g[lobal]!/{pattern}/[cmd]
54 Execute the Ex command [cmd] (default ":p") on the
55 lines within [range] where {pattern} does NOT match.
56
57 *:v* *:vglobal*
58:[range]v[global]/{pattern}/[cmd]
59 Same as :g!.
60
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000061Instead of the '/' which surrounds the {pattern}, you can use any other
Bram Moolenaare2db6952013-07-24 19:53:36 +020062single byte character, but not an alphabetic character, '\', '"' or '|'.
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000063This is useful if you want to include a '/' in the search pattern or
64replacement string.
65
66For the definition of a pattern, see |pattern|.
67
Bram Moolenaar32efaf62014-11-05 17:02:17 +010068NOTE [cmd] may contain a range; see |collapse| and |edit-paragraph-join| for
69examples.
70
Bram Moolenaar071d4272004-06-13 20:20:40 +000071The global commands work by first scanning through the [range] lines and
72marking each line where a match occurs (for a multi-line pattern, only the
73start of the match matters).
74In a second scan the [cmd] is executed for each marked line with its line
75number prepended. For ":v" and ":g!" the command is executed for each not
76marked line. If a line is deleted its mark disappears.
77The default for [range] is the whole buffer (1,$). Use "CTRL-C" to interrupt
78the command. If an error message is given for a line, the command for that
79line is aborted and the global command continues with the next marked or
80unmarked line.
81
82To repeat a non-Ex command, you can use the ":normal" command: >
83 :g/pat/normal {commands}
84Make sure that {commands} ends with a whole command, otherwise Vim will wait
85for you to type the rest of the command for each match. The screen will not
86have been updated, so you don't know what you are doing. See |:normal|.
87
88The undo/redo command will undo/redo the whole global command at once.
89The previous context mark will only be set once (with "''" you go back to
90where the cursor was before the global command).
91
92The global command sets both the last used search pattern and the last used
93substitute pattern (this is vi compatible). This makes it easy to globally
94replace a string:
95 :g/pat/s//PAT/g
96This replaces all occurrences of "pat" with "PAT". The same can be done with:
97 :%s/pat/PAT/g
98Which is two characters shorter!
99
Bram Moolenaar864207d2008-06-24 22:14:38 +0000100When using "global" in Ex mode, a special case is using ":visual" as a
101command. This will move to a matching line, go to Normal mode to let you
102execute commands there until you use |Q| to return to Ex mode. This will be
103repeated for each matching line. While doing this you cannot use ":global".
104To abort this type CTRL-C twice.
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000105
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106==============================================================================
1073. Complex repeats *complex-repeat*
108
109 *q* *recording*
110q{0-9a-zA-Z"} Record typed characters into register {0-9a-zA-Z"}
111 (uppercase to append). The 'q' command is disabled
112 while executing a register, and it doesn't work inside
Bram Moolenaara0ed84a2015-11-19 17:56:13 +0100113 a mapping and |:normal|.
114
115 Note: If the register being used for recording is also
116 used for |y| and |p| the result is most likely not
117 what is expected, because the put will paste the
118 recorded macro and the yank will overwrite the
119 recorded macro. {Vi: no recording}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
121q Stops recording. (Implementation note: The 'q' that
122 stops recording is not stored in the register, unless
123 it was the result of a mapping) {Vi: no recording}
124
125 *@*
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200126@{0-9a-z".=*+} Execute the contents of register {0-9a-z".=*+} [count]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127 times. Note that register '%' (name of the current
128 file) and '#' (name of the alternate file) cannot be
Bram Moolenaar2a8a3ec2011-01-08 16:06:37 +0100129 used.
130 The register is executed like a mapping, that means
131 that the difference between 'wildchar' and 'wildcharm'
132 applies.
133 For "@=" you are prompted to enter an expression. The
134 result of the expression is then executed.
135 See also |@:|. {Vi: only named registers}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000137 *@@* *E748*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138@@ Repeat the previous @{0-9a-z":*} [count] times.
139
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200140:[addr]*{0-9a-z".=+} *:@* *:star*
141:[addr]@{0-9a-z".=*+} Execute the contents of register {0-9a-z".=*+} as an Ex
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 command. First set cursor at line [addr] (default is
143 current line). When the last line in the register does
144 not have a <CR> it will be added automatically when
145 the 'e' flag is present in 'cpoptions'.
146 Note that the ":*" command is only recognized when the
147 '*' flag is present in 'cpoptions'. This is NOT the
148 default when 'nocompatible' is used.
149 For ":@=" the last used expression is used. The
150 result of evaluating the expression is executed as an
151 Ex command.
152 Mappings are not recognized in these commands.
153 {Vi: only in some versions} Future: Will execute the
154 register for each line in the address range.
155
156 *:@:*
157:[addr]@: Repeat last command-line. First set cursor at line
158 [addr] (default is current line). {not in Vi}
159
160 *:@@*
161:[addr]@@ Repeat the previous :@{0-9a-z"}. First set cursor at
162 line [addr] (default is current line). {Vi: only in
163 some versions}
164
165==============================================================================
1664. Using Vim scripts *using-scripts*
167
168For writing a Vim script, see chapter 41 of the user manual |usr_41.txt|.
169
170 *:so* *:source* *load-vim-script*
171:so[urce] {file} Read Ex commands from {file}. These are commands that
172 start with a ":".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000173 Triggers the |SourcePre| autocommand.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174
175:so[urce]! {file} Read Vim commands from {file}. These are commands
176 that are executed from Normal mode, like you type
177 them.
178 When used after |:global|, |:argdo|, |:windo|,
179 |:bufdo|, in a loop or when another command follows
180 the display won't be updated while executing the
181 commands.
182 {not in Vi}
183
184 *:ru* *:runtime*
185:ru[ntime][!] {file} ..
186 Read Ex commands from {file} in each directory given
187 by 'runtimepath'. There is no error for non-existing
188 files. Example: >
189 :runtime syntax/c.vim
190
191< There can be multiple {file} arguments, separated by
192 spaces. Each {file} is searched for in the first
193 directory from 'runtimepath', then in the second
194 directory, etc. Use a backslash to include a space
195 inside {file} (although it's better not to use spaces
196 in file names, it causes trouble).
197
198 When [!] is included, all found files are sourced.
199 When it is not included only the first found file is
200 sourced.
201
202 When {file} contains wildcards it is expanded to all
203 matching files. Example: >
204 :runtime! plugin/*.vim
205< This is what Vim uses to load the plugin files when
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000206 starting up. This similar command: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 :runtime plugin/*.vim
208< would source the first file only.
209
210 When 'verbose' is one or higher, there is a message
211 when no file could be found.
212 When 'verbose' is two or higher, there is a message
213 about each searched file.
214 {not in Vi}
215
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100216 *:loadp* *:loadplugin*
217:loadp[lugin] {name} Search for an optional plugin directory and source the
218 plugin files found. It is similar to: >
219 :runtime pack/*/opt/{name}/plugin/*.vim
220< However, `:loadplugin` uses 'packpath' instead of
221 'runtimepath'. And the directory found is added to
222 'runtimepath'.
223
Bram Moolenaardae8d212016-02-27 22:40:16 +0100224 If you have a directory under 'packpath' that doesn't
225 actually have a plugin file, just create an empty one.
226 This will still add the directory to 'runtimepath'.
227
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100228 Note that {name} is the directory name, not the name
229 of the .vim file. If the "{name}/plugin" directory
230 contains more than one file they are all sourced.
231
232 Also see |load-plugin|.
233
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234:scripte[ncoding] [encoding] *:scripte* *:scriptencoding* *E167*
235 Specify the character encoding used in the script.
236 The following lines will be converted from [encoding]
237 to the value of the 'encoding' option, if they are
238 different. Examples: >
239 scriptencoding iso-8859-5
240 scriptencoding cp932
241<
242 When [encoding] is empty, no conversion is done. This
243 can be used to restrict conversion to a sequence of
244 lines: >
245 scriptencoding euc-jp
246 ... lines to be converted ...
247 scriptencoding
248 ... not converted ...
249
250< When conversion isn't supported by the system, there
251 is no error message and no conversion is done.
252
253 Don't use "ucs-2" or "ucs-4", scripts cannot be in
254 these encodings (they would contain NUL bytes).
255 When a sourced script starts with a BOM (Byte Order
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200256 Mark) in utf-8 format Vim will recognize it, no need
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 to use ":scriptencoding utf-8" then.
258
259 When compiled without the |+multi_byte| feature this
260 command is ignored.
261 {not in Vi}
262
Bram Moolenaar8feef4f2015-01-07 16:57:10 +0100263 *:scr* *:scriptnames*
264:scr[iptnames] List all sourced script names, in the order they were
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 first sourced. The number is used for the script ID
266 |<SID>|.
267 {not in Vi} {not available when compiled without the
268 |+eval| feature}
269
270 *:fini* *:finish* *E168*
271:fini[sh] Stop sourcing a script. Can only be used in a Vim
272 script file. This is a quick way to skip the rest of
273 the file. If it is used after a |:try| but before the
274 matching |:finally| (if present), the commands
275 following the ":finally" up to the matching |:endtry|
276 are executed first. This process applies to all
277 nested ":try"s in the script. The outermost ":endtry"
278 then stops sourcing the script. {not in Vi}
279
280All commands and command sequences can be repeated by putting them in a named
281register and then executing it. There are two ways to get the commands in the
282register:
283- Use the record command "q". You type the commands once, and while they are
284 being executed they are stored in a register. Easy, because you can see
285 what you are doing. If you make a mistake, "p"ut the register into the
286 file, edit the command sequence, and then delete it into the register
287 again. You can continue recording by appending to the register (use an
288 uppercase letter).
289- Delete or yank the command sequence into the register.
290
291Often used command sequences can be put under a function key with the ':map'
292command.
293
294An alternative is to put the commands in a file, and execute them with the
295':source!' command. Useful for long command sequences. Can be combined with
296the ':map' command to put complicated commands under a function key.
297
298The ':source' command reads Ex commands from a file line by line. You will
299have to type any needed keyboard input. The ':source!' command reads from a
300script file character by character, interpreting each character as if you
301typed it.
302
303Example: When you give the ":!ls" command you get the |hit-enter| prompt. If
304you ':source' a file with the line "!ls" in it, you will have to type the
305<Enter> yourself. But if you ':source!' a file with the line ":!ls" in it,
306the next characters from that file are read until a <CR> is found. You will
307not have to type <CR> yourself, unless ":!ls" was the last line in the file.
308
309It is possible to put ':source[!]' commands in the script file, so you can
310make a top-down hierarchy of script files. The ':source' command can be
311nested as deep as the number of files that can be opened at one time (about
31215). The ':source!' command can be nested up to 15 levels deep.
313
314You can use the "<sfile>" string (literally, this is not a special key) inside
315of the sourced file, in places where a file name is expected. It will be
316replaced by the file name of the sourced file. For example, if you have a
317"other.vimrc" file in the same directory as your ".vimrc" file, you can source
318it from your ".vimrc" file with this command: >
319 :source <sfile>:h/other.vimrc
320
321In script files terminal-dependent key codes are represented by
322terminal-independent two character codes. This means that they can be used
323in the same way on different kinds of terminals. The first character of a
324key code is 0x80 or 128, shown on the screen as "~@". The second one can be
325found in the list |key-notation|. Any of these codes can also be entered
326with CTRL-V followed by the three digit decimal code. This does NOT work for
327the <t_xx> termcap codes, these can only be used in mappings.
328
329 *:source_crnl* *W15*
330MS-DOS, Win32 and OS/2: Files that are read with ":source" normally have
331<CR><NL> <EOL>s. These always work. If you are using a file with <NL> <EOL>s
332(for example, a file made on Unix), this will be recognized if 'fileformats'
333is not empty and the first line does not end in a <CR>. This fails if the
334first line has something like ":map <F1> :help^M", where "^M" is a <CR>. If
335the first line ends in a <CR>, but following ones don't, you will get an error
336message, because the <CR> from the first lines will be lost.
337
Bram Moolenaar520470a2005-06-16 21:59:56 +0000338Mac Classic: Files that are read with ":source" normally have <CR> <EOL>s.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339These always work. If you are using a file with <NL> <EOL>s (for example, a
340file made on Unix), this will be recognized if 'fileformats' is not empty and
341the first line does not end in a <CR>. Be careful not to use a file with <NL>
342linebreaks which has a <CR> in first line.
343
344On other systems, Vim expects ":source"ed files to end in a <NL>. These
345always work. If you are using a file with <CR><NL> <EOL>s (for example, a
346file made on MS-DOS), all lines will have a trailing <CR>. This may cause
347problems for some commands (e.g., mappings). There is no automatic <EOL>
348detection, because it's common to start with a line that defines a mapping
349that ends in a <CR>, which will confuse the automaton.
350
351 *line-continuation*
352Long lines in a ":source"d Ex command script file can be split by inserting
353a line continuation symbol "\" (backslash) at the start of the next line.
354There can be white space before the backslash, which is ignored.
355
356Example: the lines >
357 :set comments=sr:/*,mb:*,el:*/,
358 \://,
359 \b:#,
360 \:%,
361 \n:>,
362 \fb:-
363are interpreted as if they were given in one line:
364 :set comments=sr:/*,mb:*,el:*/,://,b:#,:%,n:>,fb:-
365
366All leading whitespace characters in the line before a backslash are ignored.
367Note however that trailing whitespace in the line before it cannot be
368inserted freely; it depends on the position where a command is split up
369whether additional whitespace is allowed or not.
370
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100371When a space is required it's best to put it right after the backslash. A
372space at the end of a line is hard to see and may be accidentally deleted. >
373 :syn match Comment
374 \ "very long regexp"
375 \ keepend
376
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377There is a problem with the ":append" and ":insert" commands: >
378 :1append
379 \asdf
380 .
381The backslash is seen as a line-continuation symbol, thus this results in the
382command: >
383 :1appendasdf
384 .
385To avoid this, add the 'C' flag to the 'cpoptions' option: >
386 :set cpo+=C
387 :1append
388 \asdf
389 .
390 :set cpo-=C
391
392Note that when the commands are inside a function, you need to add the 'C'
393flag when defining the function, it is not relevant when executing it. >
394 :set cpo+=C
395 :function Foo()
396 :1append
397 \asdf
398 .
399 :endfunction
400 :set cpo-=C
401
402Rationale:
403 Most programs work with a trailing backslash to indicate line
404 continuation. Using this in Vim would cause incompatibility with Vi.
405 For example for this Vi mapping: >
406 :map xx asdf\
407< Therefore the unusual leading backslash is used.
408
409==============================================================================
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01004105. Using Vim packages *packages*
411
412A Vim package is a directory that contains one or more plugins. The
413advantages over normal plugins:
414- A package can be downloaded as an archive and unpacked in its own directory.
415 That makes it easy to updated and/or remove.
Bram Moolenaar91715872016-03-03 17:13:03 +0100416- A package can be a git, mercurial, etc. repository. That makes it really
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100417 easy to update.
418- A package can contain multiple plugins that depend on each other.
419- A package can contain plugins that are automatically loaded on startup and
420 ones that are only loaded when needed with `:loadplugin`.
421
422Let's assume your Vim files are in the "~/.vim" directory and you want to add a
423package from a zip archive "/tmp/mypack.zip":
424 % mkdir -p ~/.vim/pack/my
425 % cd ~/.vim/pack/my
426 % unzip /tmp/mypack.zip
427
428The directory name "my" is arbitrary, you can pick anything you like.
429
430You would now have these files under ~/.vim:
431 pack/my/README.txt
432 pack/my/ever/always/plugin/always.vim
433 pack/my/ever/always/syntax/always.vim
434 pack/my/opt/mydebug/plugin/debugger.vim
435
436When Vim starts up it scans all directories in 'packpath' for plugins under the
437"ever" directory and loads them. When found that directory is added to
438'runtimepath'.
439
440In the example Vim will find "my/ever/always/plugin/always.vim" and adds
441"~/.vim/pack/my/ever/always" to 'runtimepath'.
442
443If the "always" plugin kicks in and sets the 'filetype' to "always", Vim will
444find the syntax/always.vim file, because its directory is in 'runtimepath'.
445
Bram Moolenaar91715872016-03-03 17:13:03 +0100446Vim will also load ftdetect files, like with |:loadplugin|.
447
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100448 *load-plugin*
449To load an optional plugin from a pack use the `:loadplugin` command: >
450 :loadplugin mydebug
451This could be done inside always.vim, if some conditions are met.
452Or you could add this command to your |.vimrc|.
453
454It is perfectly normal for a package to only have files in the "opt"
455directory. You then need to load each plugin when you want to use it.
456
457Loading packages will not happen if loading plugins is disabled, see
458|load-plugins|.
459
460==============================================================================
4616. Debugging scripts *debug-scripts*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462
463Besides the obvious messages that you can add to your scripts to find out what
464they are doing, Vim offers a debug mode. This allows you to step through a
465sourced file or user function and set breakpoints.
466
467NOTE: The debugging mode is far from perfect. Debugging will have side
468effects on how Vim works. You cannot use it to debug everything. For
469example, the display is messed up by the debugging messages.
470{Vi does not have a debug mode}
471
472An alternative to debug mode is setting the 'verbose' option. With a bigger
473number it will give more verbose messages about what Vim is doing.
474
475
476STARTING DEBUG MODE *debug-mode*
477
478To enter debugging mode use one of these methods:
4791. Start Vim with the |-D| argument: >
480 vim -D file.txt
481< Debugging will start as soon as the first vimrc file is sourced. This is
482 useful to find out what is happening when Vim is starting up. A side
483 effect is that Vim will switch the terminal mode before initialisations
484 have finished, with unpredictable results.
485 For a GUI-only version (Windows, Macintosh) the debugging will start as
486 soon as the GUI window has been opened. To make this happen early, add a
487 ":gui" command in the vimrc file.
488 *:debug*
4892. Run a command with ":debug" prepended. Debugging will only be done while
490 this command executes. Useful for debugging a specific script or user
491 function. And for scripts and functions used by autocommands. Example: >
492 :debug edit test.txt.gz
493
4943. Set a breakpoint in a sourced file or user function. You could do this in
495 the command line: >
496 vim -c "breakadd file */explorer.vim" .
497< This will run Vim and stop in the first line of the "explorer.vim" script.
498 Breakpoints can also be set while in debugging mode.
499
500In debugging mode every executed command is displayed before it is executed.
501Comment lines, empty lines and lines that are not executed are skipped. When
502a line contains two commands, separated by "|", each command will be displayed
503separately.
504
505
506DEBUG MODE
507
508Once in debugging mode, the usual Ex commands can be used. For example, to
509inspect the value of a variable: >
510 echo idx
511When inside a user function, this will print the value of the local variable
512"idx". Prepend "g:" to get the value of a global variable: >
513 echo g:idx
514All commands are executed in the context of the current function or script.
515You can also set options, for example setting or resetting 'verbose' will show
516what happens, but you might want to set it just before executing the lines you
517are interested in: >
518 :set verbose=20
519
520Commands that require updating the screen should be avoided, because their
521effect won't be noticed until after leaving debug mode. For example: >
522 :help
523won't be very helpful.
524
525There is a separate command-line history for debug mode.
526
527The line number for a function line is relative to the start of the function.
528If you have trouble figuring out where you are, edit the file that defines
529the function in another Vim, search for the start of the function and do
530"99j". Replace "99" with the line number.
531
532Additionally, these commands can be used:
533 *>cont*
534 cont Continue execution until the next breakpoint is hit.
535 *>quit*
536 quit Abort execution. This is like using CTRL-C, some
537 things might still be executed, doesn't abort
538 everything. Still stops at the next breakpoint.
539 *>next*
540 next Execute the command and come back to debug mode when
541 it's finished. This steps over user function calls
542 and sourced files.
543 *>step*
544 step Execute the command and come back to debug mode for
545 the next command. This steps into called user
546 functions and sourced files.
547 *>interrupt*
548 interrupt This is like using CTRL-C, but unlike ">quit" comes
549 back to debug mode for the next command that is
550 executed. Useful for testing |:finally| and |:catch|
551 on interrupt exceptions.
552 *>finish*
553 finish Finish the current script or user function and come
554 back to debug mode for the command after the one that
555 sourced or called it.
Bram Moolenaarf1f60f82016-01-16 15:40:53 +0100556 *>bt*
557 *>backtrace*
558 *>where*
559 backtrace Show the call stacktrace for current debugging session.
560 bt
561 where
562 *>frame*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100563 frame N Goes to N backtrace level. + and - signs make movement
Bram Moolenaarf1f60f82016-01-16 15:40:53 +0100564 relative. E.g., ":frame +3" goes three frames up.
565 *>up*
566 up Goes one level up from call stacktrace.
567 *>down*
568 down Goes one level down from call stacktrace.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569
570About the additional commands in debug mode:
571- There is no command-line completion for them, you get the completion for the
572 normal Ex commands only.
Bram Moolenaardae8d212016-02-27 22:40:16 +0100573- You can shorten them, up to a single character, unless more than one command
Bram Moolenaarf1f60f82016-01-16 15:40:53 +0100574 starts with the same letter. "f" stands for "finish", use "fr" for "frame".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575- Hitting <CR> will repeat the previous one. When doing another command, this
576 is reset (because it's not clear what you want to repeat).
577- When you want to use the Ex command with the same name, prepend a colon:
578 ":cont", ":next", ":finish" (or shorter).
579
Bram Moolenaarf1f60f82016-01-16 15:40:53 +0100580The backtrace shows the hierarchy of function calls, e.g.:
581 >bt ~
582 3 function One[3] ~
583 2 Two[3] ~
584 ->1 Three[3] ~
585 0 Four ~
586 line 1: let four = 4 ~
587
588The "->" points to the current frame. Use "up", "down" and "frame N" to
589select another frame.
590
591In the current frame you can evaluate the local function variables. There is
592no way to see the command at the current line yet.
593
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594
595DEFINING BREAKPOINTS
596 *:breaka* *:breakadd*
597:breaka[dd] func [lnum] {name}
598 Set a breakpoint in a function. Example: >
599 :breakadd func Explore
600< Doesn't check for a valid function name, thus the breakpoint
601 can be set before the function is defined.
602
603:breaka[dd] file [lnum] {name}
604 Set a breakpoint in a sourced file. Example: >
605 :breakadd file 43 .vimrc
606
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000607:breaka[dd] here
608 Set a breakpoint in the current line of the current file.
609 Like doing: >
610 :breakadd file <cursor-line> <current-file>
611< Note that this only works for commands that are executed when
612 sourcing the file, not for a function defined in that file.
613
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614The [lnum] is the line number of the breakpoint. Vim will stop at or after
615this line. When omitted line 1 is used.
616
Bram Moolenaar05159a02005-02-26 23:04:13 +0000617 *:debug-name*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{name} is a pattern that is matched with the file or function name. The
619pattern is like what is used for autocommands. There must be a full match (as
620if the pattern starts with "^" and ends in "$"). A "*" matches any sequence
621of characters. 'ignorecase' is not used, but "\c" can be used in the pattern
622to ignore case |/\c|. Don't include the () for the function name!
623
Bram Moolenaar843ee412004-06-30 16:16:41 +0000624The match for sourced scripts is done against the full file name. If no path
625is specified the current directory is used. Examples: >
626 breakadd file explorer.vim
627matches "explorer.vim" in the current directory. >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 breakadd file *explorer.vim
Bram Moolenaar843ee412004-06-30 16:16:41 +0000629matches ".../plugin/explorer.vim", ".../plugin/iexplorer.vim", etc. >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 breakadd file */explorer.vim
Bram Moolenaar843ee412004-06-30 16:16:41 +0000631matches ".../plugin/explorer.vim" and "explorer.vim" in any other directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632
633The match for functions is done against the name as it's shown in the output
634of ":function". For local functions this means that something like "<SNR>99_"
635is prepended.
636
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000637Note that functions are first loaded and later executed. When they are loaded
638the "file" breakpoints are checked, when they are executed the "func"
639breakpoints.
640
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641
642DELETING BREAKPOINTS
643 *:breakd* *:breakdel* *E161*
644:breakd[el] {nr}
645 Delete breakpoint {nr}. Use |:breaklist| to see the number of
646 each breakpoint.
647
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000648:breakd[el] *
649 Delete all breakpoints.
650
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651:breakd[el] func [lnum] {name}
652 Delete a breakpoint in a function.
653
654:breakd[el] file [lnum] {name}
655 Delete a breakpoint in a sourced file.
656
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000657:breakd[el] here
658 Delete a breakpoint at the current line of the current file.
659
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660When [lnum] is omitted, the first breakpoint in the function or file is
661deleted.
662The {name} must be exactly the same as what was typed for the ":breakadd"
663command. "explorer", "*explorer.vim" and "*explorer*" are different.
664
665
666LISTING BREAKPOINTS
667 *:breakl* *:breaklist*
668:breakl[ist]
669 List all breakpoints.
670
671
672OBSCURE
673
674 *:debugg* *:debuggreedy*
675:debugg[reedy]
676 Read debug mode commands from the normal input stream, instead
677 of getting them directly from the user. Only useful for test
678 scripts. Example: >
679 echo 'q^Mq' | vim -e -s -c debuggreedy -c 'breakadd file script.vim' -S script.vim
680
681:0debugg[reedy]
682 Undo ":debuggreedy": get debug mode commands directly from the
683 user, don't use typeahead for debug commands.
684
Bram Moolenaar05159a02005-02-26 23:04:13 +0000685==============================================================================
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006867. Profiling *profile* *profiling*
Bram Moolenaar05159a02005-02-26 23:04:13 +0000687
Bram Moolenaar996343d2010-07-04 22:20:21 +0200688Profiling means that Vim measures the time that is spent on executing
Bram Moolenaar05159a02005-02-26 23:04:13 +0000689functions and/or scripts. The |+profile| feature is required for this.
690It is only included when Vim was compiled with "huge" features.
691{Vi does not have profiling}
692
Bram Moolenaar433f7c82006-03-21 21:29:36 +0000693You can also use the |reltime()| function to measure time. This only requires
694the |+reltime| feature, which is present more often.
695
Bram Moolenaar16ea3672013-07-28 16:02:18 +0200696For profiling syntax highlighting see |:syntime|.
697
Bram Moolenaar76f3b1a2014-03-27 22:30:07 +0100698For example, to profile the one_script.vim script file: >
699 :profile start /tmp/one_script_profile
700 :profile file one_script.vim
701 :source one_script.vim
702 :exit
703
Bram Moolenaar16ea3672013-07-28 16:02:18 +0200704
Bram Moolenaar05159a02005-02-26 23:04:13 +0000705:prof[ile] start {fname} *:prof* *:profile* *E750*
706 Start profiling, write the output in {fname} upon exit.
Bram Moolenaar0a63ded2015-04-15 13:31:24 +0200707 "~/" and environment variables in {fname} will be expanded.
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000708 If {fname} already exists it will be silently overwritten.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000709 The variable |v:profiling| is set to one.
710
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000711:prof[ile] pause
712 Don't profile until the following ":profile continue". Can be
713 used when doing something that should not be counted (e.g., an
714 external command). Does not nest.
715
716:prof[ile] continue
717 Continue profiling after ":profile pause".
718
Bram Moolenaar05159a02005-02-26 23:04:13 +0000719:prof[ile] func {pattern}
720 Profile function that matches the pattern {pattern}.
721 See |:debug-name| for how {pattern} is used.
722
723:prof[ile][!] file {pattern}
724 Profile script file that matches the pattern {pattern}.
725 See |:debug-name| for how {pattern} is used.
726 This only profiles the script itself, not the functions
727 defined in it.
728 When the [!] is added then all functions defined in the script
Bram Moolenaar76f3b1a2014-03-27 22:30:07 +0100729 will also be profiled.
730 Note that profiling only starts when the script is loaded
731 after this command. A :profile command in the script itself
732 won't work.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000733
734
Bram Moolenaard9fba312005-06-26 22:34:35 +0000735:profd[el] ... *:profd* *:profdel*
736 Stop profiling for the arguments specified. See |:breakdel|
737 for the arguments.
738
739
Bram Moolenaar05159a02005-02-26 23:04:13 +0000740You must always start with a ":profile start fname" command. The resulting
741file is written when Vim exits. Here is an example of the output, with line
742numbers prepended for the explanation:
743
744 1 FUNCTION Test2() ~
745 2 Called 1 time ~
746 3 Total time: 0.155251 ~
747 4 Self time: 0.002006 ~
748 5 ~
749 6 count total (s) self (s) ~
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000750 7 9 0.000096 for i in range(8) ~
751 8 8 0.153655 0.000410 call Test3() ~
752 9 8 0.000070 endfor ~
753 10 " Ask a question ~
754 11 1 0.001341 echo input("give me an answer: ") ~
Bram Moolenaar05159a02005-02-26 23:04:13 +0000755
756The header (lines 1-4) gives the time for the whole function. The "Total"
757time is the time passed while the function was executing. The "Self" time is
758the "Total" time reduced by time spent in:
759- other user defined functions
760- sourced scripts
761- executed autocommands
762- external (shell) commands
763
764Lines 7-11 show the time spent in each executed line. Lines that are not
765executed do not count. Thus a comment line is never counted.
766
767The Count column shows how many times a line was executed. Note that the
768"for" command in line 7 is executed one more time as the following lines.
769That is because the line is also executed to detect the end of the loop.
770
771The time Vim spends waiting for user input isn't counted at all. Thus how
772long you take to respond to the input() prompt is irrelevant.
773
774Profiling should give a good indication of where time is spent, but keep in
775mind there are various things that may clobber the results:
776
777- The accuracy of the time measured depends on the gettimeofday() system
778 function. It may only be as accurate as 1/100 second, even though the times
779 are displayed in micro seconds.
780
781- Real elapsed time is measured, if other processes are busy they may cause
782 delays at unpredictable moments. You may want to run the profiling several
783 times and use the lowest results.
784
785- If you have several commands in one line you only get one time. Split the
786 line to see the time for the individual commands.
787
788- The time of the lines added up is mostly less than the time of the whole
789 function. There is some overhead in between.
790
791- Functions that are deleted before Vim exits will not produce profiling
792 information. You can check the |v:profiling| variable if needed: >
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000793 :if !v:profiling
Bram Moolenaar05159a02005-02-26 23:04:13 +0000794 : delfunc MyFunc
795 :endif
796<
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000797- Profiling may give weird results on multi-processor systems, when sleep
798 mode kicks in or the processor frequency is reduced to save power.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000799
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000800- The "self" time is wrong when a function is used recursively.
801
802
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 vim:tw=78:ts=8:ft=help:norl: