Walkthrough of my .vimrc file for Ruby development

There are a lot of people who want to use vim as their text editor of choice. There are several problems that might turn off newcomers and one of them is extensive configuration file that you need to write in order for it to work properly. That's why I created this walkthrough for my .vimrc file explaining what each line does. Quite frankly this will allow me to understand my vimrc better and remove unnecessary things. With that in mind, let's begin:

set nocompatible " be iMproved

Just signals to vim that it does not have to be compatible with vi. This line is a must.

" For vundle
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#rc()

These few lines are needed to initialize Vundle plugin. This plugin is responsible for managing dependencies. Next lines are just plugin definitions, where the string is github repository.

" Dependencies of snipmate
Bundle "MarcWeber/vim-addon-mw-utils"
Bundle "tomtom/tlib_vim"
Bundle "honza/vim-snippets"

" Snippets for our use :)
Bundle 'garbas/vim-snipmate'

These are the dependencies of snipmate, which allows to autocomplete many exprssions. For example, yo can type def<tab> and that will expand to:

def method_name
" Git tools
Bundle 'tpope/vim-fugitive'

These are tools that will make your VIM in to the supercharged tool to work with GIT. I won't dive in to the details, just read the docs.

" Dependency managment
Bundle 'gmarik/vundle'

Dependency management plugin itself.

" Rails :/
Bundle 'tpope/vim-rails.git'

These are the tools that will make your rails development easier. Some of the commands that I use will be listed below.

" Commenting and uncommenting stuff
Bundle 'tomtom/tcomment_vim'

This allows commenting and uncommenting sections of your code. I use gc to do that.

" Molokai theme
Bundle 'tomasr/molokai'

Molokai theme

" Vim Ruby
Bundle 'vim-ruby/vim-ruby'

Adds syntax highlighting for Ruby.

" Surround your code :)
Bundle 'tpope/vim-surround'

Great plugin if you want to change braces to curly braces or p tag to div tag or any other combination that you might imagine. Make sure to check it out.

" Every one should have a pair (Autogenerate pairs for "{[( )
Bundle 'jiangmiao/auto-pairs'

Will generate pairs for most common code blocks. This is default behaviour in most text editors.

" Tab completions
Bundle 'ervandew/supertab'

Tab completion. When typing something that vim has already opened, it will try to autocomplete method names, variables e.t.c.

" Fuzzy finder for vim (CTRL+P)
Bundle 'kien/ctrlp.vim'

This is a fuzzy finder, similar to Atoms or Sublimes. Press CTRL+P and start writing appctrlrsapp and it will show up app/controllers/application_controller.rb in the search results. Very convenient.

" For tests
Bundle 'janko-m/vim-test'

Now this is one one great plugin. Instead of finding test runners for all your code, you can just use one and configure one. This works with Elixir and Ruby (My most commonly used languages) out of the box.

" Navigation tree
Bundle 'scrooloose/nerdtree'

Nerd tree is a nice file system navigation sidebar that is similar to one you see in atom or sublime.

" Dispatching the test runner to tmux pane
Bundle 'tpope/vim-dispatch'

This plugin is used to dispatch the tests to tmux pane and then output to a horizontal split to bottom. Really handy.

set tags=./tags; " Set tags directory

This line sets the ctags directory. Ctags can help with file navigation. It is quite tricky to make it work with ruby, but I had some success with it.

set autoindent " Auto indention should be on

Autoidention. Just makes sure that when pressing enter, the line will be automatically idented.

" Ruby stuff: Thanks Ben :)
" ================
syntax on                 " Enable syntax highlighting
filetype plugin indent on " Enable filetype-specific indenting and plugins

augroup myfiletypes
	" Clear old autocmds in group
	" autoindent with two spaces, always expand tabs
	autocmd FileType ruby,eruby,yaml,markdown set ai sw=2 sts=2 et
augroup END
" ================

Apparently sets two spaces for ruby, eruby, yaml and markdown files. This line has been added a long time ago, so I am even not sure anymore :)

" Syntax highlighting and theme
syntax enable

Enables syntax highlighting

" Configs to make Molokai look great
set background=dark
let g:molokai_original=1
let g:rehash256=1
set t_Co=256
colorscheme molokai

Molokay theme specific configs

" Show trailing whitespace and spaces before a tab:
:highlight ExtraWhitespace ctermbg=red guibg=red
:autocmd Syntax * syn match ExtraWhitespace /\s\+$\| \+\ze\\t/

Highlights the trailing wihitespace with red if you have missed it.
Trailing whitespace

" Lovely linenumbers
set nu

Enableling linumbers

" My leader key
let mapleader=","

I map leader to comma, because it is much easier on OS-X.

" Searching
set hlsearch

Highlights the areas that you search for.

set incsearch

Searches incremetally as you type.

set ignorecase

Ignores the case in the file.

set smartcase

If you start writing in camel case, it will assume that you want camelcased. Otherwise it will be case insesative.

" Remove highlights with leader + enter
nmap <Leader><CR> :nohlsearch<cr>

Removing search highlights with ,Enter

" Buffer switching
map <leader>p :bp<CR> " ,p previous buffer
map <leader>n :bn<CR> " ,n next buffer
map <leader>d :bd<CR> " ,d delete buffer

I usually move between buffers and want ability to delete them. Comments above explain my preferences on this.

map <Leader>c :call <CR>
nmap <silent> <leader>c :TestFile<CR>
nmap <silent> <leader>s :TestNearest<CR>

Running the test files - Current test file with ,c and current test under cursor with ,s.

map <leader>t :A<CR> " \t to jump to test file
map <leader>r :r<cr> " \t to jump to related file

These commands come with rails-vim. Allow me to jump to test files and such.

set laststatus=2

Shows current status of the file

" Don't be a noob, join the no arrows key movement
inoremap  <Up>     <NOP>
inoremap  <Down>   <NOP>
inoremap  <Left>   <NOP>
inoremap  <Right>  <NOP>
noremap   <Up>     <NOP>
noremap   <Down>   <NOP>
noremap   <Left>   <NOP>
noremap   <Right>  <NOP>

These lines will help you to avoid habbit of using arrows to do things :)

