经过三天痛苦经历,终于搞定了Ubuntu下的vim搭配python配置,百度看了好多人的教程,都不顺利,所以想写写自己的安装过程,希望对一些人有帮助。
其中遇到了好多问题,最主要是YouCompleteMe的安装,简直折磨,什么ycmd啊,什么future啊,什么install.py啊,什么无法自动补全啊等等
这代码里有很多功能我标注了选用,根据个人喜好吧,想用了就把引号去掉,最后建议大家把中文都删掉,以免遇到问题虽然我没遇到,下边我还会做详细解释
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 'tpope/vim-fugitive'
" plugin from http://vim-scripts.org/vim/scripts.html
" Plugin 'L9'
" Git plugin not hosted on GitHub
Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" Install L9 and avoid a Naming conflict if you've already installed a
" different version somewhere else.
" Plugin 'ascenator/L9', {'name': 'newL9'}
"文件浏览,文件树形结构插件
Plugin 'scrooloose/nerdtree'
"想用TAB键配合文件树的插件
Plugin 'jistr/vim-nerdtree-tabs'
"语法检测和高亮的插件
Plugin 'scrooloose/syntastic'
"这个小插件可以添加PEP8风格检查
Plugin 'nvie/vim-flake8'
"阅读项目
Plugin 'majutsushi/tagbar'
"配色方案插件
Plugin 'jnurmine/Zenburn'
Plugin 'altercation/vim-colors-solarized'
"代码折叠优化的插件(不安装也不影响折叠,根据个人喜好吧)
Plugin 'tmhedberg/SimpylFold'
"解决某些情况下缩进混乱的插件
Plugin 'vim-scripts/indentpython.vim'
" cs Plugin "支持自动补全的最好的插件,非常的难装,99%的人都卡在这个插件
Plugin 'Valloric/YouCompleteMe'
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" 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
"对YouCompleteMe插件的一个小补充(用不用都可以)
"第一行代码让完成操作后自动补全窗口不会消失
"第二行定义"转到定义"的快捷方式
let g:ycm_autoclose_preview_window_after_completion=1
map g :YcmCompleter GoToDefinitionElseDeclaration
"let g:ycm_python_binary_path = ' /home/brucelau/anaconda3/bin/python3.6'
"let g:ycm_python_binary_path = '/home/brucelau/anaconda3/envs/fcn/bin/python3.7'
"let g:ycm_python_binary_path = '/home/brucelau/anaconda3/envs/fcn/bin/python2.7'
"split navigations "使用ctrl+hjkl代替切换屏幕(建议加上,很方便)
nnoremap
nnoremap
nnoremap
nnoremap
" Enable folding "开启方法和类的折叠(建议加上,很方便)
set foldmethod=indent
set foldlevel=99
" Enable folding with the spacebar "使用空格折叠(代替手动折叠)
nnoremap za
"看到折叠代码的文档字符串(可选用)
let g:SimpylFold_docstring_preview=1
"对其他类型文件配置PEP8风格缩进
au BufNewFile,BufRead *.js, *.html, *.css
\ set tabstop=2
\ set softtabstop=2
\ set shiftwidth=2
"标示不必要的空白字符
hi BadWhitespace guifg=gray guibg=red ctermfg=gray ctermbg=red
au BufRead,BufNewFile *.py,*.pyw,*.c,*.h match BadWhitespace /\s\+$/
"支持UTF-8编码
set encoding=utf-8
"让代码变漂亮(根据个人喜好添加)
let python_highlight_all=1
syntax on
"颜色主题配置
colorscheme zenburn
set background=dark
set t_Co=256
"显示行号
set nu
"高亮查找
set hlsearch
"配置项目阅读设置
nmap :TagbarToggle "按f8开启关闭
"let g:tagbar_autopreview = 1 "顶部自动预览
let g:tagbar_sort = 0 "关闭自动排序,按照书写顺序排序
let g:tagbar_width=30 "设置窗口宽度
autocmd BufReadPost *.py,*.cpp,*.c,*.h,*.hpp,*.cc,*.cxx call tagbar#autoopen() "设置在哪些文件中自动开启窗口
"目录树
map : NERDTreeToggle "按F2开启关闭
" 自动打开目录树(根据个人喜好选择,我选择不开启)
"autocmd VimEnter * NERDTree
"设置括号自动补全
inoremap ' ''<ESC>i
inoremap " ""i
inoremap ( ()i
inoremap [ []i
inoremap { {}i
" 括号补全后再输入右括号自动忽略
function! RemoveNextDoubleChar(char)
let l:line = getline(".")
let l:next_char = l:line[col(".")]
if a:char == l:next_char
execute "normal! l"
else
execute "normal!a". a:char .""
end
endfunction
inoremap ) <ESC>:call RemoveNextDoubleChar(')')<CR>a
inoremap ] <ESC>:call RemoveNextDoubleChar(']')<CR>a
inoremap } <ESC>:call RemoveNextDoubleChar('}')<CR>a
"实现当删除左边括号的时候,自动删除右括号和括号内的内容(根据自己喜好设置,我是不喜欢)
"function! RemovePairs()
"let l:line = getline(".")
"let l:previous_char = l:line[col(".")-1]
"if index(["(", "[", "{"], l:previous_char) != -1
"let l:original_pos = getpos(".")
"execute "normal %"
"let l:new_pos = getpos(".")
"if l:original_pos == l:new_pos
"execute "normal! a\"
"return
"end
"let l:line2 = getline(".")
"if len(l:line2) == col(".")
"execute "normal! v%xa"
"else
"execute "normal! v%xi"
"end
"else
"execute "normal! a\"
"end
"endfunction
"inoremap :call RemovePairs()a
" 设置背景透明度(选用,开启后,再把终端的透明度打开会很炫酷,至于终端透明度怎么开,百度一大把)
hi Normal ctermfg=252 ctermbg=none
"设置项目阅读的,个人觉得没有tagbar好用,所以注释掉了
"let Tlist_Auto_Highlight_Tag=1
"let Tlist_Auto_Open=1
"let Tlist_Auto_Update=1
"let Tlist_Display_Tag_Scope=1
"let Tlist_Exit_OnlyWindow=1
"let Tlist_Enable_Dold_Column=1
"let Tlist_File_Fold_Auto_Close=1
"let Tlist_Show_One_File=1
"let Tlist_Use_Right_Window=1
"let Tlist_Use_SingleClick=1
"nnoremap <F8> :TlistToggle<CR>
"设定F8为taglist开关
"设置语言的代码自动补全
filetype plugin on
autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascrīpt set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
autocmd FileType c set omnifunc=ccomplete#Complete
"设置自动缩进
let g:pydiction_location='~/.vim/tools/pydiction/complete-dict'
set autoindent
set tabstop=4
set shiftwidth=4
set expandtab
set number
声明一下,这些环境搭配我不是很明白,总之把该装的都装上,省的后边麻烦,安装也很方便
终端中执行
sudo apt-get update
sudo apt-get upgrade
这条比较长,复制到终端直接回车,如果遇到问题看下边
sudo apt-get install libncurses5-dev libgnome2-dev libgnomeui-dev libgtk2.0-dev libatk1.0-dev libbonoboui2-dev ibcairo2-dev libx11-dev libxpm-dev libxt-dev python-dev python3-dev ruby-dev lua5.1 liblua5.1-dev libperl-dev git cmake
如果出现类似这种无法定位软件包的情况,把上述长代码复制到文本里,把无法定位的删除掉,然后再复制粘贴到终端,回车
sudo apt-get install vim-gtk
sudo apt-get install vim-gnome
sudo apt-get install ctags
sudo apt-get install vim-scripts
sudo apt-get install vim-addon-manager
vim-addons install taglist
检测vim是否支持python3,终端中运行下面代码,如果python3前是+号,就可以,不需要管python前边的符号,如果python3前是-号,就百度vim怎么支持python3,一大把教程
vim --version | grep python
安装pydiction(实现代码补全),分别在终端运行下边代码,分别运行
wget http://www.pythonclub.org/_media/python-basic/pydiction-1.2.zip
sudo unzip pydiction-1.2.zip
sudo mkdir ~/.vim/after/ftplugin -p
sudo mkdir ~/.vim/tools/pydiction/ -p
sudo cp pydiction-1.2/python_pydiction.vim ~/.vim/after/ftplugin
sudo cp pydiction-1.2/complete-dict ~/.vim/tools/pydiction/complete-dict
先下vundle,第三行可能会慢一点,耐心点,终端中执行
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle
git clone https://github.com/Valloric/YouCompleteMe.git ~/.vim/bundle/YouCompleteMe
下面就比较慢了,有可能要反复执行好几次,如果失败或者卡在一个环节特别久,就ctrl+c,再执行一次,特别容易卡在ycmd这个克隆环节,如果ycmd这环节实在过不去,接着往下看
这两行先称为“重要代码”,因为下边可能用到
cd ~/.vim/bundle/YouCompleteMe
git submodule update --init --recursive
一直卡这里的话,可以百度搜搜ycmd下载,我推荐一个网址,不知道这网站能用多久,要用ubuntu的浏览器打开(方便),我是用虚拟机的ubuntu下载的
https://www.cnblogs.com/halberd-lee/p/10267132.html
然后
然后最好下载到桌面
下载到桌面,解压,把里面的文件全部复制粘贴到目录,手动复制,最好不要用命令了
~/.vim/bundle/YouCompleteMe/third_party/ycmd/
然后再执行“重要代码”,往上翻一下,会快一点,不过仍然很慢,耐心耐心,个把小时很正常,这个环节一定要耐心,
因为这个网站的ycmd资源包不完整,所以还需要终端自动克隆一部分,
如果谁能从网上找到完整的ycmd包就更好了,再提供一个备用下载ycmd网址,需要注册
https://gitee.com/wangfile/ycmd
接下来先创建~/.vimrc文件,终端输入
sudo touch ~/.vimrc
把下边代码复制粘贴到~/.vimrc文档里
set nocompatible
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'tpope/vim-fugitive'
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
Plugin 'Valloric/YouCompleteMe'
call vundle#end()
filetype plugin indent on
然后安装必须插件,在终端中输入
vim
在vim的末行模式中输入下边命令,别忘了冒号,如果不清楚末行模式,那就在输入前按几下ESC
:PluginInstall
这里稍微有一点点慢,需要几分钟大概,跟网速有关,如果失败了再执行vim 和 :PluginInstall这两步
进入YouCompleteMe目录,终端输入
cd ~/.vim/bundle/YouCompleteMe
安装py环境,终端输入
python3 install.py
然后把下列代码写入~/.vimrc文件中,就可以自动补全代码和缩进了
filetype plugin on
autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascrīpt set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
autocmd FileType c set omnifunc=ccomplete#Complete
"自动缩进
let g:pydiction_location='~/.vim/tools/pydiction/complete-dict'
set autoindent
set tabstop=4
set shiftwidth=4
set expandtab
set number
设置括号自动补全
inoremap ' ''<ESC>i
inoremap " ""<ESC>i
inoremap ( ()<ESC>i
inoremap [ []<ESC>i
inoremap { {}<ESC>i
括号补全后再输入右括号自动忽略(这个配置我百度找了好久,发现他们都不合理,我和他么的就一点区别就是execute “normal!a”. a:char .""这一行,网上疯狂转载的都有一个bug,举个例子:我在123后边输入],会变成12]3,很不合理,而我改的这个可以正常显示为123]),这个代码如果有人转载请备注出处:ch_improve的csdn博客
function! RemoveNextDoubleChar(char)
let l:line = getline(".")
let l:next_char = l:line[col(".")]
if a:char == l:next_char
execute "normal! l"
else
execute "normal!a". a:char .""
end
endfunction
inoremap ) <ESC>:call RemoveNextDoubleChar(')')<CR>a
inoremap ] <ESC>:call RemoveNextDoubleChar(']')<CR>a
inoremap } <ESC>:call RemoveNextDoubleChar('}')<CR>a
实现当删除左边括号的时候,自动删除右括号和括号内的内容(根据自己喜好设置,我是不喜欢,括号内的内容不能保留,有点坑)
"function! RemovePairs()
"let l:line = getline(".")
"let l:previous_char = l:line[col(".")-1]
"if index(["(", "[", "{"], l:previous_char) != -1
"let l:original_pos = getpos(".")
"execute "normal %"
"let l:new_pos = getpos(".")
"if l:original_pos == l:new_pos
"execute "normal! a\<BS>"
"return
"end
"let l:line2 = getline(".")
"if len(l:line2) == col(".")
"execute "normal! v%xa"
"else
"execute "normal! v%xi"
"end
"else
"execute "normal! a\<BS>"
"end
"endfunction
"inoremap <BS> <ESC>:call RemovePairs()<CR>a
语法检测和高亮设置插件,这个建议一定开启,很美观
"语法检测和高亮的插件
Plugin 'scrooloose/syntastic'
"配色方案插件
Plugin 'jnurmine/Zenburn'
Plugin 'altercation/vim-colors-solarized'
"让代码变漂亮(根据个人喜好添加)
let python_highlight_all=1
syntax on
"颜色主题配置
colorscheme zenburn
set background=dark
set t_Co=256
文件浏览,目录树插件和设置
“插件
Plugin 'scrooloose/nerdtree'
"想用TAB键配合文件树的插件
Plugin 'jistr/vim-nerdtree-tabs'
"目录树
map <F2> : NERDTreeToggle<CR> "按F2开启关闭目录树
" 自动打开目录树(根据个人喜好选择,我选择不开启,想开启就把下一行的引号去掉)
"autocmd VimEnter * NERDTree
代码折叠优化的插件(不安装也不影响折叠,根据个人喜好吧)
"代码折叠插件
Plugin 'tmhedberg/SimpylFold'
"解决某些情况下缩进混乱的插件
Plugin 'vim-scripts/indentpython.vim'
"开启方法和类的折叠(建议加上,很方便)
set foldmethod=indent
set foldlevel=99
"使用空格折叠(代替手动折叠)
nnoremap <space> za
"看到折叠代码的文档字符串(可选用)
let g:SimpylFold_docstring_preview=1
阅读项目(建议开启,特别好用)
"项目插件
Plugin 'majutsushi/tagbar'
"配置项目阅读设置
nmap <F8> :TagbarToggle<CR> "按f8开启关闭
"let g:tagbar_autopreview = 1 "顶部自动预览(不建议默认开启,特别占地方,所以用引号引了)
let g:tagbar_sort = 0 "关闭自动排序,按照书写顺序排序
let g:tagbar_width=30 "设置窗口宽度
autocmd BufReadPost *.py,*.cpp,*.c,*.h,*.hpp,*.cc,*.cxx call tagbar#autoopen() "设置在哪些文件中自动开启窗口
还有一个项目阅读配置,个人感觉没上面的tagbar的好用,我也贴出来,有兴趣的人可以试试,如果用下边的配置,记得把上面的项目阅读给引号引了
"设置项目阅读的,个人觉得没有tagbar好用,所以注释掉了,想尝试的吧代码前的引号去掉即可
"let Tlist_Auto_Highlight_Tag=1
"let Tlist_Auto_Open=1
"let Tlist_Auto_Update=1
"let Tlist_Display_Tag_Scope=1
"let Tlist_Exit_OnlyWindow=1
"let Tlist_Enable_Dold_Column=1
"let Tlist_File_Fold_Auto_Close=1
"let Tlist_Show_One_File=1
"let Tlist_Use_Right_Window=1
"let Tlist_Use_SingleClick=1
"nnoremap <F8> :TlistToggle<CR> "设定F8为taglist开关
"对YouCompleteMe插件的一个小补充(用不用都可以,建议开启吧,不影响)
"第一行代码让完成操作后自动补全窗口不会消失
"第二行定义"转到定义"的快捷方式
let g:ycm_autoclose_preview_window_after_completion=1
map <leader>g :YcmCompleter GoToDefinitionElseDeclaration<CR>
使用ctrl+h,j,k,l 代替切换窗口(建议加上,很方便,谁用谁知道),vim自带的是ctrl+w,然后再按一次w,但是是用这个配置,就是光标的移动快捷键配上ctrl,舒服
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>
这个小插件可以添加PEP8风格检查
Plugin 'nvie/vim-flake8'
"对其他类型文件配置PEP8风格缩进
au BufNewFile,BufRead *.js, *.html, *.css
set tabstop=2
set softtabstop=2
set shiftwidth=2
标示不必要的空白字符
hi BadWhitespace guifg=gray guibg=red ctermfg=gray ctermbg=red
au BufRead,BufNewFile *.py,*.pyw,*.c,*.h match BadWhitespace /\s\+$/
支持UTF-8编码
set encoding=utf-8
显示行号
set nu
高亮查找
set hlsearch
这个透明度设置我个人很喜欢,再把终端的透明度开启,配合一个好看的桌面背景,很帅,终端透明度开启教程百度一下,太简单
hi Normal ctermfg=252 ctermbg=none
本人写本博客参考了以下三个人
参考https://blog.csdn.net/qq_38691608/article/details/79931358
参考https://blog.csdn.net/qq_33039859/article/details/81490382
参考https://blog.csdn.net/demorngel/article/details/69053870