1

I often open multiple files in vim using tab pages:

$ vim --help | grep tab -p[N] Open N tab pages (default: one for each file) 

I also use find with xargs and grep -l to obtain a list of files.

find . -type f -name "*.txt" | xargs grep -l "some text" 

I can then quickly review the files output by find in vim:

vim -p `find . -type f -name "*.txt" | xargs grep -l "some text"` 

The earlier grep command would fail if there are spaces in the files or directories, so -print0 can be added to the arguments to find; and -0 can be added to the arguments to xargs:

find . -type f -name "*.txt" -print0 | xargs -0 grep -l "some text" 

But if I then try to pass the output of this command to vim tab pages (as below), the paths including spaces are split, and opened as non-existent files. Is there a way to get past the problem?

vim -p `find . -type f -name "*.txt" -print0 | xargs -0 grep -l "some text"` 
3
  • Where does this fs:: come from? Commented May 14 at 12:52
  • It's arbitrary; same as "*.rs" and .. I've edited the question to make the examples more generic. Commented May 14 at 13:07
  • Welcome to Vi and Vim! This looks mainly like a problem quoting strings in shell, doesn't it? The problem would be the same if the leftmost command wasn't vim. Maybe you'll be able to find answers on Unix & Linux? Btw, the obvious solution is to never ever use paths with spaces but we all know that already :-) Commented May 14 at 20:30

1 Answer 1

0

It looks like the problem lies with the command substitution. Note that $() appears to behave the same as `` in this case.

The following version should work, even with whitespace in filenames:

$ find . -type f -name "*.txt" -print0 | xargs -0 grep -l "some text" | xargs -o vim -p 

It makes more sense that way to me as I am more likely to iterate from left to right:

  1. see if find finds what I want,
  2. then play around with filtering,
  3. then pass it all to my editor.

Plus my knowledge of command substitution is spotty at best.

6
  • $() behaves the same” with a few minor exceptions, it is the same (command substitution as you point out) Commented May 15 at 2:15
  • Also, should the last xargs have -0? Looks like small typo but don’t want to assume Commented May 15 at 2:16
  • @D.BenKnoble I think -o is correct. See xargs --help | grep tty. Commented May 15 at 8:46
  • @romaini Thanks. I like this way of working, though I will need to build intuition on when -o is needed in general. But ... it still doesn't work: paths with spaces continue to be split and appear as new files within vim tabs with truncated names. Commented May 15 at 8:50
  • Doh, good catch on the o/0 thing. Commented May 15 at 16:46

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.