Jump to content

Vim/zh: Difference between revisions

From NixOS Wiki
Ardenet (talk | contribs)
Created page with "== 系统范围的 vim/nvim 配置 =="
Ardenet (talk | contribs)
Created page with "=== 使用 vim 的内置打包功能 ==="
Tags: Mobile edit Mobile web edit
 
(14 intermediate revisions by the same user not shown)
Line 94: Line 94:
== 系统范围的 vim/nvim 配置 ==
== 系统范围的 vim/nvim 配置 ==


<div lang="en" dir="ltr" class="mw-content-ltr">
如果您想要一个 vim/nvim 的系统范围“基本”配置,这里有两个示例:
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">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 110: Line 108:
     package = (pkgs.vim-full.override {  }).customize{
     package = (pkgs.vim-full.override {  }).customize{
       name = "vim";
       name = "vim";
       <div lang="en" dir="ltr" class="mw-content-ltr">
       # 安装插件,例如用于 nix 文件的语法高亮
# 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 118: Line 114:
       };
       };
       vimrcConfig.customRC = ''
       vimrcConfig.customRC = ''
         <div lang="en" dir="ltr" class="mw-content-ltr">
         " 你的自定义 vimrc
" your custom vimrc
</div>
         set nocompatible
         set nocompatible
         set backspace=indent,eol,start
         set backspace=indent,eol,start
         <div lang="en" dir="ltr" class="mw-content-ltr">
         " 默认打开语法高亮
" Turn on syntax highlighting by default
</div>
         syntax on
         syntax on
         " ...
         " ...
Line 142: Line 134:
     configure = {
     configure = {
       customRC = ''
       customRC = ''
         <div lang="en" dir="ltr" class="mw-content-ltr">
         " 你的自定义 vimrc
" your custom vimrc
</div>
         set nocompatible
         set nocompatible
         set backspace=indent,eol,start
         set backspace=indent,eol,start
Line 158: Line 148:
</syntaxHighlight>
</syntaxHighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
将这些导入到您的 <code>configuration.nix</code> 中并使用
import these in your <code>configuration.nix</code> and
</div>
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
{     
{     
Line 171: Line 159:
</syntaxHighlight>
</syntaxHighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Custom_setup_without_using_Home_Manager"></span>
== Custom setup without using Home Manager ==
== 不使用 Home Manager 的自定义设置 ==
</div>


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


<div lang="en" dir="ltr" class="mw-content-ltr">
Vim 插件可以通过 nix 安装。您可以不使用 vim 插件管理器,直接在 <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>.
</div>


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


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Customizations"></span>
=== Customizations ===
=== 自定义 ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
vim neovim 都可以进一步配置,以包含您喜欢的插件和其他库。要列出所有可用的 vim 插件,请运行 <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>.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
将以下代码添加到您的<code>~/.nixpkgs/config.nix</code>
Add the following code to your <code>~/.nixpkgs/config.nix</code>:
</div>


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 204: Line 184:
     myVim = vim-full.customize {
     myVim = vim-full.customize {
       name = "vim-with-plugins";
       name = "vim-with-plugins";
       <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 = ''
           <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; {
           <div lang="en" dir="ltr" class="mw-content-ltr">
           # 请参阅以下示例了解如何使用自定义包
# see examples below how to use custom packages
</div>
           start = [ ];
           start = [ ];
           opt = [ ];
           opt = [ ];
Line 228: Line 202:
</syntaxhighlight>
</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
之后,您可以安装特殊移植的 `myVim` `myNeovim` 包。
After that you can install your special grafted `myVim` or `myNeovim` packages.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Examples"></span>
=== Examples ===
=== 示例 ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Apply_custom_vimrc_configuration"></span>
==== Apply custom vimrc configuration ====
==== 应用自定义 vimrc 配置 ====
</div>


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


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Using_vim&#039;s_builtin_packaging_capability"></span>
=== Using vim's builtin packaging capability ===
=== 使用 vim 的内置打包功能 ===
</div>


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 288: Line 257:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Using_Pathogen_as_manager"></span>
=== Using Pathogen as manager ===
=== 使用 Pathogen 作为管理器 ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 301: Line 269:
</syntaxhighlight>
</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Using_Vim-Plug_as_manager"></span>
=== Using Vim-Plug as manager ===
=== 使用 Vim-Plug 作为管理器 ===
</div>


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 309: Line 276:
</syntaxhighlight>
</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Adding_new_plugins"></span>
=== Adding new plugins ===
=== 添加新插件 ===
</div>


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


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Notes_Regarding_Plugins"></span>
==== Notes Regarding Plugins ====
==== 关于插件的说明 ====
</div>


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


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Add_a_new_custom_plugin_to_the_users_packages"></span>
=== Add a new custom plugin to the users packages  ===
=== 向用户包添加一个新的自定义插件 ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 355: Line 319:
</syntaxhighlight>
</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Using_flake"></span>
==== Using flake ====
==== 使用 Flakes ====
</div>


<code>configuration.nix</code>:
<code>configuration.nix</code>:
Line 412: Line 375:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Vim_as_a_Python_IDE"></span>
=== Vim as a Python IDE ===
=== Vim 用作 Python IDE ===
</div>


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


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Using_language_client"></span>
==== Using language client ====
==== 使用语言客户端 ====
</div>


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 460: Line 421:
</syntaxHighlight>
</syntaxHighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Real_life_examples"></span>
=== Real life examples ===
=== 真实示例 ===
</div>


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


<div lang="en" dir="ltr" class="mw-content-ltr">
=== YouCompleteMe ===
=== YouCompleteMe ===
</div>


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


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="gvim_and_gview"></span>
== gvim and gview ==
== gvim gview ==
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">

Latest revision as of 11:04, 8 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

使用 Home Manager 可以轻松设置 Vim。这是一个简单的例子:

  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 配置带有 Python 支持的 Coc

系统范围的 vim/nvim 配置

如果您想要一个 vim/nvim 的系统范围“基本”配置,这里有两个示例:

On 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";
      # 在此处添加示例部分的代码
    };
    myNeovim = neovim.override {
      configure = {
        customRC = ''
          # 您的自定义配置就在这里!
        '';
        packages.myVimPackage = with pkgs.vimPlugins; {
          # 请参阅以下示例了解如何使用自定义包
          start = [ ];
          opt = [ ];
        }; 
      };     
    };
  };
}

之后,您可以安装特殊移植的 `myVim` 或 `myNeovim` 包。

示例

应用自定义 vimrc 配置

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

使用 vim 的内置打包功能

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.

使用 Pathogen 作为管理器

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" ];

使用 Vim-Plug 作为管理器

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

添加新插件

关于插件的说明

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

向用户包添加一个新的自定义插件

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 ];
      };
    })
  ];
};

使用 Flakes

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 用作 Python IDE

The following snippet will make a full featured python IDE.

使用语言客户端

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
]))

真实示例

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 和 gview

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

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