repeat.txt 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. *repeat.txt* Nvim
  2. VIM REFERENCE MANUAL by Bram Moolenaar
  3. Repeating commands, Vim scripts and debugging *repeating*
  4. Chapter 26 of the user manual introduces repeating |usr_26.txt|.
  5. Type |gO| to see the table of contents.
  6. ==============================================================================
  7. Single repeats *single-repeat*
  8. *.*
  9. . Repeat last change, with count replaced with [count].
  10. Also repeat a yank command, when the 'y' flag is
  11. included in 'cpoptions'. Does not repeat a
  12. command-line command.
  13. Simple changes can be repeated with the "." command. Without a count, the
  14. count of the last change is used. If you enter a count, it will replace the
  15. last one. |v:count| and |v:count1| will be set.
  16. If the last change included a specification of a numbered register, the
  17. register number will be incremented. See |redo-register| for an example how
  18. to use this.
  19. Note that when repeating a command that used a Visual selection, the same SIZE
  20. of area is used, see |visual-repeat|.
  21. *@:*
  22. @: Repeat last command-line [count] times.
  23. ==============================================================================
  24. Multiple repeats *multi-repeat*
  25. *:g* *:global* *E148*
  26. :[range]g[lobal]/{pattern}/[cmd]
  27. Execute the Ex command [cmd] (default ":p") on the
  28. lines within [range] where {pattern} matches.
  29. :[range]g[lobal]!/{pattern}/[cmd]
  30. Execute the Ex command [cmd] (default ":p") on the
  31. lines within [range] where {pattern} does NOT match.
  32. *:v* *:vglobal*
  33. :[range]v[global]/{pattern}/[cmd]
  34. Same as :g!.
  35. Example: >
  36. :g/^Obsolete/d _
  37. Using the underscore after `:d` avoids clobbering registers or the clipboard.
  38. This also makes it faster.
  39. Instead of the '/' which surrounds the {pattern}, you can use any other
  40. single byte character, but not an alphabetic character, '\', '"' or '|'.
  41. This is useful if you want to include a '/' in the search pattern or
  42. replacement string.
  43. For the definition of a pattern, see |pattern|.
  44. NOTE [cmd] may contain a range; see |collapse| and |edit-paragraph-join| for
  45. examples.
  46. The global commands work by first scanning through the [range] lines and
  47. marking each line where a match occurs (for a multi-line pattern, only the
  48. start of the match matters).
  49. In a second scan the [cmd] is executed for each marked line, as if the cursor
  50. was in that line. For ":v" and ":g!" the command is executed for each not
  51. marked line. If a line is deleted its mark disappears.
  52. The default for [range] is the whole buffer (1,$). Use "CTRL-C" to interrupt
  53. the command. If an error message is given for a line, the command for that
  54. line is aborted and the global command continues with the next marked or
  55. unmarked line.
  56. *E147*
  57. When the command is used recursively, it only works on one line. Giving a
  58. range is then not allowed. This is useful to find all lines that match a
  59. pattern and do not match another pattern: >
  60. :g/found/v/notfound/{cmd}
  61. This first finds all lines containing "found", but only executes {cmd} when
  62. there is no match for "notfound".
  63. Any Ex command can be used, see |ex-cmd-index|. To execute a Normal mode
  64. command, you can use the `:normal` command: >
  65. :g/pat/normal {commands}
  66. Make sure that {commands} ends with a whole command, otherwise Vim will wait
  67. for you to type the rest of the command for each match. The screen will not
  68. have been updated, so you don't know what you are doing. See |:normal|.
  69. The undo/redo command will undo/redo the whole global command at once.
  70. The previous context mark will only be set once (with "''" you go back to
  71. where the cursor was before the global command).
  72. The global command sets both the last used search pattern and the last used
  73. substitute pattern (this is vi compatible). This makes it easy to globally
  74. replace a string:
  75. :g/pat/s//PAT/g
  76. This replaces all occurrences of "pat" with "PAT". The same can be done with:
  77. :%s/pat/PAT/g
  78. Which is two characters shorter!
  79. When using "global" in Ex mode, a special case is using ":visual" as a
  80. command. This will move to a matching line, go to Normal mode to let you
  81. execute commands there until you use |gQ| to return to Ex mode. This will be
  82. repeated for each matching line. While doing this you cannot use ":global".
  83. To abort this type CTRL-C twice.
  84. ==============================================================================
  85. Complex repeats *complex-repeat*
  86. *q* *recording*
  87. q{0-9a-zA-Z"} Record typed characters into register {0-9a-zA-Z"}
  88. (uppercase to append). The 'q' command is disabled
  89. while executing a register, and it doesn't work inside
  90. a mapping and |:normal|.
  91. Note: If the register being used for recording is also
  92. used for |y| and |p| the result is most likely not
  93. what is expected, because the put will paste the
  94. recorded macro and the yank will overwrite the
  95. recorded macro.
  96. Note: The recording happens while you type, replaying
  97. the register happens as if the keys come from a
  98. mapping. This matters, for example, for undo, which
  99. only syncs when commands were typed.
  100. q Stops recording.
  101. Implementation note: The 'q' that stops recording is
  102. not stored in the register, unless it was the result
  103. of a mapping
  104. *@*
  105. @{0-9a-z".=*+} Execute the contents of register {0-9a-z".=*+} [count]
  106. times. Note that register '%' (name of the current
  107. file) and '#' (name of the alternate file) cannot be
  108. used.
  109. The register is executed like a mapping, that means
  110. that the difference between 'wildchar' and 'wildcharm'
  111. applies, and undo might not be synced in the same way.
  112. For "@=" you are prompted to enter an expression. The
  113. result of the expression is then executed.
  114. See also |@:|.
  115. *@@* *E748*
  116. @@ Repeat the previous @{0-9a-z":*} [count] times.
  117. *Q*
  118. Q Repeat the last recorded register [count] times.
  119. See |reg_recorded()|.
  120. *:@*
  121. :[addr]@{0-9a-z".=*+} Execute the contents of register {0-9a-z".=*+} as an Ex
  122. command. First set cursor at line [addr] (default is
  123. current line). When the last line in the register does
  124. not have a <CR> it will be added automatically when
  125. the 'e' flag is present in 'cpoptions'.
  126. For ":@=" the last used expression is used. The
  127. result of evaluating the expression is executed as an
  128. Ex command.
  129. Mappings are not recognized in these commands.
  130. When the |line-continuation| character (\) is present
  131. at the beginning of a line in a linewise register,
  132. then it is combined with the previous line. This is
  133. useful for yanking and executing parts of a Vim
  134. script.
  135. *:@:*
  136. :[addr]@: Repeat last command-line. First set cursor at line
  137. [addr] (default is current line).
  138. :[addr]@ *:@@*
  139. :[addr]@@ Repeat the previous :@{register}. First set cursor at
  140. line [addr] (default is current line).
  141. ==============================================================================
  142. Using Vim scripts *using-scripts*
  143. For writing a Vim script, see chapter 41 of the user manual |usr_41.txt|.
  144. *:so* *:source* *load-vim-script*
  145. :[range]so[urce] [file] Runs |Ex| commands or Lua code (".lua" files) from
  146. [file], or current buffer if no [file].
  147. Triggers the |SourcePre| autocommand.
  148. *:source!*
  149. :[range]so[urce]! {file}
  150. Runs |Normal-mode| commands from {file}. When used
  151. after |:global|, |:argdo|, |:windo|, |:bufdo|, in
  152. a loop or when another command follows the display
  153. won't be updated while executing the commands.
  154. *:ru* *:runtime*
  155. :ru[ntime][!] [where] {file} ..
  156. Sources |Ex| commands or Lua code (".lua" files) read
  157. from {file} (a relative path) in each directory given
  158. by 'runtimepath' and/or 'packpath'.
  159. Ignores non-existing files.
  160. Example: >
  161. :runtime syntax/c.vim
  162. :runtime syntax/c.lua
  163. < There can be multiple space-separated {file}
  164. arguments. Each {file} is searched for in the first
  165. directory from 'runtimepath', then in the second
  166. directory, etc.
  167. When [!] is included, all found files are sourced.
  168. Else only the first found file is sourced.
  169. When [where] is omitted, first 'runtimepath' is
  170. searched, then directories under "start" in 'packpath'
  171. are searched.
  172. Other values:
  173. START search only under "start" in 'packpath'
  174. OPT search only under "opt" in 'packpath'
  175. PACK search under "start" and "opt" in
  176. 'packpath'
  177. ALL first use 'runtimepath', then search
  178. under "start" and "opt" in 'packpath'
  179. When {file} contains wildcards it is expanded to all
  180. matching files. Example: >
  181. :runtime! plugin/**/*.vim
  182. < This is what Vim uses to load the plugin files when
  183. starting up. This similar command: >
  184. :runtime plugin/**/*.vim
  185. < would source the first file only.
  186. When 'verbose' is one or higher, there is a message
  187. when no file could be found.
  188. When 'verbose' is two or higher, there is a message
  189. about each searched file.
  190. *:pa* *:packadd* *E919*
  191. :pa[ckadd][!] {name} Search for an optional plugin directory in 'packpath'
  192. and source any plugin files found. The directory must
  193. match:
  194. pack/*/opt/{name} ~
  195. The directory is added to 'runtimepath' if it wasn't
  196. there yet.
  197. If the directory pack/*/opt/{name}/after exists it is
  198. added at the end of 'runtimepath'.
  199. If loading packages from "pack/*/start" was skipped,
  200. then this directory is searched first:
  201. pack/*/start/{name} ~
  202. Note that {name} is the directory name, not the name
  203. of the .vim file. All the files matching the pattern
  204. pack/*/opt/{name}/plugin/**/*.vim ~
  205. and
  206. pack/*/opt/{name}/plugin/**/*.lua ~
  207. will be sourced. This allows for using subdirectories
  208. below "plugin", just like with plugins in
  209. 'runtimepath'.
  210. If the filetype detection was already enabled (this
  211. is usually done with a "syntax enable" or "filetype
  212. on" command in your |init.vim|, or automatically during
  213. |initialization|), and the package was found in
  214. "pack/*/opt/{name}", this command will also look
  215. for "{name}/ftdetect/*.vim" files.
  216. When the optional ! is added no plugin files or
  217. ftdetect scripts are loaded, only the matching
  218. directories are added to 'runtimepath'. This is
  219. useful in your |init.vim|. The plugins will then be
  220. loaded during |initialization|, see |load-plugins| (note
  221. that the loading order will be reversed, because each
  222. directory is inserted before others). In this case, the
  223. ftdetect scripts will be loaded during |initialization|,
  224. before the |load-plugins| step.
  225. Also see |pack-add|.
  226. *:packl* *:packloadall*
  227. :packl[oadall][!] Load all packages in the "start" directory under each
  228. entry in 'packpath'.
  229. First all the directories found are added to
  230. 'runtimepath', then the plugins found in the
  231. directories are sourced. This allows for a plugin to
  232. depend on something of another plugin, e.g. an
  233. "autoload" directory. See |packload-two-steps| for
  234. how this can be useful.
  235. This is normally done automatically during startup,
  236. after loading your .vimrc file. With this command it
  237. can be done earlier.
  238. Packages will be loaded only once. Using
  239. `:packloadall` a second time will have no effect.
  240. When the optional ! is added this command will load
  241. packages even when done before.
  242. Note that when using `:packloadall` in the |vimrc|
  243. file, the 'runtimepath' option is updated, and later
  244. all plugins in 'runtimepath' will be loaded, which
  245. means they are loaded again. Plugins are expected to
  246. handle that.
  247. An error only causes sourcing the script where it
  248. happens to be aborted, further plugins will be loaded.
  249. See |packages|.
  250. :scripte[ncoding] [encoding] *:scripte* *:scriptencoding* *E167*
  251. Specify the character encoding used in the script.
  252. The following lines will be converted from [encoding]
  253. to the value of the 'encoding' option, if they are
  254. different. Examples: >
  255. scriptencoding iso-8859-5
  256. scriptencoding cp932
  257. <
  258. When [encoding] is empty, no conversion is done. This
  259. can be used to restrict conversion to a sequence of
  260. lines: >
  261. scriptencoding euc-jp
  262. ... lines to be converted ...
  263. scriptencoding
  264. ... not converted ...
  265. < When conversion isn't supported by the system, there
  266. is no error message and no conversion is done. When a
  267. line can't be converted there is no error and the
  268. original line is kept.
  269. Don't use "ucs-2" or "ucs-4", scripts cannot be in
  270. these encodings (they would contain NUL bytes).
  271. When a sourced script starts with a BOM (Byte Order
  272. Mark) in utf-8 format Vim will recognize it, no need
  273. to use ":scriptencoding utf-8" then.
  274. *:scr* *:scriptnames*
  275. :scr[iptnames] List all sourced script names, in the order they were
  276. first sourced. The number is used for the script ID
  277. |<SID>|.
  278. :scr[iptnames][!] {scriptId} *:script*
  279. Edit script {scriptId}. Although ":scriptnames name"
  280. works, using ":script name" is recommended.
  281. When the current buffer can't be |abandon|ed and the !
  282. is not present, the command fails.
  283. *:fini* *:finish* *E168*
  284. :fini[sh] Stop sourcing a script. Can only be used in a Vim
  285. script file. This is a quick way to skip the rest of
  286. the file. If it is used after a |:try| but before the
  287. matching |:finally| (if present), the commands
  288. following the ":finally" up to the matching |:endtry|
  289. are executed first. This process applies to all
  290. nested ":try"s in the script. The outermost ":endtry"
  291. then stops sourcing the script.
  292. All commands and command sequences can be repeated by putting them in a named
  293. register and then executing it. There are two ways to get the commands in the
  294. register:
  295. - Use the record command "q". You type the commands once, and while they are
  296. being executed they are stored in a register. Easy, because you can see
  297. what you are doing. If you make a mistake, "p"ut the register into the
  298. file, edit the command sequence, and then delete it into the register
  299. again. You can continue recording by appending to the register (use an
  300. uppercase letter).
  301. - Delete or yank the command sequence into the register.
  302. Often used command sequences can be put under a function key with the ':map'
  303. command.
  304. An alternative is to put the commands in a file, and execute them with the
  305. ':source!' command. Useful for long command sequences. Can be combined with
  306. the ':map' command to put complicated commands under a function key.
  307. The ':source' command reads Ex commands from a file line by line. You will
  308. have to type any needed keyboard input. The ':source!' command reads from a
  309. script file character by character, interpreting each character as if you
  310. typed it.
  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. It is possible to put ':source[!]' commands in the script file, so you can
  317. make a top-down hierarchy of script files. The ':source' command can be
  318. nested as deep as the number of files that can be opened at one time (about
  319. 15). The ':source!' command can be nested up to 15 levels deep.
  320. You can use the "<sfile>" string (literally, this is not a special key) inside
  321. of the sourced file, in places where a file name is expected. It will be
  322. replaced by the file name of the sourced file. For example, if you have a
  323. "other.vimrc" file in the same directory as your |init.vim| file, you can
  324. source it from your |init.vim| file with this command: >
  325. :source <sfile>:h/other.vimrc
  326. In script files terminal-dependent key codes are represented by
  327. terminal-independent two character codes. This means that they can be used
  328. in the same way on different kinds of terminals. The first character of a
  329. key code is 0x80 or 128, shown on the screen as "~@". The second one can be
  330. found in the list |key-notation|. Any of these codes can also be entered
  331. with CTRL-V followed by the three digit decimal code.
  332. *:source_crnl* *W15*
  333. Windows: Files that are read with ":source" normally have <CR><NL> <EOL>s.
  334. These always work. If you are using a file with <NL> <EOL>s (for example, a
  335. file made on Unix), this will be recognized if 'fileformats' is not empty and
  336. the first line does not end in a <CR>. This fails if the first line has
  337. something like ":map <F1> :help^M", where "^M" is a <CR>. If the first line
  338. ends in a <CR>, but following ones don't, you will get an error message,
  339. because the <CR> from the first lines will be lost.
  340. On other systems, Vim expects ":source"ed files to end in a <NL>. These
  341. always work. If you are using a file with <CR><NL> <EOL>s (for example, a
  342. file made on MS-Windows), all lines will have a trailing <CR>. This may cause
  343. problems for some commands (e.g., mappings). There is no automatic <EOL>
  344. detection, because it's common to start with a line that defines a mapping
  345. that ends in a <CR>, which will confuse the automaton.
  346. *line-continuation*
  347. Long lines in a ":source"d Ex command script file can be split by inserting
  348. a line continuation symbol "\" (backslash) at the start of the next line.
  349. There can be white space before the backslash, which is ignored.
  350. Example: the lines >
  351. :set comments=sr:/*,mb:*,el:*/,
  352. \://,
  353. \b:#,
  354. \:%,
  355. \n:>,
  356. \fb:-
  357. are interpreted as if they were given in one line:
  358. :set comments=sr:/*,mb:*,el:*/,://,b:#,:%,n:>,fb:-
  359. All leading whitespace characters in the line before a backslash are ignored.
  360. Note however that trailing whitespace in the line before it cannot be
  361. inserted freely; it depends on the position where a command is split up
  362. whether additional whitespace is allowed or not.
  363. When a space is required it's best to put it right after the backslash. A
  364. space at the end of a line is hard to see and may be accidentally deleted. >
  365. :syn match Comment
  366. \ "very long regexp"
  367. \ keepend
  368. There is a problem with the ":append" and ":insert" commands: >
  369. :1append
  370. \asdf
  371. .
  372. The backslash is seen as a line-continuation symbol, thus this results in the
  373. command: >
  374. :1appendasdf
  375. .
  376. To avoid this, add the 'C' flag to the 'cpoptions' option: >
  377. :set cpo+=C
  378. :1append
  379. \asdf
  380. .
  381. :set cpo-=C
  382. Note that when the commands are inside a function, you need to add the 'C'
  383. flag when defining the function, it is not relevant when executing it. >
  384. :set cpo+=C
  385. :function Foo()
  386. :1append
  387. \asdf
  388. .
  389. :endfunction
  390. :set cpo-=C
  391. <
  392. *line-continuation-comment*
  393. To add a comment in between the lines start with `'"\ '`. Notice the space
  394. after the backslash. Example: >
  395. let array = [
  396. "\ first entry comment
  397. \ 'first',
  398. "\ second entry comment
  399. \ 'second',
  400. \ ]
  401. Rationale:
  402. Most programs work with a trailing backslash to indicate line
  403. continuation. Using this in Vim would cause incompatibility with Vi.
  404. For example for this Vi mapping: >
  405. :map xx asdf\
  406. < Therefore the unusual leading backslash is used.
  407. Starting a comment in a continuation line results in all following
  408. continuation lines to be part of the comment. Since it was like this
  409. for a long time, when making it possible to add a comment halfway a
  410. sequence of continuation lines, it was not possible to use \", since
  411. that was a valid continuation line. Using `'"\ '` comes closest, even
  412. though it may look a bit weird. Requiring the space after the
  413. backslash is to make it very unlikely this is a normal comment line.
  414. ==============================================================================
  415. Using Vim packages *packages*
  416. A Vim "package" is a directory that contains |plugin|s. Compared to normal
  417. plugins, a package can...
  418. - be downloaded as an archive and unpacked in its own directory, so the files
  419. are not mixed with files of other plugins.
  420. - be a git, mercurial, etc. repository, thus easy to update.
  421. - contain multiple plugins that depend on each other.
  422. - contain plugins that are automatically loaded on startup ("start" packages,
  423. located in "pack/*/start/*") and ones that are only loaded when needed with
  424. |:packadd| ("opt" packages, located in "pack/*/opt/*").
  425. *runtime-search-path*
  426. Nvim searches for |:runtime| files in:
  427. 1. all paths in 'runtimepath'
  428. 2. all "pack/*/start/*" dirs
  429. Note that the "pack/*/start/*" paths are not explicitly included in
  430. 'runtimepath', so they will not be reported by ":set rtp" or "echo &rtp".
  431. Scripts can use |nvim_list_runtime_paths()| to list all used directories, and
  432. |nvim_get_runtime_file()| to query for specific files or sub-folders within
  433. the runtime path. Example: >
  434. " List all runtime dirs and packages with Lua paths.
  435. :echo nvim_get_runtime_file("lua/", v:true)
  436. Using a package and loading automatically ~
  437. Let's assume your Nvim files are in "~/.local/share/nvim/site" and you want to
  438. add a package from a zip archive "/tmp/foopack.zip":
  439. % mkdir -p ~/.local/share/nvim/site/pack/foo
  440. % cd ~/.local/share/nvim/site/pack/foo
  441. % unzip /tmp/foopack.zip
  442. The directory name "foo" is arbitrary, you can pick anything you like.
  443. You would now have these files under ~/.local/share/nvim/site:
  444. pack/foo/README.txt
  445. pack/foo/start/foobar/plugin/foo.vim
  446. pack/foo/start/foobar/syntax/some.vim
  447. pack/foo/opt/foodebug/plugin/debugger.vim
  448. On startup after processing your |config|, Nvim scans all directories in
  449. 'packpath' for plugins in "pack/*/start/*", then loads the plugins.
  450. In the example Nvim will find "pack/foo/start/foobar/plugin/foo.vim" and load
  451. it.
  452. If the "foobar" plugin kicks in and sets the 'filetype' to "some", Nvim will
  453. find the syntax/some.vim file, because its directory is in the runtime search
  454. path.
  455. Nvim will also load ftdetect files, if there are any.
  456. Note that the files under "pack/foo/opt" are not loaded automatically, only the
  457. ones under "pack/foo/start". See |pack-add| below for how the "opt" directory
  458. is used.
  459. Loading packages automatically will not happen if loading plugins is disabled,
  460. see |load-plugins|.
  461. To load packages earlier, so that plugin/ files are sourced:
  462. :packloadall
  463. This also works when loading plugins is disabled. The automatic loading will
  464. only happen once.
  465. If the package has an "after" directory, that directory is added to the end of
  466. 'runtimepath', so that anything there will be loaded later.
  467. Using a single plugin and loading it automatically ~
  468. If you don't have a package but a single plugin, you need to create the extra
  469. directory level:
  470. % mkdir -p ~/.local/share/nvim/site/pack/foo/start/foobar
  471. % cd ~/.local/share/nvim/site/pack/foo/start/foobar
  472. % unzip /tmp/someplugin.zip
  473. You would now have these files:
  474. pack/foo/start/foobar/plugin/foo.vim
  475. pack/foo/start/foobar/syntax/some.vim
  476. From here it works like above.
  477. Optional plugins ~
  478. *pack-add*
  479. To load an optional plugin from a pack use the `:packadd` command: >
  480. :packadd foodebug
  481. This searches for "pack/*/opt/foodebug" in 'packpath' and will find
  482. ~/.local/share/nvim/site/pack/foo/opt/foodebug/plugin/debugger.vim and source
  483. it.
  484. This could be done if some conditions are met. For example, depending on
  485. whether Nvim supports a feature or a dependency is missing.
  486. You can also load an optional plugin at startup, by putting this command in
  487. your |config|: >
  488. :packadd! foodebug
  489. The extra "!" is so that the plugin isn't loaded if Nvim was started with
  490. |--noplugin|.
  491. It is perfectly normal for a package to only have files in the "opt"
  492. directory. You then need to load each plugin when you want to use it.
  493. Where to put what ~
  494. Since color schemes, loaded with `:colorscheme`, are found below
  495. "pack/*/start" and "pack/*/opt", you could put them anywhere. We recommend
  496. you put them below "pack/*/opt", for example
  497. "~/.config/nvim/pack/mycolors/opt/dark/colors/very_dark.vim".
  498. Filetype plugins should go under "pack/*/start", so that they are always
  499. found. Unless you have more than one plugin for a file type and want to
  500. select which one to load with `:packadd`. E.g. depending on the compiler
  501. version: >
  502. if foo_compiler_version > 34
  503. packadd foo_new
  504. else
  505. packadd foo_old
  506. endif
  507. The "after" directory is most likely not useful in a package. It's not
  508. disallowed though.
  509. ==============================================================================
  510. Creating Vim packages *package-create*
  511. This assumes you write one or more plugins that you distribute as a package.
  512. If you have two unrelated plugins you would use two packages, so that Vim
  513. users can choose what they include or not. Or you can decide to use one
  514. package with optional plugins, and tell the user to add the preferred ones with
  515. `:packadd`.
  516. Decide how you want to distribute the package. You can create an archive or
  517. you could use a repository. An archive can be used by more users, but is a
  518. bit harder to update to a new version. A repository can usually be kept
  519. up-to-date easily, but it requires a program like "git" to be available.
  520. You can do both, github can automatically create an archive for a release.
  521. Your directory layout would be like this:
  522. start/foobar/plugin/foo.vim " always loaded, defines commands
  523. start/foobar/plugin/bar.vim " always loaded, defines commands
  524. start/foobar/autoload/foo.vim " loaded when foo command used
  525. start/foobar/doc/foo.txt " help for foo.vim
  526. start/foobar/doc/tags " help tags
  527. opt/fooextra/plugin/extra.vim " optional plugin, defines commands
  528. opt/fooextra/autoload/extra.vim " loaded when extra command used
  529. opt/fooextra/doc/extra.txt " help for extra.vim
  530. opt/fooextra/doc/tags " help tags
  531. This allows for the user to do: >
  532. mkdir ~/.local/share/nvim/site/pack
  533. cd ~/.local/share/nvim/site/pack
  534. git clone https://github.com/you/foobar.git myfoobar
  535. Here "myfoobar" is a name that the user can choose, the only condition is that
  536. it differs from other packages.
  537. In your documentation you explain what the plugins do, and tell the user how
  538. to load the optional plugin: >
  539. :packadd! fooextra
  540. You could add this packadd command in one of your plugins, to be executed when
  541. the optional plugin is needed.
  542. Run the `:helptags` command to generate the doc/tags file. Including this
  543. generated file in the package means that the user can drop the package in the
  544. pack directory and the help command works right away. Don't forget to re-run
  545. the command after changing the plugin help: >
  546. :helptags path/start/foobar/doc
  547. :helptags path/opt/fooextra/doc
  548. Dependencies between plugins ~
  549. *packload-two-steps*
  550. Suppose you have two plugins that depend on the same functionality. You can
  551. put the common functionality in an autoload directory, so that it will be
  552. found automatically. Your package would have these files:
  553. pack/foo/start/one/plugin/one.vim >
  554. call foolib#getit()
  555. < pack/foo/start/two/plugin/two.vim >
  556. call foolib#getit()
  557. < pack/foo/start/lib/autoload/foolib.vim >
  558. func foolib#getit()
  559. This works, because start packages will be searchd for autoload files, when
  560. sourcing the plugins.
  561. ==============================================================================
  562. Debugging scripts *debug-scripts*
  563. Besides the obvious messages that you can add to your scripts to find out what
  564. they are doing, Vim offers a debug mode. This allows you to step through a
  565. sourced file or user function and set breakpoints.
  566. NOTE: The debugging mode is far from perfect. Debugging will have side
  567. effects on how Vim works. You cannot use it to debug everything. For
  568. example, the display is messed up by the debugging messages.
  569. An alternative to debug mode is setting the 'verbose' option. With a bigger
  570. number it will give more verbose messages about what Vim is doing.
  571. STARTING DEBUG MODE *debug-mode*
  572. To enter debugging mode use one of these methods:
  573. 1. Start Vim with the |-D| argument: >
  574. vim -D file.txt
  575. < Debugging will start as soon as the first vimrc file is sourced. This is
  576. useful to find out what is happening when Vim is starting up. A side
  577. effect is that Vim will switch the terminal mode before initialisations
  578. have finished, with unpredictable results.
  579. For a GUI-only version (Windows) the debugging will start as
  580. soon as the GUI window has been opened. To make this happen early, add a
  581. ":gui" command in the vimrc file.
  582. *:debug*
  583. 2. Run a command with ":debug" prepended. Debugging will only be done while
  584. this command executes. Useful for debugging a specific script or user
  585. function. And for scripts and functions used by autocommands. Example: >
  586. :debug edit test.txt.gz
  587. 3. Set a breakpoint in a sourced file or user function. You could do this in
  588. the command line: >
  589. vim -c "breakadd file */explorer.vim" .
  590. < This will run Vim and stop in the first line of the "explorer.vim" script.
  591. Breakpoints can also be set while in debugging mode.
  592. In debugging mode every executed command is displayed before it is executed.
  593. Comment lines, empty lines and lines that are not executed are skipped. When
  594. a line contains two commands, separated by "|", each command will be displayed
  595. separately.
  596. DEBUG MODE
  597. Once in debugging mode, the usual Ex commands can be used. For example, to
  598. inspect the value of a variable: >
  599. echo idx
  600. When inside a user function, this will print the value of the local variable
  601. "idx". Prepend "g:" to get the value of a global variable: >
  602. echo g:idx
  603. All commands are executed in the context of the current function or script.
  604. You can also set options, for example setting or resetting 'verbose' will show
  605. what happens, but you might want to set it just before executing the lines you
  606. are interested in: >
  607. :set verbose=20
  608. Commands that require updating the screen should be avoided, because their
  609. effect won't be noticed until after leaving debug mode. For example: >
  610. :help
  611. won't be very helpful.
  612. There is a separate command-line history for debug mode.
  613. The line number for a function line is relative to the start of the function.
  614. If you have trouble figuring out where you are, edit the file that defines
  615. the function in another Vim, search for the start of the function and do
  616. "99j". Replace "99" with the line number.
  617. Additionally, these commands can be used:
  618. *>cont*
  619. cont Continue execution until the next breakpoint is hit.
  620. *>quit*
  621. quit Abort execution. This is like using CTRL-C, some
  622. things might still be executed, doesn't abort
  623. everything. Still stops at the next breakpoint.
  624. *>next*
  625. next Execute the command and come back to debug mode when
  626. it's finished. This steps over user function calls
  627. and sourced files.
  628. *>step*
  629. step Execute the command and come back to debug mode for
  630. the next command. This steps into called user
  631. functions and sourced files.
  632. *>interrupt*
  633. interrupt This is like using CTRL-C, but unlike ">quit" comes
  634. back to debug mode for the next command that is
  635. executed. Useful for testing |:finally| and |:catch|
  636. on interrupt exceptions.
  637. *>finish*
  638. finish Finish the current script or user function and come
  639. back to debug mode for the command after the one that
  640. sourced or called it.
  641. *>bt*
  642. *>backtrace*
  643. *>where*
  644. backtrace Show the call stacktrace for current debugging session.
  645. bt
  646. where
  647. *>frame*
  648. frame N Goes to N backtrace level. + and - signs make movement
  649. relative. E.g., ":frame +3" goes three frames up.
  650. *>up*
  651. up Goes one level up from call stacktrace.
  652. *>down*
  653. down Goes one level down from call stacktrace.
  654. About the additional commands in debug mode:
  655. - There is no command-line completion for them, you get the completion for the
  656. normal Ex commands only.
  657. - You can shorten them, up to a single character, unless more than one command
  658. starts with the same letter. "f" stands for "finish", use "fr" for "frame".
  659. - Hitting <CR> will repeat the previous one. When doing another command, this
  660. is reset (because it's not clear what you want to repeat).
  661. - When you want to use the Ex command with the same name, prepend a colon:
  662. ":cont", ":next", ":finish" (or shorter).
  663. The backtrace shows the hierarchy of function calls, e.g.:
  664. >bt ~
  665. 3 function One[3] ~
  666. 2 Two[3] ~
  667. ->1 Three[3] ~
  668. 0 Four ~
  669. line 1: let four = 4 ~
  670. The "->" points to the current frame. Use "up", "down" and "frame N" to
  671. select another frame.
  672. In the current frame you can evaluate the local function variables. There is
  673. no way to see the command at the current line yet.
  674. DEFINING BREAKPOINTS
  675. *:breaka* *:breakadd*
  676. :breaka[dd] func [lnum] {name}
  677. Set a breakpoint in a function. Example: >
  678. :breakadd func Explore
  679. < Doesn't check for a valid function name, thus the breakpoint
  680. can be set before the function is defined.
  681. :breaka[dd] file [lnum] {name}
  682. Set a breakpoint in a sourced file. Example: >
  683. :breakadd file 43 init.vim
  684. :breaka[dd] here
  685. Set a breakpoint in the current line of the current file.
  686. Like doing: >
  687. :breakadd file <cursor-line> <current-file>
  688. < Note that this only works for commands that are executed when
  689. sourcing the file, not for a function defined in that file.
  690. :breaka[dd] expr {expression}
  691. Sets a breakpoint, that will break whenever the {expression}
  692. evaluates to a different value. Example: >
  693. :breakadd expr g:lnum
  694. < Will break, whenever the global variable lnum changes.
  695. Errors in evaluation are suppressed, you can use the name of a
  696. variable that does not exist yet. This also means you will
  697. not notice anything if the expression has a mistake.
  698. Note if you watch a |script-variable| this will break
  699. when switching scripts, since the script variable is only
  700. valid in the script where it has been defined and if that
  701. script is called from several other scripts, this will stop
  702. whenever that particular variable will become visible or
  703. inaccessible again.
  704. The [lnum] is the line number of the breakpoint. Vim will stop at or after
  705. this line. When omitted line 1 is used.
  706. *:debug-name*
  707. {name} is a pattern that is matched with the file or function name. The
  708. pattern is like what is used for autocommands. There must be a full match (as
  709. if the pattern starts with "^" and ends in "$"). A "*" matches any sequence
  710. of characters. 'ignorecase' is not used, but "\c" can be used in the pattern
  711. to ignore case |/\c|. Don't include the () for the function name!
  712. The match for sourced scripts is done against the full file name. If no path
  713. is specified the current directory is used. Examples: >
  714. breakadd file explorer.vim
  715. matches "explorer.vim" in the current directory. >
  716. breakadd file *explorer.vim
  717. matches ".../plugin/explorer.vim", ".../plugin/iexplorer.vim", etc. >
  718. breakadd file */explorer.vim
  719. matches ".../plugin/explorer.vim" and "explorer.vim" in any other directory.
  720. The match for functions is done against the name as it's shown in the output
  721. of ":function". For local functions this means that something like "<SNR>99_"
  722. is prepended.
  723. Note that functions are first loaded and later executed. When they are loaded
  724. the "file" breakpoints are checked, when they are executed the "func"
  725. breakpoints.
  726. DELETING BREAKPOINTS
  727. *:breakd* *:breakdel* *E161*
  728. :breakd[el] {nr}
  729. Delete breakpoint {nr}. Use |:breaklist| to see the number of
  730. each breakpoint.
  731. :breakd[el] *
  732. Delete all breakpoints.
  733. :breakd[el] func [lnum] {name}
  734. Delete a breakpoint in a function.
  735. :breakd[el] file [lnum] {name}
  736. Delete a breakpoint in a sourced file.
  737. :breakd[el] here
  738. Delete a breakpoint at the current line of the current file.
  739. When [lnum] is omitted, the first breakpoint in the function or file is
  740. deleted.
  741. The {name} must be exactly the same as what was typed for the ":breakadd"
  742. command. "explorer", "*explorer.vim" and "*explorer*" are different.
  743. LISTING BREAKPOINTS
  744. *:breakl* *:breaklist*
  745. :breakl[ist]
  746. List all breakpoints.
  747. OBSCURE
  748. *:debugg* *:debuggreedy*
  749. :debugg[reedy]
  750. Read debug mode commands from the normal input stream, instead
  751. of getting them directly from the user. Only useful for test
  752. scripts. Example: >
  753. echo 'q^Mq' | vim -e -s -c debuggreedy -c 'breakadd file script.vim' -S script.vim
  754. :0debugg[reedy]
  755. Undo ":debuggreedy": get debug mode commands directly from the
  756. user, don't use typeahead for debug commands.
  757. ==============================================================================
  758. Profiling *profile* *profiling*
  759. Profiling means that Vim measures the time that is spent on executing
  760. functions and/or scripts.
  761. You can also use the |reltime()| function to measure time.
  762. For profiling syntax highlighting see |:syntime|.
  763. For example, to profile the one_script.vim script file: >
  764. :profile start /tmp/one_script_profile
  765. :profile file one_script.vim
  766. :source one_script.vim
  767. :exit
  768. :prof[ile] start {fname} *:prof* *:profile* *E750*
  769. Start profiling, write the output in {fname} upon exit.
  770. "~/" and environment variables in {fname} will be expanded.
  771. If {fname} already exists it will be silently overwritten.
  772. The variable |v:profiling| is set to one.
  773. :prof[ile] stop
  774. Write the logfile and stop profiling.
  775. :prof[ile] pause
  776. Don't profile until the following ":profile continue". Can be
  777. used when doing something that should not be counted (e.g., an
  778. external command). Does not nest.
  779. :prof[ile] continue
  780. Continue profiling after ":profile pause".
  781. :prof[ile] func {pattern}
  782. Profile function that matches the pattern {pattern}.
  783. See |:debug-name| for how {pattern} is used.
  784. :prof[ile][!] file {pattern}
  785. Profile script file that matches the pattern {pattern}.
  786. See |:debug-name| for how {pattern} is used.
  787. This only profiles the script itself, not the functions
  788. defined in it.
  789. When the [!] is added then all functions defined in the script
  790. will also be profiled.
  791. Note that profiling only starts when the script is loaded
  792. after this command. A :profile command in the script itself
  793. won't work.
  794. :prof[ile] dump
  795. Don't wait until exiting Vim and write the current state of
  796. profiling to the log immediately.
  797. :profd[el] ... *:profd* *:profdel*
  798. Stop profiling for the arguments specified. See |:breakdel|
  799. for the arguments.
  800. You must always start with a ":profile start fname" command. The resulting
  801. file is written when Vim exits. Here is an example of the output, with line
  802. numbers prepended for the explanation:
  803. 1 FUNCTION Test2() ~
  804. 2 Called 1 time ~
  805. 3 Total time: 0.155251 ~
  806. 4 Self time: 0.002006 ~
  807. 5 ~
  808. 6 count total (s) self (s) ~
  809. 7 9 0.000096 for i in range(8) ~
  810. 8 8 0.153655 0.000410 call Test3() ~
  811. 9 8 0.000070 endfor ~
  812. 10 " Ask a question ~
  813. 11 1 0.001341 echo input("give me an answer: ") ~
  814. The header (lines 1-4) gives the time for the whole function. The "Total"
  815. time is the time passed while the function was executing. The "Self" time is
  816. the "Total" time reduced by time spent in:
  817. - other user defined functions
  818. - sourced scripts
  819. - executed autocommands
  820. - external (shell) commands
  821. Lines 7-11 show the time spent in each executed line. Lines that are not
  822. executed do not count. Thus a comment line is never counted.
  823. The Count column shows how many times a line was executed. Note that the
  824. "for" command in line 7 is executed one more time as the following lines.
  825. That is because the line is also executed to detect the end of the loop.
  826. The time Vim spends waiting for user input isn't counted at all. Thus how
  827. long you take to respond to the input() prompt is irrelevant.
  828. Profiling should give a good indication of where time is spent, but keep in
  829. mind there are various things that may clobber the results:
  830. - Real elapsed time is measured, if other processes are busy they may cause
  831. delays at unpredictable moments. You may want to run the profiling several
  832. times and use the lowest results.
  833. - If you have several commands in one line you only get one time. Split the
  834. line to see the time for the individual commands.
  835. - The time of the lines added up is mostly less than the time of the whole
  836. function. There is some overhead in between.
  837. - Functions that are deleted before Vim exits will not produce profiling
  838. information. You can check the |v:profiling| variable if needed: >
  839. :if !v:profiling
  840. : delfunc MyFunc
  841. :endif
  842. <
  843. - Profiling may give weird results on multi-processor systems, when sleep
  844. mode kicks in or the processor frequency is reduced to save power.
  845. - The "self" time is wrong when a function is used recursively.
  846. ==============================================================================
  847. Context *Context* *context*
  848. The editor state is represented by the Context concept. This includes things
  849. like the current |jumplist|, values of |registers|, and more, described below.
  850. *context-types*
  851. The following Context items are supported:
  852. "jumps" |jumplist|
  853. "regs" |registers|
  854. "bufs" |buffer-list|
  855. "gvars" |global-variable|s
  856. "sfuncs" |script-local| functions
  857. "funcs" global and |script-local| functions
  858. *context-dict*
  859. Context objects are dictionaries with the following key-value pairs:
  860. - "jumps", "regs", "bufs", "gvars":
  861. |readfile()|-style |List| representation of corresponding msgpack
  862. objects (see |msgpackdump()| and |msgpackparse()|).
  863. - "funcs" (includes |script-local| functions as well):
  864. |List| of |:function| definitions.
  865. *context-stack*
  866. An initially-empty internal Context stack is maintained by the ctx-family
  867. functions (see |ctx-functions|).
  868. vim:tw=78:ts=8:noet:ft=help:norl: