Switching from Vim to Neovim
Published by Matthew Daly 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()23" NERDTree4Plug 'scrooloose/nerdtree'56" Git integration7Plug 'tpope/vim-fugitive'8Plug 'airblade/vim-gitgutter'910" Linting11Plug 'neomake/neomake'12Plug 'w0rp/ale'1314" PHP-specific integration15Plug 'phpactor/phpactor' , {'do': 'composer install', 'for': 'php'}16Plug 'ncm2/ncm2'17Plug 'roxma/nvim-yarp'18Plug 'phpactor/ncm2-phpactor'1920" Snippets21Plug 'SirVer/ultisnips'22Plug 'honza/vim-snippets'2324" Comments25Plug 'tpope/vim-commentary'2627" Search28Plug 'ctrlpvim/ctrlp.vim'2930" Syntax31Plug 'sheerun/vim-polyglot'32Plug 'matthewbdaly/vim-filetype-settings'3334" Themes35Plug 'nanotech/jellybeans.vim' , {'as': 'jellybeans'}3637call 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"Completion2autocmd FileType * setlocal formatoptions-=c formatoptions-=r formatoptions-=o3set ofu=syntaxcomplete#Complete4autocmd FileType php setlocal omnifunc=phpactor#Complete5let g:phpactorOmniError = v:true6autocmd 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"General2syntax on3colorscheme jellybeans4set nu5filetype plugin indent on6set nocp7set ruler8set wildmenu9set mouse-=a10set t_Co=2561112"Code folding13set foldmethod=manual1415"Tabs and spacing16set autoindent17set cindent18set tabstop=419set expandtab20set shiftwidth=421set smarttab2223"Search24set hlsearch25set incsearch26set ignorecase27set smartcase28set 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 Markdown2au BufNewFile,BufReadPost *.md set filetype=markdown3let 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 config2" Full config: when writing or reading a buffer, and on changes in insert and3" 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 config2" Include use statement3nmap <Leader>u :call phpactor#UseAdd()<CR>45" Invoke the context menu6nmap <Leader>mm :call phpactor#ContextMenu()<CR>78" Invoke the navigation menu9nmap <Leader>nn :call phpactor#Navigate()<CR>1011" Goto definition of class or class member under the cursor12nmap <Leader>o :call phpactor#GotoDefinition()<CR>1314" Transform the classes in the current file15nmap <Leader>tt :call phpactor#Transform()<CR>1617" Generate a new class (replacing the current file)18nmap <Leader>cc :call phpactor#ClassNew()<CR>1920" Extract expression (normal mode)21nmap <silent><Leader>ee :call phpactor#ExtractExpression(v:false)<CR>2223" Extract expression from selection24vmap <silent><Leader>ee :<C-U>call phpactor#ExtractExpression(v:true)<CR>2526" Extract method from selection27vmap <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.