pack.txt (23895B)
1 *pack.txt* Nvim 2 3 NVIM REFERENCE MANUAL 4 5 Extending Nvim 6 7 8 Type |gO| to see the table of contents. 9 10 ============================================================================== 11 Using Vim packages *packages* 12 13 A Vim "package" is a directory that contains |plugin|s. Compared to normal 14 plugins, a package can... 15 - be downloaded as an archive and unpacked in its own directory, so the files 16 are not mixed with files of other plugins. 17 - be a git, mercurial, etc. repository, thus easy to update. 18 - contain multiple plugins that depend on each other. 19 - contain plugins that are automatically loaded on startup ("start" packages, 20 located in "pack/*/start/*") and ones that are only loaded when needed with 21 |:packadd| ("opt" packages, located in "pack/*/opt/*"). 22 23 *runtime-search-path* 24 Nvim searches for |:runtime| files in: 25 - 1. all paths in 'runtimepath' 26 - 2. all "pack/*/start/*" dirs 27 28 Note that the "pack/*/start/*" paths are not explicitly included in 29 'runtimepath', so they will not be reported by ":set rtp" or "echo &rtp". 30 Scripts can use |nvim_list_runtime_paths()| to list all used directories, and 31 |nvim_get_runtime_file()| to query for specific files or sub-folders within 32 the runtime path. Example: > 33 " List all runtime dirs and packages with Lua paths. 34 :echo nvim_get_runtime_file("lua/", v:true) 35 36 Using a package and loading automatically ~ 37 38 Let's assume your Nvim files are in "~/.local/share/nvim/site" and you want to 39 add a package from a zip archive "/tmp/foopack.zip": > 40 % mkdir -p ~/.local/share/nvim/site/pack/foo 41 % cd ~/.local/share/nvim/site/pack/foo 42 % unzip /tmp/foopack.zip 43 44 The directory name "foo" is arbitrary, you can pick anything you like. 45 46 You would now have these files under ~/.local/share/nvim/site: > 47 pack/foo/README.txt 48 pack/foo/start/foobar/plugin/foo.vim 49 pack/foo/start/foobar/syntax/some.vim 50 pack/foo/opt/foodebug/plugin/debugger.vim 51 52 On startup after processing your |config|, Nvim scans all directories in 53 'packpath' for plugins in "pack/*/start/*", then loads the plugins. 54 55 To allow for calling into package functionality while parsing your |vimrc|, 56 |:colorscheme| and |autoload| will both automatically search under 'packpath' 57 as well in addition to 'runtimepath'. See the documentation for each for 58 details. 59 60 In the example Nvim will find "pack/foo/start/foobar/plugin/foo.vim" and load 61 it. 62 63 If the "foobar" plugin kicks in and sets the 'filetype' to "some", Nvim will 64 find the syntax/some.vim file, because its directory is in the runtime search 65 path. 66 67 Nvim will also load ftdetect files, if there are any. 68 69 Note that the files under "pack/foo/opt" are not loaded automatically, only 70 the ones under "pack/foo/start". See |pack-add| below for how the "opt" 71 directory is used. 72 73 Loading packages automatically will not happen if loading plugins is disabled, 74 see |load-plugins|. 75 76 To load packages earlier, so that plugin/ files are sourced: 77 :packloadall 78 This also works when loading plugins is disabled. The automatic loading will 79 only happen once. 80 81 If the package has an "after" directory, that directory is added to the end of 82 'runtimepath', so that anything there will be loaded later. 83 84 85 Using a single plugin and loading it automatically ~ 86 87 If you don't have a package but a single plugin, you need to create the extra 88 directory level: > 89 % mkdir -p ~/.local/share/nvim/site/pack/foo/start/foobar 90 % cd ~/.local/share/nvim/site/pack/foo/start/foobar 91 % unzip /tmp/someplugin.zip 92 93 You would now have these files: > 94 pack/foo/start/foobar/plugin/foo.vim 95 pack/foo/start/foobar/syntax/some.vim 96 97 From here it works like above. 98 99 100 Optional plugins ~ 101 *pack-add* 102 To load an optional plugin from a pack use the `:packadd` command: > 103 :packadd foodebug 104 This searches for "pack/*/opt/foodebug" in 'packpath' and will find 105 ~/.local/share/nvim/site/pack/foo/opt/foodebug/plugin/debugger.vim and source 106 it. 107 108 This could be done if some conditions are met. For example, depending on 109 whether Nvim supports a feature or a dependency is missing. 110 111 You can also load an optional plugin at startup, by putting this command in 112 your |config|: > 113 :packadd! foodebug 114 The extra "!" is so that the plugin isn't loaded if Nvim was started with 115 |--noplugin|. 116 117 It is perfectly normal for a package to only have files in the "opt" 118 directory. You then need to load each plugin when you want to use it. 119 120 121 Where to put what ~ 122 123 Since color schemes, loaded with `:colorscheme`, are found below 124 "pack/*/start" and "pack/*/opt", you could put them anywhere. We recommend 125 you put them below "pack/*/opt", for example 126 "~/.config/nvim/pack/mycolors/opt/dark/colors/very_dark.vim". 127 128 Filetype plugins should go under "pack/*/start", so that they are always 129 found. Unless you have more than one plugin for a file type and want to 130 select which one to load with `:packadd`. E.g. depending on the compiler 131 version: > 132 if foo_compiler_version > 34 133 packadd foo_new 134 else 135 packadd foo_old 136 endif 137 138 The "after" directory is most likely not useful in a package. It's not 139 disallowed though. 140 141 ============================================================================== 142 Creating Vim packages *package-create* 143 144 This assumes you write one or more plugins that you distribute as a package. 145 146 If you have two unrelated plugins you would use two packages, so that Vim 147 users can choose what they include or not. Or you can decide to use one 148 package with optional plugins, and tell the user to add the preferred ones 149 with `:packadd`. 150 151 Decide how you want to distribute the package. You can create an archive or 152 you could use a repository. An archive can be used by more users, but is a 153 bit harder to update to a new version. A repository can usually be kept 154 up-to-date easily, but it requires a program like "git" to be available. 155 You can do both, github can automatically create an archive for a release. 156 157 Your directory layout would be like this: > 158 start/foobar/plugin/foo.vim " always loaded, defines commands 159 start/foobar/plugin/bar.vim " always loaded, defines commands 160 start/foobar/autoload/foo.vim " loaded when foo command used 161 start/foobar/doc/foo.txt " help for foo.vim 162 start/foobar/doc/tags " help tags 163 opt/fooextra/plugin/extra.vim " optional plugin, defines commands 164 opt/fooextra/autoload/extra.vim " loaded when extra command used 165 opt/fooextra/doc/extra.txt " help for extra.vim 166 opt/fooextra/doc/tags " help tags 167 < 168 This allows for the user to do: > 169 mkdir ~/.local/share/nvim/site/pack 170 cd ~/.local/share/nvim/site/pack 171 git clone https://github.com/you/foobar.git myfoobar 172 173 Here "myfoobar" is a name that the user can choose, the only condition is that 174 it differs from other packages. 175 176 In your documentation you explain what the plugins do, and tell the user how 177 to load the optional plugin: > 178 :packadd! fooextra 179 180 You could add this packadd command in one of your plugins, to be executed when 181 the optional plugin is needed. 182 183 Run the `:helptags` command to generate the doc/tags file. Including this 184 generated file in the package means that the user can drop the package in the 185 pack directory and the help command works right away. Don't forget to re-run 186 the command after changing the plugin help: > 187 :helptags path/start/foobar/doc 188 :helptags path/opt/fooextra/doc 189 190 191 Dependencies between plugins ~ 192 *packload-two-steps* 193 Suppose you have two plugins that depend on the same functionality. You can 194 put the common functionality in an autoload directory, so that it will be 195 found automatically. Your package would have these files: 196 197 pack/foo/start/one/plugin/one.vim > 198 call foolib#getit() 199 pack/foo/start/two/plugin/two.vim > 200 call foolib#getit() 201 pack/foo/start/lib/autoload/foolib.vim > 202 func foolib#getit() 203 204 This works, because start packages will be searched for autoload files, when 205 sourcing the plugins. 206 207 ============================================================================== 208 Plugin manager *vim.pack* 209 210 Install, update, and delete external plugins. WARNING: It is still considered 211 experimental, yet should be stable enough for daily use. 212 213 Manages plugins only in a dedicated *vim.pack-directory* (see |packages|): 214 `$XDG_DATA_HOME/nvim/site/pack/core/opt`. `$XDG_DATA_HOME/nvim/site` needs to 215 be part of 'packpath'. It usually is, but might not be in cases like |--clean| 216 or setting |$XDG_DATA_HOME| during startup. Plugin's subdirectory name matches 217 plugin's name in specification. It is assumed that all plugins in the 218 directory are managed exclusively by `vim.pack`. 219 220 Uses Git to manage plugins and requires present `git` executable. Target 221 plugins should be Git repositories with versions as named tags following 222 semver convention `v<major>.<minor>.<patch>`. 223 224 The latest state of all managed plugins is stored inside a *vim.pack-lockfile* 225 located at `$XDG_CONFIG_HOME/nvim/nvim-pack-lock.json`. It is a JSON file that 226 is used to persistently track data about plugins. For a more robust config 227 treat lockfile like its part: put under version control, etc. In this case all 228 plugins from the lockfile will be installed at once and at lockfile's revision 229 (instead of inferring from `version`). This is done on the very first 230 `vim.pack` function call to ensure that lockfile is aligned with what is 231 actually on the disk. Lockfile should not be edited by hand. Corrupted data 232 for installed plugins is repaired (including after deleting whole file), but 233 `version` fields will be missing for not yet added plugins. 234 235 *vim.pack-examples* 236 237 Basic install and management ~ 238 • Add |vim.pack.add()| call(s) to 'init.lua': >lua 239 240 vim.pack.add({ 241 -- Install "plugin1" and use default branch (usually `main` or `master`) 242 'https://github.com/user/plugin1', 243 244 -- Same as above, but using a table (allows setting other options) 245 { src = 'https://github.com/user/plugin1' }, 246 247 -- Specify plugin's name (here the plugin will be called "plugin2" 248 -- instead of "generic-name") 249 { src = 'https://github.com/user/generic-name', name = 'plugin2' }, 250 251 -- Specify version to follow during install and update 252 { 253 src = 'https://github.com/user/plugin3', 254 -- Version constraint, see |vim.version.range()| 255 version = vim.version.range('1.0'), 256 }, 257 { 258 src = 'https://github.com/user/plugin4', 259 -- Git branch, tag, or commit hash 260 version = 'main', 261 }, 262 }) 263 264 -- Plugin's code can be used directly after `add()` 265 plugin1 = require('plugin1') 266 < 267 • Restart Nvim (for example, with |:restart|). Plugins that were not yet 268 installed will be available on disk after `add()` call. Their revision is 269 taken from |vim.pack-lockfile| (if present) or inferred from the `version`. 270 • To update all plugins with new changes: 271 • Execute |vim.pack.update()|. This will download updates from source and 272 show confirmation buffer in a separate tabpage. 273 • Review changes. To confirm all updates execute |:write|. To discard 274 updates execute |:quit|. 275 • (Optionally) |:restart| to start using code from updated plugins. 276 277 Use shorter source ~ 278 279 Create custom Lua helpers: >lua 280 281 local gh = function(x) return 'https://github.com/' .. x end 282 local cb = function(x) return 'https://codeberg.org/' .. x end 283 vim.pack.add({ gh('user/plugin1'), cb('user/plugin2') }) 284 < 285 286 Another approach is to utilize Git's `insteadOf` configuration: 287 • `git config --global url."https://github.com/".insteadOf "gh:"` 288 • `git config --global url."https://codeberg.org/".insteadOf "cb:"` 289 • In 'init.lua': `vim.pack.add({ 'gh:user/plugin1', 'cb:user/plugin2' })`. 290 These sources will be used verbatim in |vim.pack-lockfile|, so reusing the 291 config on different machine will require the same Git configuration. 292 293 Explore installed plugins ~ 294 • `vim.pack.update(nil, { offline = true })` 295 • Navigate between plugins with `[[` and `]]`. List them with `gO` 296 (|vim.lsp.buf.document_symbol()|). 297 298 Switch plugin's version and/or source ~ 299 • Update 'init.lua' for plugin to have desired `version` and/or `src`. Let's 300 say, the switch is for plugin named 'plugin1'. 301 • |:restart|. The plugin's state on disk (revision and/or tracked source) is 302 not yet changed. Only plugin's `version` in |vim.pack-lockfile| is updated. 303 • Execute `vim.pack.update({ 'plugin1' })`. The plugin's source is updated. If 304 only switching version, use `{ offline = true }` option table. 305 • Review changes and either confirm or discard them. If discarded, revert 306 `version` change in 'init.lua' as well or you will be prompted again next 307 time you run |vim.pack.update()|. 308 309 Freeze plugin from being updated ~ 310 • Update 'init.lua' for plugin to have `version` set to current revision. Get 311 it from |vim.pack-lockfile| (plugin's field `rev`; looks like `abc12345`). 312 • |:restart|. 313 314 Unfreeze plugin to start receiving updates ~ 315 • Update 'init.lua' for plugin to have `version` set to whichever version you 316 want it to be updated. 317 • |:restart|. 318 319 Revert plugin after an update ~ 320 • Revert the |vim.pack-lockfile| to the state before the update: 321 • If Git tracked: `git checkout HEAD -- nvim-pack-lock.json` 322 • If not tracked: examine log file ("nvim-pack.log" at "log" |stdpath()|), 323 locate the revisions before the latest update, and (carefully) adjust 324 current lockfile to have those revisions. 325 • |:restart|. 326 • `vim.pack.update({ 'plugin' }, { offline = true, target = 'lockfile' })`. 327 Read and confirm. 328 329 Synchronize config across machines ~ 330 • On main machine: 331 • Add |vim.pack-lockfile| to VCS. 332 • Push to the remote server. 333 • On secondary machine: 334 • Pull from the server. 335 • |:restart|. New plugins (not present locally, but present in the lockfile) 336 are installed at proper revision. 337 • `vim.pack.update(nil, { target = 'lockfile' })`. Read and confirm. 338 • Manually delete outdated plugins (present locally, but were not present in 339 the lockfile prior to restart) with `vim.pack.del( { 'plugin' })`. They 340 can be located by examining the VCS difference of the lockfile 341 (`git diff -- nvim-pack-lock.json` for Git). 342 343 Remove plugins from disk ~ 344 • Remove plugin specs from |vim.pack.add()| calls in 'init.lua' or they will 345 be reinstalled later. 346 • |:restart|. 347 • Use |vim.pack.del()| with a list of plugin names to remove. Use 348 |vim.pack.get()| to get all non-active plugins: >lua 349 vim.iter(vim.pack.get()) 350 :filter(function(x) return not x.active end) 351 :map(function(x) return x.spec.name end) 352 :totable() 353 < 354 355 *vim.pack-events* 356 357 Performing actions via `vim.pack` functions can trigger these events: 358 • *PackChangedPre* - before trying to change plugin's state. 359 • *PackChanged* - after plugin's state has changed. 360 361 Each event populates the following |event-data| fields: 362 • `active` - whether plugin was added via |vim.pack.add()| to current session. 363 • `kind` - one of "install" (install on disk; before loading), "update" 364 (update already installed plugin; might be not loaded), "delete" (delete 365 from disk). 366 • `spec` - plugin's specification with defaults made explicit. 367 • `path` - full path to plugin's directory. 368 369 These events can be used to execute plugin hooks. For example: >lua 370 local hooks = function(ev) 371 -- Use available |event-data| 372 local name, kind = ev.data.spec.name, ev.data.kind 373 374 -- Run build script after plugin's code has changed 375 if name == 'plug-1' and (kind == 'install' or kind == 'update') then 376 -- Append `:wait()` if you need synchronous execution 377 vim.system({ 'make' }, { cwd = ev.data.path }) 378 end 379 380 -- If action relies on code from the plugin (like user command or 381 -- Lua code), make sure to explicitly load it first 382 if name == 'plug-2' and kind == 'update' then 383 if not ev.data.active then 384 vim.cmd.packadd('plug-2') 385 end 386 vim.cmd('PlugTwoUpdate') 387 require('plug2').after_update() 388 end 389 end 390 391 -- If hooks need to run on install, run this before `vim.pack.add()` 392 -- To act on install from lockfile, run before very first `vim.pack.add()` 393 vim.api.nvim_create_autocmd('PackChanged', { callback = hooks }) 394 < 395 396 397 *vim.pack.Spec* 398 399 Fields: ~ 400 • {src} (`string`) URI from which to install and pull updates. Any 401 format supported by `git clone` is allowed. 402 • {name}? (`string`) Name of plugin. Will be used as directory name. 403 Default: `src` repository name. 404 • {version}? (`string|vim.VersionRange`) Version to use for install and 405 updates. Can be: 406 • `nil` (no value, default) to use repository's default 407 branch (usually `main` or `master`). 408 • String to use specific branch, tag, or commit hash. 409 • Output of |vim.version.range()| to install the 410 greatest/last semver tag inside the version constraint. 411 • {data}? (`any`) Arbitrary data associated with a plugin. 412 413 414 add({specs}, {opts}) *vim.pack.add()* 415 Add plugin to current session 416 • For each specification check that plugin exists on disk in 417 |vim.pack-directory|: 418 • If exists, check if its `src` is the same as input. If not - delete 419 immediately to clean install from the new source. Otherwise do 420 nothing. 421 • If doesn't exist, install it by downloading from `src` into `name` 422 subdirectory (via partial blobless `git clone`) and update revision to 423 match `version` (via `git checkout`). Plugin will not be on disk if 424 any step resulted in an error. 425 • For each plugin execute |:packadd| (or customizable `load` function) 426 making it reachable by Nvim. 427 428 Notes: 429 • Installation is done in parallel, but waits for all to finish before 430 continuing next code execution. 431 • If plugin is already present on disk, there are no checks about its 432 current revision. The specified `version` can be not the one actually 433 present on disk. Execute |vim.pack.update()| to synchronize. 434 • Adding plugin second and more times during single session does nothing: 435 only the data from the first adding is registered. 436 437 Parameters: ~ 438 • {specs} (`(string|vim.pack.Spec)[]`) List of plugin specifications. 439 String item is treated as `src`. 440 • {opts} (`table?`) A table with the following fields: 441 • {load}? 442 (`boolean|fun(plug_data: {spec: vim.pack.Spec, path: string})`) 443 Load `plugin/` files and `ftdetect/` scripts. If `false`, 444 works like `:packadd!`. If function, called with plugin 445 data and is fully responsible for loading plugin. Default 446 `false` during |init.lua| sourcing and `true` afterwards. 447 • {confirm}? (`boolean`) Whether to ask user to confirm 448 initial install. Default `true`. 449 450 del({names}, {opts}) *vim.pack.del()* 451 Remove plugins from disk 452 453 Parameters: ~ 454 • {names} (`string[]`) List of plugin names to remove from disk. Must 455 be managed by |vim.pack|, not necessarily already added to 456 current session. 457 • {opts} (`table?`) A table with the following fields: 458 • {force}? (`boolean`) Whether to allow deleting an active 459 plugin. Default `false`. 460 461 get({names}, {opts}) *vim.pack.get()* 462 Gets |vim.pack| plugin info, optionally filtered by `names`. 463 464 Parameters: ~ 465 • {names} (`string[]?`) List of plugin names. Default: all plugins 466 managed by |vim.pack|. 467 • {opts} (`table?`) A table with the following fields: 468 • {info} (`boolean`) Whether to include extra plugin info. 469 Default `true`. 470 471 Return: ~ 472 (`table[]`) A list of objects with the following fields: 473 • {active} (`boolean`) Whether plugin was added via |vim.pack.add()| 474 to current session. 475 • {branches}? (`string[]`) Available Git branches (first is default). 476 Missing if `info=false`. 477 • {path} (`string`) Plugin's path on disk. 478 • {rev} (`string`) Current Git revision. 479 • {spec} (`vim.pack.SpecResolved`) A |vim.pack.Spec| with resolved 480 `name`. 481 • {tags}? (`string[]`) Available Git tags. Missing if `info=false`. 482 483 update({names}, {opts}) *vim.pack.update()* 484 Update plugins 485 • Download new changes from source. 486 • Infer update info (current/target revisions, changelog, etc.). 487 • Depending on `force`: 488 • If `false`, show confirmation buffer. It lists data about all set to 489 update plugins. Pending changes starting with `>` will be applied 490 while the ones starting with `<` will be reverted. It has dedicated 491 buffer-local mappings: 492 • |]]| and |[[| to navigate through plugin sections. 493 Some features are provided via LSP: 494 • 'textDocument/documentSymbol' (`gO` via |lsp-defaults| or 495 |vim.lsp.buf.document_symbol()|) - show structure of the buffer. 496 • 'textDocument/hover' (`K` via |lsp-defaults| or 497 |vim.lsp.buf.hover()|) - show more information at cursor. Like 498 details of particular pending change or newer tag. 499 • 'textDocument/codeAction' (`gra` via |lsp-defaults| or 500 |vim.lsp.buf.code_action()|) - show code actions available for 501 "plugin at cursor". Like "delete" (if plugin is not active), 502 "update" or "skip updating" (if there are pending updates). 503 Execute |:write| to confirm update, execute |:quit| to discard the 504 update. 505 • If `true`, make updates right away. 506 507 Notes: 508 • Every actual update is logged in "nvim-pack.log" file inside "log" 509 |stdpath()|. 510 • It doesn't update source's default branch if it has changed (like from 511 `master` to `main`). To have `version = nil` point to a new default 512 branch, re-install the plugin (|vim.pack.del()| + |vim.pack.add()|). 513 514 Parameters: ~ 515 • {names} (`string[]?`) List of plugin names to update. Must be managed 516 by |vim.pack|, not necessarily already added to current 517 session. Default: names of all plugins managed by |vim.pack|. 518 • {opts} (`table?`) A table with the following fields: 519 • {force}? (`boolean`) Whether to skip confirmation and make 520 updates immediately. Default `false`. 521 • {offline}? (`boolean`) Whether to skip downloading new 522 updates. Default: `false`. 523 • {target}? (`string`) How to compute a new plugin revision. 524 One of: 525 • "version" (default) - use latest revision matching 526 `version` from plugin specification. 527 • "lockfile" - use revision from the lockfile. Useful for 528 reverting or performing controlled update. 529 530 531 vim:tw=78:ts=8:sw=4:sts=4:et:ft=help:norl: