How to open a found file with vi? Piping 'find' output to vi [duplicate]

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:

locate is simple as so:

locate -0 important.txt | xargs -0 vi