资讯专栏INFORMATION COLUMN

我的.vimrc,代码完成基于YcmCompleteMe版

tinysun1234 / 2340人阅读

摘要:部分必需正确配置的目录。刚有个哥们问怎样表明就成功了,我定义了为命令,按就会执行,如果代码正确而运行不出错就成功了,若代码不正确运行就会出现窗口,给出错误提示,这也表明配置已经成功。配置文档的倒数到注释掉。

相应的安装部分见上篇文章:vim学习笔记

.vimrc配置

1.基于YcmCompleteMe、taglist.vim、The-NERD-tree等配置,F2调出目录树窗口,F3调出变量、函数窗口,有点IDE的样子,呵呵!
2.定义了一些基本的快捷键:C-a 全选,C-c 选中状态下复制,F5 编译及运行,F8 调试,gb 跳到定义处。
3.Syntastic部分必需正确配置C++ include的目录。
4.刚有个哥们问怎样表明就成功了,我定义了F4为:YcmDiags命令,按F4就会执行,如果代码正确而运行不出错就成功了,若代码不正确运行就会出现Quickfix窗口,给出错误提示,这也表明配置已经成功。但这个配置其实没多大用处,因为只要一存盘,就会自动调用该命令,呵呵!

"********************************************************
"                   一般性配置                          *
"********************************************************
"关闭vim一致性原则
set nocompatible
"显示行号
set number
"设置在编辑过程中右下角显示光标的行列信息
set ruler
"在状态栏显示正在输入的命令
set showcmd
"设置历史记录条数
set history=1000
"设置取消备份 禁止临时文件的生成
set nobackup
set noswapfile
"设置匹配模式
set showmatch
"设置C/C++方式自动对齐
set autoindent
set cindent
"开启语法高亮功能
syntax enable
syntax on
"指定配色方案为256色
set t_Co=256
"设置搜索时忽略大小写
set ignorecase
"配置backspace的工作方式
set backspace=indent,eol,start
"选中高亮
set hls 
"高亮光标所在行
"set cul "cursorline 
"set cuc
"set go=             " 不要图形按钮  
"color desert     " 设置背景主题  
color ron     " 设置背景主题  
"color torte     " 设置背景主题  
set guifont=Courier_New:h10:cANSI   " 设置字体  
"autocmd InsertLeave * se nocul  " 用浅色高亮当前行  
"autocmd InsertEnter * se cul    " 用浅色高亮当前行  
"set whichwrap+=<,>,h,l   " 允许backspace和光标键跨越行边界(不建议)  
" 光标移动到buffer的顶部和底部时保持3行距离  
set scrolloff=3
" 总是显示状态行
" set cmdheight=1
set statusline=%F%m%r%h%w [FORMAT=%{&ff}] [TYPE=%Y] [POS=%l,%v][%p%%] %{strftime("%d/%m/%y - %H:%M")}
set laststatus=1    " 启动显示状态行(1),总是显示状态行(2)  
set foldenable      " 允许折叠  
"set foldmethod=manual   " 手动折叠  
"set magic                   " 设置魔术
"set guioptions-=T           " 隐藏工具栏
"set guioptions-=m           " 隐藏菜单栏
let g:indentLine_char = "┊"
"启动时不显示帮助乌干达儿童提示
set shortmess=atI 
"设置在vim中可以使用鼠标
set mouse=a
"设置tab宽度
set tabstop=4
"设置自动对齐空格数
set shiftwidth=4
"设置退格键时可以删除4个空格
set smarttab
set softtabstop=4
"将tab键自动转换为空格
set expandtab
"设置编码方式
set encoding=utf-8
"自动判断编码时 依次尝试以下编码
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1
"检测文件类型
filetype on
"针对不同的文件采取不同的缩进方式
filetype indent on
"允许插件
filetype plugin on
"启动智能补全
filetype plugin indent on
"*********************************************************
"                  vundle 配置                           *
"*********************************************************
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
" let Vundle manage Vundle
Bundle "gmarik/vundle"
" My Bundles here:
Bundle "tpope/vim-fugitive"
Bundle "Lokaltog/vim-easymotion"
Bundle "Yggdroot/indentLine"
Bundle "rstacruz/sparkup", {"rtp": "vim/"}
Bundle "tpope/vim-rails.git"
Bundle "taglist.vim"
Bundle "The-NERD-tree"
Bundle "Syntastic"
Bundle "L9"
Bundle "FuzzyFinder"
Bundle "Lokaltog/vim-powerline"
Bundle "Valloric/YouCompleteMe" 
"*****************************************************
"                   taglist配置                      *
"*****************************************************
"不显示"press F1 to display help"
let Tlist_Compact_Format=1
"窗口在左侧显示
let Tlist_Use_Right_Window=1
"只显示当前文件的tags
let Tlist_Show_One_File=1  
"高亮显示
let Tlist_Auto_Highlight_tag=1
"随文件自动更新
let Tlist_Auto_Update=1
"设置宽度
let Tlist_WinWidth=30       
"taglist窗口是最后一个窗口,则退出vim
let Tlist_Exit_OnlyWindow=1 
"单击跳转
let Tlist_Use_SingClick=1
"打开关闭快捷键
nnoremap   :TlistToggle
"********************************************************
"                      NERD_Tree 配置                   *
"********************************************************
"显示增强
let NERDChristmasTree=1
"自动调整焦点
let NERDTreeAutoCenter=1
"鼠标模式:目录单击,文件双击
let NERDTreeMouseMode=2
"打开文件后自动关闭
let NERDTreeQuitOnOpen=0
"显示文件
let NERDTreeShowFiles=1
"显示隐藏文件
let NERDTreeShowHidden=0
"高亮显示当前文件或目录
let NERDTreeHightCursorline=1
"显示行号
let NERDTreeShowLineNumbers=1
"窗口位置
let NERDTreeWinPos="left"
"窗口宽度
let NERDTreeWinSize=31
"不显示"Bookmarks" label "Press ? for help"
let NERDTreeMinimalUI=1
"快捷键
nnoremap   :NERDTreeToggle
"当打开vim且没有文件时自动打开NERDTree
autocmd vimenter * if !argc() | NERDTree | endif
"只剩 NERDTree时自动关闭
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif

