Linux / Mac 下 Vim IDE 化

编辑于 2016-11-13

* 移动设备下, 可左滑手指以查看较宽代码

vim ide

可自定义的编辑器 Vim. 一次配置, 全平台可用.

要求 Vim 版本在 7.3 以上. 本文假定你已经知道最基本的 Vim 操作.

基本配置


先安装 Molokai 配色, 下载地址: github.com/tomasr/molokai

下载后把 color/molokai.vim 放到 vim 颜色目录下(一般在 /usr/share/vim/vim74/colors/ ). 如果是 Mac , 可能要先关掉系统保护 (rootless).

将下面的内容放在 Vim 的配置文件里即可(默认在~/.vimrc, 后文假定是此文件).

" Configuration file for vim
set modelines=0         " CVE-2007-2438

" Normally we use vim-extensions. If you want true vi-compatibility
" remove change the following statements
set nocompatible        " Use Vim defaults instead of 100% vi compatibility
set backspace=2         " more powerful backspacing

" Don't write backup file if vim is being called by "crontab -e"
au BufWrite /private/tmp/crontab.* set nowritebackup nobackup
" Don't write backup file if vim is being called by "chpass"
au BufWrite /private/etc/pw.* set nowritebackup nobackup

syntax on
set cindent
set ruler
set nu

set shiftwidth=4
set softtabstop=4

" No auto warp line
set nowarp

" Set mouse choice
set mouse=a

colorscheme molokai

" Highlight current line
au WinLeave * set nocursorline nocursorcolumn
au WinEnter * set cursorline
set cursorline

" Convert the tab key
set ts=4
set expandtab
set autoindent

" Remember last update or view postion"
" Only do this part when compiled with support for autocommands
if has("autocmd")
        " In text files, always limit the width of text to 78 characters
    autocmd BufRead *.txt set tw=78
    " When editing a file, always jump to the last cursor position
    autocmd BufReadPost *
 \   if line("'\"") > 0 && line ("'\"") <= line("$") |
 \       exe "normal g'\"" |
 \   endif
endif

上面的每条命令都有注释, 而且都可从百度找到. 这里只说下大致含义:

  • 设置兼容性

  • 语法上色

  • 显示行号和当前行/列

  • tab 缩进转为4个空格

  • tab 缩进设置为4个字符宽度

  • C 语言等自动缩进

  • 设置鼠标可选择

  • 打开文件时跳到上次位置

  • 设置配色为 molokai

插件

Vundle 能从网络上自动安装插件. 安装它的方法很简单, 输入:

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

Vundle 更多说明: github.com/VundleVim/Vundle.vim

然后把下面的配置插入到上文中 ~/. vimrc 文件的最前面:

" Vendle
set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
Plugin 'scrooloose/nerdtree'
Plugin 'Lokaltog/vim-powerline'
Plugin 'majutsushi/tagbar'
Plugin 'Rip-Rip/clang_complete'
Plugin 'rking/ag.vim'

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

"
" Brief help
" :PluginList       - lists configured plugins
" :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line

可见我装了5个 Plugin (插件), 都可以在 github 上搜索到.

然后重启 Vim, 在普通 (Normal)模式下输入 :PluginInstall (有冒号) 即可自动安装. 使用 vpn 可大幅加速安装.

下面是插件的说明和配置:

  • scrooloose/nerdtree

    左侧的目录树, 可按 F3 显示/隐藏

  • Lokaltog/vim-powerline

    好看的状态条

  • majutsushi/tagbar

    右侧的变量/函数列表, 需要系统安装 clang(轻量级 C 语言语法解析). 比如若是 ubuntu 16, 直接用 apt-get install apt-get install libclang-3.8-dev 安装. mac 直接用 brew install clang 安装即可. 在编辑 C/CPP 文件时会自动打开, 后面会给出配置.

  • Rip-Rip/clang_complete

    C 语言代码补全, 需要安装并配置 clang 的 lib 路径.
    如 Linux 下一般是: let g:clang_library_path='/usr/lib/llvm-3.8/lib'
    Mac 下一般是: let g:clang_library_path='/Library/Developer/CommandLineTools/usr/lib'
    如果找不到, 自己去谷歌找.
    此外, vim 需要开启 python2 的支持.
    安装后, 按 Ctrl+N 补全代码.

  • rking/ag.vim

    图下面的搜索结果就是他的功劳, 可搜索任意目录下的文件内容(默认是当前目录). 此插件依赖搜索工具 ag. ag 与 ack 很像, 但是速度更快.
    同 clang, Linux / Mac 下直接用命令安装 ag 即可.
    输入:Ag text 即可在当前目录下的文件中搜索文本 text.
    输入:Ag text ../a 即可在上一级的 a 目录下的文件中搜索文本 text.

最后统一给出这些插件的配置, 加入到 ~/.vimrc 文件末尾即可.

"NERDTree
autocmd vimenter * NERDTree
wincmd w
"Default foucs on right
autocmd VimEnter * wincmd w
map <F3> :NERDTreeMirror<CR>
map <F3> :NERDTreeToggle<CR>
"close vim if the only window left open is a NERDTree
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif

set autochdir
let NERDTreeShowBookmarks=1
let NERDTreeIgnore=['\~$', '\.pyc$', '\.swp$']


" Tagbar
let g:tagbar_width=35
let g:tagbar_autofocus=1
nmap <F4> :TagbarToggle<CR>

autocmd BufReadPost *.cpp,*.c,*.h,*.hpp,*.cc,*.cxx call tagbar#autoopen()


"map g<C-]> :cs find 3 <C-R>=expand(“<cword>”)<CR><CR>
"map g<C-/> :cs find 0 <C-R>=expand(“<cword>”)<CR><CR>

"clang_complete
let g:clang_library_path='/Library/Developer/CommandLineTools/usr/lib'

其他资料

一键保存、编译、运行 C/C++: www.oschina.net/code/snippet_574132_13351