公司的电脑特别渣渣,IDE开了就卡,I can't stand it ! 就折腾用vim来写脚本。之前看了好多关于设置vim的教程,但是总会出现一点小问题,后来自己去查了才解决。在此特意分享一下,centos下可以直接拿来用。
环境:centos7
首先第一点,更新一下python版本,这个很重要,无论如何,首先更新一下。我这里选择安装python2.7.10,也可以是更高版本。
安装python( --enable-unicode=ucs4很重要 ):
自行下载python2.7.10( https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz )
<pre class="prettyprint hljs sql" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">tar -xvf Python-2.7.10.tgz;cd Python-2.7.10;
./configure --prefix=/usr/local/python2.7 --enable-unicode=ucs4;
make && make install;</pre>
安装pip:
<pre class="prettyprint hljs groovy" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">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</pre>
安装vim8.0:
因为系统自带的vim版本太低,所以需要升级
<pre class="prettyprint hljs verilog" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">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</pre>
为了不跟老版本的vim冲突,设置vim命令自动关联到新版本的vim:
<pre class="prettyprint hljs ruby" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">echo -e "alias vim='/usr/local/vim/bin/vim'" >>/root/.bashrc</pre>
安装vim插件管理——bundle:
<pre class="prettyprint hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">cd ~;mkdir .vim;cd .vim;mkdir bundle
yum install -y gcc gcc-c++ make
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim</pre>
接下来是配置vim的配置文件,直接复制到~/.vimrc文件内即可,以下是我个人从网上参考的配置文件,可以复制以下命令直接使用:
<pre class="prettyprint hljs vim" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">cat <<ENDTAG >>~/.vimrc
"bundle config"
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'Lokaltog/vim-powerline'
"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/pre>, '~/pre>] "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
ENDTAG</pre>
直接打开vim并安装插件(如下图)
image.png
等待安装完毕即可:
image.png
接下来安装插件YouCompleteMe,这是一个自动补全语句的插件,非常有用:
前提是安装cmake
<pre class="prettyprint hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">wget https://cmake.org/files/v3.12/cmake-3.12.2.tar.gz;
tar -xvf cd cmake-3.12.2.tar.gz;
cd cmake-3.12.2;
ln -s /usr/local/bin/cmake /usr/bin/cmake</pre>
安装YouCompleteMe:
<pre class="prettyprint hljs vim" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">cd ~/.vim/bundle
git clone https://github.com/Valloric/YouCompleteMe.git
cd YouCompleteMe
./install.py --all</pre>
至此,大功告成,我们看下效果:
喜欢小编的帮忙给个关注哈