Vim配置:插件+常用设置(不定时更新)

完成图

前面先说一下,下面打配置是在Ubuntu下做的,只能保证在Linux下运行得好。
图1

详细配置

一、基本设置

以下是一些基本的vim本身的设置选项

set nocompatible  "It should be first line

" General {

    set background=dark     "Assume a dark background

    syntax on                   "Syntax highlighting
    set mouse=a                 "Automatically enable mouse usage
    set mousehide               "Hide mouse cursor while typing
    scriptencoding utf-8

    set cc=100                   "show a refence line
    set autochdir           "Automatically change the directory
    "autocmd BufEnter * if bufname("") !~ "^\[A-Za-z0-9\]*://" | lcd %:p:h |endif
    "Always switch to the current file directory

    "set autowrite                       " Automatically write a file when leaving a modified buffer
    set shortmess+=filmnrxoOtT          " Abbrev. of messages (avoids 'hit enter')
    set viewoptions=folds,options,cursor,unix,slash " Better Unix / Windows compatibility
    set virtualedit=onemore             " Allow for cursor beyond last character
    set history=1000                    " Store a ton of history (default is 20)
    "set spell                           " Spell checking on
    set hidden                          " Allow buffer switching without saving
    set iskeyword-=.                    " '.' is an end of word designator
    set iskeyword-=#                    " '#' is an end of word designator
    set iskeyword-=-                    " '-' is an end of word designator  set encoding=utf-8  
" }

" Vim UI {

    set laststatus=2
    colorscheme molokai         "Load a colorscheme
    set nu                      "Line numbers on
    set cursorline              "Highlight current line
    set tabpagemax=15           "Only show 15 tabs
    set showmatch               "Show matching brackets/parenthesis

" }

" Formating {
    set nowrap                  "Do not wrap long lines
    set softtabstop=4           "Let backspace delete indent
    set tabstop=4               "An indentation every four columns
    set shiftwidth=4            "Use indents of 4 spaces
    set expandtab               "Tabs are spaces, not tabs  
    set autoindent              "Indent at the same level of the previous line 
    set nojoinspaces            "Prevents inserting two spaces after punctuation on a join
    set splitright              " Puts new vsplit windows to the right of the current
    set splitbelow              " Puts new split windows to the bottom of the current
    set cindent
    set foldmethod=syntax       "Automatically fold
    "Close swap files
    set nobackup
    set nowb
    set noswapfile
    set t_Co=256

    if has("gui_running")
        set guioptions-=T
        "hide left&right scroll
        set guioptions-=l
        set guioptions-=L
        set guioptions-=r

        set guitablabel=%M\ %t
    endif
" }

二、插件配置


Vim 拥有海量插件可供使用,而且平时使用过程中如果有一个顺手的插件,编辑效率会高上不少。


I.强大的插件管理Vundle

Vundle是一个用于插件管理插件,同类型的插件管理还有Pathogen也非常受欢迎。
Vundle1
终端执行
$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
此命令会把Vundle下载到Vim的插件目录中去。
接着我们编辑位于用户目录下的.vimrc文件加入:

filetype off                  " 必须

" 初始化目录
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" 先添加进自己
Plugin 'VundleVim/Vundle.vim'

" 上面是插件
call vundle#end()            " 必须
filetype plugin indent on    " 必须

添加插件的方式有多种,我们这里采用Github用户名/插件名的方式。

简单的命令,vim内运行命令
:PluginList       - 列出配置的插件
:PluginInstall    - 安装插件
:PluginClean      - 卸载插件
只要把某个插件在配置中注释或者删去,运行:PluginClean即可卸载。

NOTE:如果你觉得麻烦,可以把本文翻到最后,有一份完整的配置文件,只需要:PluginInstall,然而可能会有问题。

2.漂亮的状态栏Airline

Airline是一个十分漂亮的状态栏插件,同类插件还有Powerline
Airline1
安装插件只需在配置文件中加入Plugin 'bling/vim-airline'

"========================================
" ariline
"========================================
let g:airline_powerline_fonts=1
let g:airline#extensions#tabline#enabled = 1
set guifont=Sauce\ Code\ Powerline\ Regular

