A Neovim setup using Lua
Neovim configuration with Lua
A Neovim configuration using Lua, with the minimal number of pluggins I need for programming. Different language servers available through the LSP protocol provide code completion and analysis.
This readme exist so I don't have to remember how to do all these things when setting up a new machine.
Setting up
Notice that Neovim doesn't have a full release version number yet. This is because it is undergoing rapid development and older versions could be incompatible with some plugins. The latest version can always be installe using the instructions below depending on your operating system.
Linux
# For stable versions
sudo snap install --beta nvim --classic
For nightly versions
sudo snap install --edge nvim --classic
We also need to install the node package manager npm since most language servers are installed that way.
sudo apt install npm
MacOS
Assume brew is installed, then installing Neovim is straighforward:
# For stable version
brew install neovim
for nightly version
brew install --HEAD neovim
To update
brew reinstall neovim
Additionally, you may need to configure the Option key to behave like Alt. In iTerm2, this can be done in Preferences -> Profiles -> Keys. Change the left option behaviour to Esc+. For kitty, you need to set macosoptionas_alt left (defualt is no) in the terminal's config file. Restarting the terminal (Command + Q, then restart) is required for this to take effect.
Installing the configuration
Clone the repo into Neovim's installation folder (usually /home/<usr>/.config/nvim):
git clone https://github.com/miltonllera/neovim-lua-config ~/.config/nvim cd ~/.config/nvim
This will create a folder with the configuration with the following structure is as follows:
|- lua | |- lsp/ | |- plugins/ | |- keymaps.lua | |- options.lua | |- plugins.lua | |- theme.lua | \- utils.lua |- plugin/ \- init.lua
This structure is important since Lua will not load files that are not located inside lua. The file init.lua loads all the modules located inside this folder to set the configuration. Most of the names are self explanatory. The most important file here is plugins.lua, which is the module that loads the relevant plugins. Some of the most important plugins are:
lazy: Manage the plugins.lspconfig: provides a client for the different language servers using the Language Server Protocol (LSP).cmp: Auto-complete functionality. Recommended by the core Neovim team.treesitter: Syntax highlighting and other functionality.NvimTree: File explorer written in Lua.fugitive: The best plugin for git.gitsigns: Git gutter highlighting and hunk management in buffer.telescope: Fuzzy finder.lualine: A status line written in Lua which is similar tovim-airline.
plugins table:
{
'<author>/<plugin-repo>', config = function() require('<plugin-name>').setup() end,
}
This will load a plugin with it's standard configuration. Custom configs for several plugins are in the plugin folder and can be accessed as:
{
'<author>/<plugin-repo>', config = function() require('plugin/<plugin-name>') end,
}
Notice that the file type is omitted from this call. See the lazy repo for more options such as forcing plugin loading at startup (i.e. layz= false or setting dependencies).
Auto-completion
The auto-complete functionality is achieved by using nvim-cmp to attach the relevant language servers to the buffers containing code. Most servers only require that the on attach function is specified so that different motions are available. Currently, the common function to attach a server to a buffer is located in lua/lsp/utils.lua . It will enable common key mappings for all language servers to display code completion.
The second part is installing the language servers themselves (described below) and enabling them. This is achieved using mason.nvim, nvim-lspconfig and mason-lspconfig.nvim.
Installing the language servers
Binaries for each language servers must be installed from their relevant repo. Most servers are installed using npm install, but others like clangd and sumneko for Lua require more involved procedures. Here is a list of servers and installation methods. These should work both on bash and zsh.
- Bash: bashls
npm i -g bash-language-server
- C/C++: clangd
sudo apt-get install clangd-13
Then we must make it the default clangd (example with clangd-13):
sudo update-alternatives --install /usr/bin/clangd clangd /usr/bin/clangd-13 100
NOTE: On MacOS clang is installed through XCode, and you probably don't need to do anything else. You can check this by running clang --version from the terminal.
- Docker: dockerls
npm i -g dockerfile-language-server-nodejs
- Julia: julials
julia --project=~/.julia/environments/nvim-lspconfig -e 'using Pkg; Pkg.add("LanguageServer")'
- JSON: jsonls
npm i -g vscode-langservers-extracted
- Lua: lua_ls
sumnekolua server is now deprecated. The version available now is luals which is much easier to install (and also doesn't seem to funnel telemetry data to a remote server by default).
brew install lua-language-server
- Python: pyright:
npm i -g pyright
- YAML: yamlls
yarn to work
yarn global add yaml-language-server
For MacOS use brew:
brew install yaml-language-server
If a module complains about the verion of node being too old (pyright will do this), then run the following:
sudo npm cache clean -f sudo npm install -g n sudo n stable Make sure to use the -g on all npm installs, otherwise the server won't be found.
Some further notes
Inline error messages are disabled in the current configuration. They create a lot of clutter. To enable them back, comment the code on line 34 of lua/options.lua. This is a nvim option related to it's lsp interface, not something provided by the servers themselves.
Web-dev Icons
To visualize fancy icons and separators, a patched font must be installed. Nerd Fonts has many already patched and offers instructions on how to create new ones (I don't recommend). To install a patched font follow these instructions:
- Head to the repo and download the font. I use Robot Mono.
- Copy the file to the relevant folder:
~/.local/share/fonts/. - MacOS: /Library/Fonts'. - Change the font in the terminal emulator's settings to the patched font.
Nerd Fonts with Kitty
If using kitty as default terminal, then the procedure above won't work. First, kitty does not support non-monospaced fonts due to how it renders text. Second, the fonts cannot be patched. In fact, kitty takes care of patching on it's own which is great. To install the fonts follow the instructions in this blog, which are straighforward.
TL;DR for MacOS:
- Download and install the fonts and put the file
Symbols-2048-em Nerd Font Complete.tff(or whatever subset you decide to use) in theLibrary/Fonts/folder for system wide use, or the local variant. - If the glyphs aren't displayed by default, then they can be specified manually by following the instructions.
- Refresh the fonts cache.
Attributions
I've stolen code from different sources which means it might be hard to acknowledge all of them explicitly though most of them are from the associated plugin's documentation.