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.

Lets say I have a file called file.txt. In it is a name of a file that I want to edit with vi. I want to do something like this so that I can edit the file:

cat file.txt | vi

However, that doesn't work. How can it be done?

To clarify things:

Here are the contents of file.txt:

textfile

So I want to somehow send the contents of file.txt to vi so that the same thing will happen as when typing vi textfile.

The contents of file.txt can change. I want vi to edit whatever file is listed in file.txt.

share|improve this question
    
I dont think that vi = vim. I dont know that that makes a difference for this question, but your tag is misleading. –  soandos May 31 '11 at 5:03
    
I think there may be more to your question you're trying to ask. Opening an arbitrarily named file is the base usage of vi. i.e. : vi file.txt. Is there something more advanced you're trying to do? –  camster342 May 31 '11 at 5:45
    
vi starts vim on OSX and on other modern Linux systems. –  tony_sid May 31 '11 at 6:04
    
I am actually trying to edit a file whose name is stored in the OSX clipboard. Typing pbpaste will display the filename. I'm trying to edit this file without actually having to type it, but by using whatever is stored in the clipboard. Linux doesn't have pbpaste, but it is essentially the same as getting the output from cat filename. –  tony_sid May 31 '11 at 6:13
    
install xclip and then use xclip -o to access the clipboard on the shell in x11. just in case you need to replace pbpaste... –  akira May 31 '11 at 7:07
add comment

2 Answers 2

up vote 7 down vote accepted

you might try this:

% vi `cat file.txt`

or, to avoid the useles use of cat:

% vi `< file.txt`

you are telling vi(m) just a bunch of arbitrary things. if you want vi(m) to do something like 'hey, open that file' you have to feed it the same commands you would use in vi(m), eg. something like :e foo.txt. but thats just more complicated than doing what i proposed.

share|improve this answer
    
Those didn't work. –  tony_sid May 31 '11 at 6:13
4  
those are backticks, you typed them in correctly? they open a subshell, execute the 'cat' ... if that does not work, try vi $(cat file.txt) –  akira May 31 '11 at 6:38
    
Oh ok, it works now. thx. –  tony_sid May 31 '11 at 7:03
add comment

At least for vim (not sure about vi), you can do

cat file.txt | vim -

The '-' tells vim to read from stdin.

share|improve this answer
    
Thanks so much, this is the correct answer –  darryn.ten May 15 '13 at 7:52
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.