2016-06-25 20:36:44 +01:00
---
title: User's Guide for Vim in Nixpkgs
2017-01-30 17:27:44 +00:00
author: Marc Weber
2016-06-25 20:36:44 +01:00
date: 2016-06-25
---
2019-10-20 11:17:58 +01:00
# Vim
2016-06-25 20:36:44 +01:00
2018-09-09 09:45:45 +01:00
Both Neovim and Vim can be configured to include your favorite plugins
and additional libraries.
2017-01-30 17:27:44 +00:00
Loading can be deferred; see examples.
2016-06-25 20:36:44 +01:00
2018-09-09 09:45:45 +01:00
At the moment we support three different methods for managing plugins:
- Vim packages (*recommend*)
- VAM (=vim-addon-manager)
- Pathogen
2018-09-20 08:20:11 +01:00
- vim-plug
2016-06-25 20:36:44 +01:00
2017-06-22 13:09:56 +01:00
## Custom configuration
Adding custom .vimrc lines can be done using the following code:
2019-07-15 01:13:34 +01:00
```nix
2017-06-22 13:09:56 +01:00
vim_configurable.customize {
2018-10-24 16:02:54 +01:00
# `name` specifies the name of the executable and package
2017-06-22 13:09:56 +01:00
name = "vim-with-plugins";
vimrcConfig.customRC = ''
set hidden
'';
}
```
2019-07-15 01:13:34 +01:00
This configuration is used when Vim is invoked with the command specified as name, in this case `vim-with-plugins` .
2018-10-24 16:02:54 +01:00
2018-09-09 09:45:45 +01:00
For Neovim the `configure` argument can be overridden to achieve the same:
2019-07-15 01:13:34 +01:00
```nix
2018-09-09 09:45:45 +01:00
neovim.override {
configure = {
customRC = ''
# here your custom configuration goes!
'';
};
}
```
2019-07-15 01:13:34 +01:00
If you want to use `neovim-qt` as a graphical editor, you can configure it by overriding Neovim in an overlay
or passing it an overridden Neovimn:
2019-01-05 12:58:00 +00:00
2019-07-15 01:13:34 +01:00
```nix
2019-01-05 12:58:00 +00:00
neovim-qt.override {
neovim = neovim.override {
configure = {
customRC = ''
# your custom configuration
'';
};
};
}
```
2018-09-09 09:45:45 +01:00
## Managing plugins with Vim packages
2017-06-22 13:09:56 +01:00
2019-07-15 01:13:34 +01:00
To store you plugins in Vim packages (the native Vim plugin manager, see `:help packages` ) the following example can be used:
2017-06-22 13:09:56 +01:00
2019-07-15 01:13:34 +01:00
```nix
2017-06-22 13:09:56 +01:00
vim_configurable.customize {
vimrcConfig.packages.myVimPackage = with pkgs.vimPlugins; {
# loaded on launch
start = [ youcompleteme fugitive ];
# manually loadable by calling `:packadd $plugin-name`
2019-07-15 01:13:34 +01:00
# however, if a Vim plugin has a dependency that is not explicitly listed in
# opt that dependency will always be added to start to avoid confusion.
2017-06-22 13:09:56 +01:00
opt = [ phpCompletion elm-vim ];
# To automatically load a plugin when opening a filetype, add vimrc lines like:
# autocmd FileType php :packadd phpCompletion
2018-09-09 09:45:45 +01:00
};
}
2017-06-22 13:09:56 +01:00
```
2018-12-24 12:13:59 +00:00
`myVimPackage` is an arbitrary name for the generated package. You can choose any name you like.
2018-09-20 08:05:33 +01:00
For Neovim the syntax is:
2018-09-09 09:45:45 +01:00
2019-07-15 01:13:34 +01:00
```nix
2018-09-09 09:45:45 +01:00
neovim.override {
configure = {
customRC = ''
# here your custom configuration goes!
'';
packages.myVimPackage = with pkgs.vimPlugins; {
# see examples below how to use custom packages
start = [ ];
2019-07-15 01:13:34 +01:00
# If a Vim plugin has a dependency that is not explicitly listed in
2018-12-24 12:13:59 +00:00
# opt that dependency will always be added to start to avoid confusion.
2018-09-09 09:45:45 +01:00
opt = [ ];
};
};
}
```
The resulting package can be added to `packageOverrides` in `~/.nixpkgs/config.nix` to make it installable:
2019-07-15 01:13:34 +01:00
```nix
2018-09-09 09:45:45 +01:00
{
packageOverrides = pkgs: with pkgs; {
myVim = vim_configurable.customize {
2018-10-24 16:02:54 +01:00
# `name` specifies the name of the executable and package
2018-09-09 09:45:45 +01:00
name = "vim-with-plugins";
# add here code from the example section
};
myNeovim = neovim.override {
configure = {
# add here code from the example section
};
};
};
}
```
After that you can install your special grafted `myVim` or `myNeovim` packages.
2018-09-20 08:05:33 +01:00
## Managing plugins with vim-plug
2018-09-20 08:20:11 +01:00
To use [vim-plug ](https://github.com/junegunn/vim-plug ) to manage your Vim
plugins the following example can be used:
2018-09-20 08:05:33 +01:00
2019-07-15 01:13:34 +01:00
```nix
2018-09-20 08:05:33 +01:00
vim_configurable.customize {
vimrcConfig.packages.myVimPackage = with pkgs.vimPlugins; {
# loaded on launch
plug.plugins = [ youcompleteme fugitive phpCompletion elm-vim ];
};
}
```
For Neovim the syntax is:
2019-07-15 01:13:34 +01:00
```nix
2018-09-20 08:05:33 +01:00
neovim.override {
configure = {
customRC = ''
# here your custom configuration goes!
'';
plug.plugins = with pkgs.vimPlugins; [
vim-go
];
};
}
```
2018-09-09 09:45:45 +01:00
## Managing plugins with VAM
2017-06-22 13:09:56 +01:00
2018-09-09 09:45:45 +01:00
### Handling dependencies of Vim plugins
2016-06-25 20:36:44 +01:00
VAM introduced .json files supporting dependencies without versioning
assuming that "using latest version" is ok most of the time.
2017-06-22 13:09:56 +01:00
### Example
2016-06-25 20:36:44 +01:00
First create a vim-scripts file having one plugin name per line. Example:
2019-07-15 01:13:34 +01:00
```
"tlib"
{'name': 'vim-addon-sql'}
{'filetype_regex': '\%(vim)$', 'names': ['reload', 'vim-dev-plugin']}
```
2016-06-25 20:36:44 +01:00
Such vim-scripts file can be read by VAM as well like this:
2019-07-15 01:13:34 +01:00
```vim
call vam#Scripts(expand('~/.vim-scripts'), {})
```
2016-06-25 20:36:44 +01:00
Create a default.nix file:
2019-07-15 01:13:34 +01:00
```nix
{ nixpkgs ? import < nixpkgs > {}, compiler ? "ghc7102" }:
nixpkgs.vim_configurable.customize { name = "vim"; vimrcConfig.vam.pluginDictionaries = [ "vim-addon-vim2nix" ]; }
```
2016-06-25 20:36:44 +01:00
Create a generate.vim file:
2019-07-15 01:13:34 +01:00
```vim
ActivateAddons vim-addon-vim2nix
let vim_scripts = "vim-scripts"
call nix#ExportPluginsForNix({
\ 'path_to_nixpkgs': eval('{"'.substitute(substitute(substitute($NIX_PATH, ':', ',', 'g'), '=',':', 'g'), '\([:,]\)', '"\1"',"g").'"}')["nixpkgs"],
\ 'cache_file': '/tmp/vim2nix-cache',
\ 'try_catch': 0,
\ 'plugin_dictionaries': ["vim-addon-manager"]+map(readfile(vim_scripts), 'eval(v:val)')
\ })
```
2016-06-25 20:36:44 +01:00
Then run
2019-07-15 01:13:34 +01:00
```bash
nix-shell -p vimUtils.vim_with_vim2nix --command "vim -c 'source generate.vim'"
```
2016-06-25 20:36:44 +01:00
You should get a Vim buffer with the nix derivations (output1) and vam.pluginDictionaries (output2).
2019-07-15 01:13:34 +01:00
You can add your Vim to your system's configuration file like this and start it by "vim-my":
2016-06-25 20:36:44 +01:00
2019-07-15 01:13:34 +01:00
```
my-vim =
let plugins = let inherit (vimUtils) buildVimPluginFrom2Nix; in {
copy paste output1 here
}; in vim_configurable.customize {
name = "vim-my";
vimrcConfig.vam.knownPlugins = plugins; # optional
vimrcConfig.vam.pluginDictionaries = [
copy paste output2 here
];
2016-06-25 20:36:44 +01:00
2019-07-15 01:13:34 +01:00
# Pathogen would be
# vimrcConfig.pathogen.knownPlugins = plugins; # plugins
# vimrcConfig.pathogen.pluginNames = ["tlib"];
};
```
2016-06-25 20:36:44 +01:00
Sample output1:
2019-07-15 01:13:34 +01:00
```
"reload" = buildVimPluginFrom2Nix { # created by nix#NixDerivation
name = "reload";
src = fetchgit {
url = "git://github.com/xolox/vim-reload";
rev = "0a601a668727f5b675cb1ddc19f6861f3f7ab9e1";
sha256 = "0vb832l9yxj919f5hfg6qj6bn9ni57gnjd3bj7zpq7d4iv2s4wdh";
};
dependencies = ["nim-misc"];
2016-06-25 20:36:44 +01:00
2019-07-15 01:13:34 +01:00
};
[...]
```
2016-06-25 20:36:44 +01:00
Sample output2:
2019-07-15 01:13:34 +01:00
```nix
[
''vim-addon-manager''
''tlib''
{ "name" = ''vim-addon-sql''; }
{ "filetype_regex" = ''\%(vim)$$''; "names" = [ ''reload'' ''vim-dev-plugin'' ]; }
]
```
2016-06-25 20:36:44 +01:00
2018-09-09 09:46:35 +01:00
## Adding new plugins to nixpkgs
2019-07-15 00:38:00 +01:00
Nix expressions for Vim plugins are stored in [pkgs/misc/vim-plugins ](/pkgs/misc/vim-plugins ). For the vast majority of plugins, Nix expressions are automatically generated by running [`./update.py` ](/pkgs/misc/vim-plugins/update.py ). This creates a [generated.nix ](/pkgs/misc/vim-plugins/generated.nix ) file based on the plugins listed in [vim-plugin-names ](/pkgs/misc/vim-plugins/vim-plugin-names ). Plugins are listed in alphabetical order in `vim-plugin-names` using the format `[github username]/[repository]` . For example https://github.com/scrooloose/nerdtree becomes `scrooloose/nerdtree` .
Some plugins require overrides in order to function properly. Overrides are placed in [overrides.nix ](/pkgs/misc/vim-plugins/overrides.nix ). Overrides are most often required when a plugin requires some dependencies, or extra steps are required during the build process. For example `deoplete-fish` requires both `deoplete-nvim` and `vim-fish` , and so the following override was added:
2019-07-15 01:13:34 +01:00
```
2019-07-15 00:38:00 +01:00
deoplete-fish = super.deoplete-fish.overrideAttrs(old: {
dependencies = with super; [ deoplete-nvim vim-fish ];
});
```
Sometimes plugins require an override that must be changed when the plugin is updated. This can cause issues when Vim plugins are auto-updated but the associated override isn't updated. For these plugins, the override should be written so that it specifies all information required to install the plugin, and running `./update.py` doesn't change the derivation for the plugin. Manually updating the override is required to update these types of plugins. An example of such a plugin is `LanguageClient-neovim` .
2020-03-31 02:01:33 +01:00
To add a new plugin, run `./update.py --add "[owner]/[name]"` . **NOTE** : This script automatically commits to your git repository. Be sure to check out a fresh branch before running.
2018-09-09 09:46:35 +01:00
2020-09-03 18:29:29 +01:00
Finally, there are some plugins that are also packaged in nodePackages because they have Javascript-related build steps, such as running webpack. Those plugins are not listed in `vim-plugin-names` or managed by `update.py` at all, and are included separately in `overrides.nix` . Currently, all these plugins are related to the `coc.nvim` ecosystem of Language Server Protocol integration with vim/neovim.
2016-06-25 20:36:44 +01:00
## Important repositories
- [vim-pi ](https://bitbucket.org/vimcommunity/vim-pi ) is a plugin repository
from VAM plugin manager meant to be used by others as well used by
2019-04-22 09:14:28 +01:00
- [vim2nix ](https://github.com/MarcWeber/vim-addon-vim2nix ) which generates the
2016-06-25 20:36:44 +01:00
.nix code