Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


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
up vote 9 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
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

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
Sorry, but this is not the correct answer. This opens vim with the contents 'textfile'. The questioner wants vim to open 'textfile' itself. I'd downvote but sadly I don't have enough reputation here. – scubbo Jan 29 '15 at 12:21
scubbo is right! My answer is more useful for just piping something from stdin. – Jeff Jan 30 '15 at 23:38

You must log in to answer this question.

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