vimrc from Scratch: My Defaults

My “sane” vimrc default settings.

In my last post I reviewed the default Vim configuration on a Debian system. Now I will take those defaults and compose them into a vimrc file that will serve as the base for my custom Vim config.

Most of the settings in /etc/vim/vimrc, /usr/share/vim/vim80/debian.vim, and /usr/share/vim/vim80/defaults.vim are sane settings that make sense for my config. I have to copy any settings in defaults.vim that I want to use into my vimrc file because defaults.vim does not get loaded if a user has a custom vimrc in their home directory (~/.vimrc or ~/.vim/vimrc). For debian.vim on the other hand, I have a decision to make: rely on the defaults in debian.vim, or duplicate them in my own vimrc. For me, duplicating most of the settings makes sense for two reasons: (1) it adds clarity to my vimrc by explicitly specifying settings as opposed to relying on my Linux distributions defaults; (2) it makes my vimrc more portable across operating systems. The second point is important because I use the same vimrc on Windows and Mac OSX as I do on Linux.

Below are some settings I do not want to copy from the defaults:

Here are the defaults that I have settled up. They make a good base for a custom vimrc.

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 nomodeline                  " security vulnerabiliy to enable
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
set display=truncate            " Show @@@ in the last line if it is truncated.
set scrolloff=5                 " show a few lines around cursor
set incsearch			" incremental search
set mouse=a			" enable mouse in all modes
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
set suffixes=.bak,~,.swp,.o,.info,.aux,.log,.dvi,.bbl,.blg,.brf,.cb,.ind,.idx,.ilg,.inx,.out,.toc
let c_comment_strings=1
syntax on                       " use enable if want to keep custom colors
filetype plugin indent on       " Enable file type detection.

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

" For Win32 GUI: remove 't' flag from 'guioptions': no tearoff menu entries.
if has('win32')
  set guioptions-=t
endif

" 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

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