本文設定成按 F5 會根據對應的檔案進行編譯及執行
同樣在 .vimrc 設定即可
map <F5> :call CompileAndRun()<CR>
func! CompileAndRun()
exec "w"
if &filetype == 'c'
exec "!gcc -std=c11 % -o /tmp/a.out && /tmp/a.out"
elseif &filetype == 'cpp'
exec "!g++ -std=c++11 % -o /tmp/a.out && /tmp/a.out"
elseif &filetype == 'java'
exec "!javac %"
exec "!java %<"
elseif &filetype == 'sh'
:!%
elseif &filetype == 'python'
exec "!python3 %"
endif
endfunc
執行結果就如GIF所示參考資料:
vim 一键编译运行c++,c,java,python, shell
在VIM下写C++能有多爽?
感受Vim的强大:进阶技巧
How to enable c11 on later versions of gcc?
How to create a mapping to execute python code from Vim?

沒有留言:
張貼留言