" 设置当文件被改动时自动载入
" set autoread
"*****************************************************
"           YouCompleteMe配置                        *
"*****************************************************
"leader映射为逗号“,”
let mapleader = ","  
"配置默认的ycm_extra_conf.py
"let g:ycm_global_ycm_extra_conf = "~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py" 
"按gb 会跳转到定义
nnoremap  gb :YcmCompleter GoToDefinitionElseDeclaration   
"打开vim时不再询问是否加载ycm_extra_conf.py配置
let g:ycm_confirm_extra_conf=0   
"使用ctags生成的tags文件
let g:ycm_collect_identifiers_from_tag_files = 1 
map  : YcmDiags
"*****************************************************
"           Syntastic配置                            *
"*****************************************************
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++"
let g:syntastic_cpp_compiler_options = "-std=c++11 -stdlib=libstdc++"
"set error or warning signs
let g:syntastic_error_symbol = "✗"
let g:syntastic_warning_symbol = "⚠"
"whether to show balloons
let g:syntastic_enable_balloons = 1
"*****************************************************
"C,C++ 按F5编译运行                                 *
"*****************************************************
map  :call CompileRunGcc()
func! CompileRunGcc()
    exec "w"
    if &filetype == "c"
        exec "!gcc % -o %<"
        exec "! ./%<"
    elseif &filetype == "cpp"
        exec "!g++ % -o %<"
        exec "! ./%<"
    elseif &filetype == "java" 
        exec "!javac %" 
        exec "!java %<"
    elseif &filetype == "sh"
        :!./%
    endif
endfunc
"*****************************************************
"C,C++的调试 F8                                      *
"*****************************************************
map  :call Rungdb()
func! Rungdb()
    exec "w"
    exec "!g++ % -g -o %<"
    exec "!gdb ./%<"
