Vim/ja: Difference between revisions

From NixOS Wiki
Created page with "<syntaxhighlight lang="nix> environment.systemPackages = with pkgs; [ vim ]; </syntaxhighlight>"
Tags: Mobile edit Mobile web edit
FuzzyBot (talk | contribs)
Updating to match new version of source page
 
(7 intermediate revisions by 2 users not shown)
Line 6: Line 6:
<span id="Installation"></span>
<span id="Installation"></span>
== インストール ==
== インストール ==
<span id="Basic_Install"></span>
=== ベーシックインストール ===


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
=== Basic Install ===
On unstable:
</div>
 
<syntaxhighlight lang="nix>
  programs.vim.enable = true;
</syntaxhighlight>
 
<div lang="en" dir="ltr" class="mw-content-ltr">
or
</div>
 
<syntaxhighlight lang="nix>
  programs.vim = {
    enable = true;
    package = pkgs.vim_configurable;
  };
</syntaxhighlight>
 
<div lang="en" dir="ltr" class="mw-content-ltr">
On 24.05 or older:
</div>
</div>


Line 17: Line 39:
もしくは
もしくは


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="nix>
<syntaxhighlight lang="nix>
   environment.systemPackages = with pkgs; [ vim_configurable ];
   environment.systemPackages = with pkgs; [ vim_configurable ];
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Using_Home_Manager"></span>
=== Using Home Manager ===
=== Home Managerを使用した例 ===
 
<div class="mw-translate-fuzzy">
Vimは[[Special:MyLanguage/Home Manager|Home Manager]]を使用して簡単にセットアップ出来ます。以下は最低限の一例です:
<syntaxhighlight lang="nix>
  programs.vim = {
    enable = true;
    plugins = with pkgs.vimPlugins; [ vim-airline ];
    settings = { ignorecase = true; };
    extraConfig = ''
      set mouse=a
    '';
  };
</syntaxhighlight>
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
Vim can easily be set up using [[Special:MyLanguage/Home Manager|Home Manager]]. Here's a minimal example:
<syntaxhighlight lang="nix>
<syntaxhighlight lang="nix>
   programs.vim = {
   programs.vim = {
Line 39: Line 70:
   };
   };
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 45: Line 75:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Vim_Spell_Files"></span>
=== Vim Spell Files ===
=== Vimスペルファイル ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
個々のスペルファイルをパッケージ化することで、ユーザーディレクトリにスペルファイルをインストールするようにhome-managerを設定することができます。以下はneovimとフランス語の例です:
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:
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
let
let
Line 60: Line 86:
   sha256 = "abfb9702b98d887c175ace58f1ab39733dc08d03b674d914f56344ef86e63b61";
   sha256 = "abfb9702b98d887c175ace58f1ab39733dc08d03b674d914f56344ef86e63b61";
};
};
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
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";
   sha256 = "0294bc32b42c90bbb286a89e23ca3773b7ef50eff1ab523b1513d6a25c6b3f58";
   sha256 = "0294bc32b42c90bbb286a89e23ca3773b7ef50eff1ab523b1513d6a25c6b3f58";
};
};
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
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";
   sha256 = "086ccda0891594c93eab143aa83ffbbd25d013c1b82866bbb48bb1cb788cc2ff";
   sha256 = "086ccda0891594c93eab143aa83ffbbd25d013c1b82866bbb48bb1cb788cc2ff";
};
};
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
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 83: Line 103:
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>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 106: Line 125:
<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
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:.
</div>
 
