3

I'm trying to write a function that will accept a gem name and run Ctrl-P in the directory of that gem based on what Bundler shows. Right now I'm stuck on how to capture the output from the shell command.

So far I have:

function! GemCtrlP(gem_name)
   execute '!bundle list ' . a:gem_name
endfunction

I want to save the output of the of that bundle call and then pipe that into CtrlP. I can do the CtrlP part, but I'm not sure how to capture the shell output

Any ideas?

2 Answers 2

6

Capturing arbitrary vim commands can be achieved by using the :redir command. For shell commands, it is usually easier to use the system() function.

See the help at

:h :redir
:h system

There should be enough examples in the help (or look at some plugins).

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

4 Comments

It seems like system is actually showing a null character or something: async_job-7ba2b1e64340^@. Is there a way to strip that extra ^@ out? I'm looking at ag.vim for examples, but I can't seem to figure it out
Those are probably binary nulls. Recent Vims replace those by SOH (0x01). You should be able to strip those using substitute() function
Was able to just pipe it into tr
@576i indeed, but you not always want to have the output thrown into your actual document.
1

More thorough version of what Christian posted:

function! GemCtrlP(gem_name)
  let path = system('bundle list ' . a:gem_name . ' | tr -d "\n"')
  execute 'CtrlP ' . path
endfunction

Comments

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.