Neovim Configuration for Beginners

Neovim is a fork of Vim aiming to improve the codebase. Learn the optimal Neovim configuration for beginners.

Written by Rahul H.
Published on Feb. 22, 2023
Image: Shutterstock / Built In
Image: Shutterstock / Built In
Brand Studio Logo

If you thought Vim was an old, obsolete text editor with a very high learning curve and limited functionality, you’d be wrong.

Vim is a very fast, cross-platform, free, open-source, customizable, extensible, popular text editor that many use. Once you start using it, you are going to hate every other text editor out there.

What Is Neovim?

Neovim is a text editor forked from Vim that’s designed to improve the codebase to create a better user experience and improve plugin and API implementation.  

Neovim is a fork of Vim aiming to improve the codebase for easier API implementation, a better user experience and plugin implementations. 

 

How To Configure Neovim

Example of a Neovim configuration.
Example of a Neovim configuration. | Image: Rahul H.

Even though the default version of Neovim is still a good text editor, it lacks some features like code completion. We’ll fix these issues through Neovim’s configuration file, which is located at ~/.config/nvim/init.vim.

set nocompatible            " disable compatibility to old-time vi
set showmatch               " show matching 
set ignorecase              " case insensitive 
set mouse=v                 " middle-click paste with 
set hlsearch                " highlight search 
set incsearch               " incremental search
set tabstop=4               " number of columns occupied by a tab 
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   "allow auto-indenting depending on file type
syntax on                   " syntax highlighting
set mouse=a                 " enable mouse click
set clipboard=unnamedplus   " using system clipboard
filetype plugin on
set cursorline              " highlight current cursorline
set ttyfast                 " Speed up scrolling in Vim
" set spell                 " enable spell check (may need to download language package)
" set noswapfile            " disable creating swap file
" set backupdir=~/.cache/vim " Directory to store backup files.

Add these files to your init.vim. Neovim and Vim use vimscript for the config file. Comment statements (“) aside, it’s self-explanatory what each line does. But I don’t recommend using the commented commands, since I do like the default behavior for them. Having a swap file is a good idea. And I don’t want to change the swap file location, which is the current directory by default.

More on Software Engineering: 5 Ways to Remove Characters From a String in Python

 

Neovim Configuration for Plugins

Now the real play comes while using plugins. Using a plugin manager is the best way to manage the plugins. I recommend vim-plug as a plugin manager, and I’d suggest using vim-plug to install the plugins.

call plug#begin(“~/.vim/plugged”)
 “ Plugin Section
 Plug 'dracula/vim'
 Plug 'ryanoasis/vim-devicons'
 Plug 'SirVer/ultisnips'
 Plug 'honza/vim-snippets'
 Plug 'scrooloose/nerdtree'
 Plug 'preservim/nerdcommenter'
 Plug 'mhinz/vim-startify'
 Plug 'neoclide/coc.nvim', {'branch': 'release'}
call plug#end()

Add the above line to init.vim and run PlugInstall in command mode to install the above plugins. You can run PlugUpdate when you want to update the plugins.

8 Neovim Plugins to Know

  1. Dracula: A really good theme for Neovim.
  2. Nerdcommenter: An easy way for commenting outlines.
  3. Nerdtree: A file explorer for neovim. Netrw comes as default for neovim.
  4. Vim-devicons: Devicon support for nerdtree.
  5. Ultisnips: ASnippets engine.
  6. Vim-snippets: A collection of snippets
  7. Vim-startify: A really handy start page with lots of customizations.
  8. Coc: A fast code completion engine
A guide on how to set up Neovim from scratch. | Video: NeuralNine

More on Software Engineering: What Is a Compiler?

 

Neovim Configuration for Keybindings

With some extra keybindings and some customization, these are real gems.

" move line or visually selected block - alt+j/k
inoremap <A-j> <Esc>:m .+1<CR>==gi
inoremap <A-k> <Esc>:m .-2<CR>==gi
vnoremap <A-j> :m '>+1<CR>gv=gv
vnoremap <A-k> :m '<-2<CR>gv=gv
" move split panes to left/bottom/top/right
 nnoremap <A-h> <C-W>H
 nnoremap <A-j> <C-W>J
 nnoremap <A-k> <C-W>K
 nnoremap <A-l> <C-W>L
" move between panes to left/bottom/top/right
 nnoremap <C-h> <C-w>h
 nnoremap <C-j> <C-w>j
 nnoremap <C-k> <C-w>k
 nnoremap <C-l> <C-w>l

" Press i to enter insert mode, and ii to exit insert mode.
:inoremap ii <Esc>
:inoremap jk <Esc>
:inoremap kj <Esc>
:vnoremap jk <Esc>
:vnoremap kj <Esc>

Below are some common Neovim keybindings to know:

  • inoremap: maps the key in insert mode
  • nnoremap: maps the key in normal mode
  • vnoremap: maps the key in visual mode
  • <C>: Represents the control key
  • <A>: Represents the alt key.

Now, let’s add some more configurations.

" open file in a text by placing text and gf
nnoremap gf :vert winc f<cr>
" copies filepath to clipboard by pressing yf
:nnoremap <silent> yf :let @+=expand('%:p')<CR>
" copies pwd to clipboard: command yd
:nnoremap <silent> yd :let @+=expand('%:p:h')<CR>
" Vim jump to the last position when reopening a file
if has("autocmd")
  au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$")
    \| exe "normal! g'\"" | endif
endif

You may find something interesting in this. Autocommand (au) helps to run the command on startup of Vim.

If you want to know more about anything on Vim, just use :help name. All of these configurations are also applicable for Vim. Instead of ~/.config/nvim/init,vim the config file will be ~/.vimrc.

There are lots of possibilities with Neovim, it’s up to you how you want to set it up.

Explore Job Matches.