open files in vim through xargs modification

2

Using a discovery command utility I can grep the location of some directories, and I need to open containing files. I cannot use find with exec such as this example find . -name "*.txt" -exec vim {} +.

If I don't modify the grep output, xargs works as this

searchUtilityCommand | grep keyword | xargs vim

But I actually need

searchUtilityCommand | grep keyword | xargs -I % vim %/extra/path

However, when doing that it opens the first hit, and after I close it opens the second; close it and open the third... I tried using

searchUtilityCommand | grep keyword | xargs -P 0 -I % vim %/extra/path

But it just fails. I also tried

vim `searchUtilityCommand | grep keyword | xargs -P 0 -I % vim %/extra/path`

No luck.

I thought awk or sed could be used, but I am not fluent with them and I dont know if its worth it. It could be something like

searchUtilityCommand | grep keyword | <awk modify grepped path> | xargs vim

Any help.

Thanks!

manolius

Posted 2019-10-04T11:16:06.297

Reputation: 123

Answers

0

Have not tested this, but no reason it should not work:

searchUtilityCommand | grep keyword | sed -e 's,$,/extra/path' | xargs vim

I have to say though, I don't know how to get rid of the "Warning: Input is not from a terminal" message that comes up when you run vim like this. (I generally prefer vim $(command pipeline) for that reason).

sitaram

Posted 2019-10-04T11:16:06.297

Reputation: 296

you where missing a comma at the end of sed regex. Works like a charm! thanks searchUtilityCommand | grep keyword | sed -e 's,$,/extra/path,' | xargs vim – manolius – 2019-10-06T08:10:53.120

0

From Vim, run this Ex command:

:args `=systemlist('searchUtilityCommand | awk ''/keyword/{print $0 "/extra/path"}''')`

Or this one:

:args `=map(filter(systemlist('searchUtilityCommand'), {_,v -> v =~# 'keyword'}), {_,v -> v.'/extra/path'})`

This should populate the arglist with a list of file paths; the ones output by searchUtilityCommand, matching keyword, with /extra/path appended at the end.

From there you can do all sort of things, such as:

" open each file in a horizontal split (run `:only` to close all splits except the current one)
:all

" open each file in a vertical split
:vert all

" open each file in a tab page (run `:tabonly` to close all tab pages except the current one)
:tab all

" diff all the files
:vert all | windo diffthis

" replace all occurrences of `pat` with `rep` in each file
:argdo %s/pat/rep/g | update

For more information, see:

:h `=
:h systemlist(
:h map(
:h filter(
:h :all
:h :argdo

user183956

Posted 2019-10-04T11:16:06.297

Reputation: 51

I marked the other answer as the solution for coherence with the question title (for future records), which funnily enough says to prefer using vim commands. This answer has full of potential tips. Thank you!! – manolius – 2019-10-06T08:13:48.967