Switching from Vim to Neovim

Published by at 9th September 2018 12:40 pm

I honestly thought it would never happen. I've been using Vim since 2008, and every other editor I've tried (including VSCode, Emacs, Sublime Text and Atom) hasn't come up to scratch. There were a few useful features in PHPStorm, to be fair, but nothing that justified the bother of moving. Also, I suffer from a degree of RSI from my prior career as an insurance clerk (years of using crap keyboards and mice on Windows XP took its toll...), and Vim has always been the most RSI-friendly editor I found.

Yet I have actually gone ahead and migrated away... to Neovim. Of course, the fact that the workflow is essentially identical helps in the migration process, as does the fact that it supports most of the same plugins.

My workflow has always been strongly CLI-based. I use GNU Screen and Byobu together to run multiple "tabs" in the terminal, so the lack of GUI support in Neovim doesn't bother me in the slightest. The only change I really made was to my .bash_aliases so that the Vim command ran screen -t Vim nvim, so that it would open up Neovim rather than Vim in a new Screen tab.

Initially I switched straight over to using the same settings and plugins I had with Vim, and they worked seamlessly. However, after a while I decided to use the opportunity to completely overhaul the plugins and settings I used and largely start over - cull the ones I no longer needed, add some new ones, and comment it properly.

Loading plugins

I used to use Pathogen to manage my Vim plugins, but it didn't actually import the plugins itself, and just provided a structure for them. This meant that the only practical way I found to pull in third-party plugins was to set them up as Git submodules, meaning I had to store my configuration in version control and clone it recursively onto a new machine. It also made updating cumbersome.

Now I've switched to vim-plug, which makes things much easier. I can define my dependencies in my .config/nvim/init.vim and pull them in with PlugInstall. If I want to update them, I run PlugUpdate, or if I need to add something else, I merely add it in the file and run PlugInstall again. Nice and easy.

The first section of my configuration file loads the dependencies:

1call plug#begin()
2
3" NERDTree
4Plug 'scrooloose/nerdtree'
5
6" Git integration
7Plug 'tpope/vim-fugitive'
8Plug 'airblade/vim-gitgutter'
9
10" Linting
11Plug 'neomake/neomake'
12Plug 'w0rp/ale'
13
14" PHP-specific integration
15Plug 'phpactor/phpactor' , {'do': 'composer install', 'for': 'php'}
16Plug 'ncm2/ncm2'
17Plug 'roxma/nvim-yarp'
18Plug 'phpactor/ncm2-phpactor'
19
20" Snippets
21Plug 'SirVer/ultisnips'
22Plug 'honza/vim-snippets'
23
24" Comments
25Plug 'tpope/vim-commentary'
26
27" Search
28Plug 'ctrlpvim/ctrlp.vim'
29
30" Syntax
31Plug 'sheerun/vim-polyglot'
32Plug 'matthewbdaly/vim-filetype-settings'
33
34" Themes
35Plug 'nanotech/jellybeans.vim' , {'as': 'jellybeans'}
36
37call plug#end()

As always, it's a good idea to comment your config and try to group things logically. Note that I have one plugin of my own listed here - this is just a collection of settings for different filetypes, such as making Javascript files use 2 spaces for indentation, and it's easier to keep that in a repository and pull it in as a dependency.

Completion

The next part of the config deals with configuration. Most of the time the default omnicompletion is pretty good, but in the process of building out this config, I discovered PHPActor, which has massively improved my development experience with PHP - it finally provides completion as good as most IDE's, and also provides similar refactoring tools. My config for completion currently looks like this:

1"Completion
2autocmd FileType * setlocal formatoptions-=c formatoptions-=r formatoptions-=o
3set ofu=syntaxcomplete#Complete
4autocmd FileType php setlocal omnifunc=phpactor#Complete
5let g:phpactorOmniError = v:true
6autocmd BufEnter * call ncm2#enable_for_buffer()
7set completeopt=noinsert,menuone,noselect

