Configure Nvim with Lua: Plugin/package management (3)

2 min read, Published on Sep 21, 2022

Nvim supports the installation of plugins, this helps to add functionality to our configuration, to install the plugins we must have a package manager. In this case I have selected Packer.nvim, since we are using Lua.

We can start to create the plugins.lua file.

  1. We move to the nvim folder:
  cd .config/nvim
  1. Then, we create the plugins.lua file (where we are going to put the configs and plugins to install).
  nvim lua/configs/plugins.lua
  1. We add this:
  local ensure_packer = function()
    local fn = vim.fn
    local install_path = fn.stdpath('data')..'/site/pack/packer/start/packer.nvim'
    if fn.empty(fn.glob(install_path)) > 0 then
      fn.system({'git', 'clone', '--depth', '1', 'https://github.com/wbthomason/packer.nvim', install_path})
      vim.cmd [[packadd packer.nvim]]
      return true
    end
    return false
  end

  local packer_bootstrap = ensure_packer()

  vim.cmd([[
    augroup packer_user_config
    autocmd!
    autocmd BufWritePost plugins.lua source <afile> | PackerCompile
    augroup end
  ]])

  local ok, packer = pcall(require, 'packer')
  if not ok then
    return
  end

  packer.init({
    display = {
      open_fn = function()
        return require('packer.util').float({ border = 'rounded' }) -- Using a floating window
      end
    }
  })

  return packer.startup(function(use)
    use 'wbthomason/packer.nvim'
    -- My plugins here
    use "nvim-lua/plenary.nvim" -- used by telescope, gitsigns, vgit, neogit, neo-tree.
    use 'lewis6991/impatient.nvim'

    -- Automatically set up your configuration after cloning packer.nvim
    -- Put this at the end after all plugins
    if packer_bootstrap then
      require('packer').sync()
    end
  end)
  1. ensure_packer function is for download packer.
  2. vim.cmd([[autogroup…]]) is for configure Neovim to automatically run :PackerCompile whenever plugins.lua is updated.
  3. To prevent unexpected errors you can use pcall().
  4. packer.init( is for custom initialization, in this case, I configured Packer to use a floating window for command outputs.
  5. The last section: return packer.startup(function(use)… is where add plugins.

Also, I installed Plenary.nvim, a useful lua functions used in lots of plugins, example:

I installed another important plugin: impatient.nvim. This plugin “Speed up loading Lua modules in Neovim to improve startup time”. You can verify the impatient.nvim performance

  1. To configure this plugin:
  nvim lua/configs/impatient.lua
  1. Add this content:
  local ok, impatient = pcall(require, "impatient")
  if not ok then
    return
  end

  impatient.enable_profile()
  1. Lastly, to use impatient, we need to call the impatient file in the main file (init.lua)
  nvim init.lua
  • Add this:
  require('configs.impatient')

We have the directory of folders like this:

  nvim
  ├─ init.lua
  └─ lua
      └─ configs
          └─ options
          |   └─ init.lua
          └─ keymaps.lua
          └─ plugins.lua
          └─ impatient.lua

Conclusion

Now you can install the plugins as you prefer. In next articles we will see some plugins that will help us to configure Nvim as our IDE. I hope it will be helpful for those who want to experiment.

Resources