Vim function to call bash script with argument and return result

浪子不回头ぞ 提交于 2020-01-17 04:07:06

问题


I have a bash script which takes a single argument and returns around 8-10 lines of text.

I'd like to be able to call this script from within vim. Ideally by highlighting a string of characters as the argument. I'd then like the output of the script to be displayed in a new pane.

I'm new to vim so I'm not sure if this can be done simply by creating a command in my vimrc file or if I need to create a plugin.

Any advice would be much appreciated.

after a bit of googling I've come up with

function! Foo(a1)
    new
    r !myscript a:a1
endfunction

This doesn't quite work yet. It seems to pass the name a:a1 rather than the value.


回答1:


I'd start with a simple function like so (note: the sample uses echo as the script... Kinda lame... but you get the idea)

function! CallMyScript(params)
    new                 " open a new buffer
    " se buftype=nofile " add spice to taste, e.g.
    silent! exec "r!echo '" . a:params . "'"
endfunction

and then a mapping for visual mode like;

:vnoremap QQ y:call CallMyScript(@")<Enter>

This will call the script with the currently selected text when you hit QQ



来源:https://stackoverflow.com/questions/6974440/vim-function-to-call-bash-script-with-argument-and-return-result

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!