Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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

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
up vote 39 down vote accepted

You can use process 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 for the command substitution alternative. – faizal Aug 13 '14 at 17:11
<(ls -la) is actually process substitution rather than command substitution. – Eliah Kagan Sep 2 '14 at 1:32
I really like vim's option, it allows me to search, find and save the output easily from data dumps. – Josue Ibarra May 11 at 21:36

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

ls -la | vim -
share|improve this answer

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

Your Answer


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.