Jump to content

Vim/ja: Difference between revisions

From NixOS Wiki
Haruki7049 (talk | contribs)
Created page with "以下のコードを<code>~/.nixpkgs/config.nix</code>に追加してください:"
FuzzyBot (talk | contribs)
Updating to match new version of source page
 
Line 9: Line 9:
<span id="Basic_Install"></span>
<span id="Basic_Install"></span>
=== ベーシックインストール ===
=== ベーシックインストール ===
unstableでは:
<syntaxhighlight lang="nix>
<syntaxhighlight lang="nix>
   programs.vim.enable = true;
   programs.vim.enable = true;
Line 18: Line 15:
もしくは
もしくは


<syntaxhighlight lang="nix>
<syntaxhighlight lang="nix">
   programs.vim = {
   programs.vim = {
     enable = true;
     enable = true;
     package = pkgs.vim_configurable;
     package = pkgs.vim-full;
   };
   };
</syntaxhighlight>
24.05かそれよりも古い場合は:
<syntaxhighlight lang="nix>
  environment.systemPackages = with pkgs; [ vim ];
</syntaxhighlight>
</syntaxhighlight>


もしくは
もしくは


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


Line 60: Line 51:
=== Vimスペルファイル ===
=== Vimスペルファイル ===


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


<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
Line 108: Line 101:
unstableでは:
unstableでは:


<syntaxHighlight lang="nix">
<syntaxhighlight lang="nix">
{ pkgs, ... }:
{ pkgs, ... }:
{
{
Line 114: Line 107:
     enable = true;
     enable = true;
     defaultEditor = true;
     defaultEditor = true;
     package = (pkgs.vim_configurable.override {  }).customize{
     package = (pkgs.vim-full.override {  }).customize{
       name = "vim";
       name = "vim";
       # 例として、Nixファイルのシンタックスハイライト表示用のプラグインをインストールする
       # 例として、Nixファイルのシンタックスハイライト表示用のプラグインをインストールする
Line 132: Line 125:
   };
   };
}
}
</syntaxHighlight>
</syntaxhighlight>
 
24.05かそれよりも古い場合は
 
<syntaxHighlight lang="nix">
{ pkgs, ... }:
{
  environment.variables = { EDITOR = "vim";
  environment.systemPackages = with pkgs; [
    ((vim_configurable.override {  }).customize{
      name = "vim";
      # 例としてNixファイルのシンタックスハイライト表示用のプラグインをインストールする
      vimrcConfig.packages.myplugins = with pkgs.vimPlugins; {
        start = [ vim-nix vim-lastplace ];
        opt = [];
      };
      vimrcConfig.customRC = ''
        " 自分のカスタムvimrc
        set nocompatible
        set backspace=indent,eol,start
        " シンタックスハイライトをデフォルトでオンにする
        syntax on
        " ...
      '';
    }
  )];
}
</syntaxHighlight>
 
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
{ pkgs, ... }:
{ pkgs, ... }:
Line 215: Line 180:
以下のコードを<code>~/.nixpkgs/config.nix</code>に追加してください:
以下のコードを<code>~/.nixpkgs/config.nix</code>に追加してください:


<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";
       <div lang="en" dir="ltr" class="mw-content-ltr">
       <div lang="en" dir="ltr" class="mw-content-ltr">
Line 242: Line 207:
   };
   };
}
}
</syntaxHighlight>
</syntaxhighlight>


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


<syntaxHighlight lang="nix">
<syntaxhighlight lang="nix">
vim_configurable.customize {
vim-full.customize {
   name = "vim-with-plugins";
   name = "vim-with-plugins";
   <div lang="en" dir="ltr" class="mw-content-ltr">
   <div lang="en" dir="ltr" class="mw-content-ltr">
Line 271: Line 236:
   '';
   '';
}
}
</syntaxHighlight>
</syntaxhighlight>


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


<syntaxHighlight lang="nix">
<syntaxhighlight lang="nix">
vim_configurable.customize {
vim-full.customize {
   vimrcConfig.packages.myVimPackage = with pkgs.vimPlugins; {
   vimrcConfig.packages.myVimPackage = with pkgs.vimPlugins; {
     <div lang="en" dir="ltr" class="mw-content-ltr">
     <div lang="en" dir="ltr" class="mw-content-ltr">
Line 298: Line 263:
   }
   }
};
};
</syntaxHighlight>
</syntaxhighlight>


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


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


