Vim: Difference between revisions

Tea (talk | contribs)
Use shorter syntax for setting xdg config files
Replace dead link.
 
(15 intermediate revisions by 6 users not shown)
Line 7: Line 7:


=== Basic Install === <!--T:3-->
=== Basic Install === <!--T:3-->
</translate>
<syntaxhighlight lang="nix>
  programs.vim.enable = true;
</syntaxhighlight>
<translate>
<!--T:74-->
or


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


<!--T:5-->
<!--T:5-->
or
or


<!--T:6-->
</translate>
<syntaxhighlight lang="nix>
<syntaxhighlight lang="nix">
   environment.systemPackages = with pkgs; [ vim_configurable ];
   environment.systemPackages = with pkgs; [ vim-full ];
</syntaxhighlight>
</syntaxhighlight>
<translate>


=== Using Home Manager === <!--T:7-->
=== Using Home Manager === <!--T:7-->
Line 25: Line 38:
<!--T:8-->
<!--T:8-->
Vim can easily be set up using [[Special:MyLanguage/Home Manager|Home Manager]]. Here's a minimal example:
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 35: Line 50:
   };
   };
</syntaxhighlight>
</syntaxhighlight>
<translate>


<!--T:9-->
<!--T:9-->
Line 42: Line 58:


<!--T:11-->
<!--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:
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:12-->
</translate>
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
let
let
Line 52: Line 68:
};
};


<!--T:13-->
nvim-spell-fr-utf8-suggestions = builtins.fetchurl {
nvim-spell-fr-utf8-suggestions = builtins.fetchurl {
   url = "http://ftp.vim.org/vim/runtime/spell/fr.utf-8.sug";
   url = "http://ftp.vim.org/vim/runtime/spell/fr.utf-8.sug";
Line 58: Line 73:
};
};


<!--T:14-->
nvim-spell-fr-latin1-dictionary = builtins.fetchurl {
nvim-spell-fr-latin1-dictionary = builtins.fetchurl {
   url = "http://ftp.vim.org/vim/runtime/spell/fr.latin1.spl";
   url = "http://ftp.vim.org/vim/runtime/spell/fr.latin1.spl";
Line 64: Line 78:
};
};


<!--T:15-->
nvim-spell-fr-latin1-suggestions = builtins.fetchurl {
nvim-spell-fr-latin1-suggestions = builtins.fetchurl {
   url = "http://ftp.vim.org/vim/runtime/spell/fr.latin1.sug";
   url = "http://ftp.vim.org/vim/runtime/spell/fr.latin1.sug";
Line 77: Line 90:
}
}
</syntaxHighlight>
</syntaxHighlight>
<translate>


==== NeoVim with Coc for Python ==== <!--T:16-->
==== NeoVim with Coc for Python ==== <!--T:16-->
Line 88: Line 102:
<!--T:19-->
<!--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;
  <!--T:20-->
    defaultEditor = true;