<div lang="en" dir="ltr" class="mw-content-ltr">
On unstable:
</div>
 
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
{ pkgs, ... }:
{ pkgs, ... }:
{
{
   environment.variables = { EDITOR = "vim"; };
   programs.vim = {
    enable = true;
    defaultEditor = true;
    package = (pkgs.vim_configurable.override { }).customize{
      name = "vim";
      <div lang="en" dir="ltr" class="mw-content-ltr">
# Install plugins for example for syntax highlighting of nix files
</div>
      vimrcConfig.packages.myplugins = with pkgs.vimPlugins; {
        start = [ vim-nix vim-lastplace ];
        opt = [];
      };
      vimrcConfig.customRC = ''
        <div lang="en" dir="ltr" class="mw-content-ltr">
" your custom vimrc
</div>
        set nocompatible
        set backspace=indent,eol,start
        <div lang="en" dir="ltr" class="mw-content-ltr">
" Turn on syntax highlighting by default
</div>
        syntax on
        " ...
      '';
    };
  };
}
</syntaxHighlight>
 
<div lang="en" dir="ltr" class="mw-content-ltr">
On 24.05 or older:
</div>
</div>


  <div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang="nix">
environment.systemPackages = with pkgs; [
{ pkgs, ... }:
{
  environment.variables = { EDITOR = "vim";
  environment.systemPackages = with pkgs; [
     ((vim_configurable.override {  }).customize{
     ((vim_configurable.override {  }).customize{
       name = "vim";
       name = "vim";
       # Install plugins for example for syntax highlighting of nix files
       <div lang="en" dir="ltr" class="mw-content-ltr">
# Install plugins for example for syntax highlighting of nix files
</div>
       vimrcConfig.packages.myplugins = with pkgs.vimPlugins; {
       vimrcConfig.packages.myplugins = with pkgs.vimPlugins; {
         start = [ vim-nix vim-lastplace ];
         start = [ vim-nix vim-lastplace ];
Line 123: Line 182:
       };
       };
       vimrcConfig.customRC = ''
       vimrcConfig.customRC = ''
         " your custom vimrc
         <div lang="en" dir="ltr" class="mw-content-ltr">
" your custom vimrc
</div>
         set nocompatible
         set nocompatible
         set backspace=indent,eol,start
         set backspace=indent,eol,start
         " Turn on syntax highlighting by default
         <div lang="en" dir="ltr" class="mw-content-ltr">
" Turn on syntax highlighting by default
</div>
         syntax on
         syntax on
         " ...
         " ...
Line 134: Line 197:
}
}
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
{ pkgs, ... }:
{ pkgs, ... }:
{
{
   environment.variables = { EDITOR = "vim"; };
   programs.neovim = {
    enable = true;
    defaultEditor = true;
    vimAlias = true;
    configure = {
      customRC = ''
        <div lang="en" dir="ltr" class="mw-content-ltr">
" your custom vimrc
</div>
</div>
  <div lang="en" dir="ltr" class="mw-content-ltr">
environment.systemPackages = with pkgs; [
    (neovim.override {
      vimAlias = true;
      extraConfig = ''
        " your custom vimrc
         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>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
import these in your <code>configuration.nix</code> and
import these in your <code>configuration.nix</code> and
</div>
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
{     
{     
Line 176: Line 235:
}
}
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 206: Line 264:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
{
{
Line 212: Line 269:
     myVim = vim_configurable.customize {
     myVim = vim_configurable.customize {
       name = "vim-with-plugins";
       name = "vim-with-plugins";
       # add here code from the example section
       <div lang="en" dir="ltr" class="mw-content-ltr">
# add here code from the example section
</div>
     };
     };
     myNeovim = neovim.override {
     myNeovim = neovim.override {
       configure = {
       configure = {
         customRC = ''
         customRC = ''
           # here your custom configuration goes!
           <div lang="en" dir="ltr" class="mw-content-ltr">
# here your custom configuration goes!
</div>
         '';
         '';
         packages.myVimPackage = with pkgs.vimPlugins; {
         packages.myVimPackage = with pkgs.vimPlugins; {
           # see examples below how to use custom packages
           <div lang="en" dir="ltr" class="mw-content-ltr">
# see examples below how to use custom packages
</div>
           start = [ ];
           start = [ ];
           opt = [ ];
           opt = [ ];
Line 229: Line 292:
}
}
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 247: Line 309:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
vim_configurable.customize {
vim_configurable.customize {
   name = "vim-with-plugins";
   name = "vim-with-plugins";
   # add custom .vimrc lines like this:
   <div lang="en" dir="ltr" class="mw-content-ltr">
# add custom .vimrc lines like this:
</div>
   vimrcConfig.customRC = ''
   vimrcConfig.customRC = ''
     set hidden
     set hidden
Line 258: Line 321:
}
}
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 268: Line 330:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
vim_configurable.customize {
vim_configurable.customize {
   vimrcConfig.packages.myVimPackage = with pkgs.vimPlugins; {
   vimrcConfig.packages.myVimPackage = with pkgs.vimPlugins; {
     # loaded on launch
     <div lang="en" dir="ltr" class="mw-content-ltr">
# loaded on launch
</div>
     start = [ YouCompleteMe fugitive ];
     start = [ YouCompleteMe fugitive ];
     # manually loadable by calling `:packadd $plugin-name`
     <div lang="en" dir="ltr" class="mw-content-ltr">
# manually loadable by calling `:packadd $plugin-name`
</div>
     opt = [ phpCompletion elm-vim ];
     opt = [ phpCompletion elm-vim ];
     # To automatically load a plugin when opening a filetype, add vimrc lines like:
     <div lang="en" dir="ltr" class="mw-content-ltr">
# To automatically load a plugin when opening a filetype, add vimrc lines like:
</div>
     # autocmd FileType php :packadd phpCompletion
     # autocmd FileType php :packadd phpCompletion
   }
   }
};
};
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 292: Line 358:


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
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.
</div>
 
<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>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 303: Line 370:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<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>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 315: Line 380:
<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
As per the instructions found in https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/vim.section.md
As per the instructions found in https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/vim.section.md
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* First run <code>./update.py</code>.
* First run <code>./update.py</code>.
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* Commit the changes with the commit message "vimPlugins: Update".
* Commit the changes with the commit message "vimPlugins: Update".
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* 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)
* 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)
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* Run <code>./update.py</code> once again to generate the plugin's nix expression.
* Run <code>./update.py</code> once again to generate the plugin's nix expression.
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* Commit your changes one more time, this time with the message formated as such: "vimPlugins.[plugin-name]: init at [version]".
* Commit your changes one more time, this time with the message formated as such: "vimPlugins.[plugin-name]: init at [version]".
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* 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.
* 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.
</div>
</div>
Line 339: Line 422:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
let
let
Line 351: Line 433:
     };
     };
   };
   };
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
in {
in {
   users.users.<yourNickname>.packages = [
   users.users.<yourNickname>.packages = [
Line 364: Line 443:
};
};
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 370: Line 448:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<code>configuration.nix</code>:
<code>configuration.nix</code>:
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
Line 394: Line 471:
   };
   };
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<code>flake.nix</code>:
<code>flake.nix</code>:
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
Line 407: Line 482:
     };
     };
   };
   };
</div>


   <div lang="en" dir="ltr" class="mw-content-ltr">
   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 423: Line 496:
}
}
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 441: Line 513:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
vim_configurable.customize {
vim_configurable.customize {
Line 462: Line 533:
};
};
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
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:
</div>
 
<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
   <div lang="en" dir="ltr" class="mw-content-ltr">
# the following plugins are optional, they provide type checking, import sorting and code formatting
</div>
   ps.pyls-mypy ps.pyls-isort ps.pyls-black
   ps.pyls-mypy ps.pyls-isort ps.pyls-black
]))
]))
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 483: Line 555:
<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
* [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]
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* [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]
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* [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)]
</div>
</div>
Line 505: Line 583:
<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
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:
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:
</div>
 
<syntaxHighlight  lang="nix">
<syntaxHighlight  lang="nix">
(pkgs.callPackage ./my_vim.nix {                                                                                                                                                           
(pkgs.callPackage ./my_vim.nix {                                                                                                                                                           
Line 511: Line 590:
})
})
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 519: Line 597:
<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
<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>).
<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>).
</div>
<syntaxhighlight lang=bash>
<syntaxhighlight lang=bash>
$ nix-env -iA nixos.vimHugeX
$ nix-env -iA nixos.vimHugeX
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
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>:
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>:
</div>
<syntaxhighlight lang=nix>
<syntaxhighlight lang=nix>
vim_configured = pkgs.vim_configurable.customize {
vim_configured = pkgs.vim_configurable.customize {
Line 532: Line 613:
};
};
</syntaxhighlight>
</syntaxhighlight>
</div>


