记一次vim折腾

自己在网上看教学视频的时候,看见一位老师使用的vim编辑器,玩的很顺溜,而且界面也很实用酷炫,再加上vim的编辑器之神的称号,于是开始尝试自己配一个。


centos7
vim8.1
python3.6

我的linux是centos7系统,系统自带vim7.4版本。

$ vim        #进入vim命令模式
:version     #查看vim的版本和支持,里面有一个支持(+)和不支持(-)的列表选项

我发现我的是 ‘+python-python3 意思是支持python2,不支持python3 ,如果不支持就不能使用相关的插件功能,比如我的列表里面有 -lua,这意味着我即使装了自动补全的插件也不能使用该功能,很操蛋

网上查阅相关方法,发现只有自己重新编译vim,以使得支持相关功能。

这里只介绍步骤,具体代码根据自己的linux发行版本在网上查询资料。(^_^) (其实是间隔时间有点长了,相关的网址自己也懒得再找一遍了,大家自己百度吧,2333)

1.卸载原版vim,下载新的vim

$ sudo yum remove vim   #centos卸载方法

wget 下载vim。由于我要使用ale(异步检测错误)这个插件,而且 它只支持vim8以上,所以我下载的是vim8.1版本。

2.编译安装

我们要想让vim支持某项功能,这一步就很重要。

+python3
+lua

解压压缩包,进入解压缩后的目录。

设置编译属性,并且进行编译安装


$ ./configure --with-features=huge \
--enable-multibyte \
--enable-rubyinterp=yes \
--enable-python3interp=yes \
--with-python-config-dir=/usr/lib/python3.6/config-i386-linux-gnu \
--enable-perlinterp=yes \
--enable-luainterp=yes \
--enable-gui=gtk2 \
--enable-cscope \
--prefix=/usr/share/vim/vim81

$ make VIMRUNTIMEDIR=/usr/share/vim/vim81  #vim8版本及其以上要加上你的安装路径
$ make install

说明:

  • –with-features=huge:支持最大特性
    –enable-rubyinterp:启用Vim对ruby编写的插件的支持
    –enable-python3interp:启用Vim对python3编写的插件的支持 注意这里
    –enable-luainterp:启用Vim对lua编写的插件的支持 注意这里
    –enable-perlinterp:启用Vim对perl编写的插件的支持
    –enable-multibyte:多字节支持 可以在Vim中输入中文
    –enable-cscope:Vim对cscope支持
    –enable-gui=gtk2:gtk2支持,也可以使用gnome,表示生成gvim
    –with-python-config-dir=/usr/lib/python3.6/config-i386-linux-gnu/ 指定 python 路径 注意这里
    –prefix=/usr:编译安装路径 自定义

如果我们只是--enable-python3interp就只是默认开启了python3支持,你会在支持列表中看到+python3/dev,意思是支持python3但找不到动态链接,所以还要指定相对应的python路径--with-python-config-dir=路径是你的python目录,注意一定要指定到有config这个文件的目录路径我这里的是 config-i386-linux-gnu ,不同的机器不同的名字很正常。之后编译完成后就会发现+python/dev变成了+python3,而且有了+lua

自此基本完成了vim的重新编译。

3.开始插件之旅

人要衣装,vim同样也需要有插件的支持彰显气质

我主要使用python,所以大多找的是python相关的插件,网上自行百度,可以找到很多相关插件支持。同 yum之于centos ; pip之于python ,vim也有许多插件管理的工具,比如vundle还有vim-plug,我开始的时候使用的是vundle,为什么是开始,因为我变了,23333。

vundle 介绍及其相关配置博客:https://blog.csdn.net/zhangpower1993/article/details/52184581

由于一个个找插件很麻烦,所以我使用了网上推荐的插件包spf13,这是一个集合了很多高效插件的整合包,包括python,js等等……,安装方法自行百度。

下面注意啦!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

基本上很多插件都需要配置相关的软件包,要不然就不能使用相关的插件,比如你没有lua,你就甭想补全了(调皮的ycm除外),没有pep8就不能检测python语法,没有ctags就不能使用tagers等等,所以很烦很烦,这也是我认为安装插件很烦的地方。这具体到插件包上,更加烦,因为插件实在太多了,不过,当你缺少某些安装包导致某些功能无法使用,使用vim编辑时都会有提示,再根据提示百度之。

spf13官网 :http://vim.spf13.com/ 这里有下载说明和相关插件介绍推荐
spf13 git项目地址 : https://github.com/spf13/spf13-vim 这里还有下载说明和相关插件介绍
其它参考博客 https://blog.csdn.net/dark_tone/article/details/52866742 这是一个系列介绍spf13插件

