danmc.net

vimrc from Scratch: Vim vs Neovim Defaults

And now for something a little different...

In the last two posts I looked at the Debian default Vim configs and combined them into a sane custom default config to build upon. Now I want to take a look at a Vim alternative, Neovim, and compare what a similar "sane" default config looks like with it.

Neovim is a fork of Vim. I don't claim to be an expert on its genesis, but I think it was started due to frustration with the Vim development community being too conservative with accepting new features. Forks like this are always controversial, but Neovim is the shiny new thing, so I couldn't help myself and had to check it out. Now seemed like a good time since I am overhauling my Vim configs.

The first thing that struck me is the difference in some of the default settings for Neovim. Most of the things that I added to my sane custom default config are actually the default settings in Neovim (see :help nvim-defaults. Below is what the equivalent "sane" custom default config looks like in Neovim.

" ~/.config/nvim/init.vim
set nomodeline                  " security vulnerabiliy to enable
set scrolloff=5                 " show a few lines around cursor
set mouse=a			" enable mouse in all modes
set suffixes=.bak,~,.swp,.o,.info,.aux,.log,.dvi,.bbl,.blg,.brf,.cb,.ind,.idx,.ilg,.inx,.out,.toc
let c_comment_strings=1

" CTRL-U in insert mode deletes a lot.  Use CTRL-G u to first break undo,
" so that you can undo CTRL-U after inserting a line break.
" Revert with ":iunmap <C-U>".
inoremap <C-U> <C-G>u<C-U>

" Put these in an autocmd group, so that you can revert them with:
" ":augroup vimStartup | au! | augroup END"
augroup vimStartup
au!

" When editing a file, always jump to the last known cursor position.
" Don't do it when the position is invalid or when inside an event handler
" (happens when dropping a file on gvim).
autocmd BufReadPost *
\ if line("'\"") >= 1 && line("'\"") <= line("$") |
\   exe "normal! g`\"" |
\ endif

augroup END

" Convenient command to see the difference between the current buffer and the
" file it was loaded from, thus the changes you made.
" Only define it when not defined already.
" Revert with: ":delcommand DiffOrig".
if !exists(":DiffOrig")
  command DiffOrig vert new | set bt=nofile | r ++edit # | 0d_ | diffthis
		  \ | wincmd p | diffthis
endif

All of the following is no longer needed because they are the defaults in Neovim:

syntax on                       " use enable if want to keep custom colors
filetype plugin indent on       " Enable file type detection.
set nocompatible	        " Use Vim defaults instead of 100% vi compatibility
set backspace=indent,eol,start	" more powerful backspacing
set history=200		        " keep 200 lines of command line history
set ruler		        " show the cursor position all the time
set showcmd		        " display incomplete commands
set wildmenu		        " display completion matches in a status line
set ttimeout		        " time out for key codes
set ttimeoutlen=100	        " wait up to 100ms after Esc for special key
(close,lastline,msgsep) set display=truncate            " Show @@@ in the last line if it is truncated.
set incsearch			" incremental search
if has('langmap') && exists('+langremap')
  " Prevent that the langmap option applies to characters that result from a
  " mapping.  If set (default), this may break plugins (but it's backward
  " compatible).
  set nolangremap
endif

And the following is no longer needed because they are effectively obsolete in Neovim.

set t_Co=16                     " 16 colors
set t_Sf=[3%dm                " forground color escape code template
set t_Sb=[4%dm                " background color escape code template
" For Win32 GUI: remove 't' flag from 'guioptions': no tearoff menu entries.
if has('win32')
  set guioptions-=t
endif
if has('gui')
   " Make shift-insert work like in Xterm
  autocmd GUIEnter * if empty(maparg("<S-Insert>", "nvso")) | execute "map <S-Insert> <MiddleMouse>" | endif
  autocmd GUIEnter * if empty(maparg("<S-Insert>", "ic")) | execute "map! <S-Insert> <MiddleMouse>" | endif
endif

There are also quite a few other defaults that are changed from Vim to Neovim. For a complete list, see :help nvim-defaults in Neovim.

Conclusion

Using Neovim results in a lot less boilerplate in my vimrc because Vim tries to make sane choices for defaults instead of maintaining backward compatibility with its defaults.

This does have trade offs though. Because Neovim is more cavalier about changing defaults than Vim, and you end up explicitly specifying less in your vimrc, your config may end up being changed by upgrades to new versions of Neovim.