endfunc
"*****************************************************
"键盘命令                                            *
"*****************************************************
nmap w :w!
nmap f :find
" 映射全选+复制 ctrl+a
map  ggVGY
map!  ggVGY
" 选中状态下 Ctrl+c 复制
vmap  "+y
"*****************************************************
"新文件标题                                          *
"*****************************************************
"新建.c,.h,.sh,.java文件,自动插入文件头 
autocmd BufNewFile *.cpp,*.[ch],*.sh,*.rb,*.java,*.py exec ":call SetTitle()" 
""定义函数SetTitle,自动插入文件头 
func SetTitle() 
    "如果文件类型为.sh文件 
    if &filetype == "sh" 
        call setline(1,"#!/bin/bash") 
        call append(line("."), "") 
    elseif &filetype == "python"
        call setline(1,"#!/usr/bin/env python")
        call append(line("."),"# coding=utf-8")
        call append(line(".")+1, "") 

    elseif &filetype == "ruby"
        call setline(1,"#!/usr/bin/env ruby")
        call append(line("."),"# encoding: utf-8")
        call append(line(".")+1, "")
    else 
        call setline(1, "/*************************************************************************") 
        call append(line("."),   "  > File Name   : ".expand("%")) 
        call append(line(".")+1, "  > Author      : zhoutk") 
        call append(line(".")+2, "  > Mail        : zhoutk@189.cn") 
        call append(line(".")+3, "  > Created Time: ".strftime("%c")) 
        call append(line(".")+4, " ************************************************************************/") 
        call append(line(".")+5, "")
    endif
    if expand("%:e") == "cpp"
        call append(line(".")+6, "#include")
        call append(line(".")+7, "using namespace std;")
        call append(line(".")+8, "")
    endif
    if &filetype == "c"
        call append(line(".")+6, "#include")
        call append(line(".")+7, "")
    endif
    if expand("%:e") == "h"
        call append(line(".")+6, "#ifndef _".toupper(expand("%:r"))."_H")
        call append(line(".")+7, "#define _".toupper(expand("%:r"))."_H")
        call append(line(".")+8, "#endif")
    endif
    if &filetype == "java"
        call append(line(".")+6,"public class ".expand("%:r"))
        call append(line(".")+7,"")
    endif
    "新建文件后,自动定位到文件末尾
endfunc 
autocmd BufNewFile * normal G

"*****************************************************
"代码格式优化化                                      *
"*****************************************************

map  :call FormartSrc()

"定义FormartSrc()
func FormartSrc()
    exec "w"
    if &filetype == "c"
        exec "!astyle --style=ansi -a --suffix=none %"
    elseif &filetype == "cpp" || &filetype == "hpp"
        exec "r !astyle --style=ansi --one-line=keep-statements -a --suffix=none %> /dev/null 2>&1"
    elseif &filetype == "perl"
        exec "!astyle --style=gnu --suffix=none %"
    elseif &filetype == "py"||&filetype == "python"
        exec "r !autopep8 -i --aggressive %"
    elseif &filetype == "java"
        exec "!astyle --style=java --suffix=none %"
    elseif &filetype == "jsp"
        exec "!astyle --style=gnu --suffix=none %"
    elseif &filetype == "xml"
        exec "!astyle --style=gnu --suffix=none %"
    else
        exec "normal gg=G"
        return
    endif
    exec "e! %"
endfunc
"结束定义FormartSrc

"下面两个试验没成功,注释后留在这做个记录,去空行若是定义为F4是成功的。
"去空行  
"nnoremap  :g/^s*$/d 
"比较文件  
"nnoremap  :vert diffsplit 
.ycm_extra_conf.py配置

拷贝模板~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py到~/
下,主要有两点改动:
1.flags增加对c++相关目录的配置,我把针对OS X的配置删除了。
2.配置文档的倒数8到10注释掉。

# 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 os
import 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",
"-DNDEBUG",
# You 100% do NOT need -DUSE_CLANG_COMPLETER in your flags; only the YCM
# source code needs it.
"-DUSE_CLANG_COMPLETER",
# 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++",
"-isystem",
"../BoostParts",
"-isystem",
"/usr/include",
"-isystem",
"/usr/include/c++/",
"-isystem",
"/usr/include/c++/5.2.1",
]


# 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
#
# You can get CMake to generate this file for you by adding:
#   set( CMAKE_EXPORT_COMPILE_COMMANDS 1 )
# to your CMakeLists.txt file.
#
# 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 = None

SOURCE_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_flags


def 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
  }
2015-11-24更新版.vimrc
function! WrapForTmux(s)
  if !exists("$TMUX")
    return a:s
  endif

  let tmux_start = "Ptmux;"
  let tmux_end = ""

  return tmux_start . substitute(a:s, "", "", "g") . tmux_end
endfunction

let &t_SI .= WrapForTmux("[?2004h")
let &t_EI .= WrapForTmux("[?2004l")