安装spf13的初衷就是因为自己懒,想一劳永逸解决插件配置问题,结果却发现自己掉进了更大的坑。插件太多,有的按键设置冲突,所需要的依赖包也很多,如果相关包没安装,vim编辑器动不动就发出红色警告,对于我有点洁癖的来说不能忍啊,解决了一个又来一个,简直了,而且我再window上安装了vim和spf13后,每次一运行vim,就会有cmd程序在任务栏一闪而过,看的极其不爽,所以啊,我觉得我们新手还是要脚踏实地,一步步来,插件一个个装。



这里我再推荐排坑过程遇到的另外一个插件包 k-vim,这是我目前正在使用的(别问我为什么还在使用插件包,因为 我懒啊)。这是一款 国人创建的git项目,里面的阅读文档都是汉字,及其方便,插件管理工具是 vim-plug,相比较vundle各有优势,我比较喜欢它的提示功能,因为我曾经在使用 vundle安装插件的时候,很多次都在傻傻的看着屏幕,根本不知道安装进度,连错误都不给我一个,简单点就是 屁都不给我放一个

k-vim 的 git 地址 :https://github.com/wklken/k-vim ,里面的安装和配置说明都是汉字,方便阅读。

4.YouComepleteMe的安装

最好请参阅官方文档:https://github.com/Valloric/YouCompleteMe#full-installation-guide
可以说除了是英文的,这真的是很全面的,遇到的问题,你也能在这里面找到解决方法。及其推荐。

k-vim里面的插件安装都自动化,唯一调皮的就YouComepleteMe同学,这位号称史上最难装的软件,当然其补全的功能也是很强大的。越是得不到的我就越想要,我在曾经使用vundle对它进行了N次教育,但是无赖网络限制,老是下载不下来,而且vundle没有提示功能,我也不知道下载的时候它有没有挂掉,无数次的ctrl+c,后来使用了k-vim,这里面携带了ycm,但同样需要安装,不同的是它安装的时候有提示,好歹给我一个心理安慰,注意当你下载过慢,导致根本没有下全,但是却发送管理工具默认下载完成,你需要重新删除这个包,再一次更新下载。

我中途就下载挂掉了,但是vim-plug却给了我一个提示:cd "~/.vim/bundle/YouCompleteMe/" && git submodule update --init --recursive 2>&1
进入YouCompleteMe的下载目录,然后执行 git submodule update --init --recursive 2>&1这是检测安装完整性的命令,一旦你下载中断,你可以在该目录下执行此命令,继续下载。如果你经历了无数次下载失败后,看见这条命令肯定会喜极而泣的。

下载完毕后,进入vim,有提示ycm内核没有编译,所以我们还需要一个编译过程。

推荐博文:https://www.jianshu.com/p/c24f919097b3 编译过程作者写的简单明了。

按照步骤完成后,再次启动vim,依然报错,说我使用的是python2版本编译的,但我之前的支持路径是python3的,我了个大擦,我终于意识到python2的好了。因为即使用python2也需要比如python-devel之类的包,而我也必须要找一个python3的开发包python3-devel。

我用yum install python3-devel发现我的yum源没有这个包。

使用yum search python3* | grep devel找到了yum源里的一个python3.4_x86-devel的包,安装。

然后再对编译命令进行更改

推荐博文:https://www.cnblogs.com/shanql/articles/6051593.html

我取其精华:

cmake -G "Unix Makefiles"  -DUSE_PYTHON2=OFF . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/
#关闭使用python2的默认行为,关闭宏USE_PYTHON2,官网上默认使用的是python2编译,所以改之。

最后总结编译命令,我不需要c家族的语义支持,但我依然使用系统自带的libclang包,所以我使用的命令是:

cmake -G "Unix Makefiles" -DUSE_SYSTEM_LIBCLANG=ON -DUSE_PYTHON2=OFF . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp

编译过程当中,如果遇到相关依赖包缺失需要安装,请自行百度安装。

以上就是我遇到的一些问题,接着空闲时间记录下来,以供有着类似问题的同学提供参考。

谢谢阅览。



上面博文的截取:

Download the latest version of libclang: 下载libclang(版本>=3.9)
翻译: 下载最新版本的libclang(3.9以上版本)
下载地址:http://llvm.org/releases/download.html

官方建议下二进制包:(别下错了)

Clang for x86_64 Ubuntu 14.04 (.sig)
Clang for x86_64 Ubuntu 16.04 (.sig)
Clang for Mac OS X (.sig)
编译安装ycm_core库之前戏
Compile the ycm_core library that YCM needs.
This library is the C++ engine that YCM uses to get fast completions.

翻译: 编译YCM需要的ycm_core库。 这个库是YCM用来获得快速完成的C ++引擎。

编译安装ycm_core需要cmake和python-dev支持。

