12

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
1
  • @PeterMortensen Because vim has a :find command, so this is the most pointless use of find there is.
    – muru
    Feb 16, 2015 at 17:51

6 Answers 6

14

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

vim $(find /home/user -name "important*")
5
  • You might want to add -print because sometimes find just hangs there without it Feb 16, 2015 at 13:13
  • 2
    @Serg print is the default action, so no.
    – muru
    Feb 16, 2015 at 13:15
  • this doesn't work if the file names have spaced
    – retorquere
    Jun 12, 2020 at 18:40
  • @retorquere Just put command substitution in double quotes if the file name contains spaces. For example: vim "$(find /home/user -name "important*")"
    – Panta
    Jun 13, 2020 at 4:48
  • Doesn't work if it returns multiple files. I now do vim $(find . -name "important*" -print0 | quote0), where quote0 is gist.github.com/c3ce8d39c371c75705c1c3a22005890d
    – retorquere
    Jun 14, 2020 at 11:39
10

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

find /home/user -type f -name 'important' -exec vi {} \;
4
  • 1
    Can you please explain last symbols, {} \; what they mean? Feb 16, 2015 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, 2015 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, 2018 at 15:31
  • @alexche8 find /home/user -type f -name 'important' -exec vi -p {} + vi.stackexchange.com/questions/5276/… Mar 30, 2020 at 21:23
9

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

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.

1

locate is simple as so:

locate -0 important.txt | xargs -0 vi

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.

Not the answer you're looking for? Browse other questions tagged .