vim配置

来源:互联网 发布:linux 删除虚拟网卡 编辑:程序博客网 时间:2024/06/14 22:20

第一次学vim,折腾了不知道多少天,终于弄好了基本的配置,以后再慢慢补充。

先说下目前配置的问题:

复制的代码进来格式会混乱,由于括号补全会混入一些括号、引号什么的。

代码大部分来自互联网。

第一部分:基本配置

syntax on" 自动语法高亮set number" 显示行号set cursorline" 突出显示当前行set shiftwidth=4" 设定 << 和 >> 命令移动时的宽度为 4set softtabstop=4" 使得按退格键时可以一次删掉 4 个空格set tabstop=4" 设定 tab 长度为 4set nobackup" 覆盖文件时不备份set autochdir" 自动切换当前目录为当前文件所在的目录filetype plugin indent on " 开启插件set backupcopy=yes" 设置备份时的行为为覆盖set ignorecase smartcase" 搜索时忽略大小写,但在有一个或以上大写字母时仍保持对大小写敏感set incsearch" 输入搜索内容时就显示搜索结果set hlsearch" 搜索时高亮显示被找到的文本set noerrorbells" 关闭错误信息响铃set novisualbell" 关闭使用可视响铃代替呼叫set t_vb=" 置空错误铃声的终端代码set showmatch " 插入括号时,短暂地跳转到匹配的对应括号set matchtime=2 " 短暂跳转到匹配括号的时间set magic" 设置魔术set smartindent" 开启新行时使用智能自动缩进set foldmethod=syntax " 设置语法折叠setlocal foldlevel=1 " 设置折叠层数为nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>map <F2> :set nu!<CR>imap <F2> <ESC>:set nu!<CR>i

由于没有其他太多要求,感觉这足已。


第二部分:括号补全

function! AutoPair(open, close)let line = getline('.')if col('.') > strlen(line) || line[col('.') - 1] == ' 'return a:open.a:close."\<ESC>i"elsereturn a:openendifendffunction! ClosePair(char)if getline('.')[col('.') - 1] == a:charreturn "\<Right>"elsereturn a:charendifendffunction! SamePair(char)let line = getline('.')if col('.') > strlen(line) || line[col('.') - 1] == ' 'return a:char.a:char."\<ESC>i"elseif line[col('.') - 1] == a:charreturn "\<Right>"elsereturn a:charendifendffunction! EnterPair()let line = getline('.')if col('.') >= 1 &&  col('.') <= strlen(line) && line[col('.') - 2] == '{' && line[col('.') - 1] == '}'return "\<CR>\<ESC>bi\<Right>\<CR>"elsereturn "\<CR>"endifendfinoremap ( <c-r>=AutoPair('(', ')')<CR>inoremap ) <c-r>=ClosePair(')')<CR>inoremap { <c-r>=AutoPair('{', '}')<CR>inoremap } <c-r>=ClosePair('}')<CR>inoremap [ <c-r>=AutoPair('[', ']')<CR>inoremap ] <c-r>=ClosePair(']')<CR>inoremap " <c-r>=SamePair('"')<CR>inoremap ' <c-r>=SamePair("'")<CR>inoremap ` <c-r>=SamePair('`')<CR>inoremap <CR> <c-r>=EnterPair()<CR>

最后一部分EnterPair(),是按个人习惯自己加的,作用是打完“{}”按Enter后可以自动空出一行并调整缩进。

第三部分:快捷键编译运行(这里直接copy)过来了一段比较完整的代码

