Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 1 | *repeat.txt* For Vim version 7.4. Last change: 2016 Mar 07 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | VIM REFERENCE MANUAL by Bram Moolenaar |
| 5 | |
| 6 | |
| 7 | Repeating commands, Vim scripts and debugging *repeating* |
| 8 | |
| 9 | Chapter 26 of the user manual introduces repeating |usr_26.txt|. |
| 10 | |
| 11 | 1. Single repeats |single-repeat| |
| 12 | 2. Multiple repeats |multi-repeat| |
| 13 | 3. Complex repeats |complex-repeat| |
| 14 | 4. Using Vim scripts |using-scripts| |
Bram Moolenaar | f6fee0e | 2016-02-21 23:02:49 +0100 | [diff] [blame] | 15 | 5. Using Vim packages |packages| |
| 16 | 6. Debugging scripts |debug-scripts| |
| 17 | 7. Profiling |profiling| |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 18 | |
| 19 | ============================================================================== |
| 20 | 1. 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 Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 25 | included in 'cpoptions'. Does not repeat a |
| 26 | command-line command. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 27 | |
| 28 | Simple changes can be repeated with the "." command. Without a count, the |
| 29 | count of the last change is used. If you enter a count, it will replace the |
Bram Moolenaar | 92dff18 | 2014-02-11 19:15:50 +0100 | [diff] [blame] | 30 | last one. |v:count| and |v:count1| will be set. |
| 31 | |
| 32 | If the last change included a specification of a numbered register, the |
| 33 | register number will be incremented. See |redo-register| for an example how |
| 34 | to use this. |
| 35 | |
| 36 | Note that when repeating a command that used a Visual selection, the same SIZE |
| 37 | of area is used, see |visual-repeat|. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 38 | |
| 39 | *@:* |
| 40 | @: Repeat last command-line [count] times. |
| 41 | {not available when compiled without the |
| 42 | |+cmdline_hist| feature} |
| 43 | |
| 44 | |
| 45 | ============================================================================== |
| 46 | 2. 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 Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 61 | Instead of the '/' which surrounds the {pattern}, you can use any other |
Bram Moolenaar | e2db695 | 2013-07-24 19:53:36 +0200 | [diff] [blame] | 62 | single byte character, but not an alphabetic character, '\', '"' or '|'. |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 63 | This is useful if you want to include a '/' in the search pattern or |
| 64 | replacement string. |
| 65 | |
| 66 | For the definition of a pattern, see |pattern|. |
| 67 | |
Bram Moolenaar | 32efaf6 | 2014-11-05 17:02:17 +0100 | [diff] [blame] | 68 | NOTE [cmd] may contain a range; see |collapse| and |edit-paragraph-join| for |
| 69 | examples. |
| 70 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 71 | The global commands work by first scanning through the [range] lines and |
| 72 | marking each line where a match occurs (for a multi-line pattern, only the |
| 73 | start of the match matters). |
| 74 | In a second scan the [cmd] is executed for each marked line with its line |
| 75 | number prepended. For ":v" and ":g!" the command is executed for each not |
| 76 | marked line. If a line is deleted its mark disappears. |
| 77 | The default for [range] is the whole buffer (1,$). Use "CTRL-C" to interrupt |
| 78 | the command. If an error message is given for a line, the command for that |
| 79 | line is aborted and the global command continues with the next marked or |
| 80 | unmarked line. |
| 81 | |
| 82 | To repeat a non-Ex command, you can use the ":normal" command: > |
| 83 | :g/pat/normal {commands} |
| 84 | Make sure that {commands} ends with a whole command, otherwise Vim will wait |
| 85 | for you to type the rest of the command for each match. The screen will not |
| 86 | have been updated, so you don't know what you are doing. See |:normal|. |
| 87 | |
| 88 | The undo/redo command will undo/redo the whole global command at once. |
| 89 | The previous context mark will only be set once (with "''" you go back to |
| 90 | where the cursor was before the global command). |
| 91 | |
| 92 | The global command sets both the last used search pattern and the last used |
| 93 | substitute pattern (this is vi compatible). This makes it easy to globally |
| 94 | replace a string: |
| 95 | :g/pat/s//PAT/g |
| 96 | This replaces all occurrences of "pat" with "PAT". The same can be done with: |
| 97 | :%s/pat/PAT/g |
| 98 | Which is two characters shorter! |
| 99 | |
Bram Moolenaar | 864207d | 2008-06-24 22:14:38 +0000 | [diff] [blame] | 100 | When using "global" in Ex mode, a special case is using ":visual" as a |
| 101 | command. This will move to a matching line, go to Normal mode to let you |
| 102 | execute commands there until you use |Q| to return to Ex mode. This will be |
| 103 | repeated for each matching line. While doing this you cannot use ":global". |
| 104 | To abort this type CTRL-C twice. |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 105 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 106 | ============================================================================== |
| 107 | 3. Complex repeats *complex-repeat* |
| 108 | |
| 109 | *q* *recording* |
| 110 | q{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 Moolenaar | a0ed84a | 2015-11-19 17:56:13 +0100 | [diff] [blame] | 113 | 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 120 | |
| 121 | q 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 Moolenaar | 61d35bd | 2012-03-28 20:51:51 +0200 | [diff] [blame] | 126 | @{0-9a-z".=*+} Execute the contents of register {0-9a-z".=*+} [count] |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 127 | times. Note that register '%' (name of the current |
| 128 | file) and '#' (name of the alternate file) cannot be |
Bram Moolenaar | 2a8a3ec | 2011-01-08 16:06:37 +0100 | [diff] [blame] | 129 | 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 136 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 137 | *@@* *E748* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 138 | @@ Repeat the previous @{0-9a-z":*} [count] times. |
| 139 | |
Bram Moolenaar | 61d35bd | 2012-03-28 20:51:51 +0200 | [diff] [blame] | 140 | :[addr]*{0-9a-z".=+} *:@* *:star* |
| 141 | :[addr]@{0-9a-z".=*+} Execute the contents of register {0-9a-z".=*+} as an Ex |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 142 | 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 | ============================================================================== |
| 166 | 4. Using Vim scripts *using-scripts* |
| 167 | |
| 168 | For 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 Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 173 | Triggers the |SourcePre| autocommand. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 174 | |
| 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 Moolenaar | 13fcaaf | 2005-04-15 21:13:42 +0000 | [diff] [blame] | 206 | starting up. This similar command: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 207 | :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 Moolenaar | be82c25 | 2016-03-06 14:44:08 +0100 | [diff] [blame] | 216 | *:pa* *:packadd* *E919* |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 217 | :pa[ckadd][!] {name} Search for an optional plugin directory in 'packpath' |
| 218 | and source any plugin files found. The directory must |
| 219 | match: |
| 220 | pack/*/opt/{name} ~ |
| 221 | The directory is added to 'runtimepath' if it wasn't |
| 222 | there yet. |
Bram Moolenaar | dae8d21 | 2016-02-27 22:40:16 +0100 | [diff] [blame] | 223 | |
Bram Moolenaar | f6fee0e | 2016-02-21 23:02:49 +0100 | [diff] [blame] | 224 | Note that {name} is the directory name, not the name |
| 225 | of the .vim file. If the "{name}/plugin" directory |
| 226 | contains more than one file they are all sourced. |
| 227 | |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 228 | If the filetype detection was not enabled yet (this |
| 229 | is usually done with a "syntax enable" or "filetype |
| 230 | on" command in your .vimrc file), this will also look |
| 231 | for "{name}/ftdetect/*.vim" files. |
| 232 | |
| 233 | When the optional ! is added no plugin files or |
| 234 | ftdetect scripts are loaded, only the matching |
| 235 | directories are added to 'runtimepath'. This is |
| 236 | useful in your .vimrc. The plugins will then be |
| 237 | loaded during initialization, see |load-plugins|. |
| 238 | |
| 239 | Also see |pack-add|. |
| 240 | |
Bram Moolenaar | f6fee0e | 2016-02-21 23:02:49 +0100 | [diff] [blame] | 241 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 242 | :scripte[ncoding] [encoding] *:scripte* *:scriptencoding* *E167* |
| 243 | Specify the character encoding used in the script. |
| 244 | The following lines will be converted from [encoding] |
| 245 | to the value of the 'encoding' option, if they are |
| 246 | different. Examples: > |
| 247 | scriptencoding iso-8859-5 |
| 248 | scriptencoding cp932 |
| 249 | < |
| 250 | When [encoding] is empty, no conversion is done. This |
| 251 | can be used to restrict conversion to a sequence of |
| 252 | lines: > |
| 253 | scriptencoding euc-jp |
| 254 | ... lines to be converted ... |
| 255 | scriptencoding |
| 256 | ... not converted ... |
| 257 | |
| 258 | < When conversion isn't supported by the system, there |
| 259 | is no error message and no conversion is done. |
| 260 | |
| 261 | Don't use "ucs-2" or "ucs-4", scripts cannot be in |
| 262 | these encodings (they would contain NUL bytes). |
| 263 | When a sourced script starts with a BOM (Byte Order |
Bram Moolenaar | 06b5d51 | 2010-05-22 15:37:44 +0200 | [diff] [blame] | 264 | Mark) in utf-8 format Vim will recognize it, no need |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 265 | to use ":scriptencoding utf-8" then. |
| 266 | |
| 267 | When compiled without the |+multi_byte| feature this |
| 268 | command is ignored. |
| 269 | {not in Vi} |
| 270 | |
Bram Moolenaar | 8feef4f | 2015-01-07 16:57:10 +0100 | [diff] [blame] | 271 | *:scr* *:scriptnames* |
| 272 | :scr[iptnames] List all sourced script names, in the order they were |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 273 | first sourced. The number is used for the script ID |
| 274 | |<SID>|. |
| 275 | {not in Vi} {not available when compiled without the |
| 276 | |+eval| feature} |
| 277 | |
| 278 | *:fini* *:finish* *E168* |
| 279 | :fini[sh] Stop sourcing a script. Can only be used in a Vim |
| 280 | script file. This is a quick way to skip the rest of |
| 281 | the file. If it is used after a |:try| but before the |
| 282 | matching |:finally| (if present), the commands |
| 283 | following the ":finally" up to the matching |:endtry| |
| 284 | are executed first. This process applies to all |
| 285 | nested ":try"s in the script. The outermost ":endtry" |
| 286 | then stops sourcing the script. {not in Vi} |
| 287 | |
| 288 | All commands and command sequences can be repeated by putting them in a named |
| 289 | register and then executing it. There are two ways to get the commands in the |
| 290 | register: |
| 291 | - Use the record command "q". You type the commands once, and while they are |
| 292 | being executed they are stored in a register. Easy, because you can see |
| 293 | what you are doing. If you make a mistake, "p"ut the register into the |
| 294 | file, edit the command sequence, and then delete it into the register |
| 295 | again. You can continue recording by appending to the register (use an |
| 296 | uppercase letter). |
| 297 | - Delete or yank the command sequence into the register. |
| 298 | |
| 299 | Often used command sequences can be put under a function key with the ':map' |
| 300 | command. |
| 301 | |
| 302 | An alternative is to put the commands in a file, and execute them with the |
| 303 | ':source!' command. Useful for long command sequences. Can be combined with |
| 304 | the ':map' command to put complicated commands under a function key. |
| 305 | |
| 306 | The ':source' command reads Ex commands from a file line by line. You will |
| 307 | have to type any needed keyboard input. The ':source!' command reads from a |
| 308 | script file character by character, interpreting each character as if you |
| 309 | typed it. |
| 310 | |
| 311 | Example: When you give the ":!ls" command you get the |hit-enter| prompt. If |
| 312 | you ':source' a file with the line "!ls" in it, you will have to type the |
| 313 | <Enter> yourself. But if you ':source!' a file with the line ":!ls" in it, |
| 314 | the next characters from that file are read until a <CR> is found. You will |
| 315 | not have to type <CR> yourself, unless ":!ls" was the last line in the file. |
| 316 | |
| 317 | It is possible to put ':source[!]' commands in the script file, so you can |
| 318 | make a top-down hierarchy of script files. The ':source' command can be |
| 319 | nested as deep as the number of files that can be opened at one time (about |
| 320 | 15). The ':source!' command can be nested up to 15 levels deep. |
| 321 | |
| 322 | You can use the "<sfile>" string (literally, this is not a special key) inside |
| 323 | of the sourced file, in places where a file name is expected. It will be |
| 324 | replaced by the file name of the sourced file. For example, if you have a |
| 325 | "other.vimrc" file in the same directory as your ".vimrc" file, you can source |
| 326 | it from your ".vimrc" file with this command: > |
| 327 | :source <sfile>:h/other.vimrc |
| 328 | |
| 329 | In script files terminal-dependent key codes are represented by |
| 330 | terminal-independent two character codes. This means that they can be used |
| 331 | in the same way on different kinds of terminals. The first character of a |
| 332 | key code is 0x80 or 128, shown on the screen as "~@". The second one can be |
| 333 | found in the list |key-notation|. Any of these codes can also be entered |
| 334 | with CTRL-V followed by the three digit decimal code. This does NOT work for |
| 335 | the <t_xx> termcap codes, these can only be used in mappings. |
| 336 | |
| 337 | *:source_crnl* *W15* |
| 338 | MS-DOS, Win32 and OS/2: Files that are read with ":source" normally have |
| 339 | <CR><NL> <EOL>s. These always work. If you are using a file with <NL> <EOL>s |
| 340 | (for example, a file made on Unix), this will be recognized if 'fileformats' |
| 341 | is not empty and the first line does not end in a <CR>. This fails if the |
| 342 | first line has something like ":map <F1> :help^M", where "^M" is a <CR>. If |
| 343 | the first line ends in a <CR>, but following ones don't, you will get an error |
| 344 | message, because the <CR> from the first lines will be lost. |
| 345 | |
Bram Moolenaar | 520470a | 2005-06-16 21:59:56 +0000 | [diff] [blame] | 346 | Mac Classic: Files that are read with ":source" normally have <CR> <EOL>s. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 347 | These always work. If you are using a file with <NL> <EOL>s (for example, a |
| 348 | file made on Unix), this will be recognized if 'fileformats' is not empty and |
| 349 | the first line does not end in a <CR>. Be careful not to use a file with <NL> |
| 350 | linebreaks which has a <CR> in first line. |
| 351 | |
| 352 | On other systems, Vim expects ":source"ed files to end in a <NL>. These |
| 353 | always work. If you are using a file with <CR><NL> <EOL>s (for example, a |
| 354 | file made on MS-DOS), all lines will have a trailing <CR>. This may cause |
| 355 | problems for some commands (e.g., mappings). There is no automatic <EOL> |
| 356 | detection, because it's common to start with a line that defines a mapping |
| 357 | that ends in a <CR>, which will confuse the automaton. |
| 358 | |
| 359 | *line-continuation* |
| 360 | Long lines in a ":source"d Ex command script file can be split by inserting |
| 361 | a line continuation symbol "\" (backslash) at the start of the next line. |
| 362 | There can be white space before the backslash, which is ignored. |
| 363 | |
| 364 | Example: the lines > |
| 365 | :set comments=sr:/*,mb:*,el:*/, |
| 366 | \://, |
| 367 | \b:#, |
| 368 | \:%, |
| 369 | \n:>, |
| 370 | \fb:- |
| 371 | are interpreted as if they were given in one line: |
| 372 | :set comments=sr:/*,mb:*,el:*/,://,b:#,:%,n:>,fb:- |
| 373 | |
| 374 | All leading whitespace characters in the line before a backslash are ignored. |
| 375 | Note however that trailing whitespace in the line before it cannot be |
| 376 | inserted freely; it depends on the position where a command is split up |
| 377 | whether additional whitespace is allowed or not. |
| 378 | |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 379 | When a space is required it's best to put it right after the backslash. A |
| 380 | space at the end of a line is hard to see and may be accidentally deleted. > |
| 381 | :syn match Comment |
| 382 | \ "very long regexp" |
| 383 | \ keepend |
| 384 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 385 | There is a problem with the ":append" and ":insert" commands: > |
| 386 | :1append |
| 387 | \asdf |
| 388 | . |
| 389 | The backslash is seen as a line-continuation symbol, thus this results in the |
| 390 | command: > |
| 391 | :1appendasdf |
| 392 | . |
| 393 | To avoid this, add the 'C' flag to the 'cpoptions' option: > |
| 394 | :set cpo+=C |
| 395 | :1append |
| 396 | \asdf |
| 397 | . |
| 398 | :set cpo-=C |
| 399 | |
| 400 | Note that when the commands are inside a function, you need to add the 'C' |
| 401 | flag when defining the function, it is not relevant when executing it. > |
| 402 | :set cpo+=C |
| 403 | :function Foo() |
| 404 | :1append |
| 405 | \asdf |
| 406 | . |
| 407 | :endfunction |
| 408 | :set cpo-=C |
| 409 | |
| 410 | Rationale: |
| 411 | Most programs work with a trailing backslash to indicate line |
| 412 | continuation. Using this in Vim would cause incompatibility with Vi. |
| 413 | For example for this Vi mapping: > |
| 414 | :map xx asdf\ |
| 415 | < Therefore the unusual leading backslash is used. |
| 416 | |
| 417 | ============================================================================== |
Bram Moolenaar | f6fee0e | 2016-02-21 23:02:49 +0100 | [diff] [blame] | 418 | 5. Using Vim packages *packages* |
| 419 | |
| 420 | A Vim package is a directory that contains one or more plugins. The |
| 421 | advantages over normal plugins: |
| 422 | - A package can be downloaded as an archive and unpacked in its own directory. |
Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 423 | Thus the files are not mixed with files of other plugins. That makes it |
| 424 | easy to update and remove. |
Bram Moolenaar | 9171587 | 2016-03-03 17:13:03 +0100 | [diff] [blame] | 425 | - A package can be a git, mercurial, etc. repository. That makes it really |
Bram Moolenaar | f6fee0e | 2016-02-21 23:02:49 +0100 | [diff] [blame] | 426 | easy to update. |
| 427 | - A package can contain multiple plugins that depend on each other. |
| 428 | - A package can contain plugins that are automatically loaded on startup and |
Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 429 | ones that are only loaded when needed with `:packadd`. |
| 430 | |
| 431 | |
| 432 | Using a package and loading automatically ~ |
Bram Moolenaar | f6fee0e | 2016-02-21 23:02:49 +0100 | [diff] [blame] | 433 | |
| 434 | Let's assume your Vim files are in the "~/.vim" directory and you want to add a |
Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 435 | package from a zip archive "/tmp/foopack.zip": |
| 436 | % mkdir -p ~/.vim/pack/foo |
| 437 | % cd ~/.vim/pack/foo |
| 438 | % unzip /tmp/foopack.zip |
Bram Moolenaar | f6fee0e | 2016-02-21 23:02:49 +0100 | [diff] [blame] | 439 | |
Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 440 | The directory name "foo" is arbitrary, you can pick anything you like. |
Bram Moolenaar | f6fee0e | 2016-02-21 23:02:49 +0100 | [diff] [blame] | 441 | |
| 442 | You would now have these files under ~/.vim: |
Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 443 | pack/foo/README.txt |
| 444 | pack/foo/ever/foobar/plugin/foo.vim |
| 445 | pack/foo/ever/foobar/syntax/some.vim |
| 446 | pack/foo/opt/foodebug/plugin/debugger.vim |
Bram Moolenaar | f6fee0e | 2016-02-21 23:02:49 +0100 | [diff] [blame] | 447 | |
Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 448 | When Vim starts up, after processing your .vimrc, it scans all directories in |
| 449 | 'packpath' for plugins under the "pack/*/ever" directory and loads them. The |
| 450 | directory is added to 'runtimepath'. |
Bram Moolenaar | f365482 | 2016-03-04 22:12:23 +0100 | [diff] [blame] | 451 | |
Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 452 | In the example Vim will find "pack/foo/ever/foobar/plugin/foo.vim" and adds |
| 453 | "~/.vim/pack/foo/ever/foobar" to 'runtimepath'. |
Bram Moolenaar | f6fee0e | 2016-02-21 23:02:49 +0100 | [diff] [blame] | 454 | |
Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 455 | If the "foobar" plugin kicks in and sets the 'filetype' to "some", Vim will |
| 456 | find the syntax/some.vim file, because its directory is in 'runtimepath'. |
Bram Moolenaar | f6fee0e | 2016-02-21 23:02:49 +0100 | [diff] [blame] | 457 | |
Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 458 | Vim will also load ftdetect files, if there are any. |
Bram Moolenaar | f6fee0e | 2016-02-21 23:02:49 +0100 | [diff] [blame] | 459 | |
Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 460 | Note that the files under "pack/foo/opt" or not loaded automatically, only the |
| 461 | ones under "pack/foo/ever". See |pack-add| below for how the "opt" directory |
| 462 | is used. |
Bram Moolenaar | f6fee0e | 2016-02-21 23:02:49 +0100 | [diff] [blame] | 463 | |
| 464 | Loading packages will not happen if loading plugins is disabled, see |
| 465 | |load-plugins|. |
| 466 | |
Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 467 | |
| 468 | Using a single plugin and loading it automatically ~ |
| 469 | |
| 470 | If you don't have a package but a single plugin, you need to create the extra |
| 471 | directory level: |
| 472 | % mkdir -p ~/.vim/pack/foo/ever/foobar |
| 473 | % cd ~/.vim/pack/foo/ever/foobar |
| 474 | % unzip /tmp/someplugin.zip |
| 475 | |
| 476 | You would now have these files: |
| 477 | pack/foo/ever/foobar/plugin/foo.vim |
| 478 | pack/foo/ever/foobar/syntax/some.vim |
| 479 | |
| 480 | From here it works like above. |
| 481 | |
| 482 | |
| 483 | Optional plugins ~ |
| 484 | *pack-add* |
| 485 | To load an optional plugin from a pack use the `:packadd` command: > |
| 486 | :packadd foodebug |
| 487 | This searches for "pack/*/opt/foodebug" in 'packpath' and will find |
| 488 | ~/.vim/pack/foo/opt/foodebug/plugin/debugger.vim and source it. |
| 489 | |
| 490 | This could be done inside always.vim, if some conditions are met. Or you |
| 491 | could add this command to your |.vimrc|. |
| 492 | |
| 493 | It is perfectly normal for a package to only have files in the "opt" |
| 494 | directory. You then need to load each plugin when you want to use it. |
| 495 | |
Bram Moolenaar | f6fee0e | 2016-02-21 23:02:49 +0100 | [diff] [blame] | 496 | ============================================================================== |
| 497 | 6. Debugging scripts *debug-scripts* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 498 | |
| 499 | Besides the obvious messages that you can add to your scripts to find out what |
| 500 | they are doing, Vim offers a debug mode. This allows you to step through a |
| 501 | sourced file or user function and set breakpoints. |
| 502 | |
| 503 | NOTE: The debugging mode is far from perfect. Debugging will have side |
| 504 | effects on how Vim works. You cannot use it to debug everything. For |
| 505 | example, the display is messed up by the debugging messages. |
| 506 | {Vi does not have a debug mode} |
| 507 | |
| 508 | An alternative to debug mode is setting the 'verbose' option. With a bigger |
| 509 | number it will give more verbose messages about what Vim is doing. |
| 510 | |
| 511 | |
| 512 | STARTING DEBUG MODE *debug-mode* |
| 513 | |
| 514 | To enter debugging mode use one of these methods: |
| 515 | 1. Start Vim with the |-D| argument: > |
| 516 | vim -D file.txt |
| 517 | < Debugging will start as soon as the first vimrc file is sourced. This is |
| 518 | useful to find out what is happening when Vim is starting up. A side |
| 519 | effect is that Vim will switch the terminal mode before initialisations |
| 520 | have finished, with unpredictable results. |
| 521 | For a GUI-only version (Windows, Macintosh) the debugging will start as |
| 522 | soon as the GUI window has been opened. To make this happen early, add a |
| 523 | ":gui" command in the vimrc file. |
| 524 | *:debug* |
| 525 | 2. Run a command with ":debug" prepended. Debugging will only be done while |
| 526 | this command executes. Useful for debugging a specific script or user |
| 527 | function. And for scripts and functions used by autocommands. Example: > |
| 528 | :debug edit test.txt.gz |
| 529 | |
| 530 | 3. Set a breakpoint in a sourced file or user function. You could do this in |
| 531 | the command line: > |
| 532 | vim -c "breakadd file */explorer.vim" . |
| 533 | < This will run Vim and stop in the first line of the "explorer.vim" script. |
| 534 | Breakpoints can also be set while in debugging mode. |
| 535 | |
| 536 | In debugging mode every executed command is displayed before it is executed. |
| 537 | Comment lines, empty lines and lines that are not executed are skipped. When |
| 538 | a line contains two commands, separated by "|", each command will be displayed |
| 539 | separately. |
| 540 | |
| 541 | |
| 542 | DEBUG MODE |
| 543 | |
| 544 | Once in debugging mode, the usual Ex commands can be used. For example, to |
| 545 | inspect the value of a variable: > |
| 546 | echo idx |
| 547 | When inside a user function, this will print the value of the local variable |
| 548 | "idx". Prepend "g:" to get the value of a global variable: > |
| 549 | echo g:idx |
| 550 | All commands are executed in the context of the current function or script. |
| 551 | You can also set options, for example setting or resetting 'verbose' will show |
| 552 | what happens, but you might want to set it just before executing the lines you |
| 553 | are interested in: > |
| 554 | :set verbose=20 |
| 555 | |
| 556 | Commands that require updating the screen should be avoided, because their |
| 557 | effect won't be noticed until after leaving debug mode. For example: > |
| 558 | :help |
| 559 | won't be very helpful. |
| 560 | |
| 561 | There is a separate command-line history for debug mode. |
| 562 | |
| 563 | The line number for a function line is relative to the start of the function. |
| 564 | If you have trouble figuring out where you are, edit the file that defines |
| 565 | the function in another Vim, search for the start of the function and do |
| 566 | "99j". Replace "99" with the line number. |
| 567 | |
| 568 | Additionally, these commands can be used: |
| 569 | *>cont* |
| 570 | cont Continue execution until the next breakpoint is hit. |
| 571 | *>quit* |
| 572 | quit Abort execution. This is like using CTRL-C, some |
| 573 | things might still be executed, doesn't abort |
| 574 | everything. Still stops at the next breakpoint. |
| 575 | *>next* |
| 576 | next Execute the command and come back to debug mode when |
| 577 | it's finished. This steps over user function calls |
| 578 | and sourced files. |
| 579 | *>step* |
| 580 | step Execute the command and come back to debug mode for |
| 581 | the next command. This steps into called user |
| 582 | functions and sourced files. |
| 583 | *>interrupt* |
| 584 | interrupt This is like using CTRL-C, but unlike ">quit" comes |
| 585 | back to debug mode for the next command that is |
| 586 | executed. Useful for testing |:finally| and |:catch| |
| 587 | on interrupt exceptions. |
| 588 | *>finish* |
| 589 | finish Finish the current script or user function and come |
| 590 | back to debug mode for the command after the one that |
| 591 | sourced or called it. |
Bram Moolenaar | f1f60f8 | 2016-01-16 15:40:53 +0100 | [diff] [blame] | 592 | *>bt* |
| 593 | *>backtrace* |
| 594 | *>where* |
| 595 | backtrace Show the call stacktrace for current debugging session. |
| 596 | bt |
| 597 | where |
| 598 | *>frame* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 599 | frame N Goes to N backtrace level. + and - signs make movement |
Bram Moolenaar | f1f60f8 | 2016-01-16 15:40:53 +0100 | [diff] [blame] | 600 | relative. E.g., ":frame +3" goes three frames up. |
| 601 | *>up* |
| 602 | up Goes one level up from call stacktrace. |
| 603 | *>down* |
| 604 | down Goes one level down from call stacktrace. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 605 | |
| 606 | About the additional commands in debug mode: |
| 607 | - There is no command-line completion for them, you get the completion for the |
| 608 | normal Ex commands only. |
Bram Moolenaar | dae8d21 | 2016-02-27 22:40:16 +0100 | [diff] [blame] | 609 | - You can shorten them, up to a single character, unless more than one command |
Bram Moolenaar | f1f60f8 | 2016-01-16 15:40:53 +0100 | [diff] [blame] | 610 | starts with the same letter. "f" stands for "finish", use "fr" for "frame". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 611 | - Hitting <CR> will repeat the previous one. When doing another command, this |
| 612 | is reset (because it's not clear what you want to repeat). |
| 613 | - When you want to use the Ex command with the same name, prepend a colon: |
| 614 | ":cont", ":next", ":finish" (or shorter). |
| 615 | |
Bram Moolenaar | f1f60f8 | 2016-01-16 15:40:53 +0100 | [diff] [blame] | 616 | The backtrace shows the hierarchy of function calls, e.g.: |
| 617 | >bt ~ |
| 618 | 3 function One[3] ~ |
| 619 | 2 Two[3] ~ |
| 620 | ->1 Three[3] ~ |
| 621 | 0 Four ~ |
| 622 | line 1: let four = 4 ~ |
| 623 | |
| 624 | The "->" points to the current frame. Use "up", "down" and "frame N" to |
| 625 | select another frame. |
| 626 | |
| 627 | In the current frame you can evaluate the local function variables. There is |
| 628 | no way to see the command at the current line yet. |
| 629 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 630 | |
| 631 | DEFINING BREAKPOINTS |
| 632 | *:breaka* *:breakadd* |
| 633 | :breaka[dd] func [lnum] {name} |
| 634 | Set a breakpoint in a function. Example: > |
| 635 | :breakadd func Explore |
| 636 | < Doesn't check for a valid function name, thus the breakpoint |
| 637 | can be set before the function is defined. |
| 638 | |
| 639 | :breaka[dd] file [lnum] {name} |
| 640 | Set a breakpoint in a sourced file. Example: > |
| 641 | :breakadd file 43 .vimrc |
| 642 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 643 | :breaka[dd] here |
| 644 | Set a breakpoint in the current line of the current file. |
| 645 | Like doing: > |
| 646 | :breakadd file <cursor-line> <current-file> |
| 647 | < Note that this only works for commands that are executed when |
| 648 | sourcing the file, not for a function defined in that file. |
| 649 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 650 | The [lnum] is the line number of the breakpoint. Vim will stop at or after |
| 651 | this line. When omitted line 1 is used. |
| 652 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 653 | *:debug-name* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 654 | {name} is a pattern that is matched with the file or function name. The |
| 655 | pattern is like what is used for autocommands. There must be a full match (as |
| 656 | if the pattern starts with "^" and ends in "$"). A "*" matches any sequence |
| 657 | of characters. 'ignorecase' is not used, but "\c" can be used in the pattern |
| 658 | to ignore case |/\c|. Don't include the () for the function name! |
| 659 | |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 660 | The match for sourced scripts is done against the full file name. If no path |
| 661 | is specified the current directory is used. Examples: > |
| 662 | breakadd file explorer.vim |
| 663 | matches "explorer.vim" in the current directory. > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 664 | breakadd file *explorer.vim |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 665 | matches ".../plugin/explorer.vim", ".../plugin/iexplorer.vim", etc. > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 666 | breakadd file */explorer.vim |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 667 | matches ".../plugin/explorer.vim" and "explorer.vim" in any other directory. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 668 | |
| 669 | The match for functions is done against the name as it's shown in the output |
| 670 | of ":function". For local functions this means that something like "<SNR>99_" |
| 671 | is prepended. |
| 672 | |
Bram Moolenaar | 2ce06f6 | 2005-01-31 19:19:04 +0000 | [diff] [blame] | 673 | Note that functions are first loaded and later executed. When they are loaded |
| 674 | the "file" breakpoints are checked, when they are executed the "func" |
| 675 | breakpoints. |
| 676 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 677 | |
| 678 | DELETING BREAKPOINTS |
| 679 | *:breakd* *:breakdel* *E161* |
| 680 | :breakd[el] {nr} |
| 681 | Delete breakpoint {nr}. Use |:breaklist| to see the number of |
| 682 | each breakpoint. |
| 683 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 684 | :breakd[el] * |
| 685 | Delete all breakpoints. |
| 686 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 687 | :breakd[el] func [lnum] {name} |
| 688 | Delete a breakpoint in a function. |
| 689 | |
| 690 | :breakd[el] file [lnum] {name} |
| 691 | Delete a breakpoint in a sourced file. |
| 692 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 693 | :breakd[el] here |
| 694 | Delete a breakpoint at the current line of the current file. |
| 695 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 696 | When [lnum] is omitted, the first breakpoint in the function or file is |
| 697 | deleted. |
| 698 | The {name} must be exactly the same as what was typed for the ":breakadd" |
| 699 | command. "explorer", "*explorer.vim" and "*explorer*" are different. |
| 700 | |
| 701 | |
| 702 | LISTING BREAKPOINTS |
| 703 | *:breakl* *:breaklist* |
| 704 | :breakl[ist] |
| 705 | List all breakpoints. |
| 706 | |
| 707 | |
| 708 | OBSCURE |
| 709 | |
| 710 | *:debugg* *:debuggreedy* |
| 711 | :debugg[reedy] |
| 712 | Read debug mode commands from the normal input stream, instead |
| 713 | of getting them directly from the user. Only useful for test |
| 714 | scripts. Example: > |
| 715 | echo 'q^Mq' | vim -e -s -c debuggreedy -c 'breakadd file script.vim' -S script.vim |
| 716 | |
| 717 | :0debugg[reedy] |
| 718 | Undo ":debuggreedy": get debug mode commands directly from the |
| 719 | user, don't use typeahead for debug commands. |
| 720 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 721 | ============================================================================== |
Bram Moolenaar | f6fee0e | 2016-02-21 23:02:49 +0100 | [diff] [blame] | 722 | 7. Profiling *profile* *profiling* |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 723 | |
Bram Moolenaar | 996343d | 2010-07-04 22:20:21 +0200 | [diff] [blame] | 724 | Profiling means that Vim measures the time that is spent on executing |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 725 | functions and/or scripts. The |+profile| feature is required for this. |
| 726 | It is only included when Vim was compiled with "huge" features. |
| 727 | {Vi does not have profiling} |
| 728 | |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 729 | You can also use the |reltime()| function to measure time. This only requires |
| 730 | the |+reltime| feature, which is present more often. |
| 731 | |
Bram Moolenaar | 16ea367 | 2013-07-28 16:02:18 +0200 | [diff] [blame] | 732 | For profiling syntax highlighting see |:syntime|. |
| 733 | |
Bram Moolenaar | 76f3b1a | 2014-03-27 22:30:07 +0100 | [diff] [blame] | 734 | For example, to profile the one_script.vim script file: > |
| 735 | :profile start /tmp/one_script_profile |
| 736 | :profile file one_script.vim |
| 737 | :source one_script.vim |
| 738 | :exit |
| 739 | |
Bram Moolenaar | 16ea367 | 2013-07-28 16:02:18 +0200 | [diff] [blame] | 740 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 741 | :prof[ile] start {fname} *:prof* *:profile* *E750* |
| 742 | Start profiling, write the output in {fname} upon exit. |
Bram Moolenaar | 0a63ded | 2015-04-15 13:31:24 +0200 | [diff] [blame] | 743 | "~/" and environment variables in {fname} will be expanded. |
Bram Moolenaar | 9b2200a | 2006-03-20 21:55:45 +0000 | [diff] [blame] | 744 | If {fname} already exists it will be silently overwritten. |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 745 | The variable |v:profiling| is set to one. |
| 746 | |
Bram Moolenaar | 9b2200a | 2006-03-20 21:55:45 +0000 | [diff] [blame] | 747 | :prof[ile] pause |
| 748 | Don't profile until the following ":profile continue". Can be |
| 749 | used when doing something that should not be counted (e.g., an |
| 750 | external command). Does not nest. |
| 751 | |
| 752 | :prof[ile] continue |
| 753 | Continue profiling after ":profile pause". |
| 754 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 755 | :prof[ile] func {pattern} |
| 756 | Profile function that matches the pattern {pattern}. |
| 757 | See |:debug-name| for how {pattern} is used. |
| 758 | |
| 759 | :prof[ile][!] file {pattern} |
| 760 | Profile script file that matches the pattern {pattern}. |
| 761 | See |:debug-name| for how {pattern} is used. |
| 762 | This only profiles the script itself, not the functions |
| 763 | defined in it. |
| 764 | When the [!] is added then all functions defined in the script |
Bram Moolenaar | 76f3b1a | 2014-03-27 22:30:07 +0100 | [diff] [blame] | 765 | will also be profiled. |
| 766 | Note that profiling only starts when the script is loaded |
| 767 | after this command. A :profile command in the script itself |
| 768 | won't work. |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 769 | |
| 770 | |
Bram Moolenaar | d9fba31 | 2005-06-26 22:34:35 +0000 | [diff] [blame] | 771 | :profd[el] ... *:profd* *:profdel* |
| 772 | Stop profiling for the arguments specified. See |:breakdel| |
| 773 | for the arguments. |
| 774 | |
| 775 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 776 | You must always start with a ":profile start fname" command. The resulting |
| 777 | file is written when Vim exits. Here is an example of the output, with line |
| 778 | numbers prepended for the explanation: |
| 779 | |
| 780 | 1 FUNCTION Test2() ~ |
| 781 | 2 Called 1 time ~ |
| 782 | 3 Total time: 0.155251 ~ |
| 783 | 4 Self time: 0.002006 ~ |
| 784 | 5 ~ |
| 785 | 6 count total (s) self (s) ~ |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 786 | 7 9 0.000096 for i in range(8) ~ |
| 787 | 8 8 0.153655 0.000410 call Test3() ~ |
| 788 | 9 8 0.000070 endfor ~ |
| 789 | 10 " Ask a question ~ |
| 790 | 11 1 0.001341 echo input("give me an answer: ") ~ |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 791 | |
| 792 | The header (lines 1-4) gives the time for the whole function. The "Total" |
| 793 | time is the time passed while the function was executing. The "Self" time is |
| 794 | the "Total" time reduced by time spent in: |
| 795 | - other user defined functions |
| 796 | - sourced scripts |
| 797 | - executed autocommands |
| 798 | - external (shell) commands |
| 799 | |
| 800 | Lines 7-11 show the time spent in each executed line. Lines that are not |
| 801 | executed do not count. Thus a comment line is never counted. |
| 802 | |
| 803 | The Count column shows how many times a line was executed. Note that the |
| 804 | "for" command in line 7 is executed one more time as the following lines. |
| 805 | That is because the line is also executed to detect the end of the loop. |
| 806 | |
| 807 | The time Vim spends waiting for user input isn't counted at all. Thus how |
| 808 | long you take to respond to the input() prompt is irrelevant. |
| 809 | |
| 810 | Profiling should give a good indication of where time is spent, but keep in |
| 811 | mind there are various things that may clobber the results: |
| 812 | |
| 813 | - The accuracy of the time measured depends on the gettimeofday() system |
| 814 | function. It may only be as accurate as 1/100 second, even though the times |
| 815 | are displayed in micro seconds. |
| 816 | |
| 817 | - Real elapsed time is measured, if other processes are busy they may cause |
| 818 | delays at unpredictable moments. You may want to run the profiling several |
| 819 | times and use the lowest results. |
| 820 | |
| 821 | - If you have several commands in one line you only get one time. Split the |
| 822 | line to see the time for the individual commands. |
| 823 | |
| 824 | - The time of the lines added up is mostly less than the time of the whole |
| 825 | function. There is some overhead in between. |
| 826 | |
| 827 | - Functions that are deleted before Vim exits will not produce profiling |
| 828 | information. You can check the |v:profiling| variable if needed: > |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 829 | :if !v:profiling |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 830 | : delfunc MyFunc |
| 831 | :endif |
| 832 | < |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 833 | - Profiling may give weird results on multi-processor systems, when sleep |
| 834 | mode kicks in or the processor frequency is reduced to save power. |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 835 | |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 836 | - The "self" time is wrong when a function is used recursively. |
| 837 | |
| 838 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 839 | vim:tw=78:ts=8:ft=help:norl: |