*repeat.txt* Nvim VIM REFERENCE MANUAL by Bram Moolenaar Repeating commands, Vim scripts and debugging *repeating* Chapter 26 of the user manual introduces repeating |usr_26.txt|. Type |gO| to see the table of contents. ============================================================================== Single repeats *single-repeat* *.* . Repeat last change, with count replaced with [count]. Also repeat a yank command, when the 'y' flag is included in 'cpoptions'. Does not repeat a command-line command. Simple changes can be repeated with the "." command. Without a count, the count of the last change is used. If you enter a count, it will replace the last one. |v:count| and |v:count1| will be set. If the last change included a specification of a numbered register, the register number will be incremented. See |redo-register| for an example how to use this. Note that when repeating a command that used a Visual selection, the same SIZE of area is used, see |visual-repeat|. *@:* @: Repeat last command-line [count] times. ============================================================================== Multiple repeats *multi-repeat* *:g* *:global* *E148* :[range]g[lobal]/{pattern}/[cmd] Execute the Ex command [cmd] (default ":p") on the lines within [range] where {pattern} matches. :[range]g[lobal]!/{pattern}/[cmd] Execute the Ex command [cmd] (default ":p") on the lines within [range] where {pattern} does NOT match. *:v* *:vglobal* :[range]v[global]/{pattern}/[cmd] Same as :g!. Example: > :g/^Obsolete/d _ Using the underscore after `:d` avoids clobbering registers or the clipboard. This also makes it faster. Instead of the '/' which surrounds the {pattern}, you can use any other single byte character, but not an alphabetic character, '\', '"', '|' or '!'. This is useful if you want to include a '/' in the search pattern or replacement string. For the definition of a pattern, see |pattern|. NOTE [cmd] may contain a range; see |collapse| and |edit-paragraph-join| for examples. The global commands work by first scanning through the [range] lines and marking each line where a match occurs (for a multi-line pattern, only the start of the match matters). In a second scan the [cmd] is executed for each marked line, as if the cursor was in that line. For ":v" and ":g!" the command is executed for each not marked line. If a line is deleted its mark disappears. The default for [range] is the whole buffer (1,$). Use "CTRL-C" to interrupt the command. If an error message is given for a line, the command for that line is aborted and the global command continues with the next marked or unmarked line. *E147* When the command is used recursively, it only works on one line. Giving a range is then not allowed. This is useful to find all lines that match a pattern and do not match another pattern: > :g/found/v/notfound/{cmd} This first finds all lines containing "found", but only executes {cmd} when there is no match for "notfound". Any Ex command can be used, see |ex-cmd-index|. To execute a Normal mode command, you can use the `:normal` command: > :g/pat/normal {commands} Make sure that {commands} ends with a whole command, otherwise Vim will wait for you to type the rest of the command for each match. The screen will not have been updated, so you don't know what you are doing. See |:normal|. The undo/redo command will undo/redo the whole global command at once. The previous context mark will only be set once (with "''" you go back to where the cursor was before the global command). The global command sets both the last used search pattern and the last used substitute pattern (this is vi compatible). This makes it easy to globally replace a string: > :g/pat/s//PAT/g This replaces all occurrences of "pat" with "PAT". The same can be done with: > :%s/pat/PAT/g Which is two characters shorter! When using "global" in Ex mode, a special case is using ":visual" as a command. This will move to a matching line, go to Normal mode to let you execute commands there until you use |gQ| to return to Ex mode. This will be repeated for each matching line. While doing this you cannot use ":global". To abort this type CTRL-C twice. ============================================================================== Complex repeats *complex-repeat* *q* *recording* *macro* q{0-9a-zA-Z"} Record typed characters into register {0-9a-zA-Z"} (uppercase to append). The 'q' command is disabled while executing a register, and it doesn't work inside a mapping and |:normal|. Note: If the register being used for recording is also used for |y| and |p| the result is most likely not what is expected, because the put will paste the recorded macro and the yank will overwrite the recorded macro. Note: The recording happens while you type, replaying the register happens as if the keys come from a mapping. This matters, for example, for undo, which only syncs when commands were typed. q Stops recording. Implementation note: The 'q' that stops recording is not stored in the register, unless it was the result of a mapping *@* @{0-9a-z".=*+} Execute the contents of register {0-9a-z".=*+} [count] times. Note that register '%' (name of the current file) and '#' (name of the alternate file) cannot be used. The register is executed like a mapping, that means that the difference between 'wildchar' and 'wildcharm' applies, and undo might not be synced in the same way. For "@=" you are prompted to enter an expression. The result of the expression is then executed. See also |@:|. *@@* *E748* @@ Repeat the previous @{0-9a-z":*} [count] times. *v_@-default* {Visual}@{0-9a-z".=*+} In linewise Visual mode, execute the contents of the {Visual}@@ register for each selected line. See |visual-repeat|, |default-mappings|. *Q* Q Repeat the last recorded register [count] times. See |reg_recorded()|. *v_Q-default* {Visual}Q In linewise Visual mode, repeat the last recorded register for each selected line. See |visual-repeat|, |default-mappings|. *:@* :[addr]@{0-9a-z".=*+} Execute the contents of register {0-9a-z".=*+} as an Ex command. First set cursor at line [addr] (default is current line). When the last line in the register does not have a it will be added automatically when the 'e' flag is present in 'cpoptions'. For ":@=" the last used expression is used. The result of evaluating the expression is executed as an Ex command. Mappings are not recognized in these commands. When the |line-continuation| character (\) is present at the beginning of a line in a linewise register, then it is combined with the previous line. This is useful for yanking and executing parts of a Vim script. *:@:* :[addr]@: Repeat last command-line. First set cursor at line [addr] (default is current line). :[addr]@ *:@@* :[addr]@@ Repeat the previous :@{register}. First set cursor at line [addr] (default is current line). ============================================================================== Using Vim scripts *using-scripts* For writing a Vim script, see chapter 41 of the user manual |usr_41.txt|. *:so* *:source* :so[urce] {file} Runs |Ex-commands| or Lua code (".lua" files) from {file}. Triggers the |SourcePre| autocommand. :[range]so[urce] Read Ex commands or Lua code from the [range] of lines in the current buffer. When [range] is omitted read all lines. The buffer is treated as Lua code if its 'filetype' is "lua" or its filename ends with ".lua". When sourcing commands or Lua code from the current buffer, the same script-ID || is used even if the buffer is sourced multiple times. If a buffer is sourced more than once, then the functions in the buffer are defined again. Implementation detail: When sourcing a [range] of lines that falls inside a folded region, the range will be adjusted to the start and end of the fold, but only if a two line specifiers range was used. *:source!* :so[urce]! {file} Runs |Normal-mode| commands from {file}. When used after |:global|, |:argdo|, |:windo|, |:bufdo|, in a loop or when another command follows the display won't be updated while executing the commands. *:ru* *:runtime* :ru[ntime][!] [where] {file} .. Sources |Ex| commands or Lua code (".lua" files) read from {file} (a relative path) in each directory given by 'runtimepath' and/or 'packpath'. Ignores non-existing files. Example: > :runtime syntax/c.vim :runtime syntax/c.lua < There can be multiple space-separated {file} arguments. Each {file} is searched for in the first directory from 'runtimepath', then in the second directory, etc. When [!] is included, all found files are sourced. Else only the first found file is sourced. When [where] is omitted only 'runtimepath' is used. Other values: START search only under "start" in 'packpath' OPT search only under "opt" in 'packpath' PACK search under "start" and "opt" in 'packpath' ALL first use 'runtimepath', then search under "start" and "opt" in 'packpath' When {file} contains wildcards it is expanded to all matching files. Example: > :runtime! plugin/**/*.{vim,lua} < This is what Nvim uses to load the plugin files when starting up. This similar command: > :runtime plugin/**/*.{vim,lua} < would source the first file only. For each {file} pattern, if two `.vim` and `.lua` file names match and differ only in extension, the `.vim` file is sourced first. When 'verbose' is one or higher, there is a message when no file could be found. When 'verbose' is two or higher, there is a message about each searched file. *:pa* *:packadd* *E919* :pa[ckadd][!] {name} |pack-add| Search for an optional plugin directory in 'packpath', source any plugin files found, and add it to 'runtimepath'. The directory must match: > pack/*/opt/{name} < If the directory pack/*/opt/{name}/after exists it is added at the end of 'runtimepath'. Note: Use |vim.pack.add()| to install from a URL. If loading packages from "pack/*/start" was skipped, then this directory is searched first: > pack/*/start/{name} < Note that {name} is the directory name, not the name of the .vim file. All files matching the patterns > pack/*/opt/{name}/plugin/**/*.vim pack/*/opt/{name}/plugin/**/*.lua < will be sourced. This allows for using subdirectories below "plugin", just like with plugins in 'runtimepath'. If the filetype detection was already enabled (this is usually done with a `syntax enable` or `filetype on` command in your |vimrc|, or automatically during |initialization|), and the package was found in "pack/*/opt/{name}", this command will also look for "{name}/ftdetect/*.vim" files. When the optional "!" is given, no plugin/ files or ftdetect/ scripts are loaded, only the matching directories are added to 'runtimepath'. This is useful in your |init.vim|. The plugins will then be loaded during the |load-plugins| |initialization| step (note that the loading order will be reversed because each directory is inserted before others), after loading the ftdetect scripts. *:packl* *:packloadall* :packl[oadall][!] Load all packages in the "start" directory under each entry in 'packpath'. First all the directories found are added to 'runtimepath', then the plugins found in the directories are sourced. This allows for a plugin to depend on something of another plugin, e.g. an "autoload" directory. See |packload-two-steps| for how this can be useful. This is normally done automatically during startup, after loading your |vimrc| file. With this command it can be done earlier. Packages will be loaded only once. Using `:packloadall` a second time will have no effect. When the optional ! is added this command will load packages even when done before. Note that when using `:packloadall` in the |vimrc| file, the 'runtimepath' option is updated, and later all plugins in 'runtimepath' will be loaded, which means they are loaded again. Plugins are expected to handle that. An error only causes sourcing the script where it happens to be aborted, further plugins will be loaded. See |packages|. :scripte[ncoding] [encoding] *:scripte* *:scriptencoding* *E167* Specify the character encoding used in the script. The following lines will be converted from [encoding] to the value of the 'encoding' option, if they are different. Examples: > scriptencoding iso-8859-5 scriptencoding cp932 < When [encoding] is empty, no conversion is done. This can be used to restrict conversion to a sequence of lines: > scriptencoding euc-jp ... lines to be converted ... scriptencoding ... not converted ... < When conversion isn't supported by the system, there is no error message and no conversion is done. When a line can't be converted there is no error and the original line is kept. Don't use "ucs-2" or "ucs-4", scripts cannot be in these encodings (they would contain NUL bytes). When a sourced script starts with a BOM (Byte Order Mark) in utf-8 format Vim will recognize it, no need to use ":scriptencoding utf-8" then. *:scr* *:scriptnames* :scr[iptnames] List all sourced script names, in the order they were first sourced. The number is used for the script ID ||. Also see `getscriptinfo()`. :scr[iptnames][!] {scriptId} *:script* Edit script {scriptId}. Although ":scriptnames name" works, using ":script name" is recommended. When the current buffer can't be |abandon|ed and the ! is not present, the command fails. *:fini* *:finish* *E168* :fini[sh] Stop sourcing a script. Can only be used in a Vim script file. This is a quick way to skip the rest of the file. If it is used after a |:try| but before the matching |:finally| (if present), the commands following the ":finally" up to the matching |:endtry| are executed first. This process applies to all nested ":try"s in the script. The outermost ":endtry" then stops sourcing the script. All commands and command sequences can be repeated by putting them in a named register and then executing it. There are two ways to get the commands in the register: - Use the record command "q". You type the commands once, and while they are being executed they are stored in a register. Easy, because you can see what you are doing. If you make a mistake, "p"ut the register into the file, edit the command sequence, and then delete it into the register again. You can continue recording by appending to the register (use an uppercase letter). - Delete or yank the command sequence into the register. Often used command sequences can be put under a function key with the ':map' command. An alternative is to put the commands in a file, and execute them with the ':source!' command. Useful for long command sequences. Can be combined with the ':map' command to put complicated commands under a function key. The ':source' command reads Ex commands from a file or a buffer line by line. You will have to type any needed keyboard input. The ':source!' command reads from a script file character by character, interpreting each character as if you typed it. Example: When you give the ":!ls" command you get the |hit-enter| prompt. If you ':source' a file with the line "!ls" in it, you will have to type the yourself. But if you ':source!' a file with the line ":!ls" in it, the next characters from that file are read until a is found. You will not have to type yourself, unless ":!ls" was the last line in the file. It is possible to put ':source[!]' commands in the script file, so you can make a top-down hierarchy of script files. The ':source' command can be nested as deep as the number of files that can be opened at one time (about 15). The ':source!' command can be nested up to 15 levels deep. You can use the "