博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vim配置
阅读量:5923 次
发布时间:2019-06-19

本文共 17148 字,大约阅读时间需要 57 分钟。

 

1. 安装必要的软件

sudo apt-get install ctags cscope python-dev build-essential

2. 升级vim到7.4

方法一:手动编译

(1) 卸载原有的Vim

sudo apt-get remove vim vim-tiny vim-common vim-runtime gvim vim-gui-common

(2)编译安装vim74

hg clone https://vim.googlecode.com/hg/ vimcd vim./configure --with-features=huge \            --enable-rubyinterp=yes \            --enable-pythoninterp=yes \            --enable-python3interp=yes \            --enable-perlinterp=yes \            --enable-luainterp=yes \            --enable-gui=gtk2 --enable-cscope --prefix=/usrmake VIMRUNTIMEDIR=/usr/share/vim/vim74make install

方法二: 手动添加PPA(Personal Package Archives)

sudo add-apt-repository ppa:nmi/vim-snapshotssudo apt-get updatesudo apt-get install vim

3. 安装vim管理插件 pathogen

参见

4. 安装vim插件 nerdtree, nerdtree-tabs, powerline, conslas-powerline-vim, vim-colors-solarized

cd ~/.vim/bundlegit clone http://github.com/scrooloose/nerdtree.git   #安装nerdtreegit clone https://github.com/jistr/vim-nerdtree-tabs.git #安装nerdtree-tabsgit clone https://github.com/eugeii/consolas-powerline-vim.git #安装powerlinegit clone git://github.com/altercation/vim-colors-solarized.git #安装vim-colors-solarized

5. 安装自动补全 youcompleteme

(1) 下载youcompleteme

git clone https://github.com/Valloric/YouCompleteMe.git git submodule update --init --recursive

(2) 编译安装youcompleteme

cd ~/.vim/bundle/YouCompleteMe./install --clang-completer #对c/c++语言进行支持

正常来说,YCM会去下载clang的包,如果已经有,也可以用系统--system-libclang。

步骤(2)是使用脚本进行自动编译安装ycm对c语言的支持,也可以下载clang的源码,编译,如下所示

$: cd ~  $: mkdir ycm_build  $: cd ycm_build  $: cmake -G "Unix Makefiles" . ~/.vim/bundle/YouCompleteMe/cpp  $: cmake -G "Unix Makefiles" -DPATH_TO_LLVM_ROOT=/usr/clang_3_3/ . ~/.vim/bundle/YouCompleteMe/third_party_ycmd/cpp$: make ycm_core  $: cp /usr/clang_3_3/lib/libclang.so ~/.vim/bundle/YouCompleteMe/python/libclang.so #这一步是为了使用新的libcang.so        $: cd ~/.vim/bundle/YouCompleteMe      .vim/bunble/YouCompleteMe$: ./install.sh --clang-completer

(3)修改配置文件 ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py

