天天看點

vim設定一鍵執行python代碼

根據系統将下面代碼複制到vim配置檔案vimrc中,即可在vim中一鍵【F5】運作.py檔案。

Windows下的gvim

"一鍵運作代碼

function CheckPythonSyntax()

let mp = &makeprg

let ef = &errorformat

let exeFile = expand("%:t")

setlocal makeprg=python\ -u

set efm=%C\ %.%#,%A\ \ File\ "%f"\,\ line\ %l%.%#,%Z%[%^\ ]%\@=%m

silent make %

copen

" set efm 是設定quickfix的errorformat,以便vim識别

" makeprg 是vim内置的編譯指令,可以通過更改來實作編譯對應類型檔案。具體可參考vim官方說明檔案。

" copen是打開quickfix,n用來設定quichfix視窗大小,如 cope5。在錯誤描述上回車,可以直接跳轉到錯誤行。

let &makeprg = mp

let &errorformat = ef

endfunction

"一個是普通模式下,一個是插入模式下

au filetype python map <f5> :w <cr> :call CheckPythonSyntax() <cr>

au filetype python imap <f5> <esc> :w <cr> :call CheckPythonSyntax() <cr></cr></cr></esc></f5></cr></cr></f5>

Linux下的vim

map <F5> :call CompileRunGcc()<CR>

func! CompileRunGcc()

exec "w"

if &filetype == 'c'

exec "!g++ % -o %<"

exec "!time ./%<"

elseif &filetype == 'cpp'

elseif &filetype == 'java'

exec "!javac %"

exec "!time java %<"

elseif &filetype == 'sh'

:!time bash %

elseif &filetype == 'python'

exec "!time python %"

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