.vimrc配置(持续更新)

参考vimbook-OPL,windows下默认配置和网友的总结,感觉自己也要维护配置文件才算入门。

文件目录
unix/linux: $HOME/.vimrc
windows: $vim安装目录_vimrc

这些都可以在gvim的【启动设置】中打开
.vimrc配置(持续更新)_第1张图片
gvim和vim的区别就是前者是GUI,后者是在控制台环境中的(Linux下叫xterm,注意如果xterm本身带配色的话,它会影响.vimrc的配色)

最后附上本文重点

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" plug setting
"https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim 放在 ./vim/autoload/if has("win32")
call plug#begin('~/vimfile/plugged')    "vim安装目录下
else
call plug#begin('~/.vim/plugged')       "用户目录下
endif
"自动索引符号
Plug 'universal-ctags/ctags'
Plug 'ludovicchabant/vim-gutentags'
"语法高亮
Plug 'octol/vim-cpp-enhanced-highlight'
"编译运行
Plug 'skywind3000/asyncrun.vim'
"动态检查
Plug 'dense-analysis/ale'
"修改比较
Plug 'mhinz/vim-signify'
"文本对齐命令
Plug 'junegunn/vim-easy-align'
"代码片段快速插入
Plug 'SirVer/ultisnips' | Plug 'honza/vim-snippets'
"文本对象全家桶
Plug 'kana/vim-textobj-user'
Plug 'kana/vim-textobj-indent'
Plug 'kana/vim-textobj-syntax'
Plug 'kana/vim-textobj-function', { 'for':['c', 'cpp', 'vim', 'java'] }
Plug 'sgur/vim-textobj-parameter'
"代码补全
Plug 'ycm-core/YouCompleteMe', { 'do': './install.py --clang-completer --go-completer' }
"函数列表+文本切换
Plug 'Yggdroot/LeaderF', { 'do': './install.sh' }
"参数提示
Plug 'Shougo/echodoc.vim'
"文件浏览
Plug 'vim-scripts/a.vim'
Plug 'justinmk/vim-dirvish'
Plug 'https://github.com/junegunn/vim-github-dashboard.git'
call plug#end()

"gutentags配置
" gutentags搜索工程目录的标志,碰到这些文件/目录名就停止向上一级目录递归 "
let g:gutentags_project_root = ['.root', '.svn', '.git', '.project']
" 所生成的数据文件的名称 "
let g:gutentags_ctags_tagfile = '.tags'
" 将自动生成的 tags 文件全部放入 ~/.cache/tags 目录中,避免污染工程目录 "
let s:vim_tags = expand('~/.cache/tags')
let g:gutentags_cache_dir = s:vim_tags
" 检测 ~/.cache/tags 不存在就新建 "
if !isdirectory(s:vim_tags)
   silent! call mkdir(s:vim_tags, 'p')
endif
" 配置 ctags 的参数 "
let g:gutentags_ctags_extra_args = ['--fields=+niazS', '--extra=+q']
let g:gutentags_ctags_extra_args += ['--c++-kinds=+pxI']
let g:gutentags_ctags_extra_args += ['--c-kinds=+px']

"编译配置
" 自动打开 quickfix window ,高度为 6
let g:asyncrun_open = 6
" 任务结束时候响铃提醒
let g:asyncrun_bell = 1
" 设置 F3 打开/关闭 Quickfix 窗口
nnoremap <F3> :call asyncrun#quickfix_toggle(6)<cr>
" 编译单个文件
nnoremap <silent> <F9> :AsyncRun gcc -Wall -O2 "$(VIM_FILEPATH)" -o "$(VIM_FILEDIR)/$(VIM_FILENOEXT)" <cr>
" 运行
nnoremap <silent> <F5> :AsyncRun -raw -cwd=$(VIM_FILEDIR) "$(VIM_FILEDIR)/$(VIM_FILENOEXT)" <cr>
" 搜索项目根目录规则
let g:asyncrun_rootmarks = ['.svn', '.git', '.root', '.project', '_darcs', 'build.xml']
" 编译整个项目
nnoremap <silent> <F7> :AsyncRun -cwd=<root> make <cr>
" 运行当前项目
nnoremap <silent> <F8> :AsyncRun -cwd=<root> -raw make run <cr>

"动态检查配置
let g:ale_linters_explicit = 1
let g:ale_linters = {
  \   'csh': ['shell'],
  \   'zsh': ['shell'],
  \   'go': ['gofmt', 'golint'],
  \   'python': ['flake8', 'mypy', 'pylint'],
  \   'c': ['gcc', 'cppcheck'],
  \   'cpp': ['gcc', 'cppcheck'],
  \   'text': [],
  \}