安装cmake:

Ubuntu: sudo apt-get install cmake
Mac: brew install cmake
安装python-dev:

Ubuntu: sudo apt-get install python-dev python3-dev
Mac: they should already be present(Mac下,它们是现成的,不需要额外装)
Here we’ll assume you installed YCM with Vundle.
That means that the top-level YCM directory is in ~/.vim/bundle/YouCompleteMe.

翻译: 现在我们假设你安装YCM与Vundle。 这意味着顶层YCM目录在中~/.vim/bundle/YouCompleteMe。

正式编译安装ycm_core: Compile the ycm_core library that YCM needs. This library is the C++ engine that YCM uses to get fast completions.
We’ll create a new folder where build files will be placed. Run the following:

翻译: 创建一个新文件夹,其中将放置构建文件。 运行以下命令:

cd ~
mkdir ycm_build
cd ycm_build
下一步生成makefile,这一步很重要,有点复杂。

这一步作者的原英文介绍特别冗长,这里总结了一下,列出来。作者的原文附在后面。

如果不需要C族语言的语义支持,在ycm_build目录下执行: cmake -G “Unix Makefiles” . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
如果需要C族语言的语义支持,还得分几种情况:
从llvm的官网下载了LLVM+Clang的二进制包

解压到:~/ycm_temp/llvm_root_dir

该目录下有bin, lib, include等文件夹

然后执行: cmake -G “Unix Makefiles” -DPATH_TO_LLVM_ROOT=~/ycm_temp/llvm_root_dir . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp

如果想用系统的libclang: cmake -G “Unix Makefiles” -DUSE_SYSTEM_LIBCLANG=ON . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp

如果想用自定义的libclang: cmake -G “Unix Makefiles” -DEXTERNAL_LIBCLANG_PATH=/path/to/libclang.so . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp

/path/to/libclang.so这部分填入你自己编译libclang的路径

至此,makefile已生成。

我自己是按照作者建议,从llvm网站下载的二进制文件,安装的。

生成ycm_core
cmake –build . –target ycm_core –config Release

安装完成
至此,YouCompleteMe已经算是安装成功!

注意:这时候,ycm_build目录可以删除啦!

安装成功后,ycm_build以及ycm_temp目录都可以删除,不影响YouCompleteMe插件的使用。

===================================================================================================

==================================================================================================

(可跳过)附录:作者原文中主要步骤和关键点摘抄
Now we need to generate the makefiles. If you DON’T care about semantic support for C-family languages, run the following command in the ycm_build directory:

We’ll assume you downloaded a binary distribution of LLVM+Clang from llvm.org in step 3 and that you extracted the archive file to folder ~/ycm_temp/llvm_root_dir (with bin, lib, include etc. folders right inside that folder).

NOTE: This only works with a downloaded LLVM binary package, not a custom-built LLVM! See docs below for EXTERNAL_LIBCLANG_PATH when using a custom LLVM build.

With that in mind, run the following command in the ycm_build directory:

cmake -G “” -DPATH_TO_LLVM_ROOT=~/ycm_temp/llvm_root_dir . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp, where is Unix Makefiles on Unix systems.

Now that configuration files have been generated, compile the libraries using this command: cmake –build . –target ycm_core –config Release

The –config Release part is specific to Windows and will be ignored on a Unix OS.

For those who want to use the system version of libclang, you would pass -DUSE_SYSTEM_LIBCLANG=ON to cmake instead of the -DPATH_TO_LLVM_ROOT=… flag.

注意作者这里的NOTE提示:

NOTE: We STRONGLY recommend AGAINST use of the system libclang instead of the upstream compiled binaries. Random things may break. Save yourself the hassle and use the upstream pre-built libclang.

如果是custom libclang而不是downloaded LLVM binary package:

You could also force the use of a custom libclang library with -DEXTERNAL_LIBCLANG_PATH=/path/to/libclang.so flag (the library would end with .dylib on a Mac). Again, this flag would be used instead of the other flags.
If you compiled LLVM from source, this is the flag you should be using.

Running the cmake command will also place the libclang.[so|dylib|dll] in the YouCompleteMe/third_party/ycmd folder for you if you compiled with clang support (it needs to be there for YCM to work).

Don’t forget that if you want the C-family semantic completion engine to work,
you will need to provide the compilation flags for your project to YCM.

==================================================================================================

==================================================================================================

配置
前戏准备

cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/  
vim ~/.ycm_extra_conf.py

/*
** 注:下面需要注释的内容只有稍微老一点的版本才有,最新的是没有的
*/
// 如果有如下内容,注释掉:
try:  
  final_flags.remove( '-stdlib=libc++' )  
except ValueError:  
  pass 