environment.systemPackages = with pkgs; [
     package = (pkgs.vim-full.override {  }).customize{
     ((vim_configurable.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 104: 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>
 
<!--T:21-->
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
{ pkgs, ... }:
{ pkgs, ... }:
{
{
   environment.variables = { EDITOR = "vim"; };
   programs.neovim = {
 
    enable = true;
  <!--T:22-->
    defaultEditor = true;
environment.systemPackages = with pkgs; [
     vimAlias = true;
     (neovim.override {
    configure = {
      vimAlias = true;
       customRC = ''
       extraConfig = ''
<translate>
         " your custom vimrc
         <!--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-->
<!--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 154: Line 180:
}
}
</syntaxHighlight>
</syntaxHighlight>
<translate>


== Custom setup without using Home Manager == <!--T:24-->
== Custom setup without using Home Manager == <!--T:24-->
Line 164: Line 191:


<!--T:27-->
<!--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/pkgs/applications/editors/vim/plugins/vim-utils.nix] in nixpkgs.
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 === <!--T:28-->
=== Customizations === <!--T:28-->
Line 174: Line 201:
Add the following code to your <code>~/.nixpkgs/config.nix</code>:
Add the following code to your <code>~/.nixpkgs/config.nix</code>:


<!--T:31-->
</translate>
<syntaxHighlight lang="nix">
<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 196: Line 232:
   };
   };
}
}
</syntaxHighlight>
</syntaxhighlight>
<translate>


<!--T:32-->
<!--T:32-->
Line 208: Line 245:
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.


<!--T:36-->
</translate>
<syntaxHighlight lang="nix">
<syntaxhighlight lang="nix">
vim_configurable.customize {
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 218: Line 258:
   '';
   '';
}
}
</syntaxHighlight>
</syntaxhighlight>
<translate>


<!--T:37-->
<!--T:37-->
Line 225: Line 266:
=== Using vim's builtin packaging capability === <!--T:38-->
=== Using vim's builtin packaging capability === <!--T:38-->


<!--T:39-->
</translate>
<syntaxHighlight lang="nix">
<syntaxhighlight lang="nix">
vim_configurable.customize {
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-->
<!--T:40-->
Line 246: Line 297:
<!--T:42-->
<!--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 === <!--T:43-->
=== Using Vim-Plug as manager === <!--T:43-->


<!--T:44-->
</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 === <!--T:45-->
=== Adding new plugins === <!--T:45-->


<!--T:46-->
<!--T:46-->
As per the instructions found in https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/vim.section.md
Please see https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/vim.section.md.
* First run <code>./update.py</code>.
* 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 ==== <!--T:47-->
==== Notes Regarding Plugins ==== <!--T:47-->
Line 279: Line 328:
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:


<!--T:51-->
</translate>
<syntaxHighlight lang="nix">
<syntaxhighlight lang="nix">
let
let
   vim-better-whitespace = pkgs.vimUtils.buildVimPlugin {
   vim-better-whitespace = pkgs.vimUtils.buildVimPlugin {
Line 291: Line 340:
     };
     };
   };
   };
<!--T:52-->
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 302: Line 349:
   ];
   ];
};
};
</syntaxHighlight>
</syntaxhighlight>
<translate>


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


<!--T:54-->
</translate>
<code>configuration.nix</code>:
<code>configuration.nix</code>:
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
Line 331: Line 379:
</syntaxHighlight>
</syntaxHighlight>


<!--T:55-->
<code>flake.nix</code>:
<code>flake.nix</code>:
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
Line 343: Line 390:
   };
   };


   <!--T:56-->
   outputs = inputs@{ nixpkgs, ... }: {
outputs = inputs@{ nixpkgs, ... }: {
     nixosConfigurations.nixos = nixpkgs.lib.nixosSystem {
     nixosConfigurations.nixos = nixpkgs.lib.nixosSystem {
       system = "x86_64-linux";
       system = "x86_64-linux";
Line 357: Line 403:
}
}
</syntaxHighlight>
</syntaxHighlight>
<translate>


<!--T:57-->
<!--T:57-->
Line 364: Line 411:


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


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


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


<!--T:62-->
<!--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 === <!--T:63-->
=== Real life examples === <!--T:63-->
Line 406: Line 459:
<!--T:64-->
<!--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)]


Line 416: Line 473:
<!--T:67-->
<!--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]
== Python 3 support for vim == <!--T:68-->
<!--T:69-->
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:
<syntaxHighlight  lang="nix">
(pkgs.callPackage ./my_vim.nix {                                                                                                                                                         
      vim_configurable = vim_configurable.override { python = python3; };                                                                                                                   
})
</syntaxHighlight>


== gvim and gview == <!--T:70-->
== gvim and gview == <!--T:70-->


<!--T:71-->
<!--T:71-->
<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>).
You can enable <code>guiSupport</code> to make <code>gvim</code> available, though this won't give you <code>gview</code>:
<syntaxhighlight lang=bash>
$ nix-env -iA nixos.vimHugeX
</syntaxhighlight>


<!--T:72-->
</translate>
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">
<syntaxhighlight lang=nix>
(pkgs.vim-full.customize {
vim_configured = pkgs.vim_configurable.customize {
   guiSupport = true;
   name = "vim";
})
  wrapGui = true;
};
</syntaxhighlight>
</syntaxhighlight>
<translate>


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