保存后运行:PluginInstall即可。

NOTE:如果airline不显示箭头Airline插件需要可能需要特殊字体,上文中使用的是Source Code Pro 字体,可在这里获得。

NOTE:使用Tab与Shift+Tab切换标签。

3.优雅地查找文件Ctrlp

Ctrlp是一款快速查找文件的插件。
Ctrlp1

使用Plugin 'bling/vim-airline'安装。
配置:

"========================================
" Ctrlp
"========================================
let g:ctrlp_map = '<c-p>' "Ctrl+p 打开快速窗口
let g:ctrlp_cmd = 'CtrlP'
let g:ctrlp_working_path_mode = 'ra'
let g:ctrlp_show_hidden=1 "显示隐藏文件
" 忽略一下文件类型
let g:ctrlp_custom_ignore = {
    \ 'dir':  '\.git$\|\.hg$\|\.svn$',
    \ 'file': '\.exe$\|\.so$\|\.dll$\|\.pyc$' }
4.高效的跳转列表Tagbar

Tagbar是一个跳转列表插件其函数能够按类区分,支持按类折叠显示等,显示结果清晰简洁。

Tagbar1
使用Plugin 'majutsushi/tagbar'安装。
配置:

"========================================
" tagbar
"========================================
nmap <F3> :TagbarToggle<CR>
5.方便的文件树 Nerdtree

Nerdtree是一个“文件浏览器”插件,还提供一些基本的文件操作。
Nerdtree1

使用Plugin 'scrooloose/nerdtree'安装。
配置:

"========================================
" NerdTree
"========================================
nmap <F2> :NERDTreeToggle<CR>
let NERDTreeShowBookmarks=1
let NERDTreeIgnore=['\.py[cd]$', '\~$', '\.swo$', '\.swp$', '^\.git$', '^\.hg$', '^\.svn$', '\.bzr$']
let NERDTreeChDirMode=0
let NERDTreeQuitOnOpen=1
let NERDTreeMouseMode=2
let NERDTreeShowHidden=1 "显示隐藏文件
let NERDTreeKeepTreeInNewTab=1
let g:nerdtree_tabs_open_on_gui_startup=0
6.行云流水的代码Snipmate

Sinpmate是一款让你用起来停不下来的“自动补全”插件。
效果如下:
Sinpmate1
安装:

 Plugin 'MarcWeber/vim-addon-mw-utils'
 Plugin 'tomtom/tlib_vim'
 Plugin 'garbas/vim-snipmate'
 Plugin 'honza/vim-snippets'
7.强力的自动补全YouCompleteMe

自动补全+语法提示

Clang1

安装:

        Plugin 'Valloric/YouCompleteMe'

配置:

    " YouCompleteMe {
        " 自动补全配置
        set completeopt=longest,menu "让Vim的补全菜单行为与一般IDE一致(参考VimTip1228)
        autocmd InsertLeave * if pumvisible() == 0|pclose|endif "离开插入模式后自动关闭预览窗口
        inoremap <expr> <CR>       pumvisible() ? "\<C-y>" : "\<CR>"    "回车即选中当前项
        "上下左右键的行为 会显示其他信息
        inoremap <expr> <Down>     pumvisible() ? "\<C-n>" : "\<Down>"
        inoremap <expr> <Up>       pumvisible() ? "\<C-p>" : "\<Up>"
        inoremap <expr> <PageDown> pumvisible() ? "\<PageDown>\<C-p>\<C-n>" : "\<PageDown>"
        inoremap <expr> <PageUp>   pumvisible() ? "\<PageUp>\<C-p>\<C-n>" : "\<PageUp>"

        "youcompleteme  默认tab  s-tab 和自动补全冲突
        "let g:ycm_key_list_select_completion = ['<Down>']
        "let g:ycm_key_list_previous_completion = ['<Up>']
        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 <F5> :YcmForceCompileAndDiagnostics<CR>   "force recomile with syntastic
        "nnoremap <leader>lo :lopen<CR> "open locationlist
        "nnoremap <leader>lc :lclose<CR>    "close locationlist
        inoremap <leader><leader> <C-x><C-o>
        "在注释输入中也能补全
        let g:ycm_complete_in_comments = 1
        "在字符串输入中也能补全
        let g:ycm_complete_in_strings = 1
        "注释和字符串中的文字也会被收入补全
        let g:ycm_collect_identifiers_from_comments_and_strings = 0

        nnoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR>

     "}
