vim详细说明和配置

vim详细说明和配置

1. vim支持的功能和搜索目录

终端输入:vim –version 可以查看vim支持的功能和搜索目录。(+)代表支持的功能,(-)代表不支持的功能,有些vim插件(例如youcompleteme)需要pythone的支持,如果vim不支持python,就要重装vim。搜索目录是vim加载脚本时的查找路径,在终端输入vim,然后输入“:scriptname”就可以查看脚本调用的顺序,这在调试脚本的时候是相当有用的。
笔者vim的搜索路径如下

系统 vimrc 文件: "$VIM/vimrc"
用户 vimrc 文件: "$HOME/.vimrc"
第二用户 vimrc 文件: "~/.vim/vimrc"
用户 exrc 文件: "$HOME/.exrc"
$VIM 预设值: "/usr/share/vim"

其它网站在介绍vim的配置的时候都是推荐在$HOME/.vimrc上进行配置,这种配置最大的好处就是只对当前用户有效,但这也是弊端,有时系统上有好些用户,就要对每个用户进行配置,这是很麻烦的。所以,笔者建议在/usr/share/vim/vimrc上进行配置。

2. vim常规配置

将下面的代码复制到/usr/share/vim/vimrc中,双引号”后面是注释,读者可以大致看看,至于配置代码的具体语法可以推荐读者查阅vim的脚本语言vimscript

"============================== UI Settings =================================
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Map leader <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
let mapleader=',' "功能键的前缀
filetype on 
filetype plugin on
syntax enable "开启高亮
syntax on "打开高亮
highlight Number ctermfg=DarkCyan
map <F6> :nohl<CR>
set nowrap "禁止折行
"*.md文件允许折行
autocmd BufNewFile,BufRead *.md setlocal wrap
set mouse=a "支持鼠标
set nu "显示行数
set shiftwidth=4 "换行时行间交错使用4空格
set cindent shiftwidth=4 "自动缩进4空格
set ts=4 "tab四个空格
set expandtab
set autoread "当文件在外部修改时,Vim自动更新载入
set magic "正则表达式匹配形式
set ignorecase "搜索时忽略大小写
set hlsearch "高亮显示结果
set smartcase "有一个或一个以上大写字母时仍然大小写敏感
set smartindent "智能缩进
set backspace=indent,eol,start "backspace 删除启用
"set paste
"set formatoptions=ql

"===================折叠代码====================
set foldmethod=syntax
set nofoldenable

"=================== 状态栏 ====================
"状态栏设置
set laststatus=2
if !has('gui_running')
    set t_Co=256
endif

"====================快捷方式====================
"保存
inoremap <c-w> <Esc>:w<cr>a
nnoremap <c-w> :w<cr>
"保存并退出
inoremap <c-e> <Esc>:wq<cr>
nnoremap <c-e> :wq<cr>
"删除一行
inoremap <Delete>d <Esc>ddi
"插入一行
inoremap <a-o> <Esc>o
inoremap <a-O> <Esc>0

3. 自动关闭标签

上面的代码可以对代码进行缩进和高亮等,在编辑代码的时候有时需要自动关闭标签。读者想实现这个功能就将下面的代码附加到上面的代码后。

"==================自动补全括号等============================
inoremap ( <c-r>=OpenPair('(')<CR>
inoremap ) <c-r>=ClosePair(')')<CR>
inoremap { <c-r>=OpenPair('{')<CR>
inoremap } <c-r>=ClosePair('}')<CR>
inoremap [ <c-r>=OpenPair('[')<CR>
inoremap ] <c-r>=ClosePair(']')<CR>
inoremap <Enter> <c-r>=TestEnterPair()<CR>
function! TestEnterPair()
    let linestr = getline('.')
    let colno = col('.')
    if linestr[colno - 1] == '}' && linestr[colno - 2] == '{'
        return "\<Enter>\<Enter>\<Up>\t"
    else
        return "\<Enter>"
    endif