# This file is NOT licensed under the GPLv3, which is the license for the rest# of YouCompleteMe.## Here's the license text for this file:## This is free and unencumbered software released into the public domain.## Anyone is free to copy, modify, publish, use, compile, sell, or# distribute this software, either in source code form or as a compiled# binary, for any purpose, commercial or non-commercial, and by any# means.## In jurisdictions that recognize copyright laws, the author or authors# of this software dedicate any and all copyright interest in the# software to the public domain. We make this dedication for the benefit# of the public at large and to the detriment of our heirs and# successors. We intend this dedication to be an overt act of# relinquishment in perpetuity of all present and future rights to this# software under copyright law.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR# OTHER DEALINGS IN THE SOFTWARE.## For more information, please refer to 
import osimport ycm_core# These are the compilation flags that will be used in case there's no# compilation database set (by default, one is not set).# CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR.flags = ['-Wall','-Wextra',#'-Werror',#'-Wc++98-compat','-Wno-long-long','-Wno-variadic-macros','-fexceptions','-stdlib=libc++',# THIS IS IMPORTANT! Without a "-std=
" flag, clang won't know which# language to use when compiling headers. So it will guess. Badly. So C++# headers will be compiled as C headers. You don't want that so ALWAYS specify# a "-std=
".# For a C project, you would set this to something like 'c99' instead of# 'c++11'.'-std=c++11',# ...and the same thing goes for the magic -x option which specifies the# language that the files to be compiled are written in. This is mostly# relevant for c++ headers.# For a C project, you would set this to 'c' instead of 'c++'.'-x','c++','-I','.','-isystem','/usr/include','-isystem','/usr/local/include','-isystem','/Library/Developer/CommandLineTools/usr/include','-isystem','/Library/Developer/CommandLineTools/usr/bin/../lib/c++/v1',]# Set this to the absolute path to the folder (NOT the file!) containing the# compile_commands.json file to use that instead of 'flags'. See here for# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html## Most projects will NOT need to set this to anything; you can just change the# 'flags' list of compilation flags. Notice that YCM itself uses that approach.compilation_database_folder = ''if os.path.exists( compilation_database_folder ): database = ycm_core.CompilationDatabase( compilation_database_folder )else: database = NoneSOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]def DirectoryOfThisScript(): return os.path.dirname( os.path.abspath( __file__ ) )def MakeRelativePathsInFlagsAbsolute( flags, working_directory ): if not working_directory: return list( flags ) new_flags = [] make_next_absolute = False path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ] for flag in flags: new_flag = flag if make_next_absolute: make_next_absolute = False if not flag.startswith( '/' ): new_flag = os.path.join( working_directory, flag ) for path_flag in path_flags: if flag == path_flag: make_next_absolute = True break if flag.startswith( path_flag ): path = flag[ len( path_flag ): ] new_flag = path_flag + os.path.join( working_directory, path ) break if new_flag: new_flags.append( new_flag ) return new_flagsdef IsHeaderFile( filename ): extension = os.path.splitext( filename )[ 1 ] return extension in [ '.h', '.hxx', '.hpp', '.hh' ]def GetCompilationInfoForFile( filename ): # The compilation_commands.json file generated by CMake does not have entries # for header files. So we do our best by asking the db for flags for a # corresponding source file, if any. If one exists, the flags for that file # should be good enough. if IsHeaderFile( filename ): basename = os.path.splitext( filename )[ 0 ] for extension in SOURCE_EXTENSIONS: replacement_file = basename + extension if os.path.exists( replacement_file ): compilation_info = database.GetCompilationInfoForFile( replacement_file ) if compilation_info.compiler_flags_: return compilation_info return None return database.GetCompilationInfoForFile( filename )def FlagsForFile( filename, **kwargs ): if database: # Bear in mind that compilation_info.compiler_flags_ does NOT return a # python list, but a "list-like" StringVec object compilation_info = GetCompilationInfoForFile( filename ) if not compilation_info: return None final_flags = MakeRelativePathsInFlagsAbsolute( compilation_info.compiler_flags_, compilation_info.compiler_working_dir_ ) # NOTE: This is just for YouCompleteMe; it's highly likely that your project # does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR # ycm_extra_conf IF YOU'RE NOT 100% SURE YOU NEED IT. #try: # final_flags.remove( '-stdlib=libc++' ) #except ValueError: # pass else: relative_to = DirectoryOfThisScript() final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to ) return { 'flags': final_flags, 'do_cache': True }
View Code

5. 修改配置文件 ~/.vimrc

