vim简单配置教程,简洁方便(适用于python coding)

公司的电脑特别渣渣,IDE开了就卡,I can't stand it ! 就折腾用vim来写脚本。之前看了好多关于设置vim的教程,但是总会出现一点小问题,后来自己去查了才解决。在此特意分享一下,centos7环境(centos6应该差不多,没有试过)下可以直接拿下面的shell脚本来用。

环境:centos7.5及以下
用户:root
初始目录:/opt
安装目录:/usr/local/

首先第一点,更新一下python版本,这个很重要,无论如何,首先更新一下。我这里选择安装python2.7.10,也可以是更高版本。

1、编译安装python(参数--enable-unicode=ucs4很重要)
下载python2.7.10并编译安装,本文档安装路径为/usr/local/python2.7

cd /opt;
yum install -y wget gcc gcc-c++ make git ncurses-devel;
wget https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz;
tar -xvf Python-2.7.10.tgz;cd Python-2.7.10;
./configure --prefix=/usr/local/python2.7 --enable-unicode=ucs4;
make && make install;

安装pip:

cd /opt;
wget https://bootstrap.pypa.io/ez_setup.py;
/usr/local/python2.7/bin/python ez_setup.py;
cd /usr/local/python2.7/bin/;./easy_install pip;

2、安装vim8.0
因为系统自带的vim版本太低,所以需要升级

cd /opt;
yum intall git -y;
git clone https://github.com/vim/vim.git && cd vim;
yum install ncurses-devel -y;
./configure --with-features=huge --with-python-config-dir=/usr/local/python2.7/lib/python2.7/config --prefix=/usr/local/vim --enable-pythoninterp=yes --enable-python3interp=yes;
make && make install

为了不跟老版本的vim冲突,设置vim命令自动关联到新版本的vim:

echo -e "alias vim='/usr/local/vim/bin/vim'" >> ~/.bashrc;
source ~/.bashrc

3、安装vim插件管理——bundle

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

4、配置vim的配置文件
将配置复制到~/.vimrc文件内即可,以下是我个人从网上参考的配置文件,可以复制以下命令直接使用:

touch ~/.vimrc;
cat <<ENDTAG >>~/.vimrc
"bundle config"                                                                                                                                                                                                        
filetype off 
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'Lokaltog/vim-powerline'
"自动补全括号                                                                                                                                                                
Plugin 'jiangmiao/auto-pairs'
"YouCompleteMe config
Plugin 'Valloric/YouCompleteMe'
let g:ycm_autoclose_preview_window_after_completion=1
map <leader>g  :YcmCompleter GoToDefinitionElseDeclaration<CR>
"上面的第一行确保了在你完成操作之后,自动补全窗口不会消失,第二行则定义了“转到定义”的快捷方式
"python with virtualenv support
py << EOF
import os
import sys 
if 'VIRTUAL_ENV' in os.environ:
  project_base_dir = os.environ['VIRTUAL_ENV']
  activate_this = os.path.join(project_base_dir, 'bin/activate_this.py')
  execfile(activate_this, dict(__file__=activate_this))
EOF
"语法高亮,每次保存文件时Vim都会检查代码的语法
Plugin 'scrooloose/syntastic'
Plugin 'nvie/vim-flake8'
call vundle#end()
filetype plugin indent on  
Plugin 'scrooloose/nerdtree' "树形结构
Plugin 'jistr/vim-nerdtree-tabs'
Plugin 'posva/vim-vue' "vue editor
" NERDTree config
map <F2> :NERDTreeToggle<CR>
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif
let NERDTreeIgnore=['\.pyc$', '\~$'] "ignore files in NERDTree 
autocmd FileType vue syntax sync fromstart "auto start vue format
autocmd BufRead,BufNewFile *.vue setlocal filetype=vue.html.javascript.css
Plugin 'kien/ctrlp.vim'  "ctrl + p 搜索
"去掉vi的一致性"
set nocompatible
""显示行号"
set number
" 隐藏滚动条"    
set guioptions-=r 
set guioptions-=L
set guioptions-=b
" "隐藏顶部标签栏"
set showtabline=0
" "设置字体"
set guifont=Monaco:h13                                                                                                                                                                                                 
syntax on   "开启语法高亮"
let python_highlight_all=1
let g:solarized_termcolors=256  "solarized主题设置在终端下的设置"
set background=dark     "设置背景色"
"colorscheme solarized"
"set nowrap  "设置不折行" "
set fileformat=unix "设置以unix的格式保存文件"
set cindent     "设置C样式的缩进格式"
set tabstop=4   "设置table长度"
set shiftwidth=4        "同上"
set showmatch   "显示匹配的括号"
set scrolloff=5     "距离顶部和底部5行"
set laststatus=2    "命令行为两行"
set fenc=utf-8      "文件编码"
set backspace=2
"set mouse=a     "启用鼠标"
set selection=exclusive
set selectmode=mouse,key
set matchtime=5
set ignorecase      "忽略大小写"
set incsearch
set hlsearch        "高亮搜索项"
set noexpandtab     "不允许扩展table"
set whichwrap+=<,>,h,l
set autoread
set cursorline      "突出显示当前行"
set cursorcolumn        "突出显示当前列"
set splitbelow
set splitright
"split navigations
nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>
" Enable folding
set foldmethod=indent
set foldlevel=99
nnoremap <space> za
Plugin 'tmhedberg/SimpylFold'
let g:SimpylFold_docstring_preview=1
"按F5运行python"
map <F5> :Autopep8<CR> :w<CR> :call RunPython()<CR>
function RunPython()                                                                                                                                                                                                   
    let mp = &makeprg
    let ef = &errorformat
    let exeFile = expand("%:t")
    setlocal makeprg=python\ -u 
    set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m
    silent make %
    copen
    let &makeprg = mp
    let &errorformat = ef
endfunction
"关闭load ycm_extra_conf.py的提示
let g:ycm_confirm_extra_conf = 0
ENDTAG

5、安装插件
直接在终端输入vim,打开vim界面并输入":PluginInstall"安装插件(如下图)。后续每次在~/.vimrc添加新的插件都只需重新PluginInstall即可。


安装插件

等待安装完毕即可:


插件安装完毕

6、安装插件YouCompleteMe
这是一个自动补全单词的插件,非常有用,前提是安装cmake,安装cmake会比较慢。

cd /opt;
wget https://cmake.org/files/v3.12/cmake-3.12.2.tar.gz;
tar -xvf cmake-3.12.2.tar.gz;
cd cmake-3.12.2;
./bootstrap && make && make install;
ln -s /usr/local/bin/cmake /usr/bin/cmake;

安装YouCompleteMe:

cd ~/.vim/bundle;
git clone https://github.com/Valloric/YouCompleteMe.git;
cd YouCompleteMe;
./install.py --all;

至此,大功告成,我们看下效果:


vim

推荐阅读更多精彩内容