18

I am doing a find $PWD -name 'filename' | vim -

expecting the file filename to be opened in vim editor. but it is not working. In this case, I am sure that there exists just one file with name 'filename'.

Also the result of find gives the complete path on stdout.

3
  • 1
    using command | vim - tells vim to read the output from command and put it into a buffer. (As opposed to opening those files.) Commented Jun 13, 2011 at 5:21
  • @pydave I missed the '-' in your comment. Important! Commented Mar 1, 2013 at 14:32
  • 2
    @pydave Incidentally, vim <(command) does the same, using Bash process substitution, and this works with more than just vim. e.g. to see the differences between two directories, use diff <(ls -l dir1) <(ls -l dir2) Commented Mar 1, 2013 at 14:42

4 Answers 4

29
vim "$(find "$PWD" -name 'filename')"

or

find "$PWD" -name 'filename' -exec vim {} \;

(You can drop "$PWD", by the way. find starts the search from current directory by default.)

Sign up to request clarification or add additional context in comments.

8 Comments

This does not work for me. vim "$(find "$PWD" -name 'filename')" opens a new file whos name is a concatenation of all the filenames output from find. This is on OSX.
You probably want to use + to concatenate the results instead of ; to operate on single results when ending the find: find "$PWD" -name 'filename' -exec vim {} + should open the files in one vim instance (so they're accessibly from :args).
"You can drop "$PWD", by the way" -- note that adding $PWD can make a difference if you need the full path (instead of relative). (Like if you're writing the results to a file.)
@JonathanHartley: That doesn't work because you're quoting the output. If you don't quote the output (and don't have spaces in your filenames) does it work? Regardless, you're better off using -exec (with +).
@JonathanHartley: In your last example (with +), you could also use vim -o to open them in splits (see also -O and -p). Also, you don't need to escape + because it doesn't mean anything special to bash.
|
9

find . -name 'filename' -print0 | xargs -0 vim

should also work. You might want to read up on xargs, which is a handy thing to know about.

2 Comments

This does not work for me. find . -name 'utility' -print0 | xargs -0 vim instead opens a single new file, whose name is a concatenation of the output from find. This is on OSX.
Ah. The OP specifies there is just one matching file. My objection is therefore lessened
4

Mentioned in @idbrii's comment, but my favorite is:

find . -name 'filename' -type f -exec vim {} \+

This opens up each file found in its own buffer ready to be navigated with :next and :prev. Tested on OSX, but I'm fairly certain it will work on Linux too.

Comments

3

One way I find is very easy is enclosing the find command with backticks (character under tilde on most keyboards) and passing it to vim.

vim `find . -name myfile`

In fact, you can use backtick for any command to get the literal string output of the command.

1 Comment

How does this answer not have more votes?!? Genious!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.