My .vimrc file

Fri, Mar 4, 2011
Embracing Vim

Vim is a very flexible editor that can be customized to adjust to your editing style. One of the things you learn early on is to maintain a ~/.vimrc file to contain your favorite settings, key mappings and custom commands.

The .vimrc file is a vimscript file, and vimscript is a full programming language in itself. So your .vimrc file can contain variables, conditional code, function definitions etc, but when getting started you will mostly want to stick with enabling/disabling settings and mapping keys.

While I have only been working with Vim for a couple of weeks now, I am starting to settle on my favorite settings and key mappings. The more I work in Vim, the more these actions will start to become muscle memory and the less I will have to think about them (hopefully!), so I really want a consistent environment on all of the computers I use.

In order to keep my environment across computers I store my .vimrc file (along with many other linux . files) on a SVN repository. This means that I can checkout this SVN directory and symlink my ~/.vimrc to the svn version. An alternative option might be to use rsync, or a 3rd party application like dropbox.

So, 3 weeks in, here are some snippets from my current .vimrc file…

set nocompatible               " dump the old fashioned vi support!
set backspace=indent,eol,start " allow backspacing over everything in insert mode
set backup                     " keep a backup file
set backupdir=~/.backup        " custom backup directory
set history=100                " keep 100 lines of command line history
set ruler                      " show the cursor position all the time
set showcmd                    " display incomplete commands
set mouse=a                    " enable the mouse
set lazyredraw                 " don't update the display while executing macros
set showmode                   " so you know what mode you are in
set laststatus=2               " always put a status line in.
set scrolloff=10               " start scrolling 10 lines from the top/bottom
set ch=2                       " set command line 2 lines high
set nowrap                     " no line wrapping 
set shiftwidth=2               " round indent actions to multiple of 2
set softtabstop=2              " soft tabs = 2 spaces 
set tabstop=2                  " tabs = 2 spaces
set expandtab                  " expand tabs to spaces
set incsearch                  " do incremental searching
set hlsearch                   " highlight search terms
set ignorecase                 " case insensitive search
set gdefault                   " replace all instances on line when doing global search/replace
set number                     " enable line numbers
set showmatch                  " show matching parenthesis
set hidden                     " allow hidden buffers with unsaved changes
set whichwrap+=<,>,[,]         " allow arrow keys to line wrap
set wildmenu                   " enable enhanced command line completion
set wildignore+=*.pui,*.prj    " ignore these when completing file or directory names
set ttyfast                    " faster terminal updates
set virtualedit+=block         " allow virtual-block select to go past end of lines

syntax on                      " enable syntax highlighting
filetype plugin indent on      " enable file type detection

I like to be able to quick edit my .vimrc file and my .bashrc file. I also have a couple of project specific vimscripts that I only want to load/edit when working on that project…

" Easy edit/open commands
" =======================
command EditVim  :edit   ~/.vimrc
command EditBash :edit   ~/.bashrc
command EditCI   :edit   ~/.vim/scripts/ci
command EditLP   :edit   ~/.vim/scripts/lp
command SourceCI :source ~/.vim/scripts/ci
command SourceLP :source ~/.vim/scripts/lp

And here are some of my current key mappings…

" My favorite mappings
" ====================

map <C-a>     :Ack<Space>
map <C-f>     :NERDTreeToggle<CR>
map <C-t>     :CommandT<CR>
map <C-b>     :BufExplorer<CR>
map <C-o>     :TlistToggle<CR>

map <F11>   :ZoomWin<CR>
map <F12>   :noh<CR>

When putting text, I nearly always want to put the text before the cursor, rather than after it, so make that the default…

" switch p and P
" ==============
noremap p P
noremap P p
noremap o O
noremap O o

I really, really like split windows (on large monitors) so I use | and - to vsplit and split. I also map <Tab> to switch windows, <Shift-Tab> to switch the other direction and <C-c> to close window…

" WINDOW MANAGEMENT
" ================
map <Bar>   <C-W>v<C-W><Right>
map -       <C-W>s<C-W><Down>
map <Tab>   <C-W>w
map <Esc>[Z <C-W>W
map <C-c>   <C-W>c

I have a habit of saving often, I rely on version control (SVN) for reverting back if I make mistakes. So I want quick access to save all buffers and also quick access to quit. My process generally is <C-s><C-q> to save (all) and quit.

" QUICK SAVE
" ==========
noremap  <C-s> :wa<CR>
inoremap <C-s> <Esc>:wa<CR>
noremap  <C-q> :SaveSession<CR>:qa<CR>
inoremap <C-q> <Esc>:SaveSession<CR>:qa<CR>

I also have a habit of starting my edits with a hit of the space or del key, so make these switch into insert mode…

" QUICK ENTRY INTO INSERT MODE
" ============================
nnoremap <Space> i<Space>
nnoremap <Del>   i<Del>

… and that gets me started. I have some more advanced settings as well, and some plugin specific customizations, and I am sure that this list will continue to grow as I continue to learn and customize how I use Vim, but that is a good start.

Don’t forget to look for more inspiration from other vim users