How to open a found file with vi? Piping 'find' output to vi
Solution 1
Add * wildcard after 'important', also you can use the following command:
vim $(find /home/user -name "important*")
Solution 2
You can use -exec
along with find
. Use the following command:
find /home/user -type f -name 'important' -exec vi {} \;
Solution 3
Use:
find ./ -name 'important.txt' -print0 | xargs -0 vi
This works, and it's simple.
If you don't need a pipe then you can try this:
find ./ -name 'important.txt' -exec vi {} \;
Solution 4
If you wish to get the output of the find command (i.e. the list of files) in vim, then the following will have vim read from find's output:
find /home/user -type f -name 'important' | vi -
If you wish to open the files themselves, then xargs and find's -exec action are good options, as mentioned by other answers.
However, for the -exec action you are much better off using the following syntax:
find /home/user -type f -name 'important' -exec vi {} +
The distinction between "\;" and "+" at the end of the command is as follows:
With "\;" an instance of vim will be opened for each file that is found. This means that if 20 files are found, you will have to close vim 20 times to get back to the shell and you cannot move between the files within vim.
With "+", a single instance of vim will be opened with all of the found files. This means that you can move back and forth between the files as buffers and when you are done, there is only one instance of vim to be closed.
Solution 5
You could use first choice:
find ./ -name 'important' -print0 | xargs -0 vi
or as a second choice:
find /home/user -type f -name 'important' -exec vi {} \;
Please note, the xargs
command can correctly handle the spaces within the file names found by the find
command without you having to worrying about unexpected outcomes.
and avoid command substitution like following:
vim $(find /home/user -name "important*")
This is because in most cases, you simply don't know how many results(files) you are expecting or whether those file names have spaces in them to avoid unwanted results due to spaces.
Related videos on Youtube
micgeronimo
Updated on September 18, 2022Comments
-
micgeronimo over 1 year
I have a file,
/home/user/important.txt
. To find it, I use:find /home/user -type f -name 'important'
Then, I want to open the file in the Vim editor using a pipe.
How can I do this?
Something like
find /home/user -type f -name 'important' | vi
-
Admin about 9 years@PeterMortensen Because
vim
has a:find
command, so this is the most pointless use offind
there is.
-
-
micgeronimo about 9 yearsCan you please explain last symbols,
{} \;
what they mean? -
muru about 9 years@micgeronimo That's part of the
exec
syntax.{}
will be replaced with the name of the filename, and\;
is used to indicate that the end of the command forexec
. -
Sergiy Kolodyazhnyy about 9 yearsYou might want to add
-print
because sometimes find just hangs there without it -
muru about 9 years@Serg print is the default action, so no.
-
Red Aura about 9 years@Peter Mortensen is -print0 and -0 really required ?
-
alexche8 about 6 yearsNice tip. But there is another question - I have found 100 files and vim open them one by one after current file become closed. How to cancel whole command and exit to the shell instead of typing :q on each file or kill terminal. Thanks
-
akjain about 5 yearsdoes not work as expected - terminal hangs after quitting vi
-
Miguel A. Sanchez-Perez about 4 years@alexche8 find /home/user -type f -name 'important' -exec vi -p {} + vi.stackexchange.com/questions/5276/…
-
retorquere almost 4 yearsthis doesn't work if the file names have spaced
-
Panta almost 4 years@retorquere Just put command substitution in double quotes if the file name contains spaces. For example: vim "$(find /home/user -name "important*")"
-
retorquere almost 4 yearsDoesn't work if it returns multiple files. I now do
vim $(find . -name "important*" -print0 | quote0)
, where quote0 is gist.github.com/c3ce8d39c371c75705c1c3a22005890d