Configure Nvim with Lua: Plugin/package management (3)
2 min read, Published on Sep 21, 2022Nvim 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.
- We move to the nvim folder:
cd .config/nvim
- Then, we create the plugins.lua file (where we are going to put the configs and plugins to install).
nvim lua/configs/plugins.lua
- 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)
- ensure_packer function is for download packer.
- vim.cmd([[autogroup…]]) is for configure Neovim to automatically run :PackerCompile whenever plugins.lua is updated.
- To prevent unexpected errors you can use pcall().
- packer.init( is for custom initialization, in this case, I configured Packer to use a floating window for command outputs.
- 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
- To configure this plugin:
nvim lua/configs/impatient.lua
- Add this content:
local ok, impatient = pcall(require, "impatient")
if not ok then
return
end
impatient.enable_profile()
- 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.