Configure Nvim with Lua: Set colorscheme (4)
3 min read, Published on Sep 22, 2022Another of the facilities that Neovim offers is that it has several types of colors, which can be chosen, and can be install plugins with other colorschemes. You can read about color schemes in the documentation:
nvim
:help colorscheme
To change between the different colorscheme that exist we can execute this command:
:colorscheme <name>
We change
:colorscheme darkblue
:colorscheme industry
In this way, we are changing the colors, but when we close neovim and reopen it we will not have the one we chose previously selected. But Neovim provides the possibility for that configuration to persist. Therefore, we will follow these steps:
- We move to the nvim folder:
cd .config/nvim
- Then, we will open the options file (where we have the basic configurations).
nvim lua/configs/options/init.lua
- We add this code and save it:
local options = {
-- ...others configs...
termguicolors = true -- Enables 24-bit RGB color
}
- Later, we create the colorscheme file.
nvim lua/configs/colorscheme.lua
- We add this code and save it:
local colorscheme = "gruvbox"
local ok, _ = pcall(vim.cmd, "colorscheme " .. colorscheme)
vim.o.background = "dark" -- or "light" for light mode
if not ok then
vim.notify("colorscheme " .. colorscheme .. " not found!")
return
end
- The colorscheme variable defines the name that we are going to assign.
- Then with the command vim.cmd the colorscheme is changed.
- In case the colorscheme does not exist, we show a message on the neovim command line.
- To use these settings, we need to call the colorscheme file in the main file (init.lua)
nvim init.lua
- Add this:
require('configs.colorscheme')
- In this case, since the colorscheme gruvbox does not exist, the message will appear. You can try others that come by default such as: darkblue, blue, desert, default, etc. I like gruvbox.nvim and it is the one we are going to configure.
- Then, we add the plugin gruvbox.nvim to the plugins.lua file (where we are going to put the plugins to install).
nvim lua/configs/plugins.lua
- We add this and save it:
-- ...others configs...
return packer.startup(function(use)
-- ...others plugins...
-- My colorscheme`
use 'ellisonleao/gruvbox.nvim'
-- ...others configs...
end)
- Install the packages:
:PackerInstall
- Close and open again, and you will see the results…
There are other popular colorschemes, such as:
We have the directory of folders like this:
nvim
├─ init.lua
└─ lua
└─ configs
└─ options
| └─ init.lua
└─ keymaps.lua
└─ plugins.lua
└─ impatient.lua
└─ colorscheme.lua
Conclusion
In this way we saw how the different colorschemes that neovim brings by default can be changed and we also saw how to configure another through a plugin. Now you can test other colorschemes. I hope it will be helpful for those who want to experiment.