endfunction
" just for xml document, but need not for now.
"inoremap < <c-r>=OpenPair('<')<CR>
"inoremap > <c-r>=ClosePair('>')<CR>
function! OpenPair(char)
    let PAIRs = {
                \ '{' : '}',
                \ '[' : ']',
                \ '(' : ')',
                \ '<' : '>'
                \}
    if line('$')>2000
        let line = getline('.')

        let txt = strpart(line, col('.')-1)
    else
        let lines = getline(1,line('$'))
        let line=""
        for str in lines
            let line = line . str . "\n"
        endfor

        let blines = getline(line('.')-1, line("$"))
        let txt = strpart(getline("."), col('.')-1)
        for str in blines
            let txt = txt . str . "\n"
        endfor
    endif
    let oL = len(split(line, a:char, 1))-1
    let cL = len(split(line, PAIRs[a:char], 1))-1

    let ol = len(split(txt, a:char, 1))-1
    let cl = len(split(txt, PAIRs[a:char], 1))-1

    if oL>=cL || (oL<cL && ol>=cl)
        return a:char . PAIRs[a:char] . "\<Left>"
    else
        return a:char
    endif
endfunction
function! ClosePair(char)
    if getline('.')[col('.')-1] == a:char
        return "\<Right>"
    else
        return a:char
    endif
endf

inoremap ' <c-r>=CompleteQuote("'")<CR>
inoremap " <c-r>=CompleteQuote('"')<CR>
function! CompleteQuote(quote)
    let ql = len(split(getline('.'), a:quote, 1))-1
    let slen = len(split(strpart(getline("."), 0, col(".")-1), a:quote, 1))-1
    let elen = len(split(strpart(getline("."), col(".")-1), a:quote, 1))-1
    let isBefreQuote = getline('.')[col('.') - 1] == a:quote

    if isBefreQuote
        return "\<Right>"
    elseif '"'==a:quote && "vim"==&ft && 0==match(strpart(getline('.'), 0, col('.')-1), "^[\t ]*$")
        " for vim comment.
        return a:quote
    elseif "'"==a:quote && 0==match(getline('.')[col('.')-2], "[a-zA-Z0-9]")
        " for Name's Blog.
        return a:quote
    elseif (ql%2)==1
        " a:quote length is odd.
        return a:quote
    elseif ((slen%2)==1 && (elen%2)==1 && !isBefreQuote) || ((slen%2)==0 && (elen%2)==0)
        return a:quote . a:quote . "\<Left>"
    else
        return a:quote . a:quote . "\<Left>"
    endif
endfunction

4. vim插件的安装和管理

Vundle可以实现对vim插件的安装和管理,所以我们摒弃传统安装插件的方法,先安装Vundle,让它来系统的安装和管理vim插件

4.1 Vundle的安装

sudo git clone https://github.com/VundleVim/Vundle.vim.git /usr/share/vim/bundle/Vundle.vim

读者如果仅仅想对当前用户进行配置,执行下面的命令

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

执行完成以后需要对Vundle进行配置,配置代码附加到”/usr/share/vim/vimrc”的最前面(当前用户附加到”~/.vimrc”最前面)

"=============================== Vundle ====================================
set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=/usr/share/vim/bundle/Vundle.vim
call vundle#begin()

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

">>>>>>>>>>>>>>>>>>>>>> 插件安装在下面 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
"Plugin 'Plugin name'

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required
"============================== vundle end ===================================

Vundle安装的插件都是github上面托管的插件,如果一个vim插件的github路径是github.com/VundleVim/Vundle.vim,安装命令就是:Plugin ‘VundleVim/Vundle.vim’ 。更多的优秀的vim插件,读者可以在vimawesome.com上进行搜索。
下面是读者安装的Vundle插件

"=============================== Vundle ====================================
set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=/usr/share/vim/bundle/Vundle.vim
call vundle#begin()

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

