10

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
  • How is this not a duplicate 4 years after Ask Ubuntu launched? – Peter Mortensen Feb 16 '15 at 17:49
  • @PeterMortensen Because vim has a :find command, so this is the most pointless use of find there is. – muru Feb 16 '15 at 17:51
9

Add * wildcard after 'important', also you can use the following command:

vim $(find /home/user -name "important*")
| improve this answer | |
  • You might want to add -print because sometimes find just hangs there without it – Sergiy Kolodyazhnyy Feb 16 '15 at 13:13
  • 2
    @Serg print is the default action, so no. – muru Feb 16 '15 at 13:15
10

You can use -exec along with find. Use the following command:

find /home/user -type f -name 'important' -exec vi {} \;
| improve this answer | |
  • 1
    Can you please explain last symbols, {} \; what they mean? – micgeronimo Feb 16 '15 at 10:03
  • 4
    @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 for exec. – muru Feb 16 '15 at 12:43
  • 1
    Nice 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 – alexche8 Feb 15 '18 at 15:31
  • @alexche8 find /home/user -type f -name 'important' -exec vi -p {} + vi.stackexchange.com/questions/5276/… – Miguel A. Sanchez-Perez Mar 30 at 21:23
7

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 {} \;
| improve this answer | |
  • @Peter Mortensen is -print0 and -0 really required ? – Red Aura Feb 16 '15 at 18:20
  • 1
    does not work as expected - terminal hangs after quitting vi – akjain Mar 27 '19 at 11:31
1

locate is simple as so:

locate -0 important.txt | xargs -0 vi

| improve this answer | |
1

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.

| improve this answer | |
1

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.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.