Line 373: Line 314:
</div>
</div>


<syntaxHighlight lang="nix">
<syntaxhighlight lang="nix">
let
let
   vim-better-whitespace = pkgs.vimUtils.buildVimPlugin {
   vim-better-whitespace = pkgs.vimUtils.buildVimPlugin {
Line 386: Line 327:
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 393: Line 334:
   ];
   ];
};
};
</syntaxHighlight>
</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 457: Line 398:


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
The following snippet will make a full featured python IDE.
The following snippet will make a full featured [[python]] IDE.
</div>
</div>


Line 464: Line 405:
</div>
</div>


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


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 529: Line 470:


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
== Python 3 support for vim ==
== gvim and gview ==
</div>
</div>


<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:
You can enable <code>guiSupport</code> to make <code>gvim</code> available, though this won't give you <code>gview</code>:
</div>
</div>


<syntaxHighlight  lang="nix">
<syntaxhighlight lang="nix">
(pkgs.callPackage ./my_vim.nix {                                                                                                                                                        
(pkgs.vim-full.customize {
      vim_configurable = vim_configurable.override { python = python3; };                                                                                                                    
  guiSupport = true;
})
})
</syntaxHighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
== gvim and gview ==
</div>
<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>).
</div>
<syntaxhighlight lang=bash>
$ nix-env -iA nixos.vimHugeX
</syntaxhighlight>
<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>:
</div>
<syntaxhighlight lang=nix>
vim_configured = pkgs.vim_configurable.customize {
  name = "vim";
  wrapGui = true;
};
</syntaxhighlight>
</syntaxhighlight>


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

Latest revision as of 20:14, 7 October 2025

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

インストール

ベーシックインストール

  programs.vim.enable = true;

もしくは

  programs.vim = {
    enable = true;
    package = pkgs.vim-full;
  };

もしくは

  environment.systemPackages = with pkgs; [ vim-full ];

Home Managerを使用した例

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

  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

システム全体でのvim/nvim設定

vim/nvimのシステム全体の"ベースライン"設定が必要な場合は、次の二つの例を参照してください:

unstableでは:

{ pkgs, ... }:
{
  programs.vim = {
    enable = true;
    defaultEditor = true;
    package = (pkgs.vim-full.override {  }).customize{
      name = "vim";
      # 例として、Nixファイルのシンタックスハイライト表示用のプラグインをインストールする
      vimrcConfig.packages.myplugins = with pkgs.vimPlugins; {
        start = [ vim-nix vim-lastplace ];
        opt = [];
      };
      vimrcConfig.customRC = ''
        " 自分のカスタムvimrc
        set nocompatible
        set backspace=indent,eol,start
        " シンタックスハイライトをデフォルトでオンにする
        syntax on
        " ...
      '';
    };
  };
}
{ pkgs, ... }:
{
  programs.neovim = {
    enable = true;
    defaultEditor = true;
    vimAlias = true;
    configure = {
      customRC = ''
        " 自分のカスタムvimrc
        set nocompatible
        set backspace=indent,eol,start
        " ...
      '';
      packages.myPlugins = with pkgs.vimPlugins; {
        start = [ vim-lastplace vim-nix ]; 
        opt = [];
      };
    };
  };
}

これらのファイルを自分のconfiguration.nixでインポートする

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

Home Managerを使用しないカスタムセットアップ

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

Vimプラグインはnixの助けを借りてインストールすることができます。vimプラグインマネージャーを使用せず、すべて.nixpkgs/configで行うことができます。

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

カスタマイズする

vimもneovimも、あなたの好きなプラグインや追加ライブラリを含むようにさらに設定することができます。利用可能な全ての vim プラグインをリストアップするには、nix search nixpkgs#vimPluginsを実行してください。

以下のコードを~/.nixpkgs/config.nixに追加してください:

{
  packageOverrides = pkgs: with pkgs; {
    myVim = vim-full.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-full.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-full.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

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-full.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-full.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

gvim and gview

You can enable guiSupport to make gvim available, though this won't give you gview:

(pkgs.vim-full.customize {
  guiSupport = true;
})