">>>>>>>>>>>>>>>>>>>>>>>>>>>> git 支持状态栏显示分支信息 <<<<<<<<<<<<<<<<<<
Plugin 'tpope/vim-fugitive'

">>>>>>>>>>>>>>>>>>>>>>>>>>>> 状态栏 lightline <<<<<<<<<<<<<<<<<<<<<<<<<<<<
Plugin 'itchyny/lightline.vim'
let g:lightline = {
      \ 'colorscheme': 'wombat',
      \ 'active': {
      \   'left': [ [ 'mode', 'paste' ],
      \             [ 'fugitive', 'readonly', 'filename', 'modified' ] ]
      \ },
      \ 'component': {
      \   'readonly': '%{&filetype=="help"?"":&readonly?"!":""}',
      \   'modified': '%{&filetype=="help"?"":&modified?"+":&modifiable?"":"-"}',
      \   'fugitive': '%{exists("*fugitive#head")?"Ϡ".fugitive#head():""}'
      \ },
      \ 'component_visible_condition': {
      \   'readonly': '(&filetype!="help"&& &readonly)',
      \   'modified': '(&filetype!="help"&&(&modified||!&modifiable))',
      \   'fugitive': '(exists("*fugitive#head") && ""!=fugitive#head())'
      \ },
      \ }

">>>>>>>>>>>>>>>>>>>>>>>>>>> 文档结构 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Plugin 'scrooloose/nerdtree'
let NERDTreeWinPos='left'
let NERDTreeWinSize=30
map <F2> :NERDTreeToggle<CR>
nnoremap <leader>nt :NERDTreeFind<CR>

">>>>>>>>>>>>>>>>>>>>>>>>>> 代码结构 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Plugin 'majutsushi/tagbar'
"nmap <Leader>tb :TagbarToggle<CR>        "快捷键设置
nnoremap <leader>tb :TagbarOpen fj<CR>
nnoremap <leader>tf :TagbarSetFoldlevel 0<CR>
nnoremap <leader>tu :TagbarSetFoldlevel 99<CR>
let g:tagbar_foldlevel = 0 
let g:tagbar_ctags_bin='ctags'            "ctags程序的路径
let g:tagbar_width=30                    "窗口宽度的设置
map <F3> :Tagbar<CR>
"autocmd BufReadPost *.cpp,*.c,*.h,*.hpp,*.cc,*.cxx call tagbar#autoopen()
"如果是c语言的程序的话,tagbar自动开启

">>>>>>>>>>>>>>>>>>>>>>>>>> cscope <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Plugin 'brookhong/cscope.vim'
nnoremap <leader>l :call ToggleLocationList()<CR>
" s: Find this C symbol
nnoremap  <leader>fs :call CscopeFind('s', expand('<cword>'))<CR>
" g: Find this definition
nnoremap  <leader>fd :call CscopeFind('g', expand('<cword>'))<CR>
" d: Find functions called by this function
nnoremap  <leader>ff :call CscopeFind('d', expand('<cword>'))<CR>
" c: Find functions calling this function
nnoremap  <leader>fc :call CscopeFind('c', expand('<cword>'))<CR>
" t: Find this text string
nnoremap  <leader>ft :call CscopeFind('t', expand('<cword>'))<CR>
" e: Find this egrep pattern
nnoremap  <leader>fe :call CscopeFind('e', expand('<cword>'))<CR>
" f: Find this file
nnoremap  <leader>fl :call CscopeFind('f', expand('<cword>'))<CR>
" i: Find files #including this file
nnoremap  <leader>fi :call CscopeFind('i', expand('<cword>'))<CR>


" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

"============================== vundle end ===================================

4.2 使用Vundle进行安装

Vundle安装和配置好以后,输入”:PluginInstall”进行安装vim插件。Vundle的其它命令读者可以在Vundle的github项目上找到。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值