1

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.

0

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

6 Comments

I'm a wee bit confused about the ". a:params ." say I had two arguemnts to pass to my script how would I change that?
I'm assuming shell quoting applies; Dropping the sinqle quotest would probably do what you what
What if the script is a bash function or alias? It (didn't work) wasn't available for me in this way (possibly due to no bashrc being loaded), so I had to retype everything in a one-liner. Isn't there a smarter way?
@dezza in that case, call the right program (e.g. bash): bash -c my_alias. Be aware of shell quoting/escaping. Not for the faint of heart :) (Just make a shell script for it?)
sehe: It actually returned "command not found" .. Are you sure the chain of sources (.bashrc and from there) are happening with "bash -c" ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.