A sensible and minimal Vim

March 17th, 2018

Or: How I Learned to Stop Worrying and Love Modal Editing

As long as I can remember my text editor of choice was Sublime Text. It’s a great piece of software for which I have zero complaints for and even wrote a plugin for it two years ago. Whether prose or code—local and remote—Sublime Text always delivered and was there for me.

My only issue with Sublime (and for that matter any text editor not running inside the terminal—Atom and any other Electron-based JavaScript-using philistine abomination do not count as text editors) was the extra Cmd-Tab I’d to type every time I saved a file in order to switch to the terminal and compile or run it. Cmd-Tab would seem like a minimal context switching toll between editor and terminal but in fact it’s a huge one considering the GUI lag either in a single or multi- monitor setup.

That, along with the fact that I routinely use more than seven remote hosts over ssh where my needs vary from writing production code to lightweight text editing and that I generally spend most of my time in a terminal, made me try—and eventually switch to—Vim. Another reason is that I want to keep things simple: why use two GUIs when one does the job?

Starting with an extremely minimal .vimrc (with only syntax enable in it) I wanted to personalize Vim while keeping three rules in mind: simple, fast, efficient. What does this mean? No unecessary plugins. No uneccessary settings. My text editor is not an IDE. My text editor should be lightweight and not a power hog. My text editor and its subsequent configuration should not be platform specific. (Bonus points for my publishing system—a topic for a future post—which is editor-agnostic and made the switch easier.)

The plugins I ended up using are the following: delimitMate.vim (automated closing of quotes, parentheses, etc.,) lightline.vim (a lightweight status line,) tcomment_vim (an on-off toggle for comments,) vim-buffkill (which provides better buffer management,) vim-colors-solarized (because I need my precision colors,) vim-gitbranch (displays current branch in Lightline,) vim-gitgutter (tracks git changes in the gutter,) and vim-unimpaired (better bracket mappings.) You can find all of them in VimAwesome which is a great Vim repository.

Update: My dotfiles are now available on GitHub. You can always find the latest .vimrc version there.

I’m in the process of rewriting my dotfiles so for now my .vimrc is only available here. I’ve documented every single setting with an appropriate short comment. Essentially, I set a Solarized Light environment, use four-spaces-as-one-tab, enable persistent undo (a super cool feature,) colorize column #81 so I always know my wrap limit, customize a very minimal status line, and remap a few keyboard combinations.

Feel free to use, reuse, remix, or adjust this .vimrc and send me your comments or suggestions.

Finally, if you’re new to Vim I suggest bookmarking this cheat sheet. It’s the best I’ve found. Further, embrace the modal model — it’s sublime. (Oh.) Moreover, I would suggest not over-engineering Vim. Start minimal and start slow. The more you progress and start developing a natural flow with it, the better it is—only then you’ll spot your specific needs and properly personalize Vim. Learn the keyboard combinations. One of my favorites are [c and ]c which navigate between git hunks in a file.

I’m also attaching a screenshot below the file so you can see how my setup looks like.

syntax enable                   " Enable syntax highlighting

" Enable all Pathogen plugins
execute pathogen#infect()

set encoding=utf8               " Set UTF-8 encoding
set autoread                    " Reload files changed outside vim
set nocompatible                " Use vim rather than vi settings
set backspace=indent,eol,start  " Allow backspace in insert mode
set number                      " Line numbers are good
set ttyfast                     " Faster term redrawing, scrolling; perhaps
set nobackup                    " Disable file backups when writing files
set nowritebackup               " Don't backup before overwriting files
set expandtab                   " Use the appropriate number of spaces to tab
set smarttab                    " A tab in front of a line inserts spaces
set shiftwidth=4                " # of spaces to use for autoindent
set tabstop=4                   " # of spaces that a tab counts for
set textwidth=80                " Make all lines 80 chars or less
set wrap                        " Wrap lines longer than 80 chars 
set linebreak                   " Wrap lines when convenient
set nojoinspaces                " Set 1 space btwn lines/periods to be joined
set scrolloff=999               " Working line will always be in the center
set title                       " Set title of the Vim window
set titleold=                   " Revert to original title when exiting
set hlsearch                    " Highlight searches by default
set noshowmode                  " Don't show current mode [bc Lightline]
set noshowcmd                   " Don't show incomplete cmds [bc Lightline]
set laststatus=2                " Always show status bar
set autoindent                  " Use existing indent depth starting a new line
set smartindent                 " Do smart indenting when starting a new line
set background=light            " Set solarized background color
colorscheme solarized           " Set solarized colorscheme

" Set search results to white font, red background overriding solarized
autocmd ColorScheme * hi Search cterm=NONE ctermfg=white ctermbg=red

" Set Make tabs to tabs and not spaces
filetype on
autocmd FileType make set noexpandtab shiftwidth=4

" Show char width column
set colorcolumn=+1
" Color highlight char width col
highlight ColorColumn ctermbg=lightgrey

" Show current cursor line
set cursorline 
" Color highlight c. cursor l.
highlight CursorLine ctermbg=grey cterm=NONE

" Color highlight line #s
highlight LineNr ctermfg=NONE
" Color highlight c. cursor l.
highlight CursorLineNr ctermfg=NONE

" Map yanking in visual mode to system's copy
map <C-c> "*y

" Map toggle automatic line comment (a la ST3)
map <C-/> gcc

" Clear previous search highlights
" <C-l> would originally redraw the screen; now we first clear, then redraw
nnoremap <C-l> :nohl<CR><C-L>

" Enable Lightline in xterm-color256 mode for proper compatibility
if !has('gui_running')
  set t_Co=256
endif

" Customize Lightline with a minimal set of configs + current git branch
let g:lightline = {
\ 'colorscheme': 'solarized',
\ 'active': {
\   'left': [['mode', 'paste'], ['gitbranch'], ['filename', 'modified']],
\   'right': [['lineinfo'], ['percent']]
\   },
\ 'component_function': {
\   'gitbranch': 'gitbranch#name'
\   },
\ }

" Persisent undo --- store all change information in an undodir
" Check if an undodir exists, otherwise create it w proper permissions
if !isdirectory($HOME."/.vim/undodir")
    call mkdir($HOME."/.vim/undodir", "", 0700)
endif
set undodir=$HOME/.vim/undodir  " Set undodir path
set undofile                    " Write changes to the undofile
set undolevels=1000             " Max # of changes that can be undone
set undoreload=10000            " Max # of lines to save for undo on buf reload

Vim