DZHG  / Neovim Quick Bites EP02: Plugins
Table of contents

Packer.nvim

packer is a plugin/package management tool for Neovim.

You can write a ‘spec’ like lua file to manage all Neovim plugins.

Installation

Clone the latest packer repository to a folder as a ‘start’ package:

git clone --depth 1 https://github.com/wbthomason/packer.nvim\
 ~/.local/share/nvim/site/pack/packer/start/packer.nvim

Usage

You can write a plugin spec file in lua to manage all plugins. For example, you can create a plugins.lua in ~/.config/nvim/lua/:

return require('packer').startup(function()
  -- Packer can manage itself
  use 'wbthomason/packer.nvim'
  
  use {
    "nvim-neo-tree/neo-tree.nvim",
    branch = "v2.x",
    requires = { 
      "nvim-lua/plenary.nvim",
      "kyazdani42/nvim-web-devicons", -- not strictly required, but recommended
      "MunifTanjim/nui.nvim",
    }
  }
end)

Everytime, plugins.lua is modified, you need to run PackerSync.

:PackerSync

It will automatically sync all plugins.

packer can also run code after the plugin loaded by config option in use:

use {
    "nvim-neo-tree/neo-tree.nvim",
    branch = "v2.x",
    requires = { 
      "nvim-lua/plenary.nvim",
      "kyazdani42/nvim-web-devicons", -- not strictly required, but recommended
      "MunifTanjim/nui.nvim",
    },
    config = function()
    	require("neotree_keymap")
    end
}

~/.config/nvim/lua/neotree_keymap.lua:

vim.api.nvim_set_keymap('n', '<leader>t', ':NeoTreeRevealToggle<CR>', { noremap = true })

The keymap doesn’t work if directly put in config function. It has to load from a file. See #758