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