"---------------------------------- pathogen 设置-------------------------"pathogen插件用于管理其他vim插件,安装其他插件时候,可以直接解压覆盖到 .vim 目录下的 autoload, plugin, doc目录;"也可以安装完pathogen之后(会在autoload目录下有一个pathogen.vim,同时在.vim 下创建 bundles目录)进入 bundles目录,"执行 git clone xxxx 到bundles目录,利用 pathogen进行安装execute pathogen#infect()execute pathogen#helptags()"定义快捷键的前缀,前缀类似于命令空间,避免多个相同的快捷键冲突"如 c, 
c,
c 是三个不同的快捷键let mapleader=";""----------------------------------基本配置------------------------------"关闭兼容模式set nocompatible"文件类型检测,可以针对不同类型的文件加载不同的插件filetype on"根据侦测的文件类型,加载相应的插件filetype plugin on"vim 自身命令行 模式自动补全set wildmenu"设置语法高亮if has("syntax") syntax onendif "高亮光标所在的行set cul "用浅色高亮当前行autocmd InsertEnter * se cul"设置行间隔set linespace=0"设置退格键可用set backspace=2"设置匹配模式,显示匹配的括号set showmatch"整词换行set linebreak"设置光标可以从行首或行末换到另一行去set whichwrap=b,s,<,>,[,] "设置使用鼠标set mouse=a"set mouse="显示行号set number"标志预览窗口set previewwindow"设置历史记录条数set history=50"设置自动写回文件"自动把内容写回文件: 如文件被修改过,在每个 :next、:rewind、:last、:first、:previous、:stop、:suspend、:tag、:!、:make、CTRL-]" 和 CTRL-^命令时进行;用 :buffer、CTRL-O、CTRL-I、'{A-Z0-9} 或 `{A-Z0-9} 命令转到别的文件时亦然。set autowrite"记住上次打开文件的位置,重新打开时自动跳转到上次编辑的位置set viminfo='10,\"100,:20,%,n~/.viminfoau BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif"---------------------------------配色方案------------------------------syntax enablesyntax on"设置背景色set background=dark"vim 配色方案"colorscheme solarizedcolorscheme desertset t_Co=256"设置字体set guifont=Consolas:h11"---------------------------------设置宽度(tab等)----"设置tab宽度set tabstop=4"设置软tab宽度,软tab,用空格代替tabset softtabstop=4"自动缩进的宽度set shiftwidth=4"----------------------------------设置对齐和缩进--------"设置自动对齐,(和上一行)"set autoindent"智能对齐set smartindent"使用c/c++语言的自动缩进方式set cindent"设置c/c++语言的具体缩进方式set cinoptions={
0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s "不要用空格代替制表符set expandtab"在行和段开始处使用制表符set smarttab"-----------------------------------文件生成----------------------"去掉输入错误的提示声音set noeb "在处理未保存或只读文件的时候,弹出确认set confirm"禁止生成临时文件set nobackupset noswapfile"------------------------------------状态行设置-----------------"0 不显示状态行,1 当多于一个窗口时显示最后一个窗口的状态行,2 总是显示最后一个窗口的状态行set laststatus=2 "显示标尺,用于显示光标位置处的行号和列号。每个窗口都有自己的标尺,如果窗口有状态行,则显示在状态行,否则显示在最后一行set ruler "命令行设置"命令行显示输入的命令set showcmd"命令行显示当前vim的模式set showmode"--------------------------------------查找设置--------------------------------"输入字符串就显示匹配点set incsearch"高亮显示查找结果set hlsearch"开启实时搜索功能set incsearch"搜索时大小写不敏感set ignorecase"--------------------------------------多窗口设置------------------------------"打开一个新的标签页,然后ctags查找并在新的标签页面中显示当前光标处的变量或函数nmap
:tab split
:exec("tag ".expand("
"))
autocmd filetype go nmap
:tab split
:exec("GoDef ".expand("
")) "nmap
:vsp
:exec("tag ".expand("
"))
nmap
:tabprevious
nmap
:tabnext
"快捷键依次遍历子窗口nnoremap nw
"跳转至右方的窗口nnoremap
l
l"跳转至左方的窗口nnoremap
j
h"跳转至上方的窗口nnoremap
i
k"跳转至下方的窗口nnoremap
k
j"-------------------------------------设置多终端之间的拷贝-------------------"visual 模式下,xw 复制选中的区域, 到另一个终端下,xr粘贴if has("unix") nmap xr :r $HOME/.vimxfer
nmap xw :'a,.w! $HOME/.vimxfer
vmap xr c
:r $HOME/.vimxfer
vmap xw :w! $HOME/.vimxfer
else nmap xr :r c:/.vimxfer
nmap xw :'a,.w! c:/.vimxfer
vmap xr c
:r c:/.vimxfer
vmap xw :w! c:/.vimxfer
endif"-------------------------------------ctags设置----------------------------"按下F2重新生成tag文件,并更新taglistmap
:!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .
:TlistUpdate
imap
:!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .
:TlistUpdate
set tags=tagsset tags+=./tags "add current directory's generated tags fileset tags+=/usr/include/c++/tags"---------------------------------------taglist 设置 -----------------------"因为我们放在环境变量里,所以可以直接执行let Tlist_Ctags_Cmd='ctags'"让窗口显示在右边,0的话就是显示在左边let Tlist_Use_Right_Window=1"让taglist可以同时展示多个文件的函数列表let Tlist_Show_One_File=1"非当前文件,函数列表折叠隐藏let Tlist_File_Fold_Auto_Close=1 "当taglist是最后一个分割窗口时,自动退出vimlet Tlist_Exit_OnlyWindow=1 "是否一直处理tags.1:处理;0:不处理let Tlist_Process_File_Always=1 "实时更新tagslet Tlist_Inc_Winwidth=0"---------------------------------------- cscope 设置---------------------------------"if has("cscope")" set csprg=/usr/bin/cscope " 指定用来执行cscope的命令" set csto=0 " 设置cstag命令查找次序:0先找cscope数据库再找标签文件;1先找标签文件再找cscope数据库" set cst " 同时搜索cscope数据库和标签文件" set cscopequickfix=s-,c-,d-,i-,t-,e- " 使用QuickFix窗口来显示cscope查找结果" set nocsverb" if filereadable("cscope.out") " 若当前目录下存在cscope数据库,添加该数据库到vim" cs add cscope.out" elseif $CSCOPE_DB != "" " 否则只要环境变量CSCOPE_DB不为空,则添加其指定的数据库到vim" cs add $CSCOPE_DB" endif" set csverb"endif"map
:cs add ./cscope.out .
:cs reset
"imap
:cs add ./cscope.out .
:cs reset
" 将:cs find" c等Cscope查找命令映射为
c等快捷键(按法是先按Ctrl+Shift+-,然后很快再按下c)"nmap
s :cs find s
=expand("
")
:copen
"nmap
g :cs find g
=expand("
")
"nmap
d :cs find d
=expand("
")
:copen
"nmap
c :cs find c
=expand("
")
:copen
"nmap
t :cs find t
=expand("
")
:copen
"nmap
e :cs find e
=expand("
")
:copen
"nmap
f :cs find f
=expand("
")
"nmap
i :cs find i
=expand("
")
:copen
"--------------------------------------cscope 设置 2 --------------------------function! InitCsTag() execute "!ctags -R --languages=C++ --c++-kinds=+p --fields=+iaS --extra=+q ./" if(executable("cscope") && has("cscope") ) if(has('win32')) silent! execute "!dir /b *.c,*.cpp,*.h,*.java,*.cs >> cscope.files" else silent! execute "!find . -name '*.h' -o -name '*.c' -o -name '*.cpp' -o -name '*.inl' -o -name '*.m' -o -name '*.mm' -o -name '*.java' -o -name '*.py' -o -name '*.html' -o -name '*.php' -o -name '*.js' > cscope.files" endif silent! execute "!cscope -b" silent! execute "cs kill -1" if filereadable("cscope.out") execute "cs add cscope.out" endif endif echohl StatusLine | echo "C/C++ cscope tag updated" | echohl Noneendffunction! UpdateCsTag() silent! execute "!cscope -b" silent! execute "cs reset"endf if has("cscope") set cscopequickfix=s-,c-,d-,i-,t-,e- set csto=0 set cst set csverbendif"nnoremap
:call UpdateCsTag()nnoremap
:call InitCsTag()
nmap
:cs find s
=expand("
")
nmap
:cs find s
=expand("
")
:copen
nmap
:cp
nmap
:cn
"--------------------------------------------------nerdtree 设置-------------------------------------let NERDChristmasTree=1let NERDTreeAutoCenter=1let NERDTreeBookmarksFile=$VIM.'/Data/NerdBookmarks.txt'let NERDTreeMouseMode=2let NERDTreeShowBookmarks=1let NERDTreeShowFiles=1let NERDTreeShowHidden=1let NERDTreeShowLineNumbers=1let NERDTreeWinPos='left'let NERDTreeWinSize=31"autocmd VimEnter * NERDTreeTabs"当打开vim且没有文件时自动打开NERDTreeautocmd vimenter * if !argc() | NERDTree | endif" 只剩 NERDTree时自动关闭autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif"map
:NERDTreeMirror
"map
:NERDTreeToggle
map
:NERDTreeTabsToggle
"----------------------------------------------------powerline 设置-----------------------------------set guifont=PowerlineSymbols\ for\ Powerlineset t_Co=256let g:Powerline_symbols = 'fancy'"防止出现乱码let Powerline_symbols = 'compatible'set encoding=utf8"----------------------------------------------------代码折叠设置-------------------------------" 用语法高亮来定义折叠set foldmethod=syntax " 启动vim时不要自动折叠代码set foldlevel=100 " 设置折叠栏宽度set foldcolumn=5 "----------------------------------------------------quickfix设置-------------------------" 按下F8,执行make cleanmap
:make clean
" 按下F7,执行make编译程序,并打开quickfix窗口,显示编译信息 map
:make
:copen
" 按下F5,光标移到上一个错误所在的行 map
:cp
" 按下F6,光标移到下一个错误所在的行 map
:cn
"以下的映射是使上面的快捷键在插入模式下也能用imap
:make clean
imap
:make
:copen
imap
:cp
imap
:cn
"----------------------------------syntastic settings----------------------let g:syntastic_check_on_open = 1 let g:syntastic_cpp_include_dirs = ['/usr/include/'] let g:syntastic_cpp_remove_include_errors = 1 let g:syntastic_cpp_check_header = 1 let g:syntastic_cpp_compiler = 'clang++' "set error or warning signs "let g:syntastic_error_symbol = 'x' "let g:syntastic_warning_symbol = '!' ""whether to show balloons let g:syntastic_enable_balloons = 1 "----------------------------------YCM settings------------------------------let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py' let g:ycm_collect_identifiers_from_tags_files = 1 let g:ycm_seed_identifiers_with_syntax = 1 let g:ycm_confirm_extra_conf = 0 let g:ycm_error_symbol = '>>'let g:ycm_warning_symbol = '>*'nnoremap
gl :YcmCompleter GoToDeclaration
nnoremap
gf :YcmCompleter GoToDefinition
nnoremap
gg :YcmCompleter GoToDefinitionElseDeclaration
"nmap
:YcmDiags
"----------------------------------------------------快捷键设置---------------------------"
gnome 终端手册"更新ctags文件"map
:!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .
:TlistUpdate
"更新cscope文件"nnoremap
:call InitCsTag()"显示或关闭nerdtree"map
:NERDTreeTabsToggle
""按下F5,光标移到上一个错误所在的行"map
:cp
"按下F6,光标移到下一个错误所在的行"map
:cn
"按下F7,执行make编译程序,并打开quickfix窗口,显示编译信息"map
:make
:copen
""按下F8,执行make clean"map
:make clean
""生成cscope数据库"nnoremap
:call InitCsTag()"cscope 查找,在当前标签页面显示"nmap
:cs find s "cscope 查找,在新标签页面显示可能的结果"nmap
:cs find s
=expand("
")
:copen
"cscope前一个查找结果"nmap
:cp
"cscope后一个查找结果"nmap
:cn
""前一个tab页面"nmap
:tabprevious
"后一个tab页面"nmap
:tabnext
"快捷键依次遍历子窗口"nnoremap nw
"跳转至右方的窗口"nnoremap
l
l"跳转至左方的窗口"nnoremap
j
h"跳转至上方的窗口"nnoremap
i
k"跳转至下方的窗口"nnoremap
k
j"------------------------------------------"在ubuntu下需要安装vim-gnome来支持系统剪贴板""将文本块复制到系统剪贴板vnoremap
y "+y"系统粘贴板内容粘贴至vimnmap
p "+p"-----------------------------------------
View Code

 

转载地址:http://kxavx.baihongyu.com/

你可能感兴趣的文章
对栈溢出的分析(未完成)
查看>>
(转)EXT基础校验
查看>>
20145222黄亚奇《网络对抗》—— 网络欺诈技术防范
查看>>
python ----字符串基础练习题30道
查看>>
时间和地点介词
查看>>
Zookeeper的RPC框架
查看>>
python 基础 9.7 创建表
查看>>
验证码识别程序
查看>>
aliyun服务器ecs被ddos后无法被zabbix-server监控的处理
查看>>
mysql 主从复制
查看>>
C# 常见的面试问题(转)
查看>>
自己动手,装一个液晶电视
查看>>
MD5算法
查看>>
windows本地安装mongoDB并且安装可视化工具studio 3t
查看>>
CRegKey类的注册表使用
查看>>
iOS开发之UIView的常见属性
查看>>
团队开发----NABC分析
查看>>
我的Android进阶之旅------>Android之AutoCompleteTextView输入汉字拼音首字母实现过滤提示(支持多音字)...
查看>>
我的Java开发学习之旅------>Java经典排序算法之二分插入排序
查看>>
部署apache-tomcat环境
查看>>