本文介绍管理Vim与Neovim插件的两种方式:

附录中也提供了两种管理vim插件的可选项。

一、使用package管理Vim插件

package是Vim8内置的插件管理工具。Vim会自动加载~/.vim/pack/*/start/下的插件(其中,*可以是任意值,如:github)。

~/.vim/pack/github/start/下导入插件,如下所示:

mkdir -p ~/.vim/pack/github/start/
git clone --depth 1 https://github.com/preservim/nerdtree.git ~/.vim/pack/github/start/nerdtree
git clone --depth 1 https://github.com/vim-airline/vim-airline.git ~/.vim/pack/github/start/vim-airline

编辑vimrc文件:

vim ~/.vim/vimrc
" --------------------NerdTree--------------------
" 设置NerdTree,按F3即可显示或隐藏NerdTree区域
map <F3> :NERDTreeMirror<CR>
map <F3> :NERDTreeToggle<CR>
"打开vim时自动打开NERDTree
"autocmd vimenter * NERDTree

二、使用lazy.nvim管理Neovim插件

Neovim也支持原生package的方式管理插件。

(一)安装lazy.nvim

安装过程参见官方文档

mkdir -p ~/.config/nvim/
nvim ~/.config/nvim/init.lua
--------------------lazy.nvim--------------------
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    "--branch=stable",
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
  "nvim-lualine/lualine.nvim",
  "nvim-tree/nvim-tree.lua",
})

--------------------lualine--------------------
require('lualine').setup()

--------------------nvim-tree--------------------
require("nvim-tree").setup()

启动nvim后,上述lualine.nvimnvim-tree.lua两个插件将会自动安装。

(二)配置lazy.nvim

  • 配置LSP

编辑init.lua文件:

nvim ~/.config/nvim/init.lua
require("lazy").setup({
  "neovim/nvim-lspconfig",
}

--------------------lsp--------------------
local lspconfig = require('lspconfig')
lspconfig.pyright.setup {}

安装python的LSP服务端:

npm i -g pyright

编辑python文件:

nvim hello.py

编写任意python语句以验证语法提示等功能是否正常。同时,可使用:LspInfo查看LSP的状态。

手工安装LSP服务端的过程可能比较繁琐(如上面的npm i -g pyright),mason.nvim提供了自动安装LSP服务端的方法。

nvim ~/.config/nvim/init.lua
require("lazy").setup({
  "neovim/nvim-lspconfig",
  "williamboman/mason.nvim",
  "williamboman/mason-lspconfig.nvim",
})

require('lspconfig')
require('mason').setup()
require('mason-lspconfig').setup({
  ensure_installed = {
    "lua_ls",
    "pyright",
    "gopls"
  }
})
require("mason-lspconfig").setup_handlers {
  function (server_name)
    require("lspconfig")[server_name].setup {}
  end,
}

其中,mason实现了LSP插件的统一管理,mason-lspconfig实现了lspconfigmason之间的联动。使用mason-lspconfigsetup_handlers特性后,就无需显性启用 lspconfig的语言服务配置了(如:require('lspconfig').pyright.setup {})。

随后,使用nvim编辑任意文件,相关LSP服务将会自动下载并启用,后续也可以使用:Mason:LspInstall等命令统一管理LSP插件。

  • 配置主题

编辑init.lua文件:

vim ~/.config/nvim/init.lua
require("lazy").setup({
  {
    "folke/tokyonight.nvim",
    lazy = false,
    priority = 1000,
    config = function()
      vim.cmd([[colorscheme tokyonight]])
    end,
  },
}
  • 完整的配置
--------------------lazy.nvim--------------------
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    "--branch=stable",
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
  "nvim-lualine/lualine.nvim",
  "nvim-tree/nvim-tree.lua",
  "neovim/nvim-lspconfig",
  "williamboman/mason.nvim",
  "williamboman/mason-lspconfig.nvim",
  {
    "folke/tokyonight.nvim",
    lazy = false,
    priority = 1000,
    config = function()
      vim.cmd([[colorscheme tokyonight]])
    end,
  },
})

--------------------lualine--------------------
require('lualine').setup()

--------------------nvim-tree--------------------
require("nvim-tree").setup()
vim.keymap.set("n", "<leader>t", "<Cmd>NvimTreeToggle<CR>")

--------------------lsp--------------------
require('lspconfig')
require('mason').setup()
require('mason-lspconfig').setup({
  ensure_installed = {
    "lua_ls",
    "pyright",
    "gopls"
  }
})
require("mason-lspconfig").setup_handlers {
  function (server_name)
    require("lspconfig")[server_name].setup {}
  end,
}

附录

附录一:使用vim-plug管理Vim插件

sudo pacman -S vim-plug
  • 配置vim-plug
vim ~/.vimrc
call plug#begin('~/.vim/plugged')

Plug 'preservim/nerdtree'
Plug 'Xuyuanp/nerdtree-git-plugin'
Plug 'neoclide/coc.nvim', {'branch': 'release'}
Plug 'morhetz/gruvbox'
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
"Plug 'ycm-core/YouCompleteMe'

call plug#end()

" --------------------NerdTree--------------------
" 设置NerdTree,按F3即可显示或隐藏NerdTree区域
map <F3> :NERDTreeMirror<CR>
map <F3> :NERDTreeToggle<CR>
"打开vim时自动打开NERDTree
"autocmd vimenter * NERDTree

" --------------------gruvbox--------------------
" 使用gruvbox配色
"autocmd vimenter * ++nested colorscheme gruvbox
" 强制使用dark模式
"set bg=dark

附录二:使用Vundle管理Vim插件

Vundle项目长期未更新,本章内容已经过时。建议使用原生packagevim-plug等插件管理器替换之。

Vundle的安装过程参见在 Linux 上使用 Vundle 管理 Vim 插件VundleVim/Vundle.vim

  • 下载Vundle

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

  • 配置Vundle

Vundle的配置步骤如下所示:

  1. 编辑插件地址:在~/.vimrc文件的call vundle#begin()call vundle#end() 之间写入Plugin '......'
  2. 编辑插件配置:在~/.vimrc文件末尾写入配置信息
  3. 安装插件:vim –> : –>PluginInstall

vim ~/.vimrc

set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.

" plugin on GitHub repo
" Plugin 'tpope/vim-fugitive'
" plugin from http://vim-scripts.org/vim/scripts.html
" Plugin 'L9'

" Git plugin not hosted on GitHub
" Plugin 'git://git.wincent.com/command-t.git'

" git repos on your local machine (i.e. when working on your own plugin)
" Plugin 'file:///home/gmarik/path/to/plugin'

" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
" Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" Install L9 and avoid a Naming conflict if you've already installed a
" different version somewhere else.
" Plugin 'ascenator/L9', {'name': 'newL9'}

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList       - lists configured plugins
" :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
set nu!                                      "显示行号
syntax on                                    "语法高亮度显示
Plugin 'scrooloose/nerdtree'	
" --------------------NerdTree--------------------
" 设置NerdTree,按F3即可显示或隐藏NerdTree区域
map <F3> :NERDTreeMirror<CR>
map <F3> :NERDTreeToggle<CR>
"打开vim时自动打开NERDTree
autocmd vimenter * NERDTree
Plugin 'godlygeek/tabular'
Plugin 'plasticboy/vim-markdown'
Plugin 'suan/vim-instant-markdown'
Plugin 'dhruvasagar/vim-table-mode'	"使用\tm开启Table Model
Plugin 'jiangmiao/auto-pairs'
Plugin 'scrooloose/syntastic'
" --------------------syntastic--------------------
" syntastic官方推荐的默认设置
set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0
Plugin 'valloric/youcompleteme'
Plugin 'ervandew/supertab'
Plugin 'scrooloose/nerdcommenter'