function! XTermPasteBegin()
  set pastetoggle=[201~
  set paste
  return ""
endfunction

inoremap   [200~ XTermPasteBegin()

if exists("$ITERM_PROFILE")
  if exists("$TMUX")
    let &t_SI = "[3 q"
    let &t_EI = "[0 q"
  else
    let &t_SI = "]50;CursorShape=1x7"
    let &t_EI = "]50;CursorShape=0x7"
  endif
end

"********************************************************
"                   一般性配置                          *
"********************************************************
"关闭vim一致性原则
set nocompatible
"显示行号
set number
"设置在编辑过程中右下角显示光标的行列信息
set ruler
"在状态栏显示正在输入的命令
set showcmd
"设置历史记录条数
set history=1000
"设置取消备份 禁止临时文件的生成
set nobackup
set noswapfile
"设置匹配模式
set showmatch
"设置C/C++方式自动对齐
set autoindent
set cindent
"开启语法高亮功能
syntax enable
syntax on
"指定配色方案为256色
set t_Co=256
"设置搜索时忽略大小写
set ignorecase
"配置backspace的工作方式
set backspace=indent,eol,start
"选中高亮
set hls 
"高亮光标所在行
"set cul "cursorline 
"set cuc
"set go=             " 不要图形按钮  
"color desert     " 设置背景主题  
color ron     " 设置背景主题  
"color torte     " 设置背景主题  
set guifont=Courier_New:h10:cANSI   " 设置字体  
"autocmd InsertLeave * se nocul  " 用浅色高亮当前行  
"autocmd InsertEnter * se cul    " 用浅色高亮当前行  
"set whichwrap+=<,>,h,l   " 允许backspace和光标键跨越行边界(不建议)  
" 光标移动到buffer的顶部和底部时保持3行距离  
set scrolloff=3
" 总是显示状态行
set cmdheight=1
set statusline=%F%m%r%h%w [FORMAT=%{&ff}] [TYPE=%Y] [POS=%l,%v][%p%%] %{strftime("%d/%m/%y - %H:%M")}
set laststatus=2    " 启动显示状态行(1),总是显示状态行(2)  
set foldenable      " 允许折叠  
"set foldmethod=manual   " 手动折叠  
"set magic                   " 设置魔术
"set guioptions-=T           " 隐藏工具栏
"set guioptions-=m           " 隐藏菜单栏
let g:indentLine_char = "┊"
"启动时不显示帮助乌干达儿童提示
set shortmess=atI 
"设置在vim中可以使用鼠标
set mouse=a
"设置tab宽度
set tabstop=4
"设置自动对齐空格数
set shiftwidth=4
"设置退格键时可以删除4个空格
set smarttab
set softtabstop=4
"将tab键自动转换为空格
set expandtab
"设置编码方式
set encoding=utf-8
"自动判断编码时 依次尝试以下编码
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1
"检测文件类型
filetype on
"针对不同的文件采取不同的缩进方式
filetype indent on
"允许插件
filetype plugin on
"简化+寄存器操作
set clipboard=unnamedplus
"启动智能补全
filetype plugin indent on
cnoremap  %% getcmdtype() == ":" ? expand("%:h")."/" : "%%"
"*********************************************************
"                  vundle 配置                           *
"*********************************************************
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
" let Vundle manage Vundle
Bundle "gmarik/vundle"
" My Bundles here:
Bundle "SuperTab"
Bundle "Vim-JavaScript"
Bundle "a.vim"
Bundle "c.vim"
Bundle "Align"
Bundle "L9"
Bundle "vim-javacompleteex"
Bundle "tpope/vim-fugitive"
Bundle "Lokaltog/vim-easymotion"
Bundle "Yggdroot/indentLine"
Bundle "rstacruz/sparkup", {"rtp": "vim/"}
Bundle "ctags.vim"
Bundle "taglist.vim"
Bundle "The-NERD-tree"
Bundle "Syntastic"
Bundle "std_c.zip"
Bundle "FuzzyFinder"
Bundle "tpope/vim-commentary"
Bundle "tpope/vim-surround"
Bundle "Lokaltog/vim-powerline"
Bundle "Valloric/YouCompleteMe" 
Bundle "Valloric/MatchTagAlways"
"*****************************************************
"               MatchTagAlways配置                   *
"*****************************************************
let g:mta_use_matchparen_group = 1
"*****************************************************
"           FuzzyFinder配置                          *
"*****************************************************
map F :FufFile
map f :FufTaggedFile
map g :FufTag
map b :FufBuffer
"*****************************************************
"                   taglist配置                      *
"*****************************************************
"不显示"press F1 to display help"
let Tlist_Compact_Format=1
"窗口在左侧显示
let Tlist_Use_Right_Window=1
"只显示当前文件的tags
let Tlist_Show_One_File=1  
"高亮显示
let Tlist_Auto_Highlight_tag=1
"随文件自动更新
let Tlist_Auto_Update=1
"设置宽度
let Tlist_WinWidth=30       
"taglist窗口是最后一个窗口,则退出vim
let Tlist_Exit_OnlyWindow=1 
"单击跳转
let Tlist_Use_SingClick=1
"打开关闭快捷键
nnoremap   :TlistToggle
"********************************************************
"                      NERD_Tree 配置                   *
"********************************************************
"显示增强
let NERDChristmasTree=1
"自动调整焦点
let NERDTreeAutoCenter=1
"鼠标模式:目录单击,文件双击
let NERDTreeMouseMode=2
"打开文件后自动关闭
let NERDTreeQuitOnOpen=0
"显示文件
let NERDTreeShowFiles=1
"显示隐藏文件
let NERDTreeShowHidden=0
"高亮显示当前文件或目录
let NERDTreeHightCursorline=1
"显示行号
let NERDTreeShowLineNumbers=1
"窗口位置
let NERDTreeWinPos="left"
"窗口宽度
let NERDTreeWinSize=31
"不显示"Bookmarks" label "Press ? for help"
let NERDTreeMinimalUI=1
"快捷键
nnoremap   :NERDTreeToggle
"当打开vim且没有文件时自动打开NERDTree
autocmd vimenter * if !argc() | NERDTree | endif
"只剩 NERDTree时自动关闭
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif

" 设置当文件被改动时自动载入
" set autoread
"*****************************************************
"           YouCompleteMe配置                        *
"*****************************************************
"leader映射为逗号“,”
let mapleader = ","  
"配置默认的ycm_extra_conf.py
"let g:ycm_global_ycm_extra_conf = "~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py" 
"按gb 会跳转到定义
nnoremap  gb :YcmCompleter GoToDefinitionElseDeclaration   
"打开vim时不再询问是否加载ycm_extra_conf.py配置
let g:ycm_confirm_extra_conf=0   
"使用ctags生成的tags文件
let g:ycm_collect_identifiers_from_tag_files = 1 
map  : YcmDiags
"*****************************************************
"           Syntastic配置                            *
"*****************************************************
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++"
let g:syntastic_cpp_compiler_options = "-std=c++11 -stdlib=libstdc++"
"set error or warning signs
let g:syntastic_error_symbol = "✗"
let g:syntastic_warning_symbol = "⚠"
"whether to show balloons
let g:syntastic_enable_balloons = 1
"*****************************************************
"C,C++ 按F5编译运行                                 *
"*****************************************************
map  :call CompileRunGcc()
func! CompileRunGcc()
    exec "w"
    if &filetype == "c"
        exec "!gcc % -o %<"
        exec "! ./%<"
    elseif &filetype == "cpp"
        exec "!g++ % -o %<"
        exec "! ./%<"
    elseif &filetype == "java" 
        exec "!javac %" 
        exec "!java %<"
    elseif &filetype == "sh"
        :!./%
    endif
endfunc
"*****************************************************
"C,C++的调试 F8                                      *
"*****************************************************
map  :call Rungdb()
func! Rungdb()
    exec "w"
    exec "!g++ % -g -o %<"
    exec "!gdb ./%<"
endfunc
"*****************************************************
"新文件标题                                          *
"*****************************************************
"新建.c,.h,.sh,.java文件,自动插入文件头 
autocmd BufNewFile *.cpp,*.[ch],*.sh,*.rb,*.java,*.py exec ":call SetTitle()" 
""定义函数SetTitle,自动插入文件头 
func SetTitle() 
    "如果文件类型为.sh文件 
    if &filetype == "sh" 
        call setline(1,"#!/bin/bash") 
        call append(line("."), "") 
    elseif &filetype == "python"
        call setline(1,"#!/usr/bin/env python")
        call append(line("."),"# coding=utf-8")
        call append(line(".")+1, "") 

    elseif &filetype == "ruby"
        call setline(1,"#!/usr/bin/env ruby")
        call append(line("."),"# encoding: utf-8")
        call append(line(".")+1, "")
    else 
        call setline(1, "/*************************************************************************") 
        call append(line("."),   "  > File Name   : ".expand("%")) 
        call append(line(".")+1, "  > Author      : zhoutk") 
        call append(line(".")+2, "  > Mail        : zhoutk@189.cn") 
        call append(line(".")+3, "  > Created Time: ".strftime("%c")) 
        call append(line(".")+4, " ************************************************************************/") 
        call append(line(".")+5, "")
    endif
    if expand("%:e") == "cpp"
        call append(line(".")+6, "#include")
        call append(line(".")+7, "using namespace std;")
        call append(line(".")+8, "")
    endif
    if &filetype == "c"
        call append(line(".")+6, "#include")
        call append(line(".")+7, "")
    endif
    if expand("%:e") == "h"
        call append(line(".")+6, "#ifndef _".toupper(expand("%:r"))."_H")
        call append(line(".")+7, "#define _".toupper(expand("%:r"))."_H")
        call append(line(".")+8, "#endif")
    endif
    if &filetype == "java"
        call append(line(".")+6,"public class ".expand("%:r"))
        call append(line(".")+7,"")
    endif
    "新建文件后,自动定位到文件末尾
endfunc 
autocmd BufNewFile * normal G

"*****************************************************
"代码格式优化化                                      *
"*****************************************************

map  :call FormartSrc()

"定义FormartSrc()
func FormartSrc()
    exec "w"
    if &filetype == "c"
        exec "!astyle --style=ansi -a --suffix=none %"
    elseif &filetype == "cpp" || &filetype == "hpp"
        exec "r !astyle --style=ansi --one-line=keep-statements -a --suffix=none %> /dev/null 2>&1"
    elseif &filetype == "perl"
        exec "!astyle --style=gnu --suffix=none %"
    elseif &filetype == "py"||&filetype == "python"
        exec "r !autopep8 -i --aggressive %"
    elseif &filetype == "java"
        exec "!astyle --style=java --suffix=none %"
    elseif &filetype == "jsp"
        exec "!astyle --style=gnu --suffix=none %"
    elseif &filetype == "xml"
        exec "!astyle --style=gnu --suffix=none %"
    else
        exec "normal gg=G"
        return
    endif
    exec "e! %"
endfunc
"结束定义FormartSrc

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/24805.html

相关文章

  • Vim 的哲学(三)

    摘要:插件键位映射技巧性的配置等等都是锦上添花,它们有助于你进一步提高效率以及个性化你的工作环境,但是对于哲学的理解帮助甚少。为你开启语法高亮。你可以自定义各种语言的语法高亮,无非就是根据这些规 如果没有挑战,人生将多么无趣! 两种副本 在我的硬盘上总是保留着(至少)两份 Vim 的配置文件。其中一份是所谓完全正式版,它的文件名是 .vimrc,到本系列结束的时候,我们将了解其中...

    klinson 评论0 收藏0
  • 总结个人2015提高前端效率的方法和工具

    摘要:更多前端内容阅读之前非用户请自动忽略,此文针对用户,我个人使用频率非常高,端开发者可以选择性的尝试部分工具。或者调起的调试工具,跟编辑器亲密合体。目前在使用,尝试。 更多前端内容http://www.codefrom.com/p/JavaScript 阅读之前 非Mac用户请自动忽略,此文针对Mac用户,我个人使用频率非常高,PC端开发者可以选择性的尝试部分工具。 终端...

    Jingbin_ 评论0 收藏0
  • 可能是 Windows 下最漂亮的 Gvim 配置了

    摘要:原文一直以来被称为编辑器之神的在下很难发挥其强大的功能,本文从实用的角度阐述如何调校出一个比较好用的不过仍然要说明下,在众多构建版本中平台的是我认为最好用的一个版本。 原文:https://keelii.github.io/2016/06/13/awsome-window-vimrc/ 一直以来被称为编辑器之神的 vim 在 Windows 下很难发挥其强大的功能,本文从实用的角度阐述...

    CarterLi 评论0 收藏0
  • VIM问题合集(持续更新)

    摘要:在模式下粘贴速度很慢的问题一般当我们在模式下粘贴一段超大量的文本,比如行。更新后无法打开问题很久不使用安装东西,安装了一个小软件,结果直接更新到版本,然后导致完全无法打开。 Vim 在Insert模式下粘贴速度很慢的问题 一般当我们在Insert模式下粘贴一段超大量的文本,比如1000行。那么Vim会变得奇慢无比,大概半分钟? 所以,如果我们要粘贴文本,需要用另一种方法:在Normal...

    RyanHoo 评论0 收藏0

发表评论

0条评论

tinysun1234

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<