[英]How to create a mapping to execute python code from Vim?
這是我從Vim執行python代碼所遵循的步驟:
:w !python
執行它 並且我想要一種更短的代碼執行方式,因此我在互聯網上搜索了如下圖所示的快捷鍵:
autocmd FileType python nnoremap <buffer> <F9> :exec '!python' shellescape(@%, 1)<cr>
並將其添加到_vimrc
文件中。 但是當我按下F9時 ,會出現以下錯誤消息:
這是上圖中的相關文本:
python: can't open file ''D:/py2.py'': [Errno 22] Invalid argument
shell returned 2.
我已經搜索了Internet,但是沒有任何結果。
我可以在Windows 10中使用gVim 8.0,如果有幫助的話。
通過vim與命令行交互時,更可取的方式是使用感嘆號。
代替:
:exec '!python' shellescape(@%, 1)<cr>
嘗試做一個簡單的方法:
:update<bar>!python %<CR>
在上面的代碼中:
如果已修改( 首選 ),則update
保存文件,或者也可以使用w
。
<bar>
管道命令。
!python %
將以python filename
運行python filename
。
因此,基本上,將其放在您的.vimrc
文件中,即可完成操作:
autocmd FileType python nnoremap <buffer> <F9> :update<bar>!python %<CR>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.