"------------------------------------------------------------------------------"  < 判断操作系统是否是 Windows 还是 Linux >"------------------------------------------------------------------------------if(has("win32") || has("win64") || has("win95") || has("win16"))    let g:iswindows = 1else    let g:iswindows = 0endif "------------------------------------------------------------------------------"  < 判断是终端还是 Gvim >"------------------------------------------------------------------------------if has("gui_running")    let g:isGUI = 1else    let g:isGUI = 0endif "------------------------------------------------------------------------------"  < 编译、连接、运行配置 >"------------------------------------------------------------------------------" F9 一键保存、编译、连接存并运行map <F5> :call Run()<CR>imap <F5> <ESC>:call Run()<CR> " Ctrl + F9 一键保存并编译map <c-F9> :call Compile()<CR>imap <c-F9> <ESC>:call Compile()<CR> " Ctrl + F10 一键保存并连接map <c-F10> :call Link()<CR>imap <c-F10> <ESC>:call Link()<CR> let s:LastShellReturn_C = 0let s:LastShellReturn_L = 0let s:ShowWarning = 1let s:Obj_Extension = '.o'let s:Exe_Extension = '.exe'let s:Sou_Error = 0 let s:windows_CFlags = 'gcc\ -fexec-charset=gbk\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o'let s:linux_CFlags = 'gcc\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o' let s:windows_CPPFlags = 'g++\ -fexec-charset=gbk\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o'let s:linux_CPPFlags = 'g++\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o' func! Compile()    exe ":ccl"    exe ":update"    if expand("%:e") == "c" || expand("%:e") == "cpp" || expand("%:e") == "cxx"        let s:Sou_Error = 0        let s:LastShellReturn_C = 0        let Sou = expand("%:p")        let Obj = expand("%:p:r").s:Obj_Extension        let Obj_Name = expand("%:p:t:r").s:Obj_Extension        let v:statusmsg = ''        if !filereadable(Obj) || (filereadable(Obj) && (getftime(Obj) < getftime(Sou)))            redraw!            if expand("%:e") == "c"                if g:iswindows                    exe ":setlocal makeprg=".s:windows_CFlags                else                    exe ":setlocal makeprg=".s:linux_CFlags                endif                echohl WarningMsg | echo " compiling..."                silent make            elseif expand("%:e") == "cpp" || expand("%:e") == "cxx"                if g:iswindows                    exe ":setlocal makeprg=".s:windows_CPPFlags                else                    exe ":setlocal makeprg=".s:linux_CPPFlags                endif                echohl WarningMsg | echo " compiling..."                silent make            endif            redraw!            if v:shell_error != 0                let s:LastShellReturn_C = v:shell_error            endif            if g:iswindows                if s:LastShellReturn_C != 0                    exe ":bo cope"                    echohl WarningMsg | echo " compilation failed"                else                    if s:ShowWarning                        exe ":bo cw"                    endif                    echohl WarningMsg | echo " compilation successful"                endif            else                if empty(v:statusmsg)                    echohl WarningMsg | echo " compilation successful"                else                    exe ":bo cope"                endif            endif        else            echohl WarningMsg | echo ""Obj_Name"is up to date"        endif    else        let s:Sou_Error = 1        echohl WarningMsg | echo " please choose the correct source file"    endif    exe ":setlocal makeprg=make"endfunc func! Link()    call Compile()    if s:Sou_Error || s:LastShellReturn_C != 0        return    endif    let s:LastShellReturn_L = 0    let Sou = expand("%:p")    let Obj = expand("%:p:r").s:Obj_Extension    if g:iswindows        let Exe = expand("%:p:r").s:Exe_Extension        let Exe_Name = expand("%:p:t:r").s:Exe_Extension    else        let Exe = expand("%:p:r")        let Exe_Name = expand("%:p:t:r")    endif    let v:statusmsg = ''    if filereadable(Obj) && (getftime(Obj) >= getftime(Sou))        redraw!        if !executable(Exe) || (executable(Exe) && getftime(Exe) < getftime(Obj))            if expand("%:e") == "c"                setlocal makeprg=gcc\ -o\ %<\ %<.o                echohl WarningMsg | echo " linking..."                silent make            elseif expand("%:e") == "cpp" || expand("%:e") == "cxx"                setlocal makeprg=g++\ -o\ %<\ %<.o                echohl WarningMsg | echo " linking..."                silent make            endif            redraw!            if v:shell_error != 0                let s:LastShellReturn_L = v:shell_error            endif            if g:iswindows                if s:LastShellReturn_L != 0                    exe ":bo cope"                    echohl WarningMsg | echo " linking failed"                else                    if s:ShowWarning                        exe ":bo cw"                    endif                    echohl WarningMsg | echo " linking successful"                endif            else                if empty(v:statusmsg)                    echohl WarningMsg | echo " linking successful"                else                    exe ":bo cope"                endif            endif        else            echohl WarningMsg | echo ""Exe_Name"is up to date"        endif    endif    setlocal makeprg=makeendfunc func! Run()    let s:ShowWarning = 0    call Link()    let s:ShowWarning = 1    if s:Sou_Error || s:LastShellReturn_C != 0 || s:LastShellReturn_L != 0        return    endif    let Sou = expand("%:p")    let Obj = expand("%:p:r").s:Obj_Extension    if g:iswindows        let Exe = expand("%:p:r").s:Exe_Extension    else        let Exe = expand("%:p:r")    endif    if executable(Exe) && getftime(Exe) >= getftime(Obj) && getftime(Obj) >= getftime(Sou)        redraw!        echohl WarningMsg | echo " running..."        if g:iswindows            exe ":!%<.exe"        else            if g:isGUI                exe ":!gnome-terminal -e ./%<"            else                exe ":!gnome-terminal -e ./%<"            endif        endif        redraw!        echohl WarningMsg | echo " running finish"    endifendfunc

这里倒数第7行做了修改,源代码为 ”exe ":! ./%<",问题是,运行时没打开新的终端窗口,导致运行时看不了代码。

这里还需要在终端配置文件中的命令页面选择命令退出时“保持终端打开”。


第四部分:YouCompleteMe

史上最难安装的插件。

其实按照官方提供的方法,只要一步都不错还是可以安装的,只是用法还未完全掌握,还需要阅读官方文档学习。

https://github.com/Valloric/YouCompleteMe

我是按照”Full Installation Guide“安装的。

安装完成后需要自己手动在.vimrc中加入.ycm_extra_conf.py文件的位置,以及需要根据实际情况手动更改.ycm_extra_conf.py文件中的内容。还不是很明白要怎么改。

不过这个代码补全插件真心不错。

set nocompatible              " be iMproved, requiredfiletype 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'" Plugin 'Valloric/YouCompleteMe'" " 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)" Plugin 'file:///home/gmarik/path/to/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'}"" " 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'let g:ycm_server_python_interpreter='/usr/bin/python'"let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'let g:ycm_global_ycm_extra_conf = '~/.vim/.ycm_extra_conf.py'   

目前就这么多,学习笔记、其它内容什么的日后再说。





原创粉丝点击