Vim/zh: Difference between revisions
Created page with "# 要在打开文件类型时自动加载插件,请添加如下 vimrc 行:" |
Created page with "这也是一个 pathogen 实现,但是原有插件启动速度较慢,而 [VAM] 具有更多功能。" |
||
| Line 243: | Line 243: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
请注意,使用 opt 动态加载可能存在错误,解决方法是改用 [https://vi.stackexchange.com/a/20818/30821 start]。 | |||
<span id="Using_Pathogen_as_manager"></span> | <span id="Using_Pathogen_as_manager"></span> | ||
=== 使用 Pathogen 作为管理器 === | === 使用 Pathogen 作为管理器 === | ||
这也是一个 pathogen 实现,但是原有插件启动速度较慢,而 [VAM] 具有更多功能。 | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
Revision as of 14:29, 20 November 2025
Vim(vi 增強版)是一個高度可配置的終端模態文本編輯器。
安裝
基礎安裝
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
For NeoVim use this home manager config: https://github.com/NixOS/nixpkgs/issues/98166#issuecomment-725319238
系統範圍的 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 的自定義設置
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; {
# 启动时加载
start = [ YouCompleteMe fugitive ];
# 可通过调用 `:packadd $plugin-name` 手动加载
opt = [ phpCompletion elm-vim ];
# 要在打开文件类型时自动加载插件,请添加如下 vimrc 行:
# autocmd FileType php :packadd phpCompletion
}
};
請注意,使用 opt 動態加載可能存在錯誤,解決方法是改用 start。
使用 Pathogen 作為管理器
這也是一個 pathogen 實現,但是原有插件啟動速度較慢,而 [VAM] 具有更多功能。
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
您可以啟用 guiSupport 來使 gvim 可用,但這不會讓您獲得 gview:
(pkgs.vim-full.customize {
guiSupport = true;
})