How to use a pipe to edit a file with vi

5,850

Solution 1

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.

Solution 2

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

cat file.txt | vim -

The '-' tells vim to read from stdin.

Share:
5,850

Related videos on Youtube

tony_sid
Author by

tony_sid

Born and raised in California. Computers are my main hobby.

Updated on September 18, 2022

Comments

  • tony_sid
    tony_sid over 1 year

    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.

    • soandos
      soandos almost 13 years
      I dont think that vi = vim. I dont know that that makes a difference for this question, but your tag is misleading.
    • camster342
      camster342 almost 13 years
      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?
    • tony_sid
      tony_sid almost 13 years
      vi starts vim on OSX and on other modern Linux systems.
    • tony_sid
      tony_sid almost 13 years
      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.
    • akira
      akira almost 13 years
      install xclip and then use xclip -o to access the clipboard on the shell in x11. just in case you need to replace pbpaste...
  • tony_sid
    tony_sid almost 13 years
    Those didn't work.
  • akira
    akira almost 13 years
    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)
  • tony_sid
    tony_sid almost 13 years
    Oh ok, it works now. thx.
  • Jeff
    Jeff about 9 years
    scubbo is right! My answer is more useful for just piping something from stdin.