My .vimrc Conf File Setup

Yash Ranadive
5 min readApr 14, 2016

--

Much in lines of the Tmux conf setup post, today’s post is on my Vim config setup. Here’s a link to my .vimrc file.

Why learn Vim?
This in itself can be a huge blog post. A quick Google search will lead you to many posts talking about the advantages of using Vim. The bottomline is — Vim is a productivity booster. It has great plugin support. It is highly customizable. Has its own scripting language. That being said Vim has a pretty big learning curve. But stick with it for some time and you will see the benefits.

Terminology
Vim “Tabs” can contain one or more “Panes”. The file name appears at the tabline on top. Here I’m running Vim inside TMUX.

Here we have 4 tabs open in Vim

Key Mapping: WTF is noremap?
:map means map recursively. :noremap means map non-recursively.

:map j dd      " Pressing j deletes the line(dd)
:map / j " Pressing / deletes the line(dd)
:noremap / j " Pressing / moves cursor one line down(j)

Note: You’d think noremap’s opposite is “remap”. So what does “remap” do? “remap” is NOT evaluated as a keyword in your vimrc file. I know, confusing right? I’d expect noremap’s counterpart to be “remap”. But in Vim it is called “map”.

“map” and “noremap” can be preceded by the mode in which the mapping should operate. The options are “i” for insert mode, “v” for visual mode, and “n” for normal mode. So :inoremap jk <esc> maps “jk” to escape in the insert mode. Now that we have that out of the way, let’s get in the vimrc file.

Plugin Management

I use pathogen to manage my plugins. To install a plugin simply extract it to ~/.vim/bundle.

“ pathogen runntime injection and help indexing
execute pathogen#infect()

The Basics
Here’s a list of basic things like tab sizes, line numbers, etc.

" the basics
set nocompatible “ We’re running Vim, not Vi!
syntax on “ Enable syntax highlighting
filetype on “ Enable filetype detection
filetype indent on “ Enable filetype-specific indenting
filetype plugin on “ Enable filetype-specific plugins
“ Set line numbering, shift width
set number
set tabstop=2
set expandtab
set shiftwidth=2
set cursorline

Navigation
Let’s all agree that the ESC key is the most hard to reach key on the keyboard. I’ve mapped ESC to jk. Way easier and saves so much frustration. I wish I knew this when I was a beginner.

If you don’t like navigating tabs using gT and gt, you can map Alt/Option H/L to tabprevious and tabnext. Beware we are mapping unicode characters here and it may not always work.

I switch panes quite often. Another productivity booster is using Ctrl + j/k/h/l to navigate panes. It is much faster than pressing Ctrl+W j/k/h/l

“ This maps <ESC> to jk
:inoremap <esc> <nop>
:inoremap jk <esc>
“ This maps :qt to :tabclose
cnoreabbrev qt tabclose
“ Navigate through tabs using Alt/Option + h/l (be careful with this)
nnoremap ˙ :tabprevious<CR>
nnoremap ¬ :tabnext<CR>
“ This enables using ctrl j/k/h/l to navigate splits
nnoremap <c-j> <c-w>j
nnoremap <c-k> <c-w>k
nnoremap <c-h> <c-w>h
nnoremap <c-l> <c-w>l

Pasting from the Clipboard
Pasting pre-formatted text directly from the clipboard can throw your vim indents off. Use pastetoggle to tell vim to paste the data as is i.e. without auto indenting it. To use this feature Press F2 in Normal mode to activate paste. Go in the Insert mode and paste your pre-formatted text. Now, unset the pastetoggle by hitting F2 after going to he Normal mode.

“ try to toggle paste
set pastetoggle=<F2>

Leader Key and Custom Commands
Leader Keys are one of the most underrated features of vim. Leader keys are powerful for creating custom commands. Leader keys can add a bevy of custom key bindings without disturbing the the default key bindings. I like to set my leader key to “,”

I use <Leader>gf to open file under cursor (you will need CtrlP). When you yank a block of text in Vim using the Visual Mode (V + j/k), the cursor comes back to the start of the yanked text. A quick way to navigate to the bottom of the block is ‘] . I map that to <Leader>

