1. 程式人生 > >Linux下vim編寫python指令碼一鍵執行

Linux下vim編寫python指令碼一鍵執行

在Linux下,常常使用vim來編寫指令碼,但是每次編寫完成後要退出再執行:python xxx.py才可以測試指令碼,可在vim的配置檔案中新增一些配置,即可編寫完指令碼後不用退出即可驗證指令碼的正確性(一鍵執行)。

  1. 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
  1. 示例
    vim編寫的指令碼內容 vim 編寫的testF5.py內容

vim編寫完成後不退出直接按F5後結果:
一鍵執行結果