在 vim 中,当我使用
:make
make 的输出显示在“外部”窗口中,我不喜欢这个,我使用这个地图
nnoremap <leader>m :w <bar> make<CR><CR><CR>:copen<CR>
但是,在某些情况下,make 的输出是
make: Nothing to be done for `all'.
当 copen 有时,我如何添加自动关闭到 copen make: Nothing to be done for all.
?
您可以通过查看快速修复列表的内容getqflist()
。然后,如果第一行与您不想看到的文本不匹配,我只会有条件地打开 quickfix 窗口:
nnoremap <leader>m :w <bar> make<CR><CR><CR>
\:if get(get(getqflist(), 0, {}), 'text', '') !~# 'Nothing to be done' <Bar>
\ copen <Bar>
\endif<CR>
get()
当列表为空时,访问通过避免错误。
:cclose
如果更适合您的需要,您也可以始终打开列表,然后在条件中使用。
我有一个对我有很大帮助的 vim 脚本:
command! -nargs=* Build call s:RunBuild()
function! s:RunBuild()
let tmpfile = tempname()
"build and surpresses build status messages to stdout
"(stdout message are not very informative and may be very very long)
"Error messages are redirected to temporary file.
let buildcmd = "make -j 2> " . tmpfile . " >/dev/null"
let fname = expand("%")
if fname != ""
" save current buffer if possible (your bad if file is read only)
write
endif
" --- run build command ---
echo "Running make ... "
let cmd_output = system(buildcmd)
"if getfsize(tmpfile) == 0
if v:shell_error == 0
cclose
execute "silent! cfile " . tmpfile
echo "Build succeded"
else
let old_efm = &efm
set efm=%f:%l:%m
execute "silent! cfile " . tmpfile
let &efm = old_efm
botright copen
endif
call delete(tmpfile)
endfunction
然后我映射 F5 键以在每个 vim 模式下进行构建
"F5 - run make (in normal mode)
:nnoremap :Build
"F5 - run make (in visual mode)
:vnoremap :Build
"F5 - run make (in insert mode)
:inoremap :Build