Try   HackMD

Day of Learning: neovim 0.5.0

by Andreas Schneider

Introduction

Finally neovim 0.5.0 has been released.

This release represents ~4000 commits since v0.4.4, the previous non-maintenance release. Highlights include builtin support for Lanugage Server Protocol (LSP), new APIs for extended marks (with byte resolution tracking of changes) and buffer decorations, as well as vast improvements to lua as a plugin and configuration language. Experimental support for tree-sitter as a syntax engine is also included, building on the new core APIs for byte tracking and decorations.

https://github.com/neovim/neovim/commit/a5ac2f45ff84a688a09479f357a9909d5b914294

Till now your config was going to init.nvim. As Vimscript is an interpredted language and slow, neovim supports lua for config files now (init.lua). This makes a lot of stuff faster. However vimscript support will not go away.

A lot of modules have been rewritten in lua in the meantime. Below is a summary of cool new features I started to use.

My config files:
https://git.cryptomilk.org/users/asn/dotfiles.git/

I've moved my neovim config to lua already!

Package manager

There are a lot of package manager out there. However I think the best one is Packer now:

https://github.com/wbthomason/packer.nvim

Mapleader

If you don't have a mapleader set yet, add one! I use <space>:

let mapleader = ' '

or in lua:

vim.g.mapleader = ' '

We will see later how to use it.

Which-key

The must-have plugin! Displays which keys are available with the mapleader.

use 'https://github.com/folke/which-key.nvim'

https://github.com/folke/which-key.nvim

Git

gitcommit

I used ftplugin/gitcommit.vim for a long time to show the diff you're committing. This isn't needed anymore as there is:

git commit -v

To make it the default you can do:

git config --global commit.verbose true

gitsigns

https://github.com/lewis6991/gitsigns.nvim

I want to see what lines changed in a neovim buffer. You can display this using

    use {
        'lewis6991/gitsigns.nvim', requires = { 'nvim-lua/plenary.nvim' },
        config = function() require('gitsigns').setup() end
    }

The nice thing is you can jump from one hunk to the next using [c and ]c

If you need a quick git annotate for one line, you can do that with: <leader>hb. If you configured comma as the mapleader you type in the command mode ,hb

Tree-sitter

This is one of the new feature in neovim 0.5.0! Tree-sitter is a parser generator tool and an incremental parsing library. It can build a concrete syntax tree for a source file and efficiently update the syntax tree as the source file is edited.

https://github.com/nvim-treesitter/nvim-treesitter

    use {
        'nvim-treesitter/nvim-treesitter',
        run = ':TSUpdate',
        config = function()
            require('nvim-treesitter.configs').setup {
                ensure_installed = "maintained",

                highlight = {
                    enable = true,       -- false will disable the whole
                                         -- extension
                },

                rainbow = {
                  enable = true,
                  extended_mode = true,  -- Highlight also non-parentheses
                                         -- delimiters, boolean or table:
                                         -- lang -> boolean
                  max_file_lines = 2000, -- Do not enable for files with
                                         --more than 1000 lines, int
                }
            }
        end
    }

Code completion

neovim gained language server support. This allows you to talk to a language server for code completion and also showing programming errors directly in the buffer.

LSP

You can also use the build in LSP support. For this you need lsp configs and an completion engine

https://github.com/nvim-lua/completion-nvim
https://github.com/neovim/nvim-lspconfig
https://github.com/ray-x/lsp_signature.nvim

My lsp configuration is here (clangd, rls, python-language-server):
https://git.cryptomilk.org/users/asn/dotfiles.git/tree/nvim/.config/nvim/lua/lsp.lua

Syntax highlighting

nvim-treesitter

neovim 0.5.0 has support for tree-sitter. This allows to do better syntax highlighting. You need configureation files for that.

    use {
        'nvim-treesitter/nvim-treesitter',
        run = ':TSUpdate',
    }

Next you need to configure it:

require('nvim-treesitter.configs').setup {
    ensure_installed = "maintained",

    highlight = {
        enable = true,              -- false will disable the whole extension
    },

    rainbow = {
      enable = true,
      extended_mode = true, -- Highlight also non-parentheses delimiters, boolean or table: lang -> boolean
      max_file_lines = 2000, -- Do not enable for files with more than 1000 lines, int
    }
}

rainbow paranthesis

Tree-sitter also has support for rainbow paranthesis if you have a plugin which can handle it:

use 'p00f/nvim-ts-rainbow'

Shared session data

neovim allows you to store data in a shared database. This means several neovim instances can access this shared data and use it e.g. to copy/paste stuff or store data like cursor positions in a file.

Setting up shada:

--  ! - Save and restore global variables (their names should be without lowercase letter).
--  ' - Specify the maximum number of marked files remembered. It also saves the jump list and the change list.
--  < - Maximum of lines saved for each register. All the lines are saved if this is not included, <0 to disable pessistent registers.
--  % - Save and restore the buffer list. You can specify the maximum number of buffer stored with a number.
--  / or : - Number of search patterns and entries from the command-line history saved. vim.o.history is used if it’s not specified.
--  f - Store file (uppercase) marks, use 'f0' to disable.
--  s - Specify the maximum size of an item’s content in KiB (kilobyte).
--      For the viminfo file, it only applies to register.
--      For the shada file, it applies to all items except for the buffer list and header.
--  h - Disable the effect of 'hlsearch' when loading the shada file.
vim.o.shada             = [[!,'1000,<10000,s500,:100,/100,h]]

See also :help mksession and :help sessionoptions

Resorting the cursor position

If you reopen a file you and want to go to the cursor position as before, use:

https://github.com/ethanholz/nvim-lastplace

Additional resources

https://github.com/nanotee/nvim-lua-guide/
https://github.com/rockerBOO/awesome-neovim