General config

This is a set of standard settings for the general behaviour of the application, such as setting the colorscheme and default indentation levels. I also routinely disable the mouse because it bugs me.

1"General
2syntax on
3colorscheme jellybeans
4set nu
5filetype plugin indent on
6set nocp
7set ruler
8set wildmenu
9set mouse-=a
10set t_Co=256
11
12"Code folding
13set foldmethod=manual
14
15"Tabs and spacing
16set autoindent
17set cindent
18set tabstop=4
19set expandtab
20set shiftwidth=4
21set smarttab
22
23"Search
24set hlsearch
25set incsearch
26set ignorecase
27set smartcase
28set diffopt +=iwhite

Markdown configuration

This section sets the file type for Markdown. It disables the Markdown plugin included in vim-polyglot as I had problems with it, and sets the languages that will be highlighted in fenced code blocks. I may at some point migrate this to the filetype repository.

1"Syntax highlighting in Markdown
2au BufNewFile,BufReadPost *.md set filetype=markdown
3let g:polyglot_disabled = ['markdown']
4let g:markdown_fenced_languages = ['bash=sh', 'css', 'django', 'javascript', 'js=javascript', 'json=javascript', 'perl', 'php', 'python', 'ruby', 'sass', 'xml', 'html', 'vim']

Neomake

I used to use Syntastic for checking my code for errors, but I've always found it problematic - it was slow and would often block the editor for some time. Neovim does have support for asynchronous jobs (as does Vim 8), but Syntastic doesn't use it, so I decided to look elsewhere.

Neomake seemed a lot better, so I migrated over to it. It doesn't require much configuration, and it's really fast - unlike Syntastic, it supports asynchronous jobs. This part of the config sets it up to run on changes with no delay in writing, so I get near-instant feedback if a syntax error creeps in, and it doesn't block the editor the way Syntastic used to.

1" Neomake config
2" Full config: when writing or reading a buffer, and on changes in insert and
3" normal mode (after 1s; no delay when writing).
4call neomake#configure#automake('nrwi', 500)

PHPActor

As mentioned above, PHPActor has dramatically improved my experience when coding in PHP by providing access to features normally found only in full IDE's. Here's the fairly standard config I use for the refactoring functionality:

1" PHPActor config
2" Include use statement
3nmap <Leader>u :call phpactor#UseAdd()<CR>
4
5" Invoke the context menu
6nmap <Leader>mm :call phpactor#ContextMenu()<CR>
7
8" Invoke the navigation menu
9nmap <Leader>nn :call phpactor#Navigate()<CR>
10
11" Goto definition of class or class member under the cursor
12nmap <Leader>o :call phpactor#GotoDefinition()<CR>
13
14" Transform the classes in the current file
15nmap <Leader>tt :call phpactor#Transform()<CR>
16
17" Generate a new class (replacing the current file)
18nmap <Leader>cc :call phpactor#ClassNew()<CR>
19
20" Extract expression (normal mode)
21nmap <silent><Leader>ee :call phpactor#ExtractExpression(v:false)<CR>
22
23" Extract expression from selection
24vmap <silent><Leader>ee :<C-U>call phpactor#ExtractExpression(v:true)<CR>
25
26" Extract method from selection
27vmap <silent><Leader>em :<C-U>call phpactor#ExtractMethod()<CR>

Summary

Vim or Neovim configuration files are never static. Your needs are always changing, and you're constantly discovering new plugins and new settings to try out, and keeping ones that prove useful. It's been helpful to start over and ditch some plugins I no longer needed, pull in some new ones, and organise my configuration a bit better.

Now that I can set the dependencies in a text file rather than pulling them in as Git submodules, it makes more sense to keep my config in a Github Gist rather than a Git repository, and that's where I plan to retain it for now. Feel free to fork or cannibalise it for your own purposes if you wish.