Vim as IDE

Reblogged fromhttp://yannesposito.com/Scratch/en/blog/Vim-as-IDE/

This short article is about how the author use Vim as an IDE. Mainly by using some great plugins.


1. Vim Plugin Manager

There are a lot of Vim plugins. To manage them I use vim-plug.

To install it:

mkdir -p ~/.vim/autoload
curl -fLo ~/.vim/autoload/plug.vim \
             https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

☞ Note I have two parts in my .vimrc. The first part contains the list of all my plugins. The second part contains the personal preferences I setted for each plugin. I’ll separate each part by ... in the code.

2. Survival

2.1 Colorscheme

Solarized theme

Before anything, you should protect your eyes using a readable and low contrast colorscheme.

For this I use solarized dark. To add it, you only have to write this in your ~/.vimrc file:

call plug#begin('~/.vim/plugged')

Plug 'altercation/vim-colors-solarized'

call plug#end()

" -- solarized personal conf
set background=dark
try
    colorscheme solarized
catch
endtry

2.2 Minimal hygiene

You should be able to see and destroy trailing whitespaces.

Trim whitespaces
Plug 'bronson/vim-trailing-whitespace'

You can clean trailing whitespace with :FixWhitespace.

And also you should see your 80th column.

if (exists('+colorcolumn'))
    set colorcolumn=80
    highlight ColorColumn ctermbg=9
endif
80th column

3. File Management

One of the most important hidden skills in programming is the ability to search and find files in your projects.

The majority of people use something like NERDTree. This is the classical left column with a tree of files of your project. I stopped to use this. And you should probably too.

I switched to unite. No left column lost. Faster to find files. Mainly it works like Spotlight on OS X.

First install ag (the silver search). If you don’t know ack or ag your life is going to be upgraded. This is a simple but essential tool. It is mostly a grep on steroids.

" Unite
"   depend on vimproc
"   ------------- VERY IMPORTANT ------------
"   you have to go to .vim/plugin/vimproc.vim and do a ./make
"   -----------------------------------------
Plug 'Shougo/vimproc.vim'
Plug 'Shougo/unite.vim'

...

let g:unite_source_history_yank_enable = 1
try
  let g:unite_source_rec_async_command='ag --nocolor --nogroup -g ""'
  call unite#filters#matcher_default#use(['matcher_fuzzy'])
catch
endtry
" search a file in the filetree
nnoremap <space><space> :split<cr> :<C-u>Unite -start-insert file_rec/async<cr>
" reset not it is <C-l> normally
:nnoremap <space>r <Plug>(unite_restart)

Now type space twice. A list of files appears. Start to type some letters of the file you are searching for. Select it, type return and bingo the file opens in a new horizontal split.

Unite example

If something goes wrong just type <space>r to reset the unite cache.

Now you are able to search file by name easily and efficiently.

Now search text in many files. For this you use ag:

Plug 'rking/ag.vim'
...
" --- type ° to search the word in all files in the current dir
nmap ° :Ag <c-r>=expand("<cword>")<cr><cr>
nnoremap <space>/ :Ag

Don’t forget to add a space after the :Ag.

These are two of the most powerful shortcut for working in a project. using ° which is nicely positioned on my azerty keyboard. You should use a key close to *.

So what ° is doing? It reads the string under the cursor and search for it in all files. Really useful to search where a function is used.

If you type <space>/ followed by a string, it will search for all occurrences of this string in the project files.

So with this you should already be able to navigate between files very easily.

4. Language Agnostic Plugins

4.1 Git

Show modified lines

Show which line changed since your last commit.

Plug 'airblade/vim-gitgutter'

And the “defacto” git plugin:

Plug 'tpope/vim-fugitive'

You can reset your changes from the latest git commit with :Gread. You can stage your changes with :Gwrite.

Reset changes

4.2 Align things

Plug 'junegunn/vim-easy-align'

...

" Easy align interactive
vnoremap <silent> <Enter> :EasyAlign<cr>

Just select and type Return then space. Type Return many type to change the alignments.

If you want to align the second column, Return then 2 then space.

Easy align example

4.3 Basic auto completion: C-n & C-p

Vim has a basic auto completion system. The shortcuts are C-n and C-p while you are in insert mode. This is generally good enough in most cases. For example when I open a file not in my configured languages.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值