Sample Header Ad - 728x90

One-liner piping from find/xargs with paths including spaces

0 votes
2 answers
66 views
The following question likely does not relate specifically to Vim. I use a Vim example, as this is where I encounter the issue. Working on Ubuntu, 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 "zod"
I can then quickly review the files output by find in vim:
vim -p find . -type f -name "*.txt" | xargs grep -l "zod"
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. The following creates a MWE set of sample files and directories including spaces:
echo zod > xx.txt && echo zod > 'x x.txt' && mkdir aa && echo zod > aa/xx.txt && echo zod > 'aa/x x.txt' && mkdir 'a a' && echo zod > 'a a/xx.txt' && echo zod > 'a a/x x.txt'
The following will then list the 6 text files we expect:
find . -type f -name "*.txt" -print0 | xargs -0 grep -l "zod"
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 2 existent, and 9 non-existent files. Is there a way to get past the problem?
vim -p find . -type f -name "*.txt" -print0 | xargs -0 grep -l "zod"
I am keen to avoid side-effects such intermediate files or shell/environment variables (such as used in the top answer to a similar question [here](https://unix.stackexchange.com/a/597765)) ; and so I am looking specifically for a single-line command.
Asked by user7543 (274 rep)
May 21, 2025, 03:11 PM
Last activity: May 22, 2025, 01:23 PM