" Removing escape
ino jj <esc>
cno jj <c-c>
vno v <esc>

By pressing jj i can escape to normal mode any time instead of pressing ESC each time.

" highlight the current line
set cursorline
" Highlight active column
set cuc cul"

Says what it does. Highlights active column and line.

" Tab completion
set wildmode=list:longest,list:full
set wildignore+=*.o,*.obj,.git,*.rbc,*.class,.svn,vendor/gems/*

Configuration for autocompletion

if isdirectory($HOME . '/.vim/backup') == 0
	:silent !mkdir -p ~/.vim/backup >/dev/null 2>&1
set backupdir-=.
set backupdir+=.
set backupdir-=~/
set backupdir^=~/.vim/backup/
set backupdir^=./.vim-backup/
set backup

" Save your swp files to a less annoying place than the current directory.
" " If you have .vim-swap in the current directory, it'll use that.
" " Otherwise it saves it to ~/.vim/swap, ~/tmp or .
if isdirectory($HOME . '/.vim/swap') == 0
	:silent !mkdir -p ~/.vim/swap >/dev/null 2>&1
set directory=./.vim-swap//
set directory+=~/.vim/swap//
set directory+=~/tmp//
set directory+=.

" viminfo stores the the state of your previous editing session
set viminfo+=n~/.vim/viminfo

if exists("+undofile")
	" undofile - This allows you to use undos after exiting and restarting
	" This, like swap and backups, uses .vim-undo first, then ~/.vim/undo
	" :help undo-persistence
	" This is only present in 7.3+
	if isdirectory($HOME . '/.vim/undo') == 0
		:silent !mkdir -p ~/.vim/undo > /dev/null 2>&1
	set undodir=./.vim-undo//
	set undodir+=~/.vim/undo//
	set undofile

This huge block of code makes sure that you will have undo history, swap files are not saved in project directory, which can be annoying e.t.c. Copy this and it will work. If not, leave a comment.

" Ruby hash syntax conversion
nnoremap <F12> :%s/:\([^ ]*\)\(\s*\)=>/\1:/g<return>

When you are too lazy to change old Ruby hash syntax to new one, this is a lifesaver!

nmap <Leader><CR> :nohlsearch<cr>

,Enter removes any search highlight

map <leader>q :NERDTreeToggle<CR>

Opens and closes Nerdtree with ,q

set clipboard=unnamed

Every copy and paste will interact with system cliboard. This helps a lot.

if has('nvim')
  let test#strategy = "neovim"
  let test#strategy = "dispatch"

These lines set test strategy. I am sometimes using neovim, hence the conditional. For regular vim I use dispatch plugin for opening a test runner in tmux pane and for neovim I use the built in terminal.

Thats all folks. Hope you will find this helpful! You can find my vimrc and other rcfiles here.