准备工作
安装依赖文件
1 2 3 4 5 |
$ yum install -y gcc gcc-c++ ruby ruby-devel lua lua-devel \ ctags git tcl-devel ncurses-devel \ perl perl-devel perl-ExtUtils-ParseXS \ perl-ExtUtils-CBuilder \ perl-ExtUtils-Embed python-devel |
python3的devel
1 2 3 4 5 6 7 8 9 10 |
$ yum search python3 | grep devel output: python3-cairo-devel.x86_64 : Libraries and headers for python3-cairo python3-devel.x86_64 : Libraries and header files needed for Python 3 : development python34-devel.x86_64 : Libraries and header files needed for Python 3 : development $ yum install -y python3-devel.x86_64 |
vim安装
下载最新版的vim
1 |
$ git clone https://github.com/vim/vim.git |
卸载旧版本的vim
1 2 3 |
$ make uninstall $ make clean $ make distclean |
配置vim源码的编译属性
1 2 3 4 5 6 7 8 9 10 11 |
./configure --with-features=huge \ --enable-multibyte \ --enable-rubyinterp=yes \ --enable-python3interp=yes \ --with-python3-config-dir=/usr/local/python37/lib/python3.7/config-3.7m-x86_64-linux-gnu \ --enable-pythoninterp=yes \ --with-python-config-dir=/usr/local/lib/python2.7/~~~~ \ --enable-perlinterp=yes \ --enable-luainterp=yes \ --enable-cscope \ --prefix=/usr/local/vim8 |
这里需要注意三个参数
--enable-python3interp
这里需要设置为yes
默认值是no
--enable-python3-config-dir
这里需要设置为你自己的python的config路径 我的是/usr/local/python37/lib/python3.7/config-3.7m-x86_64-linux-gnu
--prefix
设置vim的安装目录,如果不设置会和系统应用安装在一起,最好设置一下。
安装vim
1 |
make && make install |
环境变量添加vim
通过/usr/local/vim8/bin/vim运行vim命令。将vim命令路径添加到系统变量,就可以直接运行vim了。
1 |
$ /usr/local/vim8/bin/vim /etc/profile.d/path.sh |
添加以下内容:
1 2 3 4 5 |
#!/bin/bash export PATH=$PATH:/usr/local/vim8/bin/ $ source /etc/profile.d/path.sh $ vim |
验证python3的支持
进入vim, 使用 : echo has(‘python3’)输出1,证明成功。
安装Vundle插件管理器
Vundle是vim下使用最为广泛的插件管理器,下面就为大家介绍一下如何安装Vundle。
首先在Shell命令行中运行以下命令:
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
该命令将把Vundle插件管理器下载到~/.vim/bundle/文件夹下,此后就需要使用配置.vimrc文件来安装插件。
首先要在你的主目录下新建一个名为.vimrc的vim配置文件。在shell中运行以下命令:
1 2 3 |
cd ~ touch .vimrc ls |
这样.vimrc文件就新建好了,此时打开.vimrc文件里面是空的。
然后将下面的Vundle配置添加到.vimrc文件的顶部:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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命令
安装YouCompleteMe插件
YouCompleteMe(YCM)是一个功能非常强大的代码补全工具,可说是python开发的最佳搭档。
准备工作
在安装YCM前,还需要先安装一些必须的软件:
1 2 |
yum install python-dev python3-dev cmake # 前文已安装过了,再次确认而已 |
安装步骤
vimrc安装
在~/.vimrc上文的空白处添加YCM配置:
Plugin ‘Valloric/YouCompleteMe’
PluginInstall
运行vim在normal模式下运行:PluginInstall,系统即会将YCM安装到~/.vim/bundle/目录下。
这里需要特别提醒大家的是YCM的体积比较大,等待的时间会比较长,有时会出错退出安装,这时在运行vim时,在窗口下部会出现红色的YouCompleteMe unavailable: No module named ‘future’的提示。这是由于YCM没有下载完毕所造成的,这时可以换用Git来继续安装YCM:
在Shell下输入cd ~/.vim/bundle/YouCompleteMe进入YCM目录,然后输入
git submodule update –init –recursive命令,如下载过程中又出现中断出错,就继续运行此命令。
编译安装
在克隆完成之后就需要进行安装:
如需要C语言家族支持就在终端里输入以下命令:
./install.py –clang-completer
如不需要C语言家族支持的话直接输入以下命令:
./install.py
如需C#语言支持的话可在运行./install.py命令时添加–cs-completer参数。
如需Go语言支持的话可在运行./install.py命令时添加–go-completer参数。
如需Java语言支持的话可在运行./install.py命令时添加–java-completer参数。
我们也可以使用简单一点的方法,直接添加–all参数来添加全语言支持。
./install.py –all
上述命令运行时根据你的机器环境可能会提示你先执行一个Git命令克隆一些必须的库,按照提示运行就可以了。克隆完成之后再执行./install.py。
配置YCM
运行完毕后可在.vimrc文件中添加以下配置来让完成补全之后preview窗口自动消失:
let g:SimpyIFold_docstring_preview=1
复制.ycm_extra_conf.py文件至~/.vim目录下
cp ~/.vim/bundle/YoucompleteMe/third_parth/ycmd/examples/.ycm_extra_conf.py ~/.vim/
在.vimrc中添加YCM配置,打开.vimrc文件,在文件最后加入:
1 2 |
let g:ycm_server_python_interpreter='/usr/bin/python3' let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py' |
此处要填写你自己机器上的python解释器的版本位置。
以下配置你可以根据自己需求进行配置:
set completeopt-=preview
补全内容不以分割子窗口形式出现,只显示补全列表
let g:ycm_autoclose_preview_window_after_completion=1
完成操作后自动补全窗口不消失
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
"添加YouCompleteMe代码补全插件 Plugin 'Valloric/YouCompleteMe' "youcompleteme 默认tab s-tab 和自动补全冲突 ""let g:ycm_key_list_select_completion=['<c-n>'] let g:ycm_key_list_select_completion = ['<Down>'] "let g:ycm_key_list_previous_completion=['<c-p>'] let g:ycm_key_list_previous_completion = ['<Up>'] "关闭加载.ycm_extra_conf.py提示 let g:ycm_confirm_extra_conf=0 " 开启 YCM 基于标签引擎 let g:ycm_collect_identifiers_from_tags_files=1 " 从第2个键入字符就开始罗列匹配项 let g:ycm_min_num_of_chars_for_completion=2 " 禁止缓存匹配项,每次都重新生成匹配项 let g:ycm_cache_omnifunc=0 " 语法关键字补全 let g:ycm_seed_identifiers_with_syntax=1 "force recomile with syntastic nnoremap <F5> :YcmForceCompileAndDiagnostics<CR> "nnoremap <leader>lo :lopen<CR> "open locationlist "nnoremap <leader>lc :lclose<CR> "close locationlist inoremap <leader><leader> <C-x><C-o> "在注释输入中也能补全 let g:ycm_complete_in_comments = 1 "在字符串输入中也能补全 let g:ycm_complete_in_strings = 1 "注释和字符串中的文字也会被收入补全 let g:ycm_collect_identifiers_from_comments_and_strings = 0 |
其他配置和插件
NERDTree
使用NERDTree插件可以在Vim窗口中显示一个文件树。
1 2 3 4 5 6 |
"添加nerdtree插件 Bundle 'scrooloose/nerdtree' "设置按F2启动NerdTree map <F2> :NERDTreeToggle<CR> "隐藏目录树中的.pyc文件 let NERDTreeIgnore=['\.pyc$', '\~$'] "ignore files in NERDTree |
syntastic
一款python语法检测插件
1 2 3 4 |
"python语法检测 Plugin 'scrooloose/syntastic' "添加PEP8代码风格检查 Plugin 'nvie/vim-flake8' |
配色方案
1 2 3 4 5 6 7 8 9 10 11 |
"配色方案 Plugin 'jnurmine/Zenburn' Plugin 'altercation/vim-colors-solarized' "设置主题颜色,以及设置快捷键F5 if has('gui_running') set background=dark colorscheme solarized else colorscheme zenburn endif call togglebg#map("<F5>") |
代码折叠插件
1 2 3 4 5 6 7 8 9 |
"代码折叠插件 Plugin 'tmhedberg/SimpylFold' "开启代码折叠 set foldmethod=indent set foldlevel=99 "设置快捷键为空格 noremap <space> za "显示折叠代码的文档字符串 let g:SimpylFold_docstring_preview=1 |
自动缩进
1 2 |
"自动缩进 Plugin 'vim-scripts/indentpython.vim' |
文件搜索
1 2 |
"在vim的normal模式下搜索文件 Plugin 'kien/ctrlp.vim' |
vim配置范例
Vim下的插件非常之多,在这里就简要给大家介绍了以上两个,你可以上网去搜索更多你感兴趣的插件,安装完插件后我们就要来对Vim进行配置,使其更符合我们的工作习惯。对Vim进行配置是通过.vimrc文件进行的。下面贴出的是我使用的vim配置文件,有需要的朋友可以看一看。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
"显示相关 "set shortmess=atI "不显示帮助乌干达的提示 colorscheme delek "设置配色方案 set showcmd "输入的命令显示出来 syntax on "语法高亮 set tabstop=4 "Tab键的宽度设为4 set softtabstop=4 set shiftwidth=4 "统一缩进为4 set autoindent "开启自动缩进,保持缩进值与上一行相等 autocmd InsertLeave * se nocul "用浅色高亮当前行 autocmd InsertEnter * se cul "用浅色高亮当前行 set ruler "显示当前光标行列位置 set mouse=a "支持鼠标 set laststatus=2 "显示状态栏(默认值为1,无法显示状态栏) if has('gui_running') set t_Co=256 endif set guifont=Mono set autoread "当文件在外部修改时,vim自动更新载入 set ignorecase "搜索时忽略大小写 set foldmethod=syntax set nofoldenable "此两行为折叠代码 "自定义键映射 "实现CTRL-w保存操作 nnoremap <C-w> :w<cr> imap <C-w> <Esc>:w<cr>i "映射以F5打开NERDTree nnoremap <silent> <F5> :NERDTree<CR> "映射自动补全括号 inoremap ( ()<ESC>i inoremap [ []<ESC>i inoremap { {}<ESC>i inoremap " ""<ESC>i inoremap ' ''<ESC>i set encoding=utf-8 set fenc=utf-8 set fencs=utf-8,usc-bom,euc-jp,gb18030,gbk,gb2312,cp936,big-5 set enc=utf-8 let &termencoding=&encoding set nu set nocompatible " required,关闭vi兼容模式 filetype off " required set list listchars=tab:>-,trail:-,eol:$ "设定tab键显示为>-,每行行尾显示$ set report=0 "python运行配置 map <F6> :w<cr>:!python3 %<cr>:q<cr> "按F6键进行python调试 au BufNewFile,BufRead *.py "设置py文件支持PEP8风格 \ set tabstop=4r \ set softtabstop=4 \ set shiftwidth=4 \ set textwidth=79 \ set expandtab \ set autoindent \ set fileformat=unix "auto add pyhton header --start 自动添加py文件头 autocmd BufNewFile *.py 0r ~/.vim/vim_template/vim_python_header autocmd BufNewFile *.py ks|call FileName()|'s autocmd BufNewFile *.py ks|call CreatedTime()|'s fun FileName() if line("$") > 10 let l = 10 "这里是字母L else let l =line("$") endif exe "1,".l."g/File Name:.*/s/File Name:.*/File Name:".expand("%") "最前面是数字1,这里的File Name:和模板中一致 endfun fun CreatedTime() if line("$") > 10 let l=10 else let l=line("$") endif exe "1,".l."g/Created Time:.*/s/Created Time:.*/Created Time:".strftime("%Y-%m-%d %T") "这里Create Time:要和模板中一致 endfun "auto add python header --end "vundle配置 " 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) Plugin 'Valloric/YouCompleteMe' Plugin 'jistr/vim-nerdtree-tabs' Plugin 'scrooloose/nerdtree' Bundle 'davidhalter/jedi-vim' " All of your Plugins must be added before the following line call vundle#end() " required filetype plugin indent on " required let g:ycm_server_python_interpreter='/usr/bin/python3' let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py' let NERDTreeIgnore=['\.pyc$', '\~$'] "ignore files in NERDTree let g:ycm_autoclose_preview_window_after_completion=1 |
在这个配置文件中,我主要设置了以下功能:
- 设置了vim使用的配色方案,colorscheme delek,这里我使用的是delek配色方案,大家也可以设置自己喜欢的配色方案,vim本身就自带有多套的配色方案。在Shell中输入vim命令,在normal模式下,normal模式就是你刚进入vim时的那个状态,输入:colorscheme <Tab>就可以在切换使用的配色方案,但这种方法对于配色方案的修改只是一次性的,需要永久更换配色方案的话,就请记下配色方案的名字,将.vimrc中的配色方案名称更改就可以了。如果对于自带配色方案不满意的话,也可以自行上网下载一些大神配置好配色方案。将下载好的配色方案复制到/usr/share/vim/vim74/colors/中,在你的机器上文件夹的名字可能不是vim74,你可以进入cd /usr/share/vim/文件夹中看看自己的文件夹名,拷贝进去后你的配色方案就可使用了。如果你想配置自己的配色方案,请自行百度。
- 设置了显示行号set nu,CTRL-w保存文件,各类括号和引号的自动补全。
- 设置按F5打开NERDTree插件nnoremap <silent> <F5> :NERDTree<CR>,你也可以设置自己的快捷键。
- 设置按F6自动运行编写的python程序。map <F6> :w<cr>:!python3 %<cr>:q<cr>,此处也可自行设置。
- 设置了自动添加python文件头。效果如下:
模板存放在~/.vim/vim_template/路径下,模板名为`vim_python_header’ ,具体内容如下,可自行创建:
1 2 3 4 |
#!/usr/bin/env python3 # -*- coding:utf-8 -*- #File Name: #Created Time: |
- 配置了python代码缩进
1 2 3 4 5 6 7 8 |
au BufNewFile,BufRead *.py "设置py文件支持PEP8风格 \ set tabstop=4r \ set softtabstop=4 \ set shiftwidth=4 \ set textwidth=79 \ set expandtab \ set autoindent \ set fileformat=unix |