Vim/zh: Difference between revisions
Created page with "Vim" Tags: Mobile edit Mobile web edit |
Created page with "对于 C/C++ 来说,比 youcompleteme 更好的替代方案是使用 [https://github.com/cquery-project/cquery/ cquery] 并结合 [https://github.com/autozimu/LanguageClient-neovim LanguageClient-neovim]。如果您从 nixpkgs 安装 cquery,它在 nix-shell 中使用时也能找到 C 头文件,因为它使用了一个自定义的 [https://github.com/NixOS/nixpkgs/commit/04f3b76dcec21f2fcba6b1b0afbb3ed224165050#diff-11cdfc0385b9e017089c1ac09c5b838e shell 包装器]。" |
||
| (40 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
<languages/> | <languages/> | ||
[https://www.vim.org/ Vim](vi 增强版)是一个高度可配置的终端模态文本编辑器。 | |||
[https://www.vim.org/ Vim] | |||
< | <span id="Installation"></span> | ||
== | == 安装 == | ||
< | <span id="Basic_Install"></span> | ||
=== | === 基础安装 === | ||
<syntaxhighlight lang="nix> | <syntaxhighlight lang="nix> | ||
programs.vim.enable = true; | programs.vim.enable = true; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
或者 | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
| Line 26: | Line 20: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
或者 | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
| Line 34: | Line 26: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
< | <span id="Using_Home_Manager"></span> | ||
=== | === 使用 Home Manager === | ||
使用 [[Special:MyLanguage/Home Manager|Home Manager]] 可以轻松设置 Vim。这是一个简单的例子: | |||
<syntaxhighlight lang="nix> | <syntaxhighlight lang="nix> | ||
| Line 53: | Line 42: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
有关所有选项,请参阅 [https://github.com/rycee/home-manager/blob/master/modules/programs/vim.nix]。 | |||
< | <span id="Vim_Spell_Files"></span> | ||
=== Vim | === Vim 拼写文件 === | ||
你可以配置 home-manager,通过打包单个拼写文件的方式将拼写文件安装到你的用户目录中。以下是 [[neovim]] 检查法语的示例: | |||
<syntaxHighlight lang="nix"> | <syntaxHighlight lang="nix"> | ||
| Line 95: | Line 79: | ||
</syntaxHighlight> | </syntaxHighlight> | ||
< | <span id="NeoVim_with_Coc_for_Python"></span> | ||
==== NeoVim | ==== NeoVim 配置带有 Python 支持的 Coc ==== | ||
对于 NeoVim,请使用以下 home-manager 配置: | |||
https://github.com/NixOS/nixpkgs/issues/98166#issuecomment-725319238 | https://github.com/NixOS/nixpkgs/issues/98166#issuecomment-725319238 | ||
< | <span id="System_wide_vim/nvim_configuration"></span> | ||
== | == 系统范围的 vim/nvim 配置 == | ||
如果您想要一个 vim/nvim 的系统范围“基本”配置,这里有两个示例: | |||
unstable 分支: | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
| Line 124: | Line 100: | ||
package = (pkgs.vim-full.override { }).customize{ | package = (pkgs.vim-full.override { }).customize{ | ||
name = "vim"; | name = "vim"; | ||
# 安装插件,例如用于 nix 文件的语法高亮 | |||
# | |||
vimrcConfig.packages.myplugins = with pkgs.vimPlugins; { | vimrcConfig.packages.myplugins = with pkgs.vimPlugins; { | ||
start = [ vim-nix vim-lastplace ]; | start = [ vim-nix vim-lastplace ]; | ||
| Line 132: | Line 106: | ||
}; | }; | ||
vimrcConfig.customRC = '' | vimrcConfig.customRC = '' | ||
" 你的自定义 vimrc | |||
set nocompatible | set nocompatible | ||
set backspace=indent,eol,start | set backspace=indent,eol,start | ||
" 默认打开语法高亮 | |||
syntax on | syntax on | ||
" ... | " ... | ||
| Line 156: | Line 126: | ||
configure = { | configure = { | ||
customRC = '' | customRC = '' | ||
" 你的自定义 vimrc | |||
set nocompatible | set nocompatible | ||
set backspace=indent,eol,start | set backspace=indent,eol,start | ||
| Line 172: | Line 140: | ||
</syntaxHighlight> | </syntaxHighlight> | ||
将这些导入到您的 <code>configuration.nix</code> 中并使用 | |||
<syntaxHighlight lang="nix"> | <syntaxHighlight lang="nix"> | ||
{ | { | ||
| Line 185: | Line 151: | ||
</syntaxHighlight> | </syntaxHighlight> | ||
< | <span id="Custom_setup_without_using_Home_Manager"></span> | ||
== | == 不使用 Home Manager 的自定义设置 == | ||
{{note|要了解如何在 Nix 系统中设置 Vim 的总体概述,请参阅 [https://www.mpscholten.de/nixos/2016/04/11/setting-up-vim-on-nixos.html mpscholten的博客]}} | |||
{{note| | |||
Vim 插件可以通过 nix 安装。您可以不使用 vim 插件管理器,直接在 <code>.nixpkgs/config</code> 中完成所有操作。 | |||
Vim | |||
在 nix 中对 vim 进行包管理和配置的大部分文档存储在 nixpkgs 的 [https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/vim.section.md] 中。 | |||
< | <span id="Customizations"></span> | ||
=== | === 自定义 === | ||
vim 和 neovim 都可以进一步配置,以包含您喜欢的插件和其他库。要列出所有可用的 vim 插件,请运行 <code>nix search nixpkgs#vimPlugins</code>。 | |||
将以下代码添加到您的<code>~/.nixpkgs/config.nix</code>: | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
| Line 218: | Line 172: | ||
myVim = vim-full.customize { | myVim = vim-full.customize { | ||
name = "vim-with-plugins"; | name = "vim-with-plugins"; | ||
# 在此处添加示例部分的代码 | |||
# | |||
}; | }; | ||
myNeovim = neovim.override { | myNeovim = neovim.override { | ||
configure = { | configure = { | ||
customRC = '' | customRC = '' | ||
# 您的自定义配置就在这里! | |||
# | |||
''; | ''; | ||
packages.myVimPackage = with pkgs.vimPlugins; { | packages.myVimPackage = with pkgs.vimPlugins; { | ||
# 请参阅以下示例了解如何使用自定义包 | |||
# | |||
start = [ ]; | start = [ ]; | ||
opt = [ ]; | opt = [ ]; | ||
| Line 242: | Line 190: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
之后,您可以安装特殊移植的 `myVim` 或 `myNeovim` 包。 | |||
< | <span id="Examples"></span> | ||
=== | === 示例 === | ||
< | <span id="Apply_custom_vimrc_configuration"></span> | ||
==== | ==== 应用自定义 vimrc 配置 ==== | ||
注意:您必须使用 <code>vimrcConfig.customRC</code> 而不是手动安装 <code>~/.vimrc</code>,因为自定义 Vim 会默认忽略您主目录中的任何 vimrc 文件。 | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
vim-full.customize { | vim-full.customize { | ||
name = "vim-with-plugins"; | name = "vim-with-plugins"; | ||
# 添加自定义的 .vimrc 行,例如: | |||
# | |||
vimrcConfig.customRC = '' | vimrcConfig.customRC = '' | ||
set hidden | set hidden | ||
| Line 271: | Line 211: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
如果您需要在添加插件之前运行代码,可以使用 <code>vimrcConfig.beforePlugins</code>(如果您要覆盖 [https://github.com/NixOS/nixpkgs/blob/c3df8057dad986bf7f3928de1b5233fadb52bb15/pkgs/misc/vim-plugins/vim-utils.nix#L264-L267 默认值],请务必包含 <code>set nocompatible</code>)。 | |||
</ | |||
< | <span id="Using_vim's_builtin_packaging_capability"></span> | ||
=== | === 使用 vim 的内置打包功能 === | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
vim-full.customize { | vim-full.customize { | ||
vimrcConfig.packages.myVimPackage = with pkgs.vimPlugins; { | vimrcConfig.packages.myVimPackage = with pkgs.vimPlugins; { | ||
# 启动时加载 | |||
# | |||
start = [ YouCompleteMe fugitive ]; | start = [ YouCompleteMe fugitive ]; | ||
# 可通过调用 `:packadd $plugin-name` 手动加载 | |||
# | |||
opt = [ phpCompletion elm-vim ]; | opt = [ phpCompletion elm-vim ]; | ||
# 要在打开文件类型时自动加载插件,请添加如下 vimrc 行: | |||
# | |||
# autocmd FileType php :packadd phpCompletion | # autocmd FileType php :packadd phpCompletion | ||
} | } | ||
| Line 298: | Line 229: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
请注意,使用 opt 动态加载可能存在错误,解决方法是改用 [https://vi.stackexchange.com/a/20818/30821 start]。 | |||
< | <span id="Using_Pathogen_as_manager"></span> | ||
=== | === 使用 Pathogen 作为管理器 === | ||
这 pathogen 的另一实现,原有插件启动速度较慢,而 [VAM] 具有更多功能。 | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
| Line 315: | Line 241: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
< | <span id="Using_Vim-Plug_as_manager"></span> | ||
=== | === 使用 Vim-Plug 作为管理器 === | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
| Line 323: | Line 248: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
< | <span id="Adding_new_plugins"></span> | ||
=== | === 添加新插件 === | ||
请参阅 https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/vim.section.md。 | |||
< | <span id="Notes_Regarding_Plugins"></span> | ||
==== | ==== 关于插件的说明 ==== | ||
如需更多信息,您可以查看 [https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/vim.section.md nixpkgs 存储库上的文档]。 | |||
< | <span id="Add_a_new_custom_plugin_to_the_users_packages"></span> | ||
=== | === 向用户包添加一个新的自定义插件 === | ||
有时您不想修改上游插件,为此您可以使用 <code>vimUtils.buildVimPlugin</code> 创建自己的插件: | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
| Line 369: | Line 285: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
< | <span id="Using_flake"></span> | ||
==== | ==== 使用 Flakes ==== | ||
<code>configuration.nix</code>: | <code>configuration.nix</code>: | ||
| Line 422: | Line 337: | ||
</syntaxHighlight> | </syntaxHighlight> | ||
然后我们可以使用 <code>nix flake lock --update-input winresizer-vim</code> 更新软件包,或者使用 <code>nix flake update</code> 更新 flake.nix 中的所有输入。 | |||
< | <span id="Vim_as_a_Python_IDE"></span> | ||
=== Vim | === 将 Vim 用作 Python IDE === | ||
以下代码片段将创建一个功能齐全的 [[python]] IDE。 | |||
< | <span id="Using_language_client"></span> | ||
==== | ==== 使用语言客户端 ==== | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
| Line 459: | Line 368: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
然后将以下表达式添加到 `<code>environment.systemPackages</code>` 或 home-manager 包列表中,以安装 python-language-server: | |||
<syntaxHighlight lang="nix"> | <syntaxHighlight lang="nix"> | ||
(python3.withPackages(ps: [ | (python3.withPackages(ps: [ | ||
ps.python-language-server | ps.python-language-server | ||
# 以下插件为可选插件,它们提供类型检查、导入排序和代码格式化功能。 | |||
# | |||
ps.pyls-mypy ps.pyls-isort ps.pyls-black | ps.pyls-mypy ps.pyls-isort ps.pyls-black | ||
])) | ])) | ||
</syntaxHighlight> | </syntaxHighlight> | ||
< | <span id="Real_life_examples"></span> | ||
=== | === 真实示例 === | ||
* [https://github.com/jagajaga/my_configs/blob/master/.nispkgs/common.nix Jagajaga 的配置] | |||
* [https://github.com/jagajaga/my_configs/blob/master/. | |||
* [https://github.com/andrewrk/dotfiles/blob/master/.nixpkgs/config.nix andrewrk 的配置] | |||
* [https://github.com/andrewrk/dotfiles/blob/master/.nixpkgs/config.nix andrewrk | |||
* [https://github.com/wagnerf42/nixos-config/blob/master/config/my_vim.nix wagnerf42 的配置(适用于 Rust 语言)] | |||
* [https://github.com/wagnerf42/nixos-config/blob/master/config/my_vim.nix wagnerf42 | |||
=== YouCompleteMe === | === YouCompleteMe === | ||
目前 youcompleteme 插件在 Linux 上使用 [https://github.com/NixOS/nixpkgs/blob/8e7b1f2ac2e261d5a644fef860a0d050ea227c06/pkgs/misc/vim-plugins/default.nix#L695 unwrapped clang]。这导致它找不到 <code>stdlib.h</code>。您可以在您的 <code>.ycm_extra_conf.py</code> 文件中添加 [https://github.com/andrewrk/genesis/blob/5f49cd9a8c2b61b9859a22102bc3f732add9461a/.ycm_extra_conf.py 使其正常运行的示例],其工作原理是执行 C/C++ 编译器并使其输出搜索路径列表,其中包括查找 <code>stdlib.h</code> 的搜索路径。 | |||
对于 C/C++ 来说,比 youcompleteme 更好的替代方案是使用 [https://github.com/cquery-project/cquery/ cquery] 并结合 [https://github.com/autozimu/LanguageClient-neovim LanguageClient-neovim]。如果您从 nixpkgs 安装 cquery,它在 nix-shell 中使用时也能找到 C 头文件,因为它使用了一个自定义的 [https://github.com/NixOS/nixpkgs/commit/04f3b76dcec21f2fcba6b1b0afbb3ed224165050#diff-11cdfc0385b9e017089c1ac09c5b838e shell 包装器]。 | |||
< | <span id="gvim_and_gview"></span> | ||
== gvim | == gvim 和 gview == | ||
您可以启用 <code>guiSupport</code> 来使 <code>gvim</code> 可用,但这不会让您获得 <code>gview</code>: | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||