8.神奇的多光标vim-multiple-cursors

一个类似Sublime Text的多光标插件。
MC1

使用Plugin 'terryma/vim-multiple-cursors'安装。

9.语法检查syntastic

支持语言众多的语法检查插件。
syntastic

使用Plugin 'scrooloose/syntastic'安装。

配置:

"========================================
" syntastic
"========================================
set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*

let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0
10.优雅地移动easymotion

让你优雅地在程序间自由移动。

easymotion1

使用Plugin 'easymotion/vim-easymotion'安装。

配置:

"========================================
" easymotion
"========================================
let g:EasyMotion_do_mapping = 0 " Disable default mappings
map  / <Plug>(easymotion-sn)
omap / <Plug>(easymotion-tn)
map  n <Plug>(easymotion-next)
map  N <Plug>(easymotion-prev)
map s <Plug>(easymotion-s2)
" Turn on case insensitive feature
let g:EasyMotion_smartcase = 1

.vimrc文件

修改位于/home/user/也就是 下面的.vimrc文件,这个文件只对user用户有效,如果想让配置文件在所有用户中都生效可以修改/etc下的.vimrc文件。
注意:用户文件夹下的配置文件会覆盖全局文件生效
配置如下:

set nocompatible  "It should be first line

" General {

    set background=dark     "Assume a dark background

    syntax on                   "Syntax highlighting
    set mouse=a                 "Automatically enable mouse usage
    set mousehide               "Hide mouse cursor while typing
    scriptencoding utf-8

    set cc=100                   "show a refence line
    set autochdir           "Automatically change the directory
    "autocmd BufEnter * if bufname("") !~ "^\[A-Za-z0-9\]*://" | lcd %:p:h |endif
    "Always switch to the current file directory

    "set autowrite                       " Automatically write a file when leaving a modified buffer
    set shortmess+=filmnrxoOtT          " Abbrev. of messages (avoids 'hit enter')
    set viewoptions=folds,options,cursor,unix,slash " Better Unix / Windows compatibility
    set virtualedit=onemore             " Allow for cursor beyond last character
    set history=1000                    " Store a ton of history (default is 20)
    "set spell                           " Spell checking on
    set hidden                          " Allow buffer switching without saving
    set iskeyword-=.                    " '.' is an end of word designator
    set iskeyword-=#                    " '#' is an end of word designator
    set iskeyword-=-                    " '-' is an end of word designator  set encoding=utf-8  
" }

" Vim UI {

    set laststatus=2
    colorscheme molokai         "Load a colorscheme
    set nu                      "Line numbers on
    set cursorline              "Highlight current line
    set tabpagemax=15           "Only show 15 tabs
    set showmatch               "Show matching brackets/parenthesis

" }

" Formating {
    set wrap                    "wrap long lines
    set softtabstop=4           "Let backspace delete indent
    set tabstop=4               "An indentation every four columns
    set shiftwidth=4            "Use indents of 4 spaces
    set expandtab               "Tabs are spaces, not tabs  
    set autoindent              "Indent at the same level of the previous line 
    set nojoinspaces            "Prevents inserting two spaces after punctuation on a join
    set splitright              " Puts new vsplit windows to the right of the current
    set splitbelow              " Puts new split windows to the bottom of the current
    set cindent
    set foldmethod=syntax       "Automatically fold
    "Close swap files
    set nobackup
    set nowb
    set noswapfile
    set t_Co=256

    if has("gui_running")
        set guioptions-=T
        "隐藏`滚动条
        set guioptions-=l
        set guioptions-=L
        set guioptions-=r

        set guitablabel=%M\ %t
    endif
