向Vim添加命令

Chi*_*chi 39 vim

我终于决定试用Vim,因为我越来越感到GUI编辑的沮丧.到目前为止,我很喜欢它,但我找不到任何有关我正在解决的问题的帮助......

我正在尝试使用命令将命令映射:Pyrun:!python %Vim cmap.如果输入,映射会显示正常:cmap.但是,在键入时:Pyrun,我收到此错误消息:

不是编辑命令:Pyrun.

这是我正在尝试的.vimrc:

:autocmd FileType python :cmap Pyrun<cr> !python %<cr>
:autocmd FileType python :cmap Intpyrun<cr> !python -i %<cr>
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能解决这个问题?

kar*_*rts 41

我会在你的.vimrc或你的ftplugin/python_ft.vim中尝试这样的东西

command Pyrun execute "!python %"
command Intpyrun execute "!python -i %"
Run Code Online (Sandbox Code Playgroud)

然后:Pyrun,:Intpyrun应该工作

然后,您可以将功能键映射到每个

map <F5> :Pyrun<CR>
map <F6> :Intpyrun<CR>
Run Code Online (Sandbox Code Playgroud)

  • 缓冲区本地命令(-b)会更好. (2认同)

Rao*_*ter 28

我个人更喜欢另一种方法.首先创建一个接收命令参数的函数,然后创建一个命令来调用该函数:

fun! DoSomething( arg ) "{{{
    echo a:arg
    " Do something with your arg here
endfunction "}}}

command! -nargs=* Meh call DoSomething( '<args>' )
Run Code Online (Sandbox Code Playgroud)

所以它会是这样的

fun! Pyrun( arg ) "{{{
    execute '!python ' . expand( '%' )
endfunction "}}}

command! -nargs=* Pyrun call Pyrun( '<args>' )
Run Code Online (Sandbox Code Playgroud)

但是,在Vim中有更好的方法.使用makeprg:

makeprg=python\ %
Run Code Online (Sandbox Code Playgroud)

只需键入:make即可运行当前的Python文件.使用:copen显示错误列表.


Rob*_*lls 10

天儿真好,

类似于karoberts的答案,我更喜欢更直接的:

:map <F9> :!python %<CR>
Run Code Online (Sandbox Code Playgroud)

如果我的脚本正在创建一些输出,我也喜欢在临时文件中捕获它,然后自动将该文件内容复制到另一个缓冲区,例如

:map <F9> :!python % 2>&1 \| tee /tmp/results
Run Code Online (Sandbox Code Playgroud)

然后我通过:set autoread在另一个缓冲区中输入并打开结果文件来设置autoread

:split /tmp/results<CR>
Run Code Online (Sandbox Code Playgroud)

然后,我可以通过运行开发中的脚本轻松查看缓冲区中运行的结果,该结果在结果文件更新时自动刷新.

HTH

干杯,