.vimrc脚本用于使用g ++进行编译

时间:2015-11-19 10:38:32

标签: c++ gcc vim g++

我想在.vimrc中编写一个简短的脚本,用vim中的g ++编译.cpp文件,并在新的垂直缓冲区中打开错误/输出到我的实际文件,如下所示:

  1. 创建一个vim命令" :set vimcompiling "这将导致以下步骤
  2. 保存文件:" :w "
  3. 编译它:" :make ./name" (不离开vim!)
  4. 在新缓冲区中打开文件: 如果有错误:" :vert copen n " else:新的垂直缓冲区
  5. 中执行该文件
  6. 将这些步骤(2-4)映射到" F5 "所以我可以在任何想要编译的时候执行步骤2-4
  7. 有人可以给我一些关于如何编写这样一个脚本的技巧吗?

1 个答案:

答案 0 :(得分:1)

BuildToolsWrappers已内置并绑定到F5。

执行:make %<很容易。但是,检测错误以打开quickfix窗口是比较棘手的。在BTW中我使用以下

" Function: lh#btw#build#_show_error([cop|cwin])      {{{3
function! lh#btw#build#_show_error(...) abort
  let qf_position = lh#option#get('BTW_qf_position', '', 'g')

  if a:0 == 1 && a:1 =~ '^\%(cw\%[window]\|copen\)$'
    let open_qf = a:1
  else
    let open_qf = 'cwindow'
  endif

  " --- The following code is borrowed from LaTeXSuite
  " close the quickfix window before trying to open it again, otherwise
  " whether or not we end up in the quickfix window after the :cwindow
  " command is not fixed.
  let winnum = winnr()
  cclose
  " cd . is used to avoid absolutepaths in the quickfix window
  cd .
  exe qf_position . ' ' . open_qf

  setlocal nowrap

  " if we moved to a different window, then it means we had some errors.
  if winnum != winnr()
    " resize the window to just fit in with the number of lines.
    let nl = 15 > &winfixheight ? 15 : &winfixheight
    let nl = lh#option#get('BTW_QF_size', nl, 'g')
    let nl = line('$') < nl ? line('$') : nl
    exe nl.' wincmd _'

    " Apply syntax hooks
    let syn = lh#option#get('BTW_qf_syntax', '', 'gb')
    if strlen(syn)
      silent exe 'runtime compiler/BTW/syntax/'.syn.'.vim'
    endif
    call lh#btw#filters#_apply_quick_fix_hooks('syntax')
  endif
  if lh#option#get('BTW_GotoError', 1, 'g') == 1
  else
    exe origwinnum . 'wincmd w'
  endif
endfunction