[[Category:Applications]]
[[Category:Applications]]
[[Category:Text Editor{{#translation:}}]]
[[Category:Text Editor{{#translation:}}]]

Latest revision as of 12:03, 30 July 2024

Vim (vi improved) is a highly configurable modal text editor program for the terminal.

インストール

ベーシックインストール

On unstable:

  programs.vim.enable = true;

or

  programs.vim = {
    enable = true;
    package = pkgs.vim_configurable;
  };

On 24.05 or older:

  environment.systemPackages = with pkgs; [ vim ];

もしくは

  environment.systemPackages = with pkgs; [ vim_configurable ];

Home Managerを使用した例

VimはHome Managerを使用して簡単にセットアップ出来ます。以下は最低限の一例です:

  programs.vim = {
    enable = true;
    plugins = with pkgs.vimPlugins; [ vim-airline ];
    settings = { ignorecase = true; };
    extraConfig = ''
      set mouse=a
    '';
  };
  programs.vim = {
    enable = true;
    plugins = with pkgs.vimPlugins; [ vim-airline ];
    settings = { ignorecase = true; };
    extraConfig = ''
      set mouse=a
    '';
  };

See [1] for the full set of options.

Vimスペルファイル

個々のスペルファイルをパッケージ化することで、ユーザーディレクトリにスペルファイルをインストールするようにhome-managerを設定することができます。以下はneovimとフランス語の例です:

let
nvim-spell-fr-utf8-dictionary = builtins.fetchurl {
  url = "http://ftp.vim.org/vim/runtime/spell/fr.utf-8.spl";
  sha256 = "abfb9702b98d887c175ace58f1ab39733dc08d03b674d914f56344ef86e63b61";
};

nvim-spell-fr-utf8-suggestions = builtins.fetchurl {
  url = "http://ftp.vim.org/vim/runtime/spell/fr.utf-8.sug";
  sha256 = "0294bc32b42c90bbb286a89e23ca3773b7ef50eff1ab523b1513d6a25c6b3f58";
};

nvim-spell-fr-latin1-dictionary = builtins.fetchurl {
  url = "http://ftp.vim.org/vim/runtime/spell/fr.latin1.spl";
  sha256 = "086ccda0891594c93eab143aa83ffbbd25d013c1b82866bbb48bb1cb788cc2ff";
};

nvim-spell-fr-latin1-suggestions = builtins.fetchurl {
  url = "http://ftp.vim.org/vim/runtime/spell/fr.latin1.sug";
  sha256 = "5cb2c97901b9ca81bf765532099c0329e2223c139baa764058822debd2e0d22a";
};
in
{
  xdg.configFile."nvim/spell/fr.utf-8.spl".source = nvim-spell-fr-utf8-dictionary;
  xdg.configFile."nvim/spell/fr.utf-8.sug".source = nvim-spell-fr-utf8-suggestions;
  xdg.configFile."nvim/spell/fr.latin1.spl".source = nvim-spell-fr-latin1-dictionary;
  xdg.configFile."nvim/spell/fr.latin1.sug".source = nvim-spell-fr-latin1-suggestions;
}

NeoVim with Coc for Python

System wide vim/nvim configuration

If you want a system wide "baseline" configuration for vim/nvim here are two examples:.

On unstable:

{ pkgs, ... }:
{
  programs.vim = {
    enable = true;
    defaultEditor = true;
    package = (pkgs.vim_configurable.override {  }).customize{
      name = "vim";
      <div lang="en" dir="ltr" class="mw-content-ltr">
# Install plugins for example for syntax highlighting of nix files
</div>
      vimrcConfig.packages.myplugins = with pkgs.vimPlugins; {
        start = [ vim-nix vim-lastplace ];
        opt = [];
      };
      vimrcConfig.customRC = ''
        <div lang="en" dir="ltr" class="mw-content-ltr">
" your custom vimrc
</div>
        set nocompatible
        set backspace=indent,eol,start
        <div lang="en" dir="ltr" class="mw-content-ltr">
" Turn on syntax highlighting by default
</div>
        syntax on
        " ...
      '';
    };
  };
}

On 24.05 or older:

{ pkgs, ... }:
{
  environment.variables = { EDITOR = "vim";
  environment.systemPackages = with pkgs; [
    ((vim_configurable.override {  }).customize{
      name = "vim";
      <div lang="en" dir="ltr" class="mw-content-ltr">
# Install plugins for example for syntax highlighting of nix files
</div>
      vimrcConfig.packages.myplugins = with pkgs.vimPlugins; {
        start = [ vim-nix vim-lastplace ];
        opt = [];
      };
      vimrcConfig.customRC = ''
        <div lang="en" dir="ltr" class="mw-content-ltr">
" your custom vimrc
</div>
        set nocompatible
        set backspace=indent,eol,start
        <div lang="en" dir="ltr" class="mw-content-ltr">
" Turn on syntax highlighting by default
</div>
        syntax on
        " ...
      '';
    }
  )];
}
{ pkgs, ... }:
{
  programs.neovim = {
    enable = true;
    defaultEditor = true;
    vimAlias = true;
    configure = {
      customRC = ''
        <div lang="en" dir="ltr" class="mw-content-ltr">
" your custom vimrc
</div>
        set nocompatible
        set backspace=indent,eol,start
        " ...
      '';
      packages.myPlugins = with pkgs.vimPlugins; {
        start = [ vim-lastplace vim-nix ]; 
        opt = [];
      };
    };
  };
}

import these in your configuration.nix and

{    
  imports =    
    [
      ./vim.nix
    ];
  # ...
}

Custom setup without using Home Manager

Note: To get a general overview about how to set up your vim in nix, refer to mpscholten's blog

Vim plugins can be installed with the help of nix. You can omit using vim plugin managers and do everything in your .nixpkgs/config.

A lot of documentation about package management and configuration of vim in nix is stored at [2] in nixpkgs.

Customizations

Both vim and neovim can be further configured to include your favorite plugins and additional libraries. To list all available vim plugins, run nix search nixpkgs#vimPlugins.

Add the following code to your ~/.nixpkgs/config.nix:

{
  packageOverrides = pkgs: with pkgs; {
    myVim = vim_configurable.customize {
      name = "vim-with-plugins";
      <div lang="en" dir="ltr" class="mw-content-ltr">
# add here code from the example section
</div>
    };
    myNeovim = neovim.override {
      configure = {
        customRC = ''
          <div lang="en" dir="ltr" class="mw-content-ltr">
# here your custom configuration goes!
</div>
        '';
        packages.myVimPackage = with pkgs.vimPlugins; {
          <div lang="en" dir="ltr" class="mw-content-ltr">
# see examples below how to use custom packages
</div>
          start = [ ];
          opt = [ ];
        }; 
      };     
    };
  };
}

After that you can install your special grafted `myVim` or `myNeovim` packages.

Examples

Apply custom vimrc configuration

NB: you must use vimrcConfig.customRC rather than installing a ~/.vimrc by hand, since the customized Vim will silently ignore any vimrc in your home directory.

vim_configurable.customize {
  name = "vim-with-plugins";
  <div lang="en" dir="ltr" class="mw-content-ltr">
# add custom .vimrc lines like this:
</div>
  vimrcConfig.customRC = ''
    set hidden
    set colorcolumn=80 
  '';
}

If you need to run code before plugins are added, you can use vimrcConfig.beforePlugins (be sure to include set nocompatible if you override the default value).

Using vim's builtin packaging capability

vim_configurable.customize {
  vimrcConfig.packages.myVimPackage = with pkgs.vimPlugins; {
    <div lang="en" dir="ltr" class="mw-content-ltr">
# loaded on launch
</div>
    start = [ YouCompleteMe fugitive ];
    <div lang="en" dir="ltr" class="mw-content-ltr">
# manually loadable by calling `:packadd $plugin-name`
</div>
    opt = [ phpCompletion elm-vim ];
    <div lang="en" dir="ltr" class="mw-content-ltr">
# To automatically load a plugin when opening a filetype, add vimrc lines like:
</div>
    # autocmd FileType php :packadd phpCompletion
  }
};

Note that dynamically loading with opt may be buggy and the workaround is to use start instead.

Using Pathogen as manager

There is a pathogen implementation as well, but its startup is slower and [VAM] has more features.

vimrcConfig.pathogen.knownPlugins = vimPlugins; # optional
vimrcConfig.pathogen.pluginNames = [ "vim-addon-nix" "youcompleteme" ];

Using Vim-Plug as manager

vimrcConfig.plug.plugins = with pkgs.vimPlugins; [vim-addon-nix youcompleteme];

Adding new plugins

  • First run ./update.py.
  • 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 @main to an entry (search the file for examples)
  • Run ./update.py 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 pkgs/misc/vim-plugins/vim2nix/additional-nix-code and rerun ./update.py. They will be included in the generated code.

Notes Regarding Plugins

For additional info, you may wish to look at documentation on the nixpkgs repository.

Add a new custom plugin to the users packages

Sometimes you do not want to change upstream plugins, for this you can use vimUtils.buildVimPlugin to create your own:

let
  vim-better-whitespace = pkgs.vimUtils.buildVimPlugin {
    name = "vim-better-whitespace";
    src = pkgs.fetchFromGitHub {
      owner = "ntpeters";
      repo = "vim-better-whitespace";
      rev = "984c8da518799a6bfb8214e1acdcfd10f5f1eed7";
      sha256 = "10l01a8xaivz6n01x6hzfx7gd0igd0wcf9ril0sllqzbq7yx2bbk";
    };
  };
in {
  users.users.<yourNickname>.packages = [
    (pkgs.vim_configurable.customize {
      vimrcConfig.packages.myVimPackage = with pkgs.vimPlugins; {
        start = [ vim-better-whitespace ];
      };
    })
  ];
};

Using flake

configuration.nix:

{ inputs, ... }:
{
  nixpkgs = {
    overlays = [
      (self: super:
        let
          winresizer-vim = super.vimUtils.buildVimPlugin {
            name = "winresizer-vim";
            src = inputs.winresizer-vim;
          };
        in
        {
          vimPlugins =
            super.vimPlugins // {
              inherit winresizer-vim;
            };
        }
      )
    ];
  };

flake.nix:

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-22.05";
    winresizer-vim = {
      url = "github:simeji/winresizer";
      flake = false;
    };
  };

  outputs = inputs@{ nixpkgs, ... }: {
    nixosConfigurations.nixos = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      specialArgs = { inherit inputs; };
      modules = [
        ./configuration.nix
        ./hardware-configuration.nix
        { nix.registry.nixpkgs.flake = nixpkgs; }
      ];
    };
  };
}

Then we can update the package with nix flake lock --update-input winresizer-vim, or update all inputs in flake.nix with nix flake update.

Vim as a Python IDE

The following snippet will make a full featured python IDE.

Using language client

vim_configurable.customize {
  vimrcConfig = {
    customRC = ''
      let g:LanguageClient_serverCommands = {
        \ 'python': ['pyls']
        \ }
       nnoremap <F5> :call LanguageClient_contextMenu()<CR>
       nnoremap <silent> gh :call LanguageClient_textDocument_hover()<CR>
       nnoremap <silent> gd :call LanguageClient_textDocument_definition()<CR>
       nnoremap <silent> gr :call LanguageClient_textDocument_references()<CR>
       nnoremap <silent> gs :call LanguageClient_textDocument_documentSymbol()<CR>
       nnoremap <silent> <F2> :call LanguageClient_textDocument_rename()<CR>
       nnoremap <silent> gf :call LanguageClient_textDocument_formatting()<CR>
    '';
    packages.myVimPackage = with pkgs.vimPlugins; {
      start = [ LanguageClient-neovim ];
    }
};

Then put the following expression in environment.systemPackages or in the home-manager package list, to install python-language-server:

(python3.withPackages(ps: [
  ps.python-language-server
  <div lang="en" dir="ltr" class="mw-content-ltr">
# the following plugins are optional, they provide type checking, import sorting and code formatting
</div>
  ps.pyls-mypy ps.pyls-isort ps.pyls-black
]))

Real life examples

YouCompleteMe

Currently the youcompleteme plugin uses unwrapped clang on linux. This causes it to not find stdlib.h. There is a workaround you can put in your .ycm_extra_conf.py 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 stdlib.h.

A better alternative to youcompleteme for C/C++ is to use cquery in combination with the 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 shell wrapper

Python 3 support for vim

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.callPackage ./my_vim.nix {                                                                                                                                                          
      vim_configurable = vim_configurable.override { python = python3; };                                                                                                                     
})

gvim and gview

gvim and gview may be installed using the vimHugeX attribute name (package name vim_configurable).

$ nix-env -iA nixos.vimHugeX

If you are using vim_configurable.customize, you can enable wrapGui to make gvim available, though this won't give you gview:

vim_configured = pkgs.vim_configurable.customize {
  name = "vim";
  wrapGui = true;
};