Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Say I have a file in some obscure directory that I want to open and edit. I don't want to do something like this...

vim ~/foo/bar/blah/doh/ugh.txt

I'd rather be able to say find this file and open it. I know there are commands like locate and find to find a file or directory, but I'm not sure whether these can (or even should) be utilized in what I'm trying to do. Basically, what is the simplest way to open a file with a program w/o specifying its exact location? (In cases where there isn't another file with the same name in the entire system, and cases where there are multiple).

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

This will open all the files (potentially many) with that name that have been added to the database the last time updatedb was run (typically daily). This means files created since that time will not be found.

vim $(locate ugh.txt)

Unless you supply a starting directory to find, using it may be very slow. It may still be too slow even with a starting directory if there are a lot of files below it. The same warning regarding large numbers of files applies.

vim $(find /pathto/startdir -name 'ugh.txt' -type f)

Either of these could be turned into a function to reduce typing:

viml () { vim $(locate "$@"); }

Then use it like this:

viml ugh.txt
share|improve this answer
    
I was writing up find . -name \*foo\* -print0 | xargs -0 sh -c 'vim </dev/tty "$@"' - (which handles spaces, etc. in filenames), but since you wrote this, I will desist. –  Chris Johnsen Jun 15 '10 at 20:04
    
Your function will likely completely break using any non-file arguments to vim –  Daenyth Aug 30 '10 at 20:11
    
@Daenyth: There's no question of that. The intention is that this is a convenience function for a particular purpose rather than a universal wrapper for vim. It could easily be made more robust if that were needed. –  Dennis Williamson Aug 30 '10 at 20:36
add comment

I think you need this : This helpful command lets you open only files that match a certain condition - in a directory and all it's subdirectories.

vi `grep -rl 'example.js' *

or you can see the original reference

share|improve this answer
2  
That's based on the contents of the files rather than the filenames. –  Dennis Williamson Jun 15 '10 at 19:08
1  
it also appears to be missing a trailing backtick... –  quack quixote Jun 15 '10 at 19:09
    
@Dennis: ya you right I did't consider this. –  Am1rr3zA Jun 15 '10 at 19:30
add comment

if you are on zsh then its just

vim **/foo.txt

or you can just enter vim and do the following from within vim:

:e **/foo.txt<ENTER>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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