vim-plug

repository·master·Indexed 12 days ago

https://github.com/junegunn/vim-plug

A minimalist, single-file plugin manager for Vim and Neovim featuring parallel installation, shallow clones, and on-demand loading. Supports plugin configuration via options like 'do' for post-update hooks and 'on' or 'for' for lazy loading, and provides a Lua interface for Neovim users.

Tokens
3.5K
Snippets
10
Records
12
Agent score
49%

What's inside vim-plug

  1. Handle shell characters in the `do` option

    master

    When writing shell commands inline within the do option, you must escape BARs (|) and double-quotes (") because they are otherwise interpreted as command separators or comment starts.

    To avoid complex escaping, you can store the command in a variable first.

    " Escaping required for inline commands
    Plug 'junegunn/fzf', { 'do': 'yes \| ./install' }
    
    " Recommended: Use a variable to avoid escaping
    let g:fzf_install = 'yes | ./install'
    Plug 'junegunn/fzf', { 'do': g:fzf_install }
  2. Install vim-plug for Neovim

    master

    To install vim-plug for Neovim, download plug.vim and place it in the appropriate site/autoload directory.

    Unix/Linux:

    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):

    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):

    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
    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'
  3. Install vim-plug for Vim

    master

    To install vim-plug for Vim, download plug.vim and place it in your autoload directory.

    Unix/macOS:

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

    Windows (PowerShell):

    iwr -useb https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim |`\
        ni $HOME/vimfiles/autoload/plug.vim -Force
    curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
        https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  4. Basic Usage of vim-plug

    master

    To use vim-plug, add a plugin section to your ~/.vimrc (Vim) or ~/.config/nvim/init.vim (Neovim) using the following pattern:

    1. Start the section with call plug#begin().
    2. List plugins using the Plug command.
    3. End the section with call plug#end().

    Note: plug#end() automatically executes filetype plugin indent on and syntax enable. If you wish to disable these, do so after the plug#end() call.

    After configuring, use the following commands:

    • :PlugInstall: Install plugins.
    • :PlugUpdate: Install or update plugins.
    • :PlugDiff: Review changes from the last update.
    • :PlugClean: Remove plugins not present in your configuration.
    call plug#begin()
    
    " List your plugins here
    Plug 'tpope/vim-sensible'
    
    call plug#end()
  5. Configure plugins with Plug options

    master

    When calling Plug, you can pass an options dictionary to customize plugin behavior. Common options include:

    • branch, tag, commit: Specify a specific branch, tag, or commit hash.
    • rtp: Specify a subdirectory within the plugin that contains the Vim plugin.
    • dir: Install the plugin in a custom directory.
    • as: Use a different name for the plugin.
    • do: A post-update hook (can be a shell command string or a lambda/function reference).
    • on: On-demand loading triggered by specific Commands or <Plug>-mappings.
    • for: On-demand loading triggered by specific file types.
    • frozen: Prevents the plugin from being removed or updated unless explicitly specified.
  6. Use vim-plug in Lua (Neovim)

    master

    In Neovim, you can use vim-plug within an init.lua file by accessing the Vim functions via vim.fn.

    Example pattern:

    local Plug = vim.fn['plug#']
    
    vim.call('plug#begin')
    
    Plug('junegunn/seoul256.vim')
    Plug('fatih/vim-go', { ['tag'] = '*' })
    Plug('preservim/nerdtree', { ['on'] = 'NERDTreeToggle' })
    
    vim.call('plug#end')
    
    -- Load color schemes after plug#end()
    vim.cmd('silent! colorscheme seoul256')
    local vim = vim
    local Plug = vim.fn['plug#']
    
    vim.call('plug#begin')
    
    Plug('junegunn/seoul256.vim')
    Plug('https://github.com/vim-easy-align/vim-easy-align.git')
    Plug('fatih/vim-go', { ['tag'] = '*' })
    Plug('neoclide/coc.nvim', { ['branch'] = 'release' })
    Plug('junegunn/fzf', { ['dir'] = '~/.fzf' })
    Plug('junegunn/fzf', { ['dir'] = '~/.fzf', ['do'] = './install --all' })
    Plug('junegunn/fzf', { ['do'] = function()
      vim.fn['fzf#install']()
    end })
    Plug('nsf/gocode', { ['rtp'] = 'vim' })
    Plug('preservim/nerdtree', { ['on'] = 'NERDTreeToggle' })
    Plug('tpope/vim-fireplace', { ['for'] = 'clojure' })
    Plug('~/my-prototype-plugin')
    
    vim.call('plug#end')
    
    vim.cmd('silent! colorscheme seoul256')
  7. Configure post-update hooks with the `do` option

    master

    Use the do option to execute tasks (like building or installing dependencies) automatically after a plugin is installed or updated. The hook runs inside the plugin's directory and only executes if the repository has changed.

    Supported formats:

    • Shell command: A string representing a shell command (e.g., 'make').
    • Vim command: A string starting with : (e.g., ':GoInstallBinaries').
    • Lambda expression: A Vim function passed as a lambda (e.g., { -> fzf#install() }).
    • Vim function reference: A function that accepts a dictionary argument containing plugin metadata.

    To force these hooks to run even if the plugin hasn't changed, use PlugInstall! or PlugUpdate!.

    " Shell command
    Plug 'Shougo/vimproc.vim', { 'do': 'make' }
    
    " Vim command
    Plug 'fatih/vim-go', { 'do': ':GoInstallBinaries' }
    
    " Lambda expression
    Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
    
    " Vim function with metadata
    function! BuildYCM(info)
      " info contains: name, status ('installed', 'updated', 'unchanged'), and force
      if a:info.status == 'installed' || a:info.force
        !./install.py
      endif
    endfunction
    Plug 'ycm-core/YouCompleteMe', { 'do': function('BuildYCM') }
  8. Implement on-demand loading of plugins

    master

    You can delay plugin loading using the on and for options. This is a last-resort optimization for plugins that do not already implement lazy loading via :help autoload.

    • on: Loads the plugin when specific Vim commands are invoked. Can accept a single command string or a list of strings.
    • for: Loads the plugin when specific file types are opened. Can accept a single filetype string or a list of strings.
    • Combined: You can use both on and for to require both conditions to be met.
    • Disable loading: Pass an empty list [] to either option to prevent the plugin from loading automatically. You can then load it manually using plug#load(NAMES...).
    " Load on command
    Plug 'preservim/nerdtree', { 'on': 'NERDTreeToggle' }
    Plug 'junegunn/vim-github-dashboard', { 'on': ['GHDashboard', 'GHActivity'] }
    
    " Load on filetype
    Plug 'tpope/vim-fireplace', { 'for': 'clojure' }
    Plug 'kovisoft/paredit', { 'for': ['clojure', 'scheme'] }
    
    " Load on both command and filetype
    Plug 'junegunn/vader.vim',  { 'on': 'Vader', 'for': 'vader' }
    
    " Disable automatic loading
    Plug 'some/plugin', { 'on': [] }
  9. Reference: vim-plug Global Options

    master

    Configure the behavior of vim-plug using these global variables:

    FlagDefaultDescription
    g:plug_threads16Default number of threads to use
    g:plug_timeout60Time limit of each task in seconds (Ruby & Python)
    g:plug_retries2Number of retries in case of timeout (Ruby & Python)
    g:plug_shallow1Use shallow clone
    g:plug_window-tabnewCommand to open plug window
    g:plug_pwindowvertical rightbelow newCommand to open preview window in PlugDiff
    g:plug_url_formathttps://git::@github.com/%s.gitprintf format to build repo URL (Only applies to subsequent Plug commands)
    | 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`     | `-tabnew`                         | Command to open plug window |
    | `g:plug_pwindow`     | `vertical rightbelow new`         | 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) |
  10. Reference: vim-plug Commands

    master

    The following commands are available for managing plugins:

    CommandDescription
    PlugInstall [name ...] [#threads]Install plugins
    PlugUpdate [name ...] [#threads]Install or update plugins
    PlugClean[!]Remove unlisted plugins (use ! to skip confirmation prompt)
    PlugUpgradeUpgrade vim-plug itself
    PlugStatusCheck the status of plugins
    PlugDiffExamine changes from the previous update and the pending changes
    PlugSnapshot[!] [output path]Generate script for restoring the current snapshot of the plugins
    | 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 |
  11. Reference: vim-plug Keybindings

    master

    When the vim-plug window is open, you can use the following keybindings:

    • D: PlugDiff
    • S: PlugStatus
    • R: Retry failed update or installation tasks
    • U: Update plugins in the selected range
    • q: Abort the running tasks or close the window

    Inside :PlugStatus window:

    • L: Load plugin

    Inside :PlugDiff window:

    • X: Revert the update
    - `D` - `PlugDiff`
    - `S` - `PlugStatus`
    - `R` - Retry failed update or installation tasks
    - `U` - Update plugins in the selected range
    - `q` - Abort the running tasks or close the window
    - `:PlugStatus`
        - `L` - Load plugin
    - `:PlugDiff`
        - `X` - Revert the update
  12. Force unconditional plugin installation or updates

    master

    By default, vim-plug only runs installation steps (like updating submodules and executing do hooks) if the plugin has changed.

    To force all steps—including git clone/fetch, branch checkout, submodule updates, and post-update hooks—to run regardless of the current state, use the bang-versions of the commands:

    • PlugInstall!
    • PlugUpdate!