Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am trying to redirect the output of a bash command into a new file.

If I try the pipe as below :

ls -la | vim

Bash shows me the errors :

Vim: Error reading input, exiting...
Vim: preserving files...
Vim: Finished.

I know that I can open Vim and then use :

:r !ls -la

But is there a way to do this in bash itself, so that Vim is opened and the output is pasted there automatically?

share|improve this question

4 Answers 4

up vote 12 down vote accepted

You can use command substitution (this also works with applications, that can't read from STDIN):

vim <(ls -la)

Or use vim's function to read from STDIN:

ls -la | vim -
share|improve this answer
1  
+1 for the command substitution alternative. –  faizal Aug 13 at 17:11
    
<(ls -la) is actually process substitution rather than command substitution. –  Eliah Kagan Sep 2 at 1:32

Here's another approach, hopefully to teach someone something new.

If you know that the command :r !ls -la works inside vim, you can do the following to open vim and make it run the command as soon as it opens, straight from bash:

vim -c ':r! ls -la'

This is the equivalent of opening vim then executing the command :r! ls -la. This should work with any vim command, where the command comes after the -c option.

share|improve this answer

You can tell vim to open stdin:

ls -la | vim -
share|improve this answer

You're really close on your own. You were just missing one character.

ls -la | vim -
share|improve this answer

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.