Vim: Difference between revisions

imported>Presto8
programs.neovim.configure is deprecated; configure.customRC -> programs.neovim.extraConfig
Replace dead link.
 
(23 intermediate revisions by 10 users not shown)
Line 1: Line 1:
== Installation ==
<languages/>
<translate>
<!--T:1-->
[https://www.vim.org/ Vim] (vi improved) is a highly configurable modal text editor program for the terminal. 


=== Basic Install ===
== Installation == <!--T:2-->


=== Basic Install === <!--T:3-->
</translate>
<syntaxhighlight lang="nix>
<syntaxhighlight lang="nix>
   environment.systemPackages = with pkgs; [ vim ];
   programs.vim.enable = true;
</syntaxhighlight>
</syntaxhighlight>
<translate>


<!--T:74-->
or
or


<syntaxhighlight lang="nix>
</translate>
   environment.systemPackages = with pkgs; [ vim_configurable ];
<syntaxhighlight lang="nix">
  programs.vim = {
    enable = true;
    package = pkgs.vim-full;
  };
</syntaxhighlight>
<translate>
 
<!--T:5-->
or
 
</translate>
<syntaxhighlight lang="nix">
   environment.systemPackages = with pkgs; [ vim-full ];
</syntaxhighlight>
</syntaxhighlight>
<translate>


=== Using Home Manager ===
=== Using Home Manager === <!--T:7-->


Vim can easily be set up using [[Home Manager]]. Here's a minimal example:
<!--T:8-->
Vim can easily be set up using [[Special:MyLanguage/Home Manager|Home Manager]]. Here's a minimal example:
 
</translate>
<syntaxhighlight lang="nix>
<syntaxhighlight lang="nix>
   programs.vim = {
   programs.vim = {
Line 26: Line 50:
   };
   };
</syntaxhighlight>
</syntaxhighlight>
<translate>


<!--T:9-->
See [https://github.com/rycee/home-manager/blob/master/modules/programs/vim.nix] for the full set of options.
See [https://github.com/rycee/home-manager/blob/master/modules/programs/vim.nix] for the full set of options.


=== Vim Spell Files ===
=== Vim Spell Files === <!--T:10-->
You can configure home-manager to install spelling files into your user directory by packaging individual spell files.  Here' an example for neovim and French:


<!--T:11-->
You can configure home-manager to install spelling files into your user directory by packaging individual spell files.  Here' an example for [[neovim]] and French:
</translate>
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
let
let
Line 55: Line 84:
in
in
{
{
   home.file."${config.xdg.configHome}/nvim/spell/fr.utf-8.spl".source = nvim-spell-fr-utf8-dictionary;
   xdg.configFile."nvim/spell/fr.utf-8.spl".source = nvim-spell-fr-utf8-dictionary;
   home.file."${config.xdg.configHome}/nvim/spell/fr.utf-8.sug".source = nvim-spell-fr-utf8-suggestions;
   xdg.configFile."nvim/spell/fr.utf-8.sug".source = nvim-spell-fr-utf8-suggestions;
   home.file."${config.xdg.configHome}/nvim/spell/fr.latin1.spl".source = nvim-spell-fr-latin1-dictionary;
   xdg.configFile."nvim/spell/fr.latin1.spl".source = nvim-spell-fr-latin1-dictionary;
   home.file."${config.xdg.configHome}/nvim/spell/fr.latin1.sug".source = nvim-spell-fr-latin1-suggestions;
   xdg.configFile."nvim/spell/fr.latin1.sug".source = nvim-spell-fr-latin1-suggestions;
}
}
</syntaxHighlight>
</syntaxHighlight>
<translate>


==== NeoVim with Coc for Python ====
==== NeoVim with Coc for Python ==== <!--T:16-->


<!--T:17-->
For NeoVim use this home manager config:
For NeoVim use this home manager config:
https://github.com/NixOS/nixpkgs/issues/98166#issuecomment-725319238
https://github.com/NixOS/nixpkgs/issues/98166#issuecomment-725319238


== System wide vim/nvim configuration ==
== System wide vim/nvim configuration == <!--T:18-->


<!--T:19-->
If you want a system wide "baseline" configuration for vim/nvim here are two examples:.
If you want a system wide "baseline" configuration for vim/nvim here are two examples:.
 
<syntaxHighlight lang="nix">
<!--T:76-->
On unstable:
 
</translate>
<syntaxhighlight lang="nix">
{ pkgs, ... }:
{ pkgs, ... }:
{
{
   environment.variables = { EDITOR = "vim"; };
   programs.vim = {
 
    enable = true;
  environment.systemPackages = with pkgs; [
    defaultEditor = true;
     ((vim_configurable.override {  }).customize{
     package = (pkgs.vim-full.override {  }).customize{
       name = "vim";
       name = "vim";
       # Install plugins for example for syntax highlighting of nix files
<translate>
       <!--T:77-->
# Install plugins for example for syntax highlighting of nix files
</translate>
       vimrcConfig.packages.myplugins = with pkgs.vimPlugins; {
       vimrcConfig.packages.myplugins = with pkgs.vimPlugins; {
         start = [ vim-nix vim-lastplace ];
         start = [ vim-nix vim-lastplace ];
Line 85: Line 124:
       };
       };
       vimrcConfig.customRC = ''
       vimrcConfig.customRC = ''
         " your custom vimrc
<translate>
         <!--T:78-->
" your custom vimrc
</translate>
         set nocompatible
         set nocompatible
         set backspace=indent,eol,start
         set backspace=indent,eol,start
         " Turn on syntax highlighting by default
<translate>
         <!--T:79-->
" Turn on syntax highlighting by default
</translate>
         syntax on
         syntax on
         " ...
         " ...
       '';
       '';
     }
     };
   )];
   };
}
}
</syntaxHighlight>
</syntaxhighlight>
 
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
{ pkgs, ... }:
{ pkgs, ... }:
{
{
   environment.variables = { EDITOR = "vim"; };
   programs.neovim = {
 
    enable = true;
  environment.systemPackages = with pkgs; [
    defaultEditor = true;
     (neovim.override {
     vimAlias = true;
      vimAlias = true;
    configure = {
       extraConfig = ''
       customRC = ''
         " your custom vimrc
<translate>
         <!--T:84-->
" your custom vimrc
</translate>
         set nocompatible
         set nocompatible
         set backspace=indent,eol,start
         set backspace=indent,eol,start
         " ...
         " ...
       '';
       '';
       configure = {
       packages.myPlugins = with pkgs.vimPlugins; {
        packages.myPlugins = with pkgs.vimPlugins; {
        start = [ vim-lastplace vim-nix ];  
          start = [ vim-lastplace vim-nix ];  
        opt = [];
          opt = [];
        };
       };
       };
     }
     };
   )];
   };
}
}
</syntaxHighlight>
</syntaxHighlight>
<translate>


<!--T:23-->
import these in your <code>configuration.nix</code> and
import these in your <code>configuration.nix</code> and
</translate>
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
{     
{     
Line 132: Line 180:
}
}
</syntaxHighlight>
</syntaxHighlight>
<translate>


== Custom setup without using Home Manager ==
== Custom setup without using Home Manager == <!--T:24-->
 
<!--T:25-->
{{note|To get a general overview about how to set up your vim in nix, refer to [https://www.mpscholten.de/nixos/2016/04/11/setting-up-vim-on-nixos.html mpscholten's blog] }}
{{note|To get a general overview about how to set up your vim in nix, refer to [https://www.mpscholten.de/nixos/2016/04/11/setting-up-vim-on-nixos.html mpscholten's blog] }}
<!--T:26-->
Vim plugins can be installed with the help of nix. You can omit using vim plugin managers and do everything in your <code>.nixpkgs/config</code>.
Vim plugins can be installed with the help of nix. You can omit using vim plugin managers and do everything in your <code>.nixpkgs/config</code>.


A lot of documentation about package management and configuration of vim in nix is stored at [https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/editors/vim/plugins/vim-utils.nix] in nixpkgs.
<!--T:27-->
A lot of documentation about package management and configuration of vim in nix is stored at [https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/vim.section.md] in nixpkgs.


=== Customizations ===
=== Customizations === <!--T:28-->


<!--T:29-->
Both vim and neovim can be further configured to include your favorite plugins and additional libraries. To list all available vim plugins, run <code>nix search nixpkgs#vimPlugins</code>.
Both vim and neovim can be further configured to include your favorite plugins and additional libraries. To list all available vim plugins, run <code>nix search nixpkgs#vimPlugins</code>.


<!--T:30-->
Add the following code to your <code>~/.nixpkgs/config.nix</code>:
Add the following code to your <code>~/.nixpkgs/config.nix</code>:


<syntaxHighlight lang="nix">
</translate>
<syntaxhighlight lang="nix">
{
{
   packageOverrides = pkgs: with pkgs; {
   packageOverrides = pkgs: with pkgs; {
     myVim = vim_configurable.customize {
     myVim = vim-full.customize {
       name = "vim-with-plugins";
       name = "vim-with-plugins";
       # add here code from the example section
<translate>
       <!--T:85-->
# add here code from the example section
</translate>
     };
     };
     myNeovim = neovim.override {
     myNeovim = neovim.override {
       configure = {
       configure = {
         customRC = ''
         customRC = ''
           # here your custom configuration goes!
<translate>
           <!--T:86-->
# here your custom configuration goes!
</translate>
         '';
         '';
         packages.myVimPackage = with pkgs.vimPlugins; {
         packages.myVimPackage = with pkgs.vimPlugins; {
           # see examples below how to use custom packages
<translate>
           <!--T:87-->
# see examples below how to use custom packages
</translate>
           start = [ ];
           start = [ ];
           opt = [ ];
           opt = [ ];
Line 166: Line 232:
   };
   };
}
}
</syntaxHighlight>
</syntaxhighlight>
<translate>


<!--T:32-->
After that you can install your special grafted `myVim` or `myNeovim` packages.
After that you can install your special grafted `myVim` or `myNeovim` packages.


=== Examples ===
=== Examples === <!--T:33-->


==== Apply custom vimrc configuration ====
==== Apply custom vimrc configuration ==== <!--T:34-->


<!--T:35-->
NB: you ''must'' use <code>vimrcConfig.customRC</code> rather than installing a <code>~/.vimrc</code> by hand, since the customized Vim will silently ignore any vimrc in your home directory.
NB: you ''must'' use <code>vimrcConfig.customRC</code> rather than installing a <code>~/.vimrc</code> by hand, since the customized Vim will silently ignore any vimrc in your home directory.


<syntaxHighlight lang="nix">
</translate>
vim_configurable.customize {
<syntaxhighlight lang="nix">
vim-full.customize {
   name = "vim-with-plugins";
   name = "vim-with-plugins";
   # add custom .vimrc lines like this:
<translate>
   <!--T:88-->
# add custom .vimrc lines like this:
</translate>
   vimrcConfig.customRC = ''
   vimrcConfig.customRC = ''
     set hidden
     set hidden
Line 185: Line 258:
   '';
   '';
}
}
</syntaxHighlight>
</syntaxhighlight>
<translate>


<!--T:37-->
If you need to run code before plugins are added, you can use <code>vimrcConfig.beforePlugins</code> (be sure to include <code>set nocompatible</code> if you override [https://github.com/NixOS/nixpkgs/blob/c3df8057dad986bf7f3928de1b5233fadb52bb15/pkgs/misc/vim-plugins/vim-utils.nix#L264-L267 the default value]).
If you need to run code before plugins are added, you can use <code>vimrcConfig.beforePlugins</code> (be sure to include <code>set nocompatible</code> if you override [https://github.com/NixOS/nixpkgs/blob/c3df8057dad986bf7f3928de1b5233fadb52bb15/pkgs/misc/vim-plugins/vim-utils.nix#L264-L267 the default value]).


=== Using vim's builtin packaging capability ===
=== Using vim's builtin packaging capability === <!--T:38-->


<syntaxHighlight lang="nix">
</translate>
vim_configurable.customize {
<syntaxhighlight lang="nix">
vim-full.customize {
   vimrcConfig.packages.myVimPackage = with pkgs.vimPlugins; {
   vimrcConfig.packages.myVimPackage = with pkgs.vimPlugins; {
     # loaded on launch
<translate>
     <!--T:89-->
# loaded on launch
</translate>
     start = [ YouCompleteMe fugitive ];
     start = [ YouCompleteMe fugitive ];
     # manually loadable by calling `:packadd $plugin-name`
<translate>
     <!--T:90-->
# manually loadable by calling `:packadd $plugin-name`
</translate>
     opt = [ phpCompletion elm-vim ];
     opt = [ phpCompletion elm-vim ];
     # To automatically load a plugin when opening a filetype, add vimrc lines like:
<translate>
     <!--T:91-->
# To automatically load a plugin when opening a filetype, add vimrc lines like:
</translate>
     # autocmd FileType php :packadd phpCompletion
     # autocmd FileType php :packadd phpCompletion
   }
   }
};
};
</syntaxHighlight>
</syntaxhighlight>
<translate>


<!--T:40-->
Note that dynamically loading with opt may be buggy and the workaround is to use [https://vi.stackexchange.com/a/20818/30821 start instead].
Note that dynamically loading with opt may be buggy and the workaround is to use [https://vi.stackexchange.com/a/20818/30821 start instead].


=== Using Pathogen as manager ===
=== Using Pathogen as manager === <!--T:41-->
 
<!--T:42-->
There is a pathogen implementation as well, but its startup is slower and [VAM] has more features.  
There is a pathogen implementation as well, but its startup is slower and [VAM] has more features.  
</translate>
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
vimrcConfig.pathogen.knownPlugins = vimPlugins; # optional
vimrcConfig.pathogen.knownPlugins = vimPlugins; # optional
vimrcConfig.pathogen.pluginNames = [ "vim-addon-nix" "youcompleteme" ];
vimrcConfig.pathogen.pluginNames = [ "vim-addon-nix" "youcompleteme" ];
</syntaxhighlight>
</syntaxhighlight>
<translate>


=== Using Vim-Plug as manager ===
=== Using Vim-Plug as manager === <!--T:43-->


</translate>
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
vimrcConfig.plug.plugins = with pkgs.vimPlugins; [vim-addon-nix youcompleteme];
vimrcConfig.plug.plugins = with pkgs.vimPlugins; [vim-addon-nix youcompleteme];
</syntaxhighlight>
</syntaxhighlight>
<translate>


=== Adding new plugins ===
=== Adding new plugins === <!--T:45-->


As per the instructions found in https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/vim.section.md
<!--T:46-->
* First run <code>./update.py</code>.
Please see https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/vim.section.md.
* Commit the changes with the commit message "vimPlugins: Update".
* Add your plugin to ./vim-plugin-names (please try to maintain the list alphabetically sorted). You can customize the branch by appending for example <code>@main</code> to an entry (search the file for examples)
* Run <code>./update.py</code> once again to generate the plugin's nix expression.
* Commit your changes one more time, this time with the message formated as such: "vimPlugins.[plugin-name]: init at [version]".
* If you need to add additional code/patches to the generated code, add those lines to <code>pkgs/misc/vim-plugins/vim2nix/additional-nix-code</code> and rerun <code>./update.py</code>. They will be included in the generated code.


==== Notes Regarding Plugins ====
==== Notes Regarding Plugins ==== <!--T:47-->


<!--T:48-->
For additional info, you may wish to look at [https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/vim.section.md documentation on the nixpkgs repository].
For additional info, you may wish to look at [https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/vim.section.md documentation on the nixpkgs repository].


=== Add a new custom plugin to the users packages  ===
=== Add a new custom plugin to the users packages  === <!--T:49-->
 
<!--T:50-->
Sometimes you do not want to change upstream plugins, for this you can use  <code>vimUtils.buildVimPlugin</code> to create your own:
Sometimes you do not want to change upstream plugins, for this you can use  <code>vimUtils.buildVimPlugin</code> to create your own:


<syntaxHighlight lang="nix">
</translate>
<syntaxhighlight lang="nix">
let
let
   vim-better-whitespace = pkgs.vimUtils.buildVimPlugin {
   vim-better-whitespace = pkgs.vimUtils.buildVimPlugin {
Line 247: Line 340:
     };
     };
   };
   };
in {
in {
   users.users.<yourNickname>.packages = [
   users.users.<yourNickname>.packages = [
     (pkgs.vim_configurable.customize {
     (pkgs.vim-full.customize {
       vimrcConfig.packages.myVimPackage = with pkgs.vimPlugins; {
       vimrcConfig.packages.myVimPackage = with pkgs.vimPlugins; {
         start = [ vim-better-whitespace ];
         start = [ vim-better-whitespace ];
Line 257: Line 349:
   ];
   ];
};
};
</syntaxHighlight>
</syntaxhighlight>
<translate>


==== Using flake ====
==== Using flake ==== <!--T:53-->


</translate>
<code>configuration.nix</code>:
<code>configuration.nix</code>:
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
Line 309: Line 403:
}
}
</syntaxHighlight>
</syntaxHighlight>
<translate>


<!--T:57-->
Then we can update the package with <code>nix flake lock --update-input winresizer-vim</code>, or update all inputs in flake.nix with <code>nix flake update</code>.
Then we can update the package with <code>nix flake lock --update-input winresizer-vim</code>, or update all inputs in flake.nix with <code>nix flake update</code>.


=== Vim as a Python IDE ===
=== Vim as a Python IDE === <!--T:58-->


The following snippet will make a full featured python IDE.
<!--T:59-->
The following snippet will make a full featured [[python]] IDE.


==== Using language client ====
==== Using language client ==== <!--T:60-->


<syntaxHighlight lang="nix">
</translate>
vim_configurable.customize {
<syntaxhighlight lang="nix">
vim-full.customize {
   vimrcConfig = {
   vimrcConfig = {
     customRC = ''
     customRC = ''
Line 337: Line 435:
     }
     }
};
};
</syntaxHighlight>
</syntaxhighlight>
<translate>


<!--T:62-->
Then put the following expression in <code>environment.systemPackages</code> or in the home-manager package list,
Then put the following expression in <code>environment.systemPackages</code> or in the home-manager package list,
to install python-language-server:
to install python-language-server:
 
</translate>
<syntaxHighlight  lang="nix">
<syntaxHighlight  lang="nix">
(python3.withPackages(ps: [
(python3.withPackages(ps: [
   ps.python-language-server
   ps.python-language-server
   # the following plugins are optional, they provide type checking, import sorting and code formatting
<translate>
   <!--T:92-->
# the following plugins are optional, they provide type checking, import sorting and code formatting
</translate>
   ps.pyls-mypy ps.pyls-isort ps.pyls-black
   ps.pyls-mypy ps.pyls-isort ps.pyls-black
]))
]))
</syntaxHighlight>
</syntaxHighlight>
<translate>


=== Real life examples ===
=== Real life examples === <!--T:63-->


<!--T:64-->
* [https://github.com/jagajaga/my_configs/blob/master/.nixpkgs/common.nix Jagajaga’s config]
* [https://github.com/jagajaga/my_configs/blob/master/.nixpkgs/common.nix Jagajaga’s config]
<!--T:99-->
* [https://github.com/andrewrk/dotfiles/blob/master/.nixpkgs/config.nix andrewrk's config]
* [https://github.com/andrewrk/dotfiles/blob/master/.nixpkgs/config.nix andrewrk's config]
<!--T:100-->
* [https://github.com/wagnerf42/nixos-config/blob/master/config/my_vim.nix wagnerf42's config (good for rust language)]
* [https://github.com/wagnerf42/nixos-config/blob/master/config/my_vim.nix wagnerf42's config (good for rust language)]


=== YouCompleteMe ===
=== YouCompleteMe === <!--T:65-->
 
<!--T:66-->
Currently the youcompleteme plugin uses [https://github.com/NixOS/nixpkgs/blob/8e7b1f2ac2e261d5a644fef860a0d050ea227c06/pkgs/misc/vim-plugins/default.nix#L695 unwrapped clang on linux]. This causes it to not find <code>stdlib.h</code>. There is a [https://github.com/andrewrk/genesis/blob/5f49cd9a8c2b61b9859a22102bc3f732add9461a/.ycm_extra_conf.py workaround] you can put in your <code>.ycm_extra_conf.py</code> file, which works by executing the C/C++ compiler and getting it to output the list of search paths - which includes the search path to find <code>stdlib.h</code>.
Currently the youcompleteme plugin uses [https://github.com/NixOS/nixpkgs/blob/8e7b1f2ac2e261d5a644fef860a0d050ea227c06/pkgs/misc/vim-plugins/default.nix#L695 unwrapped clang on linux]. This causes it to not find <code>stdlib.h</code>. There is a [https://github.com/andrewrk/genesis/blob/5f49cd9a8c2b61b9859a22102bc3f732add9461a/.ycm_extra_conf.py workaround] you can put in your <code>.ycm_extra_conf.py</code> file, which works by executing the C/C++ compiler and getting it to output the list of search paths - which includes the search path to find <code>stdlib.h</code>.


<!--T:67-->
A better alternative to youcompleteme for C/C++ is to use [https://github.com/cquery-project/cquery/ cquery] in combination with the [https://github.com/autozimu/LanguageClient-neovim LanguageClient-neovim]. It will also find in c header files when used in a nix-shell if you install cquery from nixpkgs as it uses a custom [https://github.com/NixOS/nixpkgs/commit/04f3b76dcec21f2fcba6b1b0afbb3ed224165050#diff-11cdfc0385b9e017089c1ac09c5b838e shell wrapper]
A better alternative to youcompleteme for C/C++ is to use [https://github.com/cquery-project/cquery/ cquery] in combination with the [https://github.com/autozimu/LanguageClient-neovim LanguageClient-neovim]. It will also find in c header files when used in a nix-shell if you install cquery from nixpkgs as it uses a custom [https://github.com/NixOS/nixpkgs/commit/04f3b76dcec21f2fcba6b1b0afbb3ed224165050#diff-11cdfc0385b9e017089c1ac09c5b838e shell wrapper]


== Tree-sitter grammars in Neovim ==
== gvim and gview == <!--T:70-->


See this [[Tree sitters]] article.
<!--T:71-->
You can enable <code>guiSupport</code> to make <code>gvim</code> available, though this won't give you <code>gview</code>:


== Python 3 support for vim ==
</translate>
 
<syntaxhighlight lang="nix">
If you have defined your vim configuration in a `./my_vim.nix` file you can install vim with the python 3 support instead of python2 by overriding the python version like the following:
(pkgs.vim-full.customize {
  guiSupport = true;
<syntaxHighlight  lang="nix">
(pkgs.callPackage ./my_vim.nix {                                                                                                                                                        
      vim_configurable = vim_configurable.override { python = python3; };                                                                                                                    
})
})
</syntaxHighlight>
== gvim and gview ==
<code>gvim</code> and <code>gview</code> may be installed using the <code>[https://search.nixos.org/packages/?query=vimHugeX vimHugeX]</code> attribute name (package name <code>[https://search.nixos.org/packages/?query=vim_configurable vim_configurable]</code>).
<syntaxhighlight lang=bash>
$ nix-env -iA nixos.vimHugeX
</syntaxhighlight>
If you are using <code>vim_configurable.customize</code>, you can enable <code>wrapGui</code> to make <code>gvim</code> available, though this won't give you <code>gview</code>:
<syntaxhighlight lang=nix>
vim_configured = pkgs.vim_configurable.customize {
  name = "vim";
  wrapGui = true;
};
</syntaxhighlight>
</syntaxhighlight>
<translate>


</translate>
[[Category:Applications]]
[[Category:Applications]]
[[Category:CLI Applications]]
[[Category:Text Editor{{#translation:}}]]