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