Getting started with Neovim

Last modified on 2019-08-24

Text-wrangling is a big part of unlocking the power of Linux and programming. With great power comes the need for a great text editor!

Let’s go!

I have used Vim in the past but barely scratched the surface of its features motherlode. 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!

0. Install

Neovim packages are available in Debian …

$ 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

1. Launch

Launch editor …

$ nvim

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 wrap your head around the concepts of modal editing and jumping between normal mode and insert mode.

Rather than downloading one of the many existing “cheatsheets”, I recommend making your own composed of commands you wish to commit to memory.

2. 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 tabstop=4               " number of columns occupied by a tab character
set softtabstop=4           " see multiple spaces as tabstops so <BS> does the right thing
set expandtab               " converts tabs to white space
set shiftwidth=4            " width for autoindents
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=80                   " set an 80 column border for good coding style
filetype plugin indent on   " allows auto-indenting depending on file type
syntax on                   " syntax highlighting

3. Colors

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

I use the default color scheme, which picks up colors I have defined in my ~/.Xresources. In my init.vim I add …

colorscheme default

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

$ mkdir ~/.config/nvim/colors

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
8

[ Fix! ] 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
tmux-256color
$ tput colors
256

4. Custom key mappings

I can create my own neovim commands using key maps. Because the editor doesn’t have enough key combinations to remember … 🐧

First I set a map leader key that I use to preface all my custom commands, which can be any key not already used by neovim (example: the comma key). I add to init.vim

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>

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 date+time 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.

5. Resources

Start with the built-in :Tutor and :help commands. Good stuff!

Following the tutor, the Vim Tips series was a big help in moving beyond simple editing tasks.

This article is a very clear explanation of the difference between buffers and windows and tabs in (neo)vim.

To dig deeper into the fundamentals: Learning the vi and Vim Editors by Arnold Robbins, Elbert Hannah, Linda Lamb, 7e (O’Reilly, 2008).

Happy hacking! 🐧