Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Suppose that I have a file named filename123.txt and it is the single file that is named so, and I can locate it with the command locate filename123. And it returns only this file.

Now I want to open it with vi/vim. But I don't want to go to that location and type the vi command followed by filename. Here I want the result of locate filename123 to be appended to the vi command. How can I do so? I already tried:

locate filename123 | vi

But this does not works. And this error comes in terminal:

santosh@santosh:~$ locate filename123 | vi
Vim: Warning: Input is not from a terminal
Vim: Error reading input, exiting...

Vim: Finished.




santosh@santosh:~$
share|improve this question

2 Answers 2

up vote 11 down vote accepted

You can use xargs:

locate filename123 | xargs vi

By default xargs will execute as few instances of the specified command as possible, passing as many parameters as possible according to the system's ARG_MAX. To limit the number of parameters passed to an instance of vi, use xargs' -n option.

To handle file names containing spaces use xargs' -d option:

locate filename123 | xargs -d '\n' vi

To handle file names containing newlines use xargs' -0 option together with locate's -0 option:

locate -0 filename123 | xargs -0 vi

(If -0 is not available on any of them, check for --null too, or another way to specify character \000 as delimiter.)

share|improve this answer
    
Can you tell me what is what was preventing me before to open this file? –  Santosh Kumar Aug 18 '12 at 12:02
1  
@Santosh You were passing the output of the locate command as input to vi. Vi, like any other program, expects the name of the file(s) to work on as arguments. –  Gilles Aug 18 '12 at 12:13
    
@manatwork The problem is I can't locate and open filename with spaces in it. E.g. this command will open multiple files in vim: locate I\ have\ to\ download | xargs vi –  Santosh Kumar Aug 18 '12 at 12:40

You could use command expansion:

vi "$(locate filename123 | head -n1)"

To make vim read its stdin you can do vim -, but that will just give you the output of locate which are filename paths. This might work though, using vim gf normal command will then open the path that the cursor is on.

Edit

In the case of filenames with spaces in them you're better of using the xargs option mentioned or the vim solution described above. Note to make gf work you need to add space to isfname:

set isfname+=32

This tip and alternatives are described here.

Edit 2

Added quotes and head as suggested by PeterO in the comments.

share|improve this answer
    
For file with whitespaces in them, when I insert \ in file name, like vi $(locate another\ file). The vim opens it as two separate files, another and file :(. –  Santosh Kumar Aug 18 '12 at 12:20
    
Your answer only works when I have no spaces in the file which I am locating. –  Santosh Kumar Aug 18 '12 at 12:25
    
Answer updated. –  Thor Aug 18 '12 at 12:45
2  
@Thor: For a single file, as mentioned in the question, you just need to wrap quotes around the process-substitution: vi "$(locate filename123)" ... To ensure that it has only one file name to deal with: vi "$(locate filename123 |head -n1)" –  Peter.O Aug 18 '12 at 17:49
    
PeterO: You're right, I only tested with multiple hits. I've added your comments to the answer. –  Thor Aug 18 '12 at 18:29

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.