Configure Nvim with Lua: Add other features (10)
3 min read, Published on Oct 17, 2022Today we going to see a some plugins that provide us with excellent features, we going to see how to configure:
- Pairings for "", ”, [], (), etc.
- Color highlighter.
- Indentation guides to all lines.
- To format code.
cd .config/nvim
Install and configure the plugins
You can see some plugins here awesome-neovim. I selected this plugins:
- The first step: we add the plugins to the plugins.lua file.
nvim lua/configs/plugins.lua
- We add this and save it:
-- ...others configs...
return packer.startup(function(use)
-- ...others plugins...
-- Utils
use("windwp/nvim-autopairs") -- "", '', {}, [], ()
use("norcalli/nvim-colorizer.lua") -- colorizer the colors
use("lukas-reineke/indent-blankline.nvim") -- adds indentation guides to all lines (including empty lines)
use("jose-elias-alvarez/null-ls.nvim")
-- ...others configs...
end)
- Install the packages:
:PackerInstall
- Later, we create the nvim-autopairs file.
nvim lua/configs/nvim-autopairs.lua
- We add this code and save it:
local ok, nvim_autopairs = pcall(require, "nvim-autopairs")
if not ok then
return
end
nvim_autopairs.setup({
fast_wrap = {
map = "<M-e>",
chars = { "{", "[", "(", '"', "'" },
pattern = [=[[%'%"%)%>%]%)%}%,]]=],
end_key = "$",
keys = "qwertyuiopzxcvbnmasdfghjkl",
check_comma = true,
highlight = "Search",
highlight_grey = "Comment",
},
})
- We create the nvim-colorizer file.
nvim lua/configs/nvim-colorizer.lua
- We add this code and save it:
local ok, colorizer = pcall(require, "colorizer")
if not ok then
return
end
-- you can add other filetypes
colorizer.setup({
"scss",
"css",
"html",
"javascript",
"typescript",
"svelte",
html = {
mode = "foreground",
},
})
- We create the indent_blankline file.
nvim lua/configs/indent-blankline.lua
- We add this code and save it:
local ok, indent_blankline = pcall(require, "indent_blankline")
if not ok then
return
end
indent_blankline.setup({
show_current_context = true,
})
- We create the null-ls file.
nvim lua/configs/null-ls.lua
- We add this code and save it:
local ok, null_ls = pcall(require, "null-ls")
if not ok then
return
end
local formatting = null_ls.builtins.formatting
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
null_ls.setup({
sources = {
formatting.stylua,
formatting.prettier.with({
filetypes = {
"html",
"json",
"yaml",
"markdown",
"md",
"svelte",
"javascript",
"typescript",
"css",
"scss",
},
}),
},
-- you can reuse a shared lspconfig on_attach callback here
on_attach = function(client, bufnr)
if client.supports_method("textDocument/formatting") then
vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
vim.api.nvim_create_autocmd("BufWritePre", {
group = augroup,
buffer = bufnr,
callback = function()
vim.lsp.buf.format({ bufnr = bufnr }) -- this is for 0.8v
-- vim.lsp.buf.formatting_sync() -- you should use this for 0.7 or minor
end,
})
end
end,
})
In my case only use formatting for lua and prettier. You should install this:
For Lua (you can review how install this package in your OS):
brew install stylua
- For prettier:
npm install -g prettier
- To use these settings, we need to call the configurations files in the main file (init.lua)
nvim init.lua
- Add this:
-- ...
require("configs.nvim-autopairs")
require("configs.nvim-colorizer")
require("configs.indent-blankline")
require("configs.null-ls")
And that’s it!! You have already configured other functionalities. Now, you can enjoy greater comfort when working.
Directory
We have the directory of folders like this:
nvim
├─ init.lua
└─ lua
└─ configs
└─ options
| └─ init.lua
└─ keymaps.lua
└─ plugins.lua
└─ impatient.lua
└─ colorscheme.lua
└─ nvim-tree.lua
└─ lualine.lua
└─ bufferline.lua
└─ telescope.lua
└─ cmp.lua
└─ lsp.lua
└─ treesitter.lua
└─ gitsigns.lua
└─ diffview.lua
└─ project.lua
└─ alpha.lua
└─ nvim-autopairs.lua
└─ nvim-colorizer.lua
└─ indent-blankline.lua
└─ null-ls.lua
Conclusion
In this article we learned how to install and configure the plugins to improve our comfort when working.