Vim as IDE

来源:互联网 发布:二手车置换划算吗 知乎 编辑:程序博客网 时间:2024/05/14 18:35

Reblogged from: http://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/autoloadcurl -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 confset background=darktry    colorscheme solarizedcatchendtry

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=9endif
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 = 1try  let g:unite_source_rec_async_command='ag --nocolor --nogroup -g ""'  call unite#filters#matcher_default#use(['matcher_fuzzy'])catchendtry" search a file in the filetreennoremap <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 dirnmap ° :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 interactivevnoremap <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
原创粉丝点击