代码之家  ›  专栏  ›  技术社区  ›  Chinmay Kanchi

向vim添加命令

vim
  •  38
  • Chinmay Kanchi  · 技术社区  · 15 年前

    我终于决定试试看 Vim ,因为我对GUI编辑器越来越失望。到目前为止,我很喜欢它,但是我找不到任何帮助来解决我的问题…

    我正试图映射命令 :Pyrun :!python % 在VIM中使用 cmap . 如果我键入 :cmap . 但是,在打字时 吡喃 ,我收到此错误消息:

    不是编辑器命令:Pyrun。

    这是我正在尝试的。Vimrc:

    :autocmd FileType python :cmap Pyrun<cr> !python %<cr>
    :autocmd FileType python :cmap Intpyrun<cr> !python -i %<cr>
    

    我能做些什么来解决这个问题?

    3 回复  |  直到 15 年前
        1
  •  40
  •   karoberts    15 年前

    我会在你的.vimrc或ftplugin/python-ft.vim中尝试类似的方法。

    command Pyrun execute "!python %"
    command Intpyrun execute "!python -i %"
    

    然后 :Pyrun :Intpyrun 应该工作

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

    map <F5> :Pyrun<CR>
    map <F6> :Intpyrun<CR>
    
        2
  •  28
  •   Raoul Supercopter    15 年前

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

    fun! DoSomething( arg ) "{{{
        echo a:arg
        " Do something with your arg here
    endfunction "}}}
    
    command! -nargs=* Meh call DoSomething( '<args>' )
    

    所以它就像

    fun! Pyrun( arg ) "{{{
        execute '!python ' . expand( '%' )
    endfunction "}}}
    
    command! -nargs=* Pyrun call Pyrun( '<args>' )
    

    但是,有一种更好的方法可以在Vim中实现。使用makeprg:

    makeprg=python\ %
    

    只是类型 :make 运行当前的python文件。使用 :copen 显示错误列表。

        3
  •  9
  •   Rob Wells    15 年前

    G'Day.

    与Karoberts的回答类似,我更喜欢更直接的回答:

    :map <F9> :!python %<CR>
    

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

    :map <F9> :!python % 2>&1 \| tee /tmp/results
    

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

    :split /tmp/results<CR>
    

    然后,我可以很容易地在缓冲区中看到运行的结果,当通过运行正在开发的脚本更新结果文件时,缓冲区会自动刷新。

    高温高压

    干杯,