“ Set the leader key
:let mapleader = “,”
:nnoremap <leader>d dd
“Leader lt maps to last tab
let g:lasttab = 1
nmap <Leader>lt :exe “tabn “.g:lasttab<CR>
au TabLeave * let g:lasttab = tabpagenr()
“Leader gf copies word under cursor to ctrlp
nmap <leader>gf :CtrlP<CR><C-\>w
“Leader lp goes to the end of previously changed or yanked text
nmap <leader>l ‘]

Searching
I like for the case to be ignored when searching inside the file using “/”. I also like search to happen on every keystroke(incsearch).

I highly recommend the vim Silver Searcher Plugin (Ag). This has boosted my productivity quite a bit. With Ag you can search for strings across the entire project.

“ Set incremental search (as you type) and case insensitive search
set incsearch
set ignorecase
“Configure silver searcher (Ag)
let g:ag_working_path_mode=”r”

File Navigation — Drawer Style
I recommend the NerdTree plugin to navigate through your files. NerdTree is a file drawer. I like to active ate it using Ctrl+N. You can find my NerdTree cheatsheet here.

“NerdTree Settings
map <C-n> :NERDTreeToggle<CR>
autocmd bufenter * if (winnr(“$”) == 1 && exists(“b:NERDTreeType”) && b:NERDTreeType == “primary”) | q | endif
“ Show hidden files e.g. .ruby-version
let NERDTreeShowHidden=1
“ Hide the temp .swp files created by vim
let NERDTreeIgnore = [‘\.swp$’]

File Navigation — Fuzzy Search Style
Navigating a NerdTree tree structure will give you a visual understanding of the project layout. But it requires quite a few keystrokes to get to the file you want. A faster way to open files is by using a plugin called CtrlP.

“Ctrlp.vim settings
let g:ctrlp_map = ‘<c-p>’
let g:ctrlp_cmd = ‘CtrlP’
set wildignore+=*/node_modules/*,*/tmp/*,*.so,*.swp,*.zip

Highlight the Line with Cursor
This one will make the line with the cursor stand out.

“Highlight the currentline in the current pane
augroup BgHighlight
autocmd!
autocmd WinEnter * set cul
autocmd WinLeave * set nocul
augroup END

Other Misc
I prefer writing Word style large text documents in Vim.

“ File of extension .txt should have soft wrap enabled
autocmd BufNewFile,BufRead *.txt set wrap
“ Files of extension .md should be marked as markdown and not modula2
autocmd BufNewFile,BufReadPost *.md set filetype=markdown

Status Bar
I use the airline plugin for my status line.

“This sets the theme for Airline Plugin
let g:airline_theme=’luna’

Color Schemes
I use the solarized colorscheme. Very crisp and neat looking.

“Prepare vim for 256 color schemes
set t_Co=256
let g:solarized_termcolors=256
“ Set colorschemes for solarized (light|dark)
syntax enable
set background=dark
colorscheme solarized

Trailing Space Handler
Trailing white spaces are a pain. Clean them before every save using:

“Delete trailing whitespace on lines
autocmd BufWritePre * :%s/\s\+$//e

Common Typos
I use :wa and :wq quite often. :wa saves all and :wq saves and quits. I often mistype these. It’s because I end up holding the Shift a bit longer.

“ Map Commonly occurring mistypes, typos
“ Map Wa to wa
cnoreabbrev Wa wa
cnoreabbrev Wq wq
cnoreabbrev Q q

Remembering it all
Like Tmux I use only a small subset of Vim’s available features. I’d like to use more but it comes down to developing muscle memory for them.

Whenever I come across a new command that I want to commit to muscle memory, I write it down in my Vim GrepPage collection so I can quickly get to it when I need it.

--

--

Yash Ranadive

Engineering @Facebook. Founder @GrepPage. Data Eng. @Lookout. Web, Search, 3D. Dog Parent. https://evidanary.github.io