" }

" Key Mappings {
    let mapleader=","
    "Select all
    map <c-a> ggVG
    "Switch the buffer
    nmap <c-TAB> :bn!<CR>
    nmap <s-TAB> :bp!<CR>
    "Open NerdTree window
    nmap <F2> :NERDTreeToggle<CR>
    "Open tagbar window
    nmap <F3> :TagbarToggle<CR>
    "Ctrl+s to save
    map <c-s> :w<CR>
    nmap <F6> :tabprevious<CR> 
    nmap <F7> :tabnext<CR> 
    nmap w= :resize +3<CR>
    nmap w- :resize -3<CR>
    nmap w, :vertical resize -3<CR>
    nmap w. :vertical resize +3<CR>
" }

" Plugin{

    " NerdTree {
        let NERDTreeShowBookmarks=1
        let NERDTreeIgnore=['\.py[cd]$', '\~$', '\.swo$', '\.swp$', '^\.git$', '^\.hg$', '^\.svn$', '\.bzr$']
        let NERDTreeChDirMode=0
        let NERDTreeQuitOnOpen=1
        let NERDTreeMouseMode=2
        let NERDTreeShowHidden=1
        let NERDTreeKeepTreeInNewTab=1
        let g:nerdtree_tabs_open_on_gui_startup=0
        autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") &&b:NERDTreeType == "primary") | q | endif
    " }

    " easymotion {
        let g:EasyMotion_do_mapping = 0 " Disable default mappings
        nmap  / <Plug>(easymotion-sn)
        omap / <Plug>(easymotion-tn)
        nmap  n <Plug>(easymotion-next)
        nmap  N <Plug>(easymotion-prev)
        nmap s <Plug>(easymotion-s2)
        " Turn on case insensitive feature
        let g:EasyMotion_smartcase = 1

        " JK motions: Line motions
        map <Leader>j <Plug>(easymotion-j)
        map <Leader>k <Plug>(easymotion-k)

    " }

    " Syntastic {
        set statusline+=%#warningmsg#
        set statusline+=%{SyntasticStatuslineFlag()}
        set statusline+=%*
        let g:syntastic_ignore_files=[".*\.py$"]
        let g:syntastic_always_populate_loc_list = 1
        let g:syntastic_auto_loc_list = 1
        let g:syntastic_check_on_open = 1
        let g:syntastic_check_on_wq = 0
    " }

    " Ultisnips {
        " Trigger configuration. Do not use <tab> if you use https://github.com/Valloric/YouCompleteMe.
        let g:UltiSnipsExpandTrigger="<F4>"
        let g:UltiSnipsJumpForwardTrigger="<c-j>"
        let g:UltiSnipsJumpBackwardTrigger="<c-b>"

        " If you want :UltiSnipsEdit to split your window.
        let g:UltiSnipsEditSplit="vertical"
    " }

    " UndoTree {
        nnoremap <c-z> :UndotreeToggle<CR>
        " If undotree is opened, it is likely one wants to interact with it.
        let g:undotree_SetFocusWhenToggle=1
        if has("persistent_undo")
            set undodir='~/.undodir/'
            set undofile
        endif
    " }

    " Ctrlp {
        let g:ctrlp_map = '<c-p>'
        let g:ctrlp_cmd = 'CtrlP'
        let g:ctrlp_show_hidden=1
        let g:ctrlp_working_path_mode = 'ra'
        let g:ctrlp_custom_ignore = {
            \ 'dir':  '\.git$\|\.hg$\|\.svn$',
            \ 'file': '\.exe$\|\.so$\|\.dll$\|\.pyc$' }
    " }

    " ariline {
        let g:airline_powerline_fonts=1
        let g:airline#extensions#tabline#enabled = 1
        set guifont=DejaVu\ Sans\ Mono\ for\ Powerline

    " }

    " indent guides {
        let g:indent_guides_guide_size=1
    " }

    " Tabularize {
         nmap <Leader>a& :Tabularize /&<CR>
         vmap <Leader>a& :Tabularize /&<CR>
         nmap <Leader>a= :Tabularize /^[^=]*\zs=<CR>
         vmap <Leader>a= :Tabularize /^[^=]*\zs=<CR>
         nmap <Leader>a=> :Tabularize /=><CR>
         vmap <Leader>a=> :Tabularize /=><CR>
         nmap <Leader>a: :Tabularize /:<CR>
         vmap <Leader>a: :Tabularize /:<CR>
         nmap <Leader>a:: :Tabularize /:\zs<CR>
         vmap <Leader>a:: :Tabularize /:\zs<CR>
         nmap <Leader>a, :Tabularize /,<CR>
         vmap <Leader>a, :Tabularize /,<CR>
         nmap <Leader>a,, :Tabularize /,\zs<CR>
         vmap <Leader>a,, :Tabularize /,\zs<CR>
         nmap <Leader>a<Bar> :Tabularize /<Bar><CR>
         vmap <Leader>a<Bar> :Tabularize /<Bar><CR>
    " }

    " YouCompleteMe {
        " 自动补全配置
        set completeopt=longest,menu "让Vim的补全菜单行为与一般IDE一致(参考VimTip1228)
        autocmd InsertLeave * if pumvisible() == 0|pclose|endif "离开插入模式后自动关闭预览窗口
        inoremap <expr> <CR>       pumvisible() ? "\<C-y>" : "\<CR>"    "回车即选中当前项
        "上下左右键的行为 会显示其他信息
        inoremap <expr> <Down>     pumvisible() ? "\<C-n>" : "\<Down>"
        inoremap <expr> <Up>       pumvisible() ? "\<C-p>" : "\<Up>"
        inoremap <expr> <PageDown> pumvisible() ? "\<PageDown>\<C-p>\<C-n>" : "\<PageDown>"
        inoremap <expr> <PageUp>   pumvisible() ? "\<PageUp>\<C-p>\<C-n>" : "\<PageUp>"

        "youcompleteme  默认tab  s-tab 和自动补全冲突
        "let g:ycm_key_list_select_completion = ['<Down>']
        "let g:ycm_key_list_previous_completion = ['<Up>']
        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 <F5> :YcmForceCompileAndDiagnostics<CR>   "force recomile with syntastic
        "nnoremap <leader>lo :lopen<CR> "open locationlist
        "nnoremap <leader>lc :lclose<CR>    "close locationlist
        inoremap <leader><leader> <C-x><C-o>
        "在注释输入中也能补全
        let g:ycm_complete_in_comments = 1
        "在字符串输入中也能补全
        let g:ycm_complete_in_strings = 1
        "注释和字符串中的文字也会被收入补全
        let g:ycm_collect_identifiers_from_comments_and_strings = 0

        nnoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR>

     "}

    " vim-latex{
        set grepprg=grep\ -nH\ $*
        let g:tex_flavor='latex'
        set iskeyword+=:
        autocmd BufEnter*.tex
        set sw =2

    " }

    " Vundle {
        filetype off

        set rtp+=~/.vim/bundle/Vundle.vim
        call vundle#begin()

        Plugin 'VundleVim/Vundle.vim'
        Plugin 'Valloric/YouCompleteMe'
        Plugin 'bling/vim-airline'
        Plugin 'kien/ctrlp.vim'
        Plugin 'majutsushi/tagbar'
        Plugin 'scrooloose/nerdtree'
        Plugin 'tomtom/tlib_vim'
        Plugin 'MarcWeber/vim-addon-mw-utils'
        Plugin 'easymotion/vim-easymotion'
        Plugin 'godlygeek/tabular'
        Plugin 'SirVer/ultisnips'
        Plugin 'mbbill/undotree'
        Plugin 'honza/vim-snippets'
        Plugin 'terryma/vim-multiple-cursors'
        Plugin 'vim-latex/vim-latex'
        Plugin 'tpope/vim-surround'
        Plugin 'nathanaelkane/vim-indent-guides'
        Plugin 'scrooloose/nerdcommenter'
        Plugin 'scrooloose/syntastic'

        call vundle#end()
        filetype plugin indent on
    "}
" }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值