// 注释完后变成下面这样
#try:  
#  final_flags.remove( '-stdlib=libc++' )  
#except ValueError:  
#  pass 

.vimrc中的配置

” #####YouCompleteMe Configure
let g:ycm_global_ycm_extra_conf = ‘~/.ycm_extra_conf.py’
” 自动补全配置
set completeopt=longest,menu “让Vim的补全菜单行为与一般IDE一致(参考VimTip1228)
autocmd InsertLeave * if pumvisible() == 0|pclose|endif “离开插入模式后自动关闭预览窗口
inoremap pumvisible() ? “\” : “\” “回车即选中当前项
“上下左右键的行为 会显示其他信息
“inoremap pumvisible() ? “\” : “\”
“inoremap pumvisible() ? “\” : “\”
“inoremap pumvisible() ? “\\\” : “\”
“inoremap pumvisible() ? “\\\” : “\”

“youcompleteme 默认tab s-tab 和自动补全冲突
“let g:ycm_key_list_select_completion=[‘’]
let g:ycm_key_list_select_completion = [‘’]
“let g:ycm_key_list_previous_completion=[‘’]
let g:ycm_key_list_previous_completion = [‘’]
let g:ycm_confirm_extra_conf=0 “关闭加载.ycm_extra_conf.py提示

let g:ycm_collect_identifiers_from_tags_files=1 ” 开启 YCM 基于标签引擎
let g:ycm_min_num_of_chars_for_completion=2 ” 从第2个键入字符就开始罗列匹配项
let g:ycm_cache_omnifunc=0 ” 禁止缓存匹配项,每次都重新生成匹配项
let g:ycm_seed_identifiers_with_syntax=1 ” 语法关键字补全
nnoremap :YcmForceCompileAndDiagnostics “force recomile with syntastic
“nnoremap lo :lopen “open locationlist
“nnoremap lc :lclose “close locationlist
inoremap
“在注释输入中也能补全
let g:ycm_complete_in_comments = 1
“在字符串输入中也能补全
let g:ycm_complete_in_strings = 1
“注释和字符串中的文字也会被收入补全
let g:ycm_collect_identifiers_from_comments_and_strings = 0
let g:clang_user_options=’|| exit 0’
“nnoremap jd :YcmCompleter GoToDefinitionElseDeclaration ” 跳转到定义处
” #####YouCompleteMe Configure
.ycm_extra_conf.py中的配置

用命令查看库路径

echo | clang -v -E -x c++ -
结果可能如下:
clang version 3.6.2 (tags/RELEASE_362/final)
Target: i386-pc-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/lib/gcc/i686-redhat-linux/4.4.4
Found candidate GCC installation: /usr/lib/gcc/i686-redhat-linux/4.4.7
Found candidate GCC installation: /usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1
Selected GCC installation: /usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1
Candidate multilib: .;@m32
Selected multilib: .;@m32
太长了,这里省略一部分中间内容;………表示生咯的内容
“/usr/local/bin/clang” -cc1 -triple ……… -mstackrealign -fobjc-runtime=gcc directory “/include”

include “…” search starts here:

这里没有显示任何东西,所以不需要包含任何路径

include <…> search starts here:

这里就是需要包含的路径下面这些都是需要包含的路径
/usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1/../../../../include/c++/4.8.1
/usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1/../../../../include/c++/4.8.1/i686-pc-linux-gnu
/usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1/../../../../include/c++/4.8.1/backward
/usr/local/include
/usr/local/bin/../lib/clang/3.6.2/include
/usr/include
End of search list.

1 “”

1 “” 1

1 “” 3

318 “” 3

1 “” 1

1 “” 2

1 “” 2

整理上述内容,并添加到flag中

将以上内容复制出来,修改成如下:
‘-isystem’,
‘/usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1/../../../../include/c++/4.8.1’,
‘-isystem’,
‘/usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1/../../../../include/c++/4.8.1/i686-pc-linux-gnu’,
‘-isystem’,
‘/usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1/../../../../include/c++/4.8.1/backward’,
‘-isystem’,
‘/usr/local/include’,
‘-isystem’,
‘/usr/local/bin/../lib/clang/3.6.2/include’,
‘-isystem’,
‘/usr/include’,
补全 C 语言全局函数问题(vim ~/.vimrc文件修改)

默认情况下输入 ., ->, :: 之后会触发补全函数和类, 但是默认情况下是不补全全局函数的,所以 C 语言中的 printf 之类的函数就无法补全

解决办法就是手动调用补全,对应的 YCM 函数是 ycm_key_invoke_completion,将其绑定到快捷键 let g:ycm_key_invoke_completion = ‘’(默认是 )

猜你喜欢

转载自blog.csdn.net/a_lazy_zhu/article/details/80732795