本文介绍管理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

安装过程参见官方文档

mv ~/.config/nvim{,.bak}
mv ~/.local/share/nvim{,.bak}
mv ~/.local/state/nvim{,.bak}
mv ~/.cache/nvim{,.bak}

git clone https://github.com/LazyVim/starter ~/.config/nvim

rm -rf ~/.config/nvim/.git

nvim

(二)配置lazy.nvim

  • 配置LSP

LazyVim的Extra内置了大量LSP,按照以下步骤启用内置编程语言支持:

  • 输入nvim进入LazyVim主界面
  • 输入x进入Extra界面
  • 移动到对应的Language子项(如:json、python等)
  • 输入x选中
  • 退出nvim,重新进入nvim,将会自动下载对应依赖
  • 使用:LspInfo查看LSP的状态

除了使用LazyVim内置编程语言配置外,也可以自定义其它LSP,以Java为例,在~/.config/nvim/lua/plugins/下新建java.lua文件:

cat << EOF > ~/.config/nvim/lua/plugins/java.lua
return {
  {   
    "nvim-java/nvim-java",
    config = function()
      require("java").setup()
      vim.lsp.enable("jdtls")
    end,
  },    
}
EOF

编写任意Java语句以验证语法提示等功能是否正常:

nvim Hello.java

插件会自动安装jdtls、spring-boot-tools、openjdk等依赖,如果有网络问题,可调整对应的下载地址。其配置位于:~/.local/share/nvim/lazy/nvim-java/lua/pkgm/specs/init.lua

附录

附录一:使用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'