let g:ale_completion_delay = 500
let g:ale_echo_delay = 20
let g:ale_lint_delay = 500
let g:ale_echo_msg_format = '[%linter%] %code: %%s'
let g:ale_lint_on_text_changed = 'normal'
let g:ale_lint_on_insert_leave = 1
let g:airline#extensions#ale#enabled = 1
let g:ale_c_gcc_options = '-Wall -O2 -std=c99'
let g:ale_cpp_gcc_options = '-Wall -O2 -std=c++11'
let g:ale_c_cppcheck_options = ''
let g:ale_cpp_cppcheck_options = ''
"let g:ale_linters_explicit = 1

hi! clear SpellBad
hi! clear SpellCap
hi! clear SpellRare
hi! SpellBad gui=undercurl guisp=red
hi! SpellCap gui=undercurl guisp=blue
hi! SpellRare gui=undercurl guisp=magenta

"参数提示
set noshowmode

"代码补全
let g:ycm_add_preview_to_completeopt = 0
let g:ycm_show_diagnostics_ui = 0
let g:ycm_server_log_level = 'info'
let g:ycm_min_num_identifier_candidate_chars = 2
let g:ycm_collect_identifiers_from_comments_and_strings = 1
let g:ycm_complete_in_strings=1
let g:ycm_global_ycm_extra_conf = '~/.vim/plugged/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py'
let g:ycm_confirm_extra_conf = 0
let g:ycm_key_invoke_completion = ''
noremap <c-z> <NOP>
set completeopt=menu,menuone
let g:ycm_semantic_triggers =  {
			\ 'c,cpp,python,java,go,erlang,perl': ['re!\w{2}'],
			\ 'cs,lua,javascript': ['re!\w{2}'],
			\ }
" 修改补全列表配色
highlight PMenu ctermfg=0 ctermbg=242 guifg=black guibg=darkgrey
highlight PMenuSel ctermfg=242 ctermbg=8 guifg=darkgrey guibg=black

" 对指定源文件,输入两个字母后即触发语义补全
let g:ycm_semantic_triggers =  {
           \ 'c,cpp,python,java,go,erlang,perl': ['re!\w{2}'],
           \ 'cs,lua,javascript': ['re!\w{2}'],
           \ }

let g:ycm_filetype_whitelist = {
            \ "c":1,
            \ "cpp":1,
            \ "go":1,
            \ "python":1,
            \ "sh":1,
            \ "zsh":1,
            \ }

let g:ycm_filetype_blacklist = {
        \ 'markdown' : 1,
        \ 'text' : 1,
        \ 'pandoc' : 1,
        \ 'infolog' : 1,
        \}

"函数列表+文本切换
let g:Lf_ShortcutF = ''
let g:Lf_ShortcutB = ''
noremap <c-n> :LeaderfMru<cr>
noremap <m-p> :LeaderfFunction!<cr>
noremap <m-n> :LeaderfBuffer<cr>
noremap <m-m> :LeaderfTag<cr>
let g:Lf_StlSeparator = { 'left': '', 'right': '', 'font': '' }
let g:Lf_RootMarkers = ['.project', '.root', '.svn', '.git']
let g:Lf_WorkingDirectoryMode = 'Ac'
let g:Lf_WindowHeight = 0.30
let g:Lf_CacheDirectory = expand('~/.vim/cache')
let g:Lf_ShowRelativePath = 0
let g:Lf_HideHelp = 1
let g:Lf_StlColorscheme = 'powerline'
let g:Lf_PreviewResult = {'Function':0, 'BufTag':0}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" plug setting

filetype on
"关闭自动备份
set noundofile
set nobackup
set noswapfile

"设置文件的代码形式
set encoding=utf-8
set fileencodings=utf-8,chinese,latin-1
if has("win32")
    set fileencoding=chinese
else
    set fileencoding=utf-8
endif

"处理c文件和非c文件的注释文本自动补齐
:autocmd FileType * set formatoptions=tcql
\ nocindent comments&
:autocmd FileType c,cpp set formatoptions=croql
\ cindent comments=sr:/*,mb:*,ex:*/,://

"创建新文件时,根据后缀名自动加载模板文件内容
"这里的$VIM是vim安装路径,Linux下是/usr/share/vim/
"将需要的模板文件手动添加到$VIM/template/:autocmd BufNewFile *.c         0r $HOME/.vim/template/skeleton.c
:autocmd BufNewFile *.cpp       0r $HOME/.vim/template/skeleton.cpp
:autocmd BufNewFile *.go       0r $HOME/.vim/template/skeleton.go

"set cindent                  " 自动缩进4空格

"解决vim的菜单乱码
"source $VIMRUNTIME/delmenu.vim  
"source $VIMRUNTIME/menu.vim  

"解决vim提示信息乱码
language messages zh_CN.utf-8  
if has("win32")
    set termencoding=chinese
    language message zh_CN.utf-8
endif

set smartindent   "设置智能缩进
set shortmess=atI "去掉欢迎界面
colorscheme desert          "sublime的配色方案
if has("win32")
    set guifont=Lucida\ Console:h14:w7    "字体与字号
endif
if has("gui_gtk2")
    set guifont=Monospace\ 14             "字体与字号
endif
set expandtab                " 自动将tab转换成空格。如需前置插入tab,在Insert模式下使用CTRL-V<Tab>
set shiftwidth=4             " 换行时行间交错使用4个空格
set backspace=2              " 设置退格键可用
set autoindent               " 自动对齐
set smartindent              " 智能自动缩进
set number                   " 显示行号
set mouse=a                  " 启用鼠标
set ruler                    " 右下角显示光标位置的状态行
set hlsearch                 " 开启高亮显示结果
if has("gui_running")
    set incsearch            " 开启实时搜索功能,仅对gvim开放,vim会影响配色
endif
set nowrapscan               " 搜索到文件两端时不重新搜索
set nocompatible             " 关闭兼容模式
set vb t_vb=                 " 关闭提示音
au GuiEnter * set t_vb=      " 关闭闪屏
set hidden                   " 允许在有未保存的修改时切换缓冲区
set list                     " 显示Tab符
set listchars=tab:\|\        " 使用一高亮竖线代替Tab符(原版是^I)
set background=dark          " 背景色为暗色
set showcmd                  " 显示不完整的指令序列
set fileformats=unix,dos     " 打开文件时,优先当做Unix文件
set fileformat=unix          " 保存文件时,按照Unix文件保存
set endofline                " 让文本文件拥有<EOL>结束符
syntax enable                " 打开语法高亮
syntax on                    " 开启文件类型侦测
filetype indent on           " 针对不同的文件类型采用不同的缩进格式
filetype plugin on           " 针对不同的文件类型加载对应的插件
filetype plugin indent on    " 启用自动补全

" 启动时窗口最大化
" gui_gtk2和gui_running的区别是:前者判断系统是不是gtk图形界面,后者判断是不是在gui运行环境(非控制台环境),例如gvim
if has("win32")
    autocmd GUIEnter * simalt ~x
else
    if has("gui_running")
        " GUI is running or is about to start.
        " Maximize gvim window (for an alternative on Windows, see simalt below).
        set lines=999 columns=200
    else
        " This is console Vim.
        "if exists("+lines")
        "    set lines=50
        "endif
        "if exists("+columns")
        "    set columns=100
        "endif
    endi
endif

"windows下默认配置,先注释掉
"source $VIMRUNTIME/vimrc_example.vim
"set diffexpr=MyDiff()
"function MyDiff()
"    let opt = '-a --binary '
"    if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
"    if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
"    let arg1 = v:fname_in
"    if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
"    let arg1 = substitute(arg1, '!', '\!', 'g')
"    let arg2 = v:fname_new
"    if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
"    let arg2 = substitute(arg2, '!', '\!', 'g')
"    let arg3 = v:fname_out
"    if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
"    let arg3 = substitute(arg3, '!', '\!', 'g')
"    if $VIMRUNTIME =~ ' '
"        if &sh =~ '\
"            if empty(&shellxquote)
"                let l:shxq_sav = ''
"                set shellxquote&
"            endif
"            let cmd = '"' . $VIMRUNTIME . '\diff"'
"        else
"            let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
"        endif
"    else
"        let cmd = $VIMRUNTIME . '\diff'
"    endif
"    let cmd = substitute(cmd, '!', '\!', 'g')
"    silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3
"    if exists('l:shxq_sav')
"        let &shellxquote=l:shxq_sav
"    endif
"endfunction


网友提到的 set lines=999 columns=999 亲测在Ubuntu 18.04.02中会报错

The program ‘gvim’ received an X Window System error.
This probably reflects a bug in the program.
The error was ‘BadAlloc (insufficient resources for operation)’.
(Details: serial 275 error_code 11 request_code 53 minor_code 0)
(Note to programmers: normally, X errors are reported asynchronously;
that is, you will receive the error a while after causing it.
To debug your program, run it with the --sync command line
option to change this behavior. You can then get a meaningful
backtrace from your debugger if you break on the gdk_x_error() function.)
E852: The child process failed to start the GUI
请按 ENTER 或其它命令继续

所以我改小了。

你可能感兴趣的:(开发工具)