+2

Vim's Flavor

Starting Point

Vim has reputation of being hard to use because of its unique and complex way of handling text editing unlike any others editor like Notepad, Notepadd++, Sublime Text. When I first started learning how to use vim I was struggling too much, but once I get pass basic of some commands and learned how vim works, all pieces are started to fit together. For basic editing vim build-in functionalities will be enought, but as for larger editing for example when you working programming software's project things are starting to get out of hand. So in the next section I will introduce some vim configuration as well as plugins that can boost your development prodcutivity.

Plugin management

These are some plugin managers that can help you to install and manage plugin in vim: Vundle, Pathogen and Neobundle. You can choose which ever suitable for you.

Files Navigation

NERDTree: this plugin provide tree base file navigation like the following picture. Screenshot from 2015-09-23 22:17:05.png

CtrlP: this plugin provide fuzzy search for file name in the project like following picture. Screenshot from 2015-09-23 22:25:57.png

Ctags: Not really vim plugin but vim support tags functionality that make use of this program. First you need to install ctags with following command.
On Ubuntu

sudo apt-get install exuberant-ctags

On Mac

 brew install ctags

Then to generate tags for all source code run the following command and it will generate a new file name tags contain all the tag definition so you can jump back and forth to method or class definition. In vim just position the cursor on method or class name then press CTRL-] to jump to method definition and CTRL-[ to jump back. it really help you locate piece of code quickly

 ctags -R .

File information and Git Integration

Vim-GitGutter: this plugin show information about the place that change since last commit with +, - and ~ sign to indicate Added, Deleted and Modified near line number like the following picture.
Screenshot from 2015-09-23 22:39:08.png

Vim-Airline: provide status bar in vim that show file, buffer, editing mode as well as git information that tell use which branch that we are working on like above picture show home-page which is a branch that I'm currently on.

Editing extension

Vim-Snipmate: provide code snippet functionalities into vim. And you might want to get this already made snippets from vim-snippets as well. If you don't know what code snippet is? I suggest you do some research on it because it is a real life saver for programmer.

tComment: toggle block comment with east just select block of code with visual selection and press gc then you are done. For more commands checkout the documentation on github.

Rails project specific

vim-rails: this plugin provide really helpful extension commands for rails specific project like extract part of view into partial, easy navigate to rails specific component like controller, model, or view as well as rails commands that can run directly in vim.

Wrap Up

Lets wrap this post up with my personal .vimrc configuration file that I used for my day to day development.

 " Vundle configuration
set nocompatible
set t_Co=256
filetype off

set rtp+=~/.vim/bundle/vundle
call vundle#begin()

Plugin 'scrooloose/nerdtree'
Plugin 'bling/vim-airline'
Plugin 'kien/ctrlp.vim'
Plugin 'airblade/vim-gitgutter'
Plugin 'tpope/vim-fugitive'
Plugin 'godlygeek/tabular'
Plugin 'garbas/vim-snipmate'
Plugin 'marcweber/vim-addon-mw-utils'
Plugin 'tpope/vim-endwise'
Plugin 'tomtom/tlib_vim'
Plugin 'tpope/vim-rails'
Plugin 'vim-ruby/vim-ruby'
Plugin 'tpope/vim-bundler'
Plugin 'tpope/vim-cucumber'
Plugin 'tpope/vim-haml'
Plugin 'skalnik/vim-vroom'
Plugin 'vim-scripts/tComment'
Plugin 'craigemery/vim-autotag'
Plugin 'tpope/vim-surround'
Plugin 'Townk/vim-autoclose'
Plugin 'johnhamelink/blade.vim'

call vundle#end()

filetype plugin indent on

fun! <SID>StripTrailingWhitespaces()
  " get current cursor position
  let l = line(".")
  let c = col(".")

  " replace all trailing white space
  %s/\s\+$//e

  " set cursor back to original position
  call cursor(l, c)
endfun

" replace all trailing white space before save
autocmd BufWritePre * :call <SID>StripTrailingWhitespaces()
autocmd BufRead,BufNewFile *.php,*.c,*.cpp,*.java set ts=4 sts=4 sw=4
autocmd BufRead,BufNewFile *.blade.php set ft=html

let mapleader=','
noremap \ ,
set number
set numberwidth=3
set textwidth=80
set wrap
set showcmd
set showmatch
set ruler
set wildmenu
set visualbell
set listchars=tab:▷⋅,trail:·
set list
set backspace=indent,eol,start
set autoindent
set ts=2
set sw=2
set sts=2
set et
set eol
set mouse=a
set mousehide
set nobackup
set noswapfile
set incsearch
set nohlsearch
set background=dark
colorscheme solarized

" vim-airline
set laststatus=2
set encoding=utf-8
let g:airline#extensions#tabline#left_sep = ''
let g:airline_left_sep = '»'
let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#fnamemod = ':t'
let g:airline#extensions#tabline#buffer_idx_mode = 1
let g:airline#extensions#branch#enabled=1
let g:airline_theme = 'luna'
nmap <leader>1 <Plug>AirlineSelectTab1
nmap <leader>2 <Plug>AirlineSelectTab2
nmap <leader>3 <Plug>AirlineSelectTab3
nmap <leader>4 <Plug>AirlineSelectTab4
nmap <leader>5 <Plug>AirlineSelectTab5
nmap <leader>6 <Plug>AirlineSelectTab6
nmap <leader>7 <Plug>AirlineSelectTab7
nmap <leader>8 <Plug>AirlineSelectTab8
nmap <leader>9 <Plug>AirlineSelectTab9
nmap <leader>l :bnext<CR>
nmap <leader>h :bprevious<CR>
nmap <leader>bl :ls<CR>

" ctrlp
nmap <leader>p :CtrlP<CR>

" NERDTree
noremap <C-n> :NERDTreeToggle<CR>

All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí