Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
at release-19.03 255 lines 7.2 kB view raw view rendered
1--- 2title: User's Guide for Vim in Nixpkgs 3author: Marc Weber 4date: 2016-06-25 5--- 6# User's Guide to Vim Plugins/Addons/Bundles/Scripts in Nixpkgs 7 8Both Neovim and Vim can be configured to include your favorite plugins 9and additional libraries. 10 11Loading can be deferred; see examples. 12 13At the moment we support three different methods for managing plugins: 14 15- Vim packages (*recommend*) 16- VAM (=vim-addon-manager) 17- Pathogen 18- vim-plug 19 20## Custom configuration 21 22Adding custom .vimrc lines can be done using the following code: 23 24``` 25vim_configurable.customize { 26 # `name` specifies the name of the executable and package 27 name = "vim-with-plugins"; 28 29 vimrcConfig.customRC = '' 30 set hidden 31 ''; 32} 33``` 34 35This configuration is used when vim is invoked with the command specified as name, in this case `vim-with-plugins`. 36 37For Neovim the `configure` argument can be overridden to achieve the same: 38 39``` 40neovim.override { 41 configure = { 42 customRC = '' 43 # here your custom configuration goes! 44 ''; 45 }; 46} 47``` 48 49If you want to use `neovim-qt` as a graphical editor, you can configure it by overriding neovim in an overlay 50or passing it an overridden neovimn: 51 52``` 53neovim-qt.override { 54 neovim = neovim.override { 55 configure = { 56 customRC = '' 57 # your custom configuration 58 ''; 59 }; 60 }; 61} 62``` 63 64## Managing plugins with Vim packages 65 66To store you plugins in Vim packages (the native vim plugin manager, see `:help packages`) the following example can be used: 67 68``` 69vim_configurable.customize { 70 vimrcConfig.packages.myVimPackage = with pkgs.vimPlugins; { 71 # loaded on launch 72 start = [ youcompleteme fugitive ]; 73 # manually loadable by calling `:packadd $plugin-name` 74 # however, if a vim plugin has a dependency that is not explicitly listed in 75 # opt that dependency will always be added to start to avoid confusion. 76 opt = [ phpCompletion elm-vim ]; 77 # To automatically load a plugin when opening a filetype, add vimrc lines like: 78 # autocmd FileType php :packadd phpCompletion 79 }; 80} 81``` 82 83`myVimPackage` is an arbitrary name for the generated package. You can choose any name you like. 84For Neovim the syntax is: 85 86``` 87neovim.override { 88 configure = { 89 customRC = '' 90 # here your custom configuration goes! 91 ''; 92 packages.myVimPackage = with pkgs.vimPlugins; { 93 # see examples below how to use custom packages 94 start = [ ]; 95 # If a vim plugin has a dependency that is not explicitly listed in 96 # opt that dependency will always be added to start to avoid confusion. 97 opt = [ ]; 98 }; 99 }; 100} 101``` 102 103The resulting package can be added to `packageOverrides` in `~/.nixpkgs/config.nix` to make it installable: 104 105``` 106{ 107 packageOverrides = pkgs: with pkgs; { 108 myVim = vim_configurable.customize { 109 # `name` specifies the name of the executable and package 110 name = "vim-with-plugins"; 111 # add here code from the example section 112 }; 113 myNeovim = neovim.override { 114 configure = { 115 # add here code from the example section 116 }; 117 }; 118 }; 119} 120``` 121 122After that you can install your special grafted `myVim` or `myNeovim` packages. 123 124## Managing plugins with vim-plug 125 126To use [vim-plug](https://github.com/junegunn/vim-plug) to manage your Vim 127plugins the following example can be used: 128 129``` 130vim_configurable.customize { 131 vimrcConfig.packages.myVimPackage = with pkgs.vimPlugins; { 132 # loaded on launch 133 plug.plugins = [ youcompleteme fugitive phpCompletion elm-vim ]; 134 }; 135} 136``` 137 138For Neovim the syntax is: 139 140``` 141neovim.override { 142 configure = { 143 customRC = '' 144 # here your custom configuration goes! 145 ''; 146 plug.plugins = with pkgs.vimPlugins; [ 147 vim-go 148 ]; 149 }; 150} 151``` 152 153## Managing plugins with VAM 154 155### Handling dependencies of Vim plugins 156 157VAM introduced .json files supporting dependencies without versioning 158assuming that "using latest version" is ok most of the time. 159 160### Example 161 162First create a vim-scripts file having one plugin name per line. Example: 163 164 "tlib" 165 {'name': 'vim-addon-sql'} 166 {'filetype_regex': '\%(vim)$', 'names': ['reload', 'vim-dev-plugin']} 167 168Such vim-scripts file can be read by VAM as well like this: 169 170 call vam#Scripts(expand('~/.vim-scripts'), {}) 171 172Create a default.nix file: 173 174 { nixpkgs ? import <nixpkgs> {}, compiler ? "ghc7102" }: 175 nixpkgs.vim_configurable.customize { name = "vim"; vimrcConfig.vam.pluginDictionaries = [ "vim-addon-vim2nix" ]; } 176 177Create a generate.vim file: 178 179 ActivateAddons vim-addon-vim2nix 180 let vim_scripts = "vim-scripts" 181 call nix#ExportPluginsForNix({ 182 \ 'path_to_nixpkgs': eval('{"'.substitute(substitute(substitute($NIX_PATH, ':', ',', 'g'), '=',':', 'g'), '\([:,]\)', '"\1"',"g").'"}')["nixpkgs"], 183 \ 'cache_file': '/tmp/vim2nix-cache', 184 \ 'try_catch': 0, 185 \ 'plugin_dictionaries': ["vim-addon-manager"]+map(readfile(vim_scripts), 'eval(v:val)') 186 \ }) 187 188Then run 189 190 nix-shell -p vimUtils.vim_with_vim2nix --command "vim -c 'source generate.vim'" 191 192You should get a Vim buffer with the nix derivations (output1) and vam.pluginDictionaries (output2). 193You can add your vim to your system's configuration file like this and start it by "vim-my": 194 195 my-vim = 196 let plugins = let inherit (vimUtils) buildVimPluginFrom2Nix; in { 197 copy paste output1 here 198 }; in vim_configurable.customize { 199 name = "vim-my"; 200 201 vimrcConfig.vam.knownPlugins = plugins; # optional 202 vimrcConfig.vam.pluginDictionaries = [ 203 copy paste output2 here 204 ]; 205 206 # Pathogen would be 207 # vimrcConfig.pathogen.knownPlugins = plugins; # plugins 208 # vimrcConfig.pathogen.pluginNames = ["tlib"]; 209 }; 210 211 212Sample output1: 213 214 "reload" = buildVimPluginFrom2Nix { # created by nix#NixDerivation 215 name = "reload"; 216 src = fetchgit { 217 url = "git://github.com/xolox/vim-reload"; 218 rev = "0a601a668727f5b675cb1ddc19f6861f3f7ab9e1"; 219 sha256 = "0vb832l9yxj919f5hfg6qj6bn9ni57gnjd3bj7zpq7d4iv2s4wdh"; 220 }; 221 dependencies = ["nim-misc"]; 222 223 }; 224 [...] 225 226Sample output2: 227 228 [ 229 ''vim-addon-manager'' 230 ''tlib'' 231 { "name" = ''vim-addon-sql''; } 232 { "filetype_regex" = ''\%(vim)$$''; "names" = [ ''reload'' ''vim-dev-plugin'' ]; } 233 ] 234 235 236## Adding new plugins to nixpkgs 237 238In `pkgs/misc/vim-plugins/vim-plugin-names` we store the plugin names 239for all vim plugins we automatically generate plugins for. 240The format of this file `github username/github repository`: 241For example https://github.com/scrooloose/nerdtree becomes `scrooloose/nerdtree`. 242After adding your plugin to this file run the `./update.py` in the same folder. 243This will updated a file called `generated.nix` and make your plugin accessible in the 244`vimPlugins` attribute set (`vimPlugins.nerdtree` in our example). 245If additional steps to the build process of the plugin are required, add an 246override to the `pkgs/misc/vim-plugins/default.nix` in the same directory. 247 248## Important repositories 249 250- [vim-pi](https://bitbucket.org/vimcommunity/vim-pi) is a plugin repository 251 from VAM plugin manager meant to be used by others as well used by 252 253- [vim2nix](http://github.com/MarcWeber/vim-addon-vim2nix) which generates the 254 .nix code 255