vim插件管理器vim-plug

虽然配置好了 Neovim ,并且工作良好,但是对于 Vim 也不想停留在过去的水平,所以同样升级了 Vim 的配置,首先将插件管理器由 Vundle 切换为 vim-plug ,其安装及使用方法请参考官方 README.md 文件内容。

由于网络问题,访问github并不稳定,在配置插件时已经使用git@github.com: 全面取代了https://github.com/ , 但是对于插件vim-pug自己升级的问题并没有很好的解决,因为其默认是从https://github.com/下载自己的,相比较而言,neovim下的packer就要好的多,因为它可以自己管理自己。解决此问题的方法,暂时定为:

  1. 找到plug.vim第95行

    ~/.vim/autoload/plug.vim
    1
    let s:plug_src = 'https://github.com/junegunn/vim-plug.git'

  2. 修改为

    ~/.vim/autoload/plug.vim
    1
    let s:plug_src = 'git@github.com:junegunn/vim-plug.git'

  3. 升级后,此地址再次变回https://github.com, 所以每次升级时都要执行上述操作。如果官方改良了这个升级地址,则不需要自己修改了,但是貌似这个更改有点难度。

Pros.

  • Easy to set up: Single file. No boilerplate code required.
  • Easy to use: Concise, intuitive syntax
  • Super-fast parallel installation/update (with any of +job, +python, +python3, +ruby, or Neovim)
  • Creates shallow clones to minimize disk space usage and download time
  • On-demand loading for faster startup time
  • Can review and rollback updates
  • Branch/tag/commit support
  • Post-update hooks
  • Support for externally managed plugins

Installation

Download plug.vim and put it in the "autoload" directory.

Vim

Unix
1
2
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

You can automate the process by putting the command in your Vim configuration file as suggested here.

Windows (PowerShell)
1
2
iwr -useb https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim |`
ni $HOME/vimfiles/autoload/plug.vim -Force

Neovim

Unix, Linux
1
2
sh -c 'curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
Linux (Flatpak)
1
2
curl -fLo ~/.var/app/io.neovim.nvim/data/nvim/site/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
Windows (PowerShell)
1
2
iwr -useb https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim |`
ni "$(@($env:XDG_DATA_HOME, $env:LOCALAPPDATA)[$null -eq $env:XDG_DATA_HOME])/nvim-data/site/autoload/plug.vim" -Force

Getting Help

  • See tutorial page to learn the basics of vim-plug
  • See tips and FAQ pages for common problems and questions
  • See requirements page for debugging information & tested configurations
  • Create an issue

Usage

Add a vim-plug section to your ~/.vimrc (or stdpath('config') . '/init.vim' for Neovim)

  1. Begin the section with call plug#begin([PLUGIN_DIR])
  2. List the plugins with Plug commands
  3. call plug#end() to update &runtimepath and initialize plugin system
    • Automatically executes filetype plugin indent on and syntax enable. You can revert the settings after the call. e.g. filetype indent off, syntax off, etc.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
call plug#begin()
" The default plugin directory will be as follows:
" - Vim (Linux/macOS): '~/.vim/plugged'
" - Vim (Windows): '~/vimfiles/plugged'
" - Neovim (Linux/macOS/Windows): stdpath('data') . '/plugged'
" You can specify a custom plugin directory by passing it as the argument
" - e.g. `call plug#begin('~/.vim/plugged')`
" - Avoid using standard Vim directory names like 'plugin'

" Make sure you use single quotes

" Shorthand notation; fetches https://github.com/junegunn/vim-easy-align
Plug 'junegunn/vim-easy-align'

" Any valid git URL is allowed
Plug 'https://github.com/junegunn/vim-github-dashboard.git'

" Multiple Plug commands can be written in a single line using | separators
Plug 'SirVer/ultisnips' | Plug 'honza/vim-snippets'

" On-demand loading
Plug 'preservim/nerdtree', { 'on': 'NERDTreeToggle' }
Plug 'tpope/vim-fireplace', { 'for': 'clojure' }

" Using a non-default branch
Plug 'rdnetto/YCM-Generator', { 'branch': 'stable' }

" Using a tagged release; wildcard allowed (requires git 1.9.2 or above)
Plug 'fatih/vim-go', { 'tag': '*' }

" Plugin options
Plug 'nsf/gocode', { 'tag': 'v.20150303', 'rtp': 'vim' }

" Plugin outside ~/.vim/plugged with post-update hook
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }

" Unmanaged plugin (manually installed and updated)
Plug '~/my-prototype-plugin'

" Initialize plugin system
" - Automatically executes `filetype plugin indent on` and `syntax enable`.
call plug#end()
" You can revert the settings after the call like so:
" filetype indent off " Disable file-type-specific indentation
" syntax off " Disable syntax highlighting

Reload .vimrc and :PlugInstall to install plugins.

Commands

Command Description
PlugInstall [name ...] [#threads] Install plugins
PlugUpdate [name ...] [#threads] Install or update plugins
PlugClean[!] Remove unlisted plugins (bang version will clean without prompt)
PlugUpgrade Upgrade vim-plug itself
PlugStatus Check the status of plugins
PlugDiff Examine changes from the previous update and the pending changes
PlugSnapshot[!] [output path] Generate script for restoring the current snapshot of the plugins

Plug options

Option Description
branch/tag/commit Branch/tag/commit of the repository to use
rtp Subdirectory that contains Vim plugin
dir Custom directory for the plugin
as Use different name for the plugin
do Post-update hook (string or funcref)
on On-demand loading: Commands or <Plug>-mappings
for On-demand loading: File types
frozen Do not update unless explicitly specified

Global options

Flag Default Description
g:plug_threads 16 Default number of threads to use
g:plug_timeout 60 Time limit of each task in seconds (Ruby & Python)
g:plug_retries 2 Number of retries in case of timeout (Ruby & Python)
g:plug_shallow 1 Use shallow clone
g:plug_window vertical topleft new Command to open plug window
g:plug_pwindow above 12new Command to open preview window in PlugDiff
g:plug_url_format https://git::@github.com/%s.git printf format to build repo URL (Only applies to the subsequent Plug commands)

Keybindings

  • D - PlugDiff
  • S - PlugStatus
  • R - Retry failed update or installation tasks
  • U - Update plugins in the selected range
  • q - Close the window
  • :PlugStatus
    • L - Load plugin
  • :PlugDiff
    • X - Revert the update

Example: A small sensible Vim configuration

1
2
3
call plug#begin()
Plug 'tpope/vim-sensible'
call plug#end()

On-demand loading of plugins

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
" NERD tree will be loaded on the first invocation of NERDTreeToggle command
Plug 'preservim/nerdtree', { 'on': 'NERDTreeToggle' }

" Multiple commands
Plug 'junegunn/vim-github-dashboard', { 'on': ['GHDashboard', 'GHActivity'] }

" Loaded when clojure file is opened
Plug 'tpope/vim-fireplace', { 'for': 'clojure' }

" Multiple file types
Plug 'kovisoft/paredit', { 'for': ['clojure', 'scheme'] }

" On-demand loading on both conditions
Plug 'junegunn/vader.vim', { 'on': 'Vader', 'for': 'vader' }

" Code to execute when the plugin is lazily loaded on demand
Plug 'junegunn/goyo.vim', { 'for': 'markdown' }
autocmd! User goyo.vim echom 'Goyo is now loaded!'

The for option is generally not needed as most plugins for specific file types usually don't have too much code in the plugin directory. You might want to examine the output of vim --startuptime before applying the option.

Post-update hooks

There are some plugins that require extra steps after installation or update. In that case, use the do option to describe the task to be performed.

1
2
Plug 'Shougo/vimproc.vim', { 'do': 'make' }
Plug 'ycm-core/YouCompleteMe', { 'do': './install.py' }

If the value starts with :, it will be recognized as a Vim command.

1
Plug 'fatih/vim-go', { 'do': ':GoInstallBinaries' }

If you need more control, you can pass a reference to a Vim function that takes a single argument.

1
2
3
4
5
6
7
8
9
10
11
function! BuildYCM(info)
" info is a dictionary with 3 fields
" - name: name of the plugin
" - status: 'installed', 'updated', or 'unchanged'
" - force: set on PlugInstall! or PlugUpdate!
if a:info.status == 'installed' || a:info.force
!./install.py
endif
endfunction

Plug 'ycm-core/YouCompleteMe', { 'do': function('BuildYCM') }

Both forms of post-update hook are executed inside the directory of the plugin and only run when the repository has changed, but you can force it to run unconditionally with the bang-versions of the commands: PlugInstall! and PlugUpdate!.

Make sure to escape BARs and double-quotes when you write the do option inline as they are mistakenly recognized as command separator or the start of the trailing comment.

1
Plug 'junegunn/fzf', { 'do': 'yes \| ./install' }

But you can avoid the escaping if you extract the inline specification using a variable (or any Vimscript expression) as follows:

1
2
let g:fzf_install = 'yes | ./install'
Plug 'junegunn/fzf', { 'do': g:fzf_install }

PlugInstall! and PlugUpdate!

The installer takes the following steps when installing/updating a plugin:

  1. git clone or git fetch from its origin
  2. Check out branch, tag, or commit and optionally git merge remote branch
  3. If the plugin was updated (or installed for the first time)
    1. Update submodules
    2. Execute post-update hooks

The commands with the ! suffix ensure that all steps are run unconditionally.

Articles

Collaborators

License

MIT