My .Vimrc Configuration

VIM is my favorite text editor. It’s powerful, customizable, and available on all platforms (default on unix-systems, gvim/macvim on pc/osx-systems). Most importantly, it allows me to get my work done in an efficient manner. Once you learn the movements and power commands, your hands rarely have to leave the keyboard when editing.

The .vimrc is the configuration file. I consider my .vimrc pretty basic and a good starting point for beginners to VIM. I would suggest to anyone editing their .vimrc to understand each setting before using them. Here are a few of my favorite settings:

Remap Command Mode

nnoremap ; :

The nnoremap setting remaps : to ; in normal mode and thus, saves a key stroke (shift + ;) when entering command mode. It seems trivial, but when a file is saved a 100 times in a day, it adds up.

Remap <esc> to jj

inoremap jj <esc>

Similar to the setting above, this setting remaps the often-used <esc> key to jj key press combo. It’s pretty rare for code or text to contain the letters “jj” in succession. Now, a simple jj tap will bring VIM out of insert mode.

Map <leader> key

let mapleader = ","

The <leader> key is your own personal modifier key and is default as . This setting allows for easier usage of macros.

Map NERDTree toggle to <leader>

map <leader>n :NERDTreeToggle<CR>

This allows me to map the <leader> key in conjunction with n to quickly toggle NERDTree. NERDTree is a VIM plugin used to navigate and control the files in a workspace easier.

Add Google Search to VIM command line

function! Google()
   call inputsave()
   let searchterm = input('Google: ')
   call inputrestore()
   return searchterm
endfunction
map <leader>g <ESC>:! /usr/bin/open -a "/Applications/Google Chrome.app" 'https://google.com/search?q=<C-R>=Google()<CR>'<CR><CR>

With this macro, typing <leader>g brings up the prompt “Google:”. Once a search string is entered, a web browser will open (or tab if one is already opened) with a Google search of the string. This is great for those moments when I want to quickly look something up without having to locate my web browswer.

Toggle VIM Blog Mode

" macro for blog writing
function ToggleBlog()
  if &wrap
    echo "Blog OFF"
    setlocal nowrap
    set virtualedit=all
    setlocal lines=50 columns=200
    color desert
    setlocal guifont=menlo:h11
    silent! nunmap <buffer> k
    silent! nunmap <buffer> j
    silent! nunmap <buffer> 0
    silent! nunmap <buffer> $
    silent! iunmap <buffer> k
    silent! iunmap <buffer> j
    silent! iunmap <buffer> 0
    silent! iunmap <buffer> $
  else
    echo "Blog ON"
    setlocal wrap linebreak nolist
    set virtualedit=
    setlocal display+=lastline
    setlocal lines=50 columns=90
    color morning
    setlocal guifont=menlo:h14
    noremap <buffer> <silent> k gk
    noremap <buffer> <silent> j gj
    noremap <buffer> <silent> 0 g0
    noremap <buffer> <silent> $ g$
  endif
endfunction
noremap <silent> <leader>b :call ToggleBlog()<CR>

Finally, this last macro allows me to toggle between my coding sessions and my writing sessions. When writing, I prefer a lighter colorscheme (color morning) and smaller width window for readability (columns=90). I also set my text to wrap (setlocal wrap) so my text doesn’t run off the window. Since wrap is enabled, the movement keys don’t work as intuitively, so gk, gj, g0, and g$ allow the cursor to move similarly to before.

All in all, my favorite .vimrc settings increase the comfort of using the editor and add a little fun utility for the lazy side in me. I’ve found them extremely useful and hope they can be the same for you.