This page looks best with JavaScript enabled

Switching from VIM to NeoVIM

 ·  ☕ 7 min read · 👀... views
    🏷️

What I use VIM for?

I am not a big fan of “Use everything in Terminal”. Not because I am not comfortable with terminals and TUI, but I understand and miss the lacking features of GUI in a TUI in this twenty first century. Let’s talk about the details and causes for this on some other post for now.

I use VI/VIM only when I had to edit a file quickly and I am in a terminal for some work. So, for this reason, I do not need to turn VIM into an IDE like IntelliJ of Visual Studio. All I want is a very simple editor with just the basic information I need to make my editing comfortable while I am in a terminal.

So, this are the features I need in a text editor:

  • (Obviously) Vim keybindings.
  • Syntax highlighting with as much languages supported as possible. Because I will not know what type of file I have to edit!
  • Very simple configuration overhead. I want to manage just a single file with easy setup. As you might see, I will be trying to avoid some configuration paradigm. So that, when I am in a new setup, I will install VIM (from now, NeoVIM), download and put the file where it is needed and I am all set.
  • Autocompletion for popular languages.

Why I wanted to switched to NeoVIM?

There are tons of reasons anyone can find in online to switch from VIM to NeoVIM. So in this section, I am not going to restate or go over those. I am pointing just some key points that I must had to say:

  1. Better API and well documented. So if I ever wanted to debug a plugin written for NeoVIM, or wanted to contribute to my daily used text editor, I will have the benefits.
  2. Extensions support any language! (which was not the case in VIM where you’ll have to learn vim script)
  3. Front and Backend is decoupled. This opens possibility to embed NeoVIM anywhere you want!
  4. As I can see in the internet, the world is moving on. So why not me.

Lets switch!

Before starting the switch, here is the old vimrc I use.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
set viminfo+=n~/.vim/viminfo

" vim-plug
if empty(glob('~/.vim/autoload/plug.vim'))
  silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
    \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif

call plug#begin('~/.vim/plugged')

" For sudo edit and others
Plug 'tpope/vim-eunuch'

" For powerline bar
Plug 'itchyny/lightline.vim'

" Linting
Plug 'dense-analysis/ale'

" show +/- in line numbers of a file under git
Plug 'airblade/vim-gitgutter'

" Copy to system clipboard (Requires xsel)
Plug 'christoomey/vim-system-copy'

" Initialize plugin system
call plug#end()

" Required for lightline.vim
set noshowmode
set laststatus=2
set mouse=a

" Other configs goes here
" set number and bla bla

Now, first things first. Install NeoVIM. You can look over their website to know how to install in your operating system. As I am using arch based distro, the command is:

1
yay -S neovim

I found most of the “switch to NeoVIM” blog posts mentioning that it has no problem starting up with the existing vimrc file. But I got a lot of error messages (I solve some of them, but it became cumbersome at a moment, see image below) starting up NeoVIM after installation. As I had time, I preferred to manually move all the configurations from the old vimrc to NeoVIM.

NeoVIM error with vimrc file

VIM basically uses ~/.vimrc file for its startup. I used to use a trick to move this .vimrc file from ~(home) to ~/.config/vimrc. The trick is to put these 2 lines in any of your init files (in any linux OS). I put them in my ~/.zprofile.

1
2
3
# VIM
export VIMINIT='source $MYVIMRC'
export MYVIMRC='$HOME/.config/vimrc'

This works because, VIM uses VIMINIT environment variable to execute. If VIMINIT is not set in your path, then it looks for ~/.vimrc. This is also same for NeoVIM (I am not 100% sure about this though).

But NeoVIM follows the XDG Base Directory Specification out of the box. So it used ~/.config/nvim/init.vim file for its init configuration. I do not need those 2 lines anymore.

As I am using vim-Plug for plugin management, I need to set it up first. In vimrc, I used the auto-installation code snippet if found here. What it does, is it automatically installs vim-plug if it is not already installed in VIM. Vim-Plug does not have any snippet in their tips page for NeoVIM. After digging a bit, I found this issue where some people suggested similar snippets. After some inspection, I picked the snippet below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
let plug_install = 0
let autoload_plug_path = stdpath('config') . '/autoload/plug.vim'
if !filereadable(autoload_plug_path)
    silent exe '!curl -fL --create-dirs -o ' . autoload_plug_path . 
        \ ' https://raw.github.com/junegunn/vim-plug/master/plug.vim'
    execute 'source ' . fnameescape(autoload_plug_path)
    let plug_install = 1
endif
unlet autoload_plug_path

call plug#begin('~/.config/nvim/plugins')
... plugins ...
call plug#end()

if plug_install
    PlugInstall --sync
endif
unlet plug_install

... the rest of the config goes here ...

After this, I copied and pasted all the Plug '...' commands in the right places. The only plugin that did not work for me was vim-eunuch. This plugin adds some UNIX shell commands to use inside vim to do sudo operations.
NeoVIM sudo write error

I searched if there is any alternative plugin for NeoVIM, but unfortunately found this issue. Also another very similar plugin SudoEdit.vim stated the following in their FAQ:

Plugin doesn’t work in neovim, how to make it to work?

Yeah, it is a known problem that neovim broke compatibility here. Since I am not using Neovim, I am not able to fix it.

So, I gave up hope and started using sudo nvim to edit files which I am not the owner of. If you knew any better way or a plugin, I will really appreciate to know 😄.

vim-system-copy is a plugin I use to copy the visual selection from vim to the system clipboard. Vim needs to be compiled with an additional flag to support system clipboard management. On the other hand, NeoVIM supports system clipboard management without any additional compile flags. Inside NeoVIM, there is a variable named clipboard you can use.

So instead of using that plugin, I put this line in my nvim/init.vim file.

1
set clipboard+=unnamedplus

Finally after removing unnecessary lines, the bare-bones config file for my NeoVIM is given below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
let plug_install = 0
let autoload_plug_path = stdpath('config') . '/autoload/plug.vim'
if !filereadable(autoload_plug_path)
    silent exe '!curl -fL --create-dirs -o ' . autoload_plug_path .
        \ ' https://raw.github.com/junegunn/vim-plug/master/plug.vim'
    execute 'source ' . fnameescape(autoload_plug_path)
    let plug_install = 1
endif
unlet autoload_plug_path

call plug#begin('~/.config/nvim/plugins')
" To edit files in sudo mode
" TODO: :(

" Autocompletion
Plug 'neoclide/coc.nvim', {'branch': 'release'}

" show +/- in line numbers of a file under git
Plug 'airblade/vim-gitgutter'

" For powerline bar
Plug 'itchyny/lightline.vim'

" Comments
Plug 'tpope/vim-commentary'

" And so on
" Plug '...'

call plug#end()

if plug_install
    PlugInstall --sync
endif
unlet plug_install

" Required for lightline.vim
set noshowmode
set laststatus=2
set mouse=a

" Globar configurations
set number
set clipboard+=unnamedplus

Conclusion

As stated in the beginning of this article, I am not a hardcore vim user. I just like the modal editing of vim. So switching to NeoVIM truely does not impact my workflow that much. But everytime I try to edit a file now, I face this issue:

Run vim instead of NeoVIM

Old habits die hard. Though there is a workaround to use alias vim=nvim, but I prefer to make the alias in my mind.

Thanks for reading this article. Please share anything you do different than my method to switch from vim to neovim.

Share on

Rahat Zaman
WRITTEN BY
Rahat Zaman
Graduate Research Assistant, School of Computing