Linux下vim編寫python指令碼一鍵執行
阿新 • • 發佈:2018-12-26
在Linux下,常常使用vim來編寫指令碼,但是每次編寫完成後要退出再執行:python xxx.py才可以測試指令碼,可在vim的配置檔案中新增一些配置,即可編寫完指令碼後不用退出即可驗證指令碼的正確性(一鍵執行)。
- vim編輯器的配置檔案為:/etc/vimrc
通過:sudo vim /etc/vimrc
開啟vim的配置檔案
在結尾新增如下內容即可指定F5
為測試指令碼的快捷鍵:
注:此方式便於對指令碼進行單元測試。
map <F5> :call CompileRunGcc()<CR> func! CompileRunGcc() exec "w" if &filetype == 'c' exec "!g++ % -o %<" exec "!time ./%<" elseif &filetype == 'cpp' exec "!g++ % -o %<" exec "!time ./%<" elseif &filetype == 'java' exec "!javac %" exec "!time java %<" elseif &filetype == 'sh' :!time bash % elseif &filetype == 'python' exec "!time python2.7 %" exec "!time python3.6 %" elseif &filetype == 'html' exec "!firefox % &" elseif &filetype == 'go' exec "!go build %<" exec "!time go run %" elseif &filetype == 'mkd' exec "!~/.vim/markdown.pl % > %.html &" exec "!firefox %.html &" endif endfunc
- 示例
vim編寫的指令碼內容
vim編寫完成後不退出直接按F5後結果: