1、基础配置查看版本
Vim的版本要求不低于7.3
要求支持python,确保列表中有+python
vim --version
编辑vimrc文件,可以执行:
vim ~/.vimrc
基础配置内容:
set nocompatible
syntax on
filetype plugin indent on
set ic
set hlsearch
set encoding=utf-8
set fileencodings=utf-8,ucs-bom,GB2312,big5
set cursorline
set autoindent
set smartindent
set scrolloff=4
set showmatch
set nu
set mouse=a若在tmux中使用鼠标拖动分隔的屏幕则使用下面
set mouse+=a
if &term =~ '^screen'
" tmux knows the extended mouse mode
set ttymouse=xterm2
endif自动补全括号
inoremap ' ''i
inoremap " ""i
inoremap ( ()i
inoremap [ []i
inoremap { {}O窗口移动(vim的默认方式是Ctrl+w Ctrl+hjkl,但是这样太过复杂,对此进行绑定)
nnoremap
nnoremap
nnoremap
nnoremap 启动bash
noremap : belowright vert term
noremap : below term
# 终端作业模式下,按Ctrl-\ Ctrl-N或i可将模式切换为终端正常python的特殊配置
let python_highlight_all=1
au Filetype python set tabstop=4
au Filetype python set softtabstop=4
au Filetype python set shiftwidth=4
au Filetype python set textwidth=79
au Filetype python set expandtab
au Filetype python set autoindent
au Filetype python set fileformat=unix
autocmd Filetype python set foldmethod=indent
autocmd Filetype python set foldlevel=99
2、配置插件
首先安装vundle:
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
然后在vimrc中加入:
set nocompatible " 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 'gmarik/Vundle.vim'
" Add all your plugins here (note older versions of Vundle used Bundle instead of Plugin)
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
关闭后重新打开vim,在normal模式下执行:
:PluginInstall
3、推荐的插件YouCompleteMe官方安装方法github.com
第一步:
sudo apt-get install build-essential cmake python-dev python3-dev
第二步:
此过程时间较长,YCM比较大。
Plugin 'Valloric/YouCompleteMe'
若此过程耗时太长,可以直接拷贝YouCompleteMe文件。密码: 6p27pan.baidu.com
第三步:
经历过上述2个步骤后,YouCompleteMe 插件还没法使用,此时打开 Vim 时会看到如下的报错:
The ycmd server SHUT DOWN (restart with ‘:YcmRestartServer’). YCM core library not detected; you need to compile YCM before using it. Follow the instructions in the documentation.
这是因为,YouCompleteMe 需要手工编译出库文件 ycm_core.so (以及依赖的libclang.so) 才可使用。运行:
cd ~/.vim/bundle/YouCompleteMe
./install.py --clang-completer
更改自动补全颜色
highlight Pmenu ctermfg=15 ctermbg=0 guifg=#ffffff guibg=#0000ffAirline
Plugin 'https://github.com/bling/vim-airline'
set laststatus=2
set t_Co=256Nerdtree
Plugin 'https://github.com/scrooloose/nerdtree'
""将F2设置为开关NERDTree的快捷键
nnoremap :NERDTreeToggle
""修改树的显示图标
let g:NERDTreeDirArrowExpandable = '+'
let g:NERDTreeDirArrowCollapsible = '-'
""窗口位置
let g:NERDTreeWinPos='left'
""窗口尺寸
let g:NERDTreeSize=30
""窗口是否显示行号
let g:NERDTreeShowLineNumbers=1
""不显示隐藏文件
let g:NERDTreeHidden=0Ctags在线安装
sudo apt-get install ctags (ubuntu)
2. 手动安装Exuberant Ctagsctags.sourceforge.net
以5.8版本ctags-5.8.tar.gz为例,解压后:
cd ctags-5.8
./configure
make
安装成功后,要为源码文件生成tags文件
递归的为当前目录及子目录下的所有代码文件生成tags文件
ctags -R
为某些源码生成tags文件,使用如下命令
ctags filename.py
使用方法
在vim打开源码时,指定tags文件,才可正常使用,通常手动指定,在vim命令行输入:
:set tags=./tags(当前路径下的tags文件)
若要引用多个不同目录的tags文件,可以用逗号隔开,或者,设置 ~/.vimrc,加入一行,则不用手动设置tags路径:
set tags=~/path/tags
快捷键
Ctrl + ] #跳转到变量或函数的定义处,或者用命令
:ta name
Ctrl + o/t #返回到跳转前的位置。
:tags #会列出查找/跳转过程(经过的标签列表)Tagbar
tagbar有点像IDE的structure窗口,展示类的各个函数和变量等。
git clone https://github.com/majutsushi/tagbar.git ~/.vim/bundle/tagbar
cd ~/.vim/bundle/tagbar/
mv autoload/ doc/ plugin/ syntax/ ~/.vim
配置如下:
nmap :TagbarToggleAutoformat
这个插件vim-autoformat能够自动的一键格式化代码
这款插件需要已经安装有代码格式化工具,安装方式是在终端下执行:
pip install autopep8
推荐的配置是:
Plugin 'Chiel92/vim-autoformat'
nnoremap :Autoformat
let g:autoformat_autoindent = 0
let g:autoformat_retab = 0
let g:autoformat_remove_trailing_spaces = 0Rainbow
使用不同的颜色高亮匹配的括号:rainbow_parenthesesgithub.com
Plugin 'kien/rainbow_parentheses.vim'
let g:rbpt_colorpairs = [
\ ['brown', 'RoyalBlue3'],
\ ['Darkblue', 'SeaGreen3'],
\ ['darkgray', 'DarkOrchid3'],
\ ['darkgreen', 'firebrick3'],
\ ['darkcyan', 'RoyalBlue3'],
\ ['darkred', 'SeaGreen3'],
\ ['darkmagenta', 'DarkOrchid3'],
\ ['brown', 'firebrick3'],
\ ['gray', 'RoyalBlue3'],
\ ['darkmagenta', 'DarkOrchid3'],
\ ['Darkblue', 'firebrick3'],
\ ['darkgreen', 'RoyalBlue3'],
\ ['darkcyan', 'SeaGreen3'],
\ ['darkred', 'DarkOrchid3'],
\ ['red', 'firebrick3'],
\ ]
let g:rbpt_max = 16
let g:rbpt_loadcmd_toggle = 0
au VimEnter * RainbowParenthesesToggle
au Syntax * RainbowParenthesesLoadRound
au Syntax * RainbowParenthesesLoadSquare
au Syntax * RainbowParenthesesLoadBraces
4、配色方案
个人最喜欢配色:jellybeansJellybeansgithub.com