Vim: 避免在 make 成功后按 ENTER 键?
Vim: Avoid having to press ENTER after successful make?
$ ls
Makefile html-page/ page-generator.m4
Run includes/
除了 Makefile
,我还有一个脚本 Run
,它仅在 make
完成且没有错误时执行。我已经设法在我的 .vimrc
文件中实现了以下内容,如果需要,该文件还会在父目录中查找 Makefile
。
" Before the 'make' quickfix command, run my quickfix pre-commands
autocmd QuickfixCmdPre make call MyQuickfixCmdPre()
" After the 'make' quickfix command, run my quickfix post-commands
autocmd QuickfixCmdPost make call MyQuickfixCmdPost()
和
function! MyQuickfixCmdPre()
" Save current buffer, but only if it's been modified
update
" (h)ead of (p)ath of % (current buffer), i.e. path of current file
let l:dir = expand('%:p:h')
" Remove final / and smack a /Makefile on the end, glob gives empty if file doesn't exist
while empty(glob(substitute(l:dir, '/$', '', '') . '/Makefile'))
" There's no Makefile here. Are we at the root dir?
if l:dir ==# "/"
" Just use dir of current file then
let l:dir = '.'
break
else
" Try the parent dir. Get (h)ead of dir, i.e. remove rightmost dir name from it
let l:dir = fnamemodify(l:dir, ':h')
endif
endwhile
" Makefile is in this dir, so local-cd (only this window) to the dir
execute "lcd " . l:dir
endfunction
function! MyQuickfixCmdPost()
" Get number of valid quickfix entries, i.e. number of errors reported,
" using filter to check the 'valid' flag
let l:err_count = len(filter(getqflist(), 'v:val.valid'))
if l:err_count ==# 0
" The make succeeded. Execute the Run script expected in the same dir as Makefile
call system('./Run')
redraw!
endif
endfunction
有了这个,在 vim 中输入 :mak
之后,代码就生成了 运行... 有两种可能的结果:
- 如果在
make
期间出现错误,vim 将在之后用 Press ENTER or type command to continue
显示这些错误,这很好。
- 如果
make
成功且没有错误,则会执行我的 Run
脚本,以测试我的代码(在本例中是浏览器中显示的 html 文件),但是当我切换回 vim 时,我必须按 enter
才能删除来自 vim 的消息,我不需要阅读它,因为它没有告诉我错误。此消息过去看起来像这样:
"includes/m4includes/subs.m4" 34L, 759B written
:!make 2>&1| tee /var/folders/zk/0bsgbxne3pe5c86jsbgdt27f3333yd/T/vkbxFyd/255
m4 -I includes/m4includes page-generator.m4 >html-page/mypage.html
(1 of 1): m4 -I includes/m4includes page-generator.m4 >html-page/mypage.html
Press ENTER or type command to continue
但在 MyQuickfixCmdPost()
中引入 redraw!
现在减少为:
(1 of 1): m4 -I includes/m4includes page-generator.m4 >html-page/mypage.html
Press ENTER or type command to continue
但仍然需要按 enter
。
我们如何避免在编译成功后每次 return 到 vim 时都必须按 enter
?有什么想法吗?
注意:vim 有一个 -silent
命令行选项,但据我所知,这会使所有 Press ENTER
静音,并且这里的目标是仅在 make
.
成功后才避开它们
后面加上call feedkeys("\<CR>")
即可。需要feedkeys()
的地方不多(通常normal!
或者类似的命令就可以了),还有细微的效果(仔细看它带的flags)。幸运的是,这是一个有用的地方。
$ ls
Makefile html-page/ page-generator.m4
Run includes/
除了 Makefile
,我还有一个脚本 Run
,它仅在 make
完成且没有错误时执行。我已经设法在我的 .vimrc
文件中实现了以下内容,如果需要,该文件还会在父目录中查找 Makefile
。
" Before the 'make' quickfix command, run my quickfix pre-commands
autocmd QuickfixCmdPre make call MyQuickfixCmdPre()
" After the 'make' quickfix command, run my quickfix post-commands
autocmd QuickfixCmdPost make call MyQuickfixCmdPost()
和
function! MyQuickfixCmdPre()
" Save current buffer, but only if it's been modified
update
" (h)ead of (p)ath of % (current buffer), i.e. path of current file
let l:dir = expand('%:p:h')
" Remove final / and smack a /Makefile on the end, glob gives empty if file doesn't exist
while empty(glob(substitute(l:dir, '/$', '', '') . '/Makefile'))
" There's no Makefile here. Are we at the root dir?
if l:dir ==# "/"
" Just use dir of current file then
let l:dir = '.'
break
else
" Try the parent dir. Get (h)ead of dir, i.e. remove rightmost dir name from it
let l:dir = fnamemodify(l:dir, ':h')
endif
endwhile
" Makefile is in this dir, so local-cd (only this window) to the dir
execute "lcd " . l:dir
endfunction
function! MyQuickfixCmdPost()
" Get number of valid quickfix entries, i.e. number of errors reported,
" using filter to check the 'valid' flag
let l:err_count = len(filter(getqflist(), 'v:val.valid'))
if l:err_count ==# 0
" The make succeeded. Execute the Run script expected in the same dir as Makefile
call system('./Run')
redraw!
endif
endfunction
有了这个,在 vim 中输入 :mak
之后,代码就生成了 运行... 有两种可能的结果:
- 如果在
make
期间出现错误,vim 将在之后用Press ENTER or type command to continue
显示这些错误,这很好。 - 如果
make
成功且没有错误,则会执行我的Run
脚本,以测试我的代码(在本例中是浏览器中显示的 html 文件),但是当我切换回 vim 时,我必须按enter
才能删除来自 vim 的消息,我不需要阅读它,因为它没有告诉我错误。此消息过去看起来像这样:
"includes/m4includes/subs.m4" 34L, 759B written
:!make 2>&1| tee /var/folders/zk/0bsgbxne3pe5c86jsbgdt27f3333yd/T/vkbxFyd/255
m4 -I includes/m4includes page-generator.m4 >html-page/mypage.html
(1 of 1): m4 -I includes/m4includes page-generator.m4 >html-page/mypage.html
Press ENTER or type command to continue
但在 MyQuickfixCmdPost()
中引入 redraw!
现在减少为:
(1 of 1): m4 -I includes/m4includes page-generator.m4 >html-page/mypage.html
Press ENTER or type command to continue
但仍然需要按 enter
。
我们如何避免在编译成功后每次 return 到 vim 时都必须按 enter
?有什么想法吗?
注意:vim 有一个 -silent
命令行选项,但据我所知,这会使所有 Press ENTER
静音,并且这里的目标是仅在 make
.
后面加上call feedkeys("\<CR>")
即可。需要feedkeys()
的地方不多(通常normal!
或者类似的命令就可以了),还有细微的效果(仔细看它带的flags)。幸运的是,这是一个有用的地方。