blob: 9399e1f237c4e71d39856761953fac98c43d4fd1 [file] [log] [blame]
Bram Moolenaaraf1a0e32016-03-09 22:19:26 +01001*repeat.txt* For Vim version 7.4. Last change: 2016 Mar 09
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*
Bram Moolenaar8dcf2592016-03-12 22:47:14 +0100185:ru[ntime][!] [where] {file} ..
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 Read Ex commands from {file} in each directory given
Bram Moolenaar8dcf2592016-03-12 22:47:14 +0100187 by 'runtimepath' and/or 'packpath'. There is no error
188 for non-existing files.
189
190 Example: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 :runtime syntax/c.vim
192
193< There can be multiple {file} arguments, separated by
194 spaces. Each {file} is searched for in the first
195 directory from 'runtimepath', then in the second
196 directory, etc. Use a backslash to include a space
197 inside {file} (although it's better not to use spaces
198 in file names, it causes trouble).
199
200 When [!] is included, all found files are sourced.
201 When it is not included only the first found file is
202 sourced.
203
Bram Moolenaar8dcf2592016-03-12 22:47:14 +0100204 When [where] is omitted only 'runtimepath' is used.
205 Other values:
206 START search under "start" in 'packpath'
207 OPT search under "opt" in 'packpath'
208 PACK search under "start" and "opt" in
209 'packpath'
210 ALL first use 'runtimepath', then search
211 under "start" and "opt" in 'packpath'
212
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 When {file} contains wildcards it is expanded to all
214 matching files. Example: >
215 :runtime! plugin/*.vim
216< This is what Vim uses to load the plugin files when
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000217 starting up. This similar command: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 :runtime plugin/*.vim
219< would source the first file only.
220
221 When 'verbose' is one or higher, there is a message
222 when no file could be found.
223 When 'verbose' is two or higher, there is a message
224 about each searched file.
225 {not in Vi}
226
Bram Moolenaarbe82c252016-03-06 14:44:08 +0100227 *:pa* *:packadd* *E919*
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100228:pa[ckadd][!] {name} Search for an optional plugin directory in 'packpath'
229 and source any plugin files found. The directory must
230 match:
231 pack/*/opt/{name} ~
232 The directory is added to 'runtimepath' if it wasn't
233 there yet.
Bram Moolenaardae8d212016-02-27 22:40:16 +0100234
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100235 Note that {name} is the directory name, not the name
236 of the .vim file. If the "{name}/plugin" directory
237 contains more than one file they are all sourced.
238
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100239 If the filetype detection was not enabled yet (this
240 is usually done with a "syntax enable" or "filetype
241 on" command in your .vimrc file), this will also look
242 for "{name}/ftdetect/*.vim" files.
243
244 When the optional ! is added no plugin files or
245 ftdetect scripts are loaded, only the matching
246 directories are added to 'runtimepath'. This is
247 useful in your .vimrc. The plugins will then be
248 loaded during initialization, see |load-plugins|.
249
250 Also see |pack-add|.
251
Bram Moolenaar8dcf2592016-03-12 22:47:14 +0100252:packloadall[!] Load all packages in the "start" directories under
253 'packpath'. The directories found are added to
254 'runtimepath'.
255 This normally done during startup, after loading your
256 .vimrc file. With this command it can be done
257 earlier.
258 Packages will be loaded only once. After this command
259 it won't happen again. When the optional ! is added
260 this command will load packages even when done before.
261 See |packages|.
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100262
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263:scripte[ncoding] [encoding] *:scripte* *:scriptencoding* *E167*
264 Specify the character encoding used in the script.
265 The following lines will be converted from [encoding]
266 to the value of the 'encoding' option, if they are
267 different. Examples: >
268 scriptencoding iso-8859-5
269 scriptencoding cp932
270<
271 When [encoding] is empty, no conversion is done. This
272 can be used to restrict conversion to a sequence of
273 lines: >
274 scriptencoding euc-jp
275 ... lines to be converted ...
276 scriptencoding
277 ... not converted ...
278
279< When conversion isn't supported by the system, there
280 is no error message and no conversion is done.
281
282 Don't use "ucs-2" or "ucs-4", scripts cannot be in
283 these encodings (they would contain NUL bytes).
284 When a sourced script starts with a BOM (Byte Order
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200285 Mark) in utf-8 format Vim will recognize it, no need
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 to use ":scriptencoding utf-8" then.
287
288 When compiled without the |+multi_byte| feature this
289 command is ignored.
290 {not in Vi}
291
Bram Moolenaar8feef4f2015-01-07 16:57:10 +0100292 *:scr* *:scriptnames*
293:scr[iptnames] List all sourced script names, in the order they were
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 first sourced. The number is used for the script ID
295 |<SID>|.
296 {not in Vi} {not available when compiled without the
297 |+eval| feature}
298
299 *:fini* *:finish* *E168*
300:fini[sh] Stop sourcing a script. Can only be used in a Vim
301 script file. This is a quick way to skip the rest of
302 the file. If it is used after a |:try| but before the
303 matching |:finally| (if present), the commands
304 following the ":finally" up to the matching |:endtry|
305 are executed first. This process applies to all
306 nested ":try"s in the script. The outermost ":endtry"
307 then stops sourcing the script. {not in Vi}
308
309All commands and command sequences can be repeated by putting them in a named
310register and then executing it. There are two ways to get the commands in the
311register:
312- Use the record command "q". You type the commands once, and while they are
313 being executed they are stored in a register. Easy, because you can see
314 what you are doing. If you make a mistake, "p"ut the register into the
315 file, edit the command sequence, and then delete it into the register
316 again. You can continue recording by appending to the register (use an
317 uppercase letter).
318- Delete or yank the command sequence into the register.
319
320Often used command sequences can be put under a function key with the ':map'
321command.
322
323An alternative is to put the commands in a file, and execute them with the
324':source!' command. Useful for long command sequences. Can be combined with
325the ':map' command to put complicated commands under a function key.
326
327The ':source' command reads Ex commands from a file line by line. You will
328have to type any needed keyboard input. The ':source!' command reads from a
329script file character by character, interpreting each character as if you
330typed it.
331
332Example: When you give the ":!ls" command you get the |hit-enter| prompt. If
333you ':source' a file with the line "!ls" in it, you will have to type the
334<Enter> yourself. But if you ':source!' a file with the line ":!ls" in it,
335the next characters from that file are read until a <CR> is found. You will
336not have to type <CR> yourself, unless ":!ls" was the last line in the file.
337
338It is possible to put ':source[!]' commands in the script file, so you can
339make a top-down hierarchy of script files. The ':source' command can be
340nested as deep as the number of files that can be opened at one time (about
34115). The ':source!' command can be nested up to 15 levels deep.
342
343You can use the "<sfile>" string (literally, this is not a special key) inside
344of the sourced file, in places where a file name is expected. It will be
345replaced by the file name of the sourced file. For example, if you have a
346"other.vimrc" file in the same directory as your ".vimrc" file, you can source
347it from your ".vimrc" file with this command: >
348 :source <sfile>:h/other.vimrc
349
350In script files terminal-dependent key codes are represented by
351terminal-independent two character codes. This means that they can be used
352in the same way on different kinds of terminals. The first character of a
353key code is 0x80 or 128, shown on the screen as "~@". The second one can be
354found in the list |key-notation|. Any of these codes can also be entered
355with CTRL-V followed by the three digit decimal code. This does NOT work for
356the <t_xx> termcap codes, these can only be used in mappings.
357
358 *:source_crnl* *W15*
359MS-DOS, Win32 and OS/2: Files that are read with ":source" normally have
360<CR><NL> <EOL>s. These always work. If you are using a file with <NL> <EOL>s
361(for example, a file made on Unix), this will be recognized if 'fileformats'
362is not empty and the first line does not end in a <CR>. This fails if the
363first line has something like ":map <F1> :help^M", where "^M" is a <CR>. If
364the first line ends in a <CR>, but following ones don't, you will get an error
365message, because the <CR> from the first lines will be lost.
366
Bram Moolenaar520470a2005-06-16 21:59:56 +0000367Mac Classic: Files that are read with ":source" normally have <CR> <EOL>s.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368These always work. If you are using a file with <NL> <EOL>s (for example, a
369file made on Unix), this will be recognized if 'fileformats' is not empty and
370the first line does not end in a <CR>. Be careful not to use a file with <NL>
371linebreaks which has a <CR> in first line.
372
373On other systems, Vim expects ":source"ed files to end in a <NL>. These
374always work. If you are using a file with <CR><NL> <EOL>s (for example, a
375file made on MS-DOS), all lines will have a trailing <CR>. This may cause
376problems for some commands (e.g., mappings). There is no automatic <EOL>
377detection, because it's common to start with a line that defines a mapping
378that ends in a <CR>, which will confuse the automaton.
379
380 *line-continuation*
381Long lines in a ":source"d Ex command script file can be split by inserting
382a line continuation symbol "\" (backslash) at the start of the next line.
383There can be white space before the backslash, which is ignored.
384
385Example: the lines >
386 :set comments=sr:/*,mb:*,el:*/,
387 \://,
388 \b:#,
389 \:%,
390 \n:>,
391 \fb:-
392are interpreted as if they were given in one line:
393 :set comments=sr:/*,mb:*,el:*/,://,b:#,:%,n:>,fb:-
394
395All leading whitespace characters in the line before a backslash are ignored.
396Note however that trailing whitespace in the line before it cannot be
397inserted freely; it depends on the position where a command is split up
398whether additional whitespace is allowed or not.
399
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100400When a space is required it's best to put it right after the backslash. A
401space at the end of a line is hard to see and may be accidentally deleted. >
402 :syn match Comment
403 \ "very long regexp"
404 \ keepend
405
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406There is a problem with the ":append" and ":insert" commands: >
407 :1append
408 \asdf
409 .
410The backslash is seen as a line-continuation symbol, thus this results in the
411command: >
412 :1appendasdf
413 .
414To avoid this, add the 'C' flag to the 'cpoptions' option: >
415 :set cpo+=C
416 :1append
417 \asdf
418 .
419 :set cpo-=C
420
421Note that when the commands are inside a function, you need to add the 'C'
422flag when defining the function, it is not relevant when executing it. >
423 :set cpo+=C
424 :function Foo()
425 :1append
426 \asdf
427 .
428 :endfunction
429 :set cpo-=C
430
431Rationale:
432 Most programs work with a trailing backslash to indicate line
433 continuation. Using this in Vim would cause incompatibility with Vi.
434 For example for this Vi mapping: >
435 :map xx asdf\
436< Therefore the unusual leading backslash is used.
437
438==============================================================================
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01004395. Using Vim packages *packages*
440
441A Vim package is a directory that contains one or more plugins. The
442advantages over normal plugins:
443- A package can be downloaded as an archive and unpacked in its own directory.
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100444 Thus the files are not mixed with files of other plugins. That makes it
445 easy to update and remove.
Bram Moolenaar91715872016-03-03 17:13:03 +0100446- A package can be a git, mercurial, etc. repository. That makes it really
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100447 easy to update.
448- A package can contain multiple plugins that depend on each other.
449- A package can contain plugins that are automatically loaded on startup and
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100450 ones that are only loaded when needed with `:packadd`.
451
452
453Using a package and loading automatically ~
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100454
455Let's assume your Vim files are in the "~/.vim" directory and you want to add a
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100456package from a zip archive "/tmp/foopack.zip":
457 % mkdir -p ~/.vim/pack/foo
458 % cd ~/.vim/pack/foo
459 % unzip /tmp/foopack.zip
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100460
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100461The directory name "foo" is arbitrary, you can pick anything you like.
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100462
463You would now have these files under ~/.vim:
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100464 pack/foo/README.txt
Bram Moolenaaraf1a0e32016-03-09 22:19:26 +0100465 pack/foo/start/foobar/plugin/foo.vim
466 pack/foo/start/foobar/syntax/some.vim
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100467 pack/foo/opt/foodebug/plugin/debugger.vim
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100468
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100469When Vim starts up, after processing your .vimrc, it scans all directories in
Bram Moolenaaraf1a0e32016-03-09 22:19:26 +0100470'packpath' for plugins under the "pack/*/start" directory and loads them. The
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100471directory is added to 'runtimepath'.
Bram Moolenaarf3654822016-03-04 22:12:23 +0100472
Bram Moolenaaraf1a0e32016-03-09 22:19:26 +0100473In the example Vim will find "pack/foo/start/foobar/plugin/foo.vim" and adds
474"~/.vim/pack/foo/start/foobar" to 'runtimepath'.
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100475
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100476If the "foobar" plugin kicks in and sets the 'filetype' to "some", Vim will
477find the syntax/some.vim file, because its directory is in 'runtimepath'.
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100478
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100479Vim will also load ftdetect files, if there are any.
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100480
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100481Note that the files under "pack/foo/opt" or not loaded automatically, only the
Bram Moolenaaraf1a0e32016-03-09 22:19:26 +0100482ones under "pack/foo/start". See |pack-add| below for how the "opt" directory
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100483is used.
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100484
Bram Moolenaar8dcf2592016-03-12 22:47:14 +0100485Loading packages automatically will not happen if loading plugins is disabled,
486see |load-plugins|.
487
488To load packages earlier, so that 'runtimepath' gets updated: >
489 :packloadall
490This also works when loading plugins is disabled. The automatic loading will
491only happen once.
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100492
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100493
494Using a single plugin and loading it automatically ~
495
496If you don't have a package but a single plugin, you need to create the extra
497directory level:
Bram Moolenaaraf1a0e32016-03-09 22:19:26 +0100498 % mkdir -p ~/.vim/pack/foo/start/foobar
499 % cd ~/.vim/pack/foo/start/foobar
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100500 % unzip /tmp/someplugin.zip
501
502You would now have these files:
Bram Moolenaaraf1a0e32016-03-09 22:19:26 +0100503 pack/foo/start/foobar/plugin/foo.vim
504 pack/foo/start/foobar/syntax/some.vim
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100505
506From here it works like above.
507
508
509Optional plugins ~
510 *pack-add*
511To load an optional plugin from a pack use the `:packadd` command: >
512 :packadd foodebug
513This searches for "pack/*/opt/foodebug" in 'packpath' and will find
514~/.vim/pack/foo/opt/foodebug/plugin/debugger.vim and source it.
515
516This could be done inside always.vim, if some conditions are met. Or you
517could add this command to your |.vimrc|.
518
519It is perfectly normal for a package to only have files in the "opt"
520directory. You then need to load each plugin when you want to use it.
521
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100522==============================================================================
5236. Debugging scripts *debug-scripts*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524
525Besides the obvious messages that you can add to your scripts to find out what
526they are doing, Vim offers a debug mode. This allows you to step through a
527sourced file or user function and set breakpoints.
528
529NOTE: The debugging mode is far from perfect. Debugging will have side
530effects on how Vim works. You cannot use it to debug everything. For
531example, the display is messed up by the debugging messages.
532{Vi does not have a debug mode}
533
534An alternative to debug mode is setting the 'verbose' option. With a bigger
535number it will give more verbose messages about what Vim is doing.
536
537
538STARTING DEBUG MODE *debug-mode*
539
540To enter debugging mode use one of these methods:
5411. Start Vim with the |-D| argument: >
542 vim -D file.txt
543< Debugging will start as soon as the first vimrc file is sourced. This is
544 useful to find out what is happening when Vim is starting up. A side
545 effect is that Vim will switch the terminal mode before initialisations
546 have finished, with unpredictable results.
547 For a GUI-only version (Windows, Macintosh) the debugging will start as
548 soon as the GUI window has been opened. To make this happen early, add a
549 ":gui" command in the vimrc file.
550 *:debug*
5512. Run a command with ":debug" prepended. Debugging will only be done while
552 this command executes. Useful for debugging a specific script or user
553 function. And for scripts and functions used by autocommands. Example: >
554 :debug edit test.txt.gz
555
5563. Set a breakpoint in a sourced file or user function. You could do this in
557 the command line: >
558 vim -c "breakadd file */explorer.vim" .
559< This will run Vim and stop in the first line of the "explorer.vim" script.
560 Breakpoints can also be set while in debugging mode.
561
562In debugging mode every executed command is displayed before it is executed.
563Comment lines, empty lines and lines that are not executed are skipped. When
564a line contains two commands, separated by "|", each command will be displayed
565separately.
566
567
568DEBUG MODE
569
570Once in debugging mode, the usual Ex commands can be used. For example, to
571inspect the value of a variable: >
572 echo idx
573When inside a user function, this will print the value of the local variable
574"idx". Prepend "g:" to get the value of a global variable: >
575 echo g:idx
576All commands are executed in the context of the current function or script.
577You can also set options, for example setting or resetting 'verbose' will show
578what happens, but you might want to set it just before executing the lines you
579are interested in: >
580 :set verbose=20
581
582Commands that require updating the screen should be avoided, because their
583effect won't be noticed until after leaving debug mode. For example: >
584 :help
585won't be very helpful.
586
587There is a separate command-line history for debug mode.
588
589The line number for a function line is relative to the start of the function.
590If you have trouble figuring out where you are, edit the file that defines
591the function in another Vim, search for the start of the function and do
592"99j". Replace "99" with the line number.
593
594Additionally, these commands can be used:
595 *>cont*
596 cont Continue execution until the next breakpoint is hit.
597 *>quit*
598 quit Abort execution. This is like using CTRL-C, some
599 things might still be executed, doesn't abort
600 everything. Still stops at the next breakpoint.
601 *>next*
602 next Execute the command and come back to debug mode when
603 it's finished. This steps over user function calls
604 and sourced files.
605 *>step*
606 step Execute the command and come back to debug mode for
607 the next command. This steps into called user
608 functions and sourced files.
609 *>interrupt*
610 interrupt This is like using CTRL-C, but unlike ">quit" comes
611 back to debug mode for the next command that is
612 executed. Useful for testing |:finally| and |:catch|
613 on interrupt exceptions.
614 *>finish*
615 finish Finish the current script or user function and come
616 back to debug mode for the command after the one that
617 sourced or called it.
Bram Moolenaarf1f60f82016-01-16 15:40:53 +0100618 *>bt*
619 *>backtrace*
620 *>where*
621 backtrace Show the call stacktrace for current debugging session.
622 bt
623 where
624 *>frame*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100625 frame N Goes to N backtrace level. + and - signs make movement
Bram Moolenaarf1f60f82016-01-16 15:40:53 +0100626 relative. E.g., ":frame +3" goes three frames up.
627 *>up*
628 up Goes one level up from call stacktrace.
629 *>down*
630 down Goes one level down from call stacktrace.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
632About the additional commands in debug mode:
633- There is no command-line completion for them, you get the completion for the
634 normal Ex commands only.
Bram Moolenaardae8d212016-02-27 22:40:16 +0100635- You can shorten them, up to a single character, unless more than one command
Bram Moolenaarf1f60f82016-01-16 15:40:53 +0100636 starts with the same letter. "f" stands for "finish", use "fr" for "frame".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637- Hitting <CR> will repeat the previous one. When doing another command, this
638 is reset (because it's not clear what you want to repeat).
639- When you want to use the Ex command with the same name, prepend a colon:
640 ":cont", ":next", ":finish" (or shorter).
641
Bram Moolenaarf1f60f82016-01-16 15:40:53 +0100642The backtrace shows the hierarchy of function calls, e.g.:
643 >bt ~
644 3 function One[3] ~
645 2 Two[3] ~
646 ->1 Three[3] ~
647 0 Four ~
648 line 1: let four = 4 ~
649
650The "->" points to the current frame. Use "up", "down" and "frame N" to
651select another frame.
652
653In the current frame you can evaluate the local function variables. There is
654no way to see the command at the current line yet.
655
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
657DEFINING BREAKPOINTS
658 *:breaka* *:breakadd*
659:breaka[dd] func [lnum] {name}
660 Set a breakpoint in a function. Example: >
661 :breakadd func Explore
662< Doesn't check for a valid function name, thus the breakpoint
663 can be set before the function is defined.
664
665:breaka[dd] file [lnum] {name}
666 Set a breakpoint in a sourced file. Example: >
667 :breakadd file 43 .vimrc
668
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000669:breaka[dd] here
670 Set a breakpoint in the current line of the current file.
671 Like doing: >
672 :breakadd file <cursor-line> <current-file>
673< Note that this only works for commands that are executed when
674 sourcing the file, not for a function defined in that file.
675
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676The [lnum] is the line number of the breakpoint. Vim will stop at or after
677this line. When omitted line 1 is used.
678
Bram Moolenaar05159a02005-02-26 23:04:13 +0000679 *:debug-name*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680{name} is a pattern that is matched with the file or function name. The
681pattern is like what is used for autocommands. There must be a full match (as
682if the pattern starts with "^" and ends in "$"). A "*" matches any sequence
683of characters. 'ignorecase' is not used, but "\c" can be used in the pattern
684to ignore case |/\c|. Don't include the () for the function name!
685
Bram Moolenaar843ee412004-06-30 16:16:41 +0000686The match for sourced scripts is done against the full file name. If no path
687is specified the current directory is used. Examples: >
688 breakadd file explorer.vim
689matches "explorer.vim" in the current directory. >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 breakadd file *explorer.vim
Bram Moolenaar843ee412004-06-30 16:16:41 +0000691matches ".../plugin/explorer.vim", ".../plugin/iexplorer.vim", etc. >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 breakadd file */explorer.vim
Bram Moolenaar843ee412004-06-30 16:16:41 +0000693matches ".../plugin/explorer.vim" and "explorer.vim" in any other directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
695The match for functions is done against the name as it's shown in the output
696of ":function". For local functions this means that something like "<SNR>99_"
697is prepended.
698
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000699Note that functions are first loaded and later executed. When they are loaded
700the "file" breakpoints are checked, when they are executed the "func"
701breakpoints.
702
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703
704DELETING BREAKPOINTS
705 *:breakd* *:breakdel* *E161*
706:breakd[el] {nr}
707 Delete breakpoint {nr}. Use |:breaklist| to see the number of
708 each breakpoint.
709
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000710:breakd[el] *
711 Delete all breakpoints.
712
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713:breakd[el] func [lnum] {name}
714 Delete a breakpoint in a function.
715
716:breakd[el] file [lnum] {name}
717 Delete a breakpoint in a sourced file.
718
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000719:breakd[el] here
720 Delete a breakpoint at the current line of the current file.
721
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722When [lnum] is omitted, the first breakpoint in the function or file is
723deleted.
724The {name} must be exactly the same as what was typed for the ":breakadd"
725command. "explorer", "*explorer.vim" and "*explorer*" are different.
726
727
728LISTING BREAKPOINTS
729 *:breakl* *:breaklist*
730:breakl[ist]
731 List all breakpoints.
732
733
734OBSCURE
735
736 *:debugg* *:debuggreedy*
737:debugg[reedy]
738 Read debug mode commands from the normal input stream, instead
739 of getting them directly from the user. Only useful for test
740 scripts. Example: >
741 echo 'q^Mq' | vim -e -s -c debuggreedy -c 'breakadd file script.vim' -S script.vim
742
743:0debugg[reedy]
744 Undo ":debuggreedy": get debug mode commands directly from the
745 user, don't use typeahead for debug commands.
746
Bram Moolenaar05159a02005-02-26 23:04:13 +0000747==============================================================================
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007487. Profiling *profile* *profiling*
Bram Moolenaar05159a02005-02-26 23:04:13 +0000749
Bram Moolenaar996343d2010-07-04 22:20:21 +0200750Profiling means that Vim measures the time that is spent on executing
Bram Moolenaar05159a02005-02-26 23:04:13 +0000751functions and/or scripts. The |+profile| feature is required for this.
752It is only included when Vim was compiled with "huge" features.
753{Vi does not have profiling}
754
Bram Moolenaar433f7c82006-03-21 21:29:36 +0000755You can also use the |reltime()| function to measure time. This only requires
756the |+reltime| feature, which is present more often.
757
Bram Moolenaar16ea3672013-07-28 16:02:18 +0200758For profiling syntax highlighting see |:syntime|.
759
Bram Moolenaar76f3b1a2014-03-27 22:30:07 +0100760For example, to profile the one_script.vim script file: >
761 :profile start /tmp/one_script_profile
762 :profile file one_script.vim
763 :source one_script.vim
764 :exit
765
Bram Moolenaar16ea3672013-07-28 16:02:18 +0200766
Bram Moolenaar05159a02005-02-26 23:04:13 +0000767:prof[ile] start {fname} *:prof* *:profile* *E750*
768 Start profiling, write the output in {fname} upon exit.
Bram Moolenaar0a63ded2015-04-15 13:31:24 +0200769 "~/" and environment variables in {fname} will be expanded.
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000770 If {fname} already exists it will be silently overwritten.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000771 The variable |v:profiling| is set to one.
772
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000773:prof[ile] pause
774 Don't profile until the following ":profile continue". Can be
775 used when doing something that should not be counted (e.g., an
776 external command). Does not nest.
777
778:prof[ile] continue
779 Continue profiling after ":profile pause".
780
Bram Moolenaar05159a02005-02-26 23:04:13 +0000781:prof[ile] func {pattern}
782 Profile function that matches the pattern {pattern}.
783 See |:debug-name| for how {pattern} is used.
784
785:prof[ile][!] file {pattern}
786 Profile script file that matches the pattern {pattern}.
787 See |:debug-name| for how {pattern} is used.
788 This only profiles the script itself, not the functions
789 defined in it.
790 When the [!] is added then all functions defined in the script
Bram Moolenaar76f3b1a2014-03-27 22:30:07 +0100791 will also be profiled.
792 Note that profiling only starts when the script is loaded
793 after this command. A :profile command in the script itself
794 won't work.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000795
796
Bram Moolenaard9fba312005-06-26 22:34:35 +0000797:profd[el] ... *:profd* *:profdel*
798 Stop profiling for the arguments specified. See |:breakdel|
799 for the arguments.
800
801
Bram Moolenaar05159a02005-02-26 23:04:13 +0000802You must always start with a ":profile start fname" command. The resulting
803file is written when Vim exits. Here is an example of the output, with line
804numbers prepended for the explanation:
805
806 1 FUNCTION Test2() ~
807 2 Called 1 time ~
808 3 Total time: 0.155251 ~
809 4 Self time: 0.002006 ~
810 5 ~
811 6 count total (s) self (s) ~
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000812 7 9 0.000096 for i in range(8) ~
813 8 8 0.153655 0.000410 call Test3() ~
814 9 8 0.000070 endfor ~
815 10 " Ask a question ~
816 11 1 0.001341 echo input("give me an answer: ") ~
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817
818The header (lines 1-4) gives the time for the whole function. The "Total"
819time is the time passed while the function was executing. The "Self" time is
820the "Total" time reduced by time spent in:
821- other user defined functions
822- sourced scripts
823- executed autocommands
824- external (shell) commands
825
826Lines 7-11 show the time spent in each executed line. Lines that are not
827executed do not count. Thus a comment line is never counted.
828
829The Count column shows how many times a line was executed. Note that the
830"for" command in line 7 is executed one more time as the following lines.
831That is because the line is also executed to detect the end of the loop.
832
833The time Vim spends waiting for user input isn't counted at all. Thus how
834long you take to respond to the input() prompt is irrelevant.
835
836Profiling should give a good indication of where time is spent, but keep in
837mind there are various things that may clobber the results:
838
839- The accuracy of the time measured depends on the gettimeofday() system
840 function. It may only be as accurate as 1/100 second, even though the times
841 are displayed in micro seconds.
842
843- Real elapsed time is measured, if other processes are busy they may cause
844 delays at unpredictable moments. You may want to run the profiling several
845 times and use the lowest results.
846
847- If you have several commands in one line you only get one time. Split the
848 line to see the time for the individual commands.
849
850- The time of the lines added up is mostly less than the time of the whole
851 function. There is some overhead in between.
852
853- Functions that are deleted before Vim exits will not produce profiling
854 information. You can check the |v:profiling| variable if needed: >
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000855 :if !v:profiling
Bram Moolenaar05159a02005-02-26 23:04:13 +0000856 : delfunc MyFunc
857 :endif
858<
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000859- Profiling may give weird results on multi-processor systems, when sleep
860 mode kicks in or the processor frequency is reduced to save power.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000861
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000862- The "self" time is wrong when a function is used recursively.
863
864
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 vim:tw=78:ts=8:ft=help:norl: