How to use a pipe to edit a file with vi
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.
Related videos on Youtube
tony_sid
Born and raised in California. Computers are my main hobby.
Updated on September 18, 2022Comments
-
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 almost 13 yearsI dont think that vi = vim. I dont know that that makes a difference for this question, but your tag is misleading.
-
camster342 almost 13 yearsI 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 almost 13 yearsvi starts vim on OSX and on other modern Linux systems.
-
tony_sid almost 13 yearsI 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 almost 13 yearsinstall
xclip
and then usexclip -o
to access the clipboard on the shell in x11. just in case you need to replacepbpaste
...
-
-
tony_sid almost 13 yearsThose didn't work.
-
akira almost 13 yearsthose 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 almost 13 yearsOh ok, it works now. thx.
-
Jeff about 9 yearsscubbo is right! My answer is more useful for just piping something from stdin.