Getting started with Neovim

Last edited on 2022-02-06 Tagged under  #neovim   #linux 

I have used the Vim text editor in the past but barely scratched the surface of its bountiful features. A friend who is a passionate vim user recommended I try Neovim, which "strives to be a superset of Vim except for some intentionally-removed misfeatures ... [and] is built for users who want the good parts of Vim, and more".

Sounds good!

1. Install

Neovim packages are available in Debian/Mint/Ubuntu ...

$ sudo apt install neovim

Optional: Debian sets nano as the default editor for system tasks like visudo. If already comfortable using Vim, change the default editor to neovim using command update-alternatives ...

$ sudo update-alternatives --config editor

2. Launch

Launch editor ...

$ nvim


If you are a beginner not only to neovim but vim itself, the best place to start learning is the built-in tutorial. Type :Tutor and hit <Enter> to access. Spend a week getting comfortable with these oft-used commands, and the concepts of modal editing and jumping between normal mode and insert mode.

3. Configuration

On first launch the ~/.local/share/nvim/{shada,swap} directories are auto-generated.

Manually create a new ~/.config/nvim directory and a init.vim configuration file ...

$ mkdir ~/.config/nvim
$ touch ~/.config/nvim/init.vim

Options I set in my init.vim ...

set nocompatible            " disable compatibility to old-time vi
set showmatch               " show matching brackets.
set ignorecase              " case insensitive matching
set mouse=v                 " middle-click paste with mouse
set hlsearch                " highlight search results
set autoindent              " indent a new line the same amount as the line just typed
set number                  " add line numbers
set wildmode=longest,list   " get bash-like tab completions
set cc=88                   " set colour columns for good coding style
filetype plugin indent on   " allows auto-indenting depending on file type
set tabstop=4               " number of columns occupied by a tab character
set expandtab               " convert tabs to white space
set shiftwidth=4            " width for autoindents
set softtabstop=4           " see multiple spaces as tabstops so <BS> does the right thing

3.1 Colors

Neovim includes several built-in color schemes installed in /usr/share/nvim/runtime/colors. Enter :colorscheme <SPACE> <TAB> to view schemes available and :colorscheme NAME to enable.

Extra color schemes can be installed and made visible to :colorscheme. Create a colors directory to store schemes ...

$ mkdir ~/.config/nvim/colors

I use the Nord Vim color scheme in conjunction with the Nord theme for my terminal. After installing the theme via vim-plug (see below), I add ...

syntax on
colorscheme nord

I ran into the problem where the color scheme worked in the terminal but not inside a tmux session. Turns out tmux was not seeing the 256 color palette ...

$ tput colors

Add this setting to ~/.tmux.conf ...

set -g default-terminal "tmux-256color"

It is important to kill all existing tmux sessions to see the changes take effect. It is not enough to simply start a fresh session.

Colors now work as expected ...

$ echo $TERM
$ tput colors

Source: tmux.conf

3.2 Plugin manager

Extra features for the editor can be added and managed via a plugin manager. I use vim-plug.

Install ...

sh -c 'curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs'

Setup a directory to hold plugins, install plugins (example: nord-vim color scheme), and initialize ...

" specify directory for plugins
call plug#begin('~/.config/nvim/plugged')

Plug 'arcticicestudio/nord-vim'

" initialize plugin system
call plug#end()

3.3 Custom key mappings

Create custom editor commands using key maps.

Set a map leader key that is used to preface all custom commands. This can be any key not already used by the editor (example: the comma key) ...

" map leader
let g:mapleader = ','

Neovim has several modes and its possible to create a key map that works in any or all of them. My focus is on the normal (command) and insert (editing) modes.

To map keys that work only in normal mode, use the :nnoremap command. Example: A command to toggle the spell checker using ,s ...

nnoremap <leader>s :set invspell<CR>

Note: When invoking an ex command from a map, <CR> is added at the end.

To map keys that work only in insert mode, use the :inoremap command.

Example: A command to insert a datetime stamp using ,d ...

inoremap <leader>d <C-R>=strftime("%Y-%m-%dT%H:%M")<CR>

<C-R>= is used to insert the result of an expression at the cursor.

Source: init.vim

Thanks for reading! Read other posts?

» Next: Migrating away from a Google-hosted custom email address

« Previous: Generate a list of installed deb packages on one device (and install on another)