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?

7 Answers 7


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 -
  • 4
    <(ls -la) is actually process substitution rather than command substitution. Sep 2, 2014 at 1:32
  • 3
    I really like vim's option, it allows me to search, find and save the output easily from data dumps. May 11, 2016 at 21:36
  • vim will warn of your buffer is not saved. You may pass -R or -c "setlocal buftype=nofile bufhidden=hide noswapfile" to avoid this. Dec 11, 2020 at 12:24
  • This question/answer is the closest to what I've been trying to search on SO, but if anyone can help me: Is it possible to be using a shell (ie bash), and then turn my current shell buffer (ie, everything above my cursor) into a doc I can temp edit in vim? Even read only? For example ls somdir/ and then after it dumps output, i can search and yank from it? Much appreciated!!! Jan 28, 2021 at 6:17
  • @EvanMorrison This is a good question, why not asking it?
    – chaos
    Jan 28, 2021 at 6:27

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

ls -la | vim -
  • 17
    Well technically it's two characters. ;)
    – Cory Klein
    Oct 24, 2017 at 22:14

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.

  • I like it. This results in a little cleaner launch of vim on my system (doesn't hang the bash terminal) but results in the output from git diff missing syntax highlighting. This may be a particularity of my system. Mar 1, 2021 at 7:14
  • I meant, cleaner launch of gvim... And bash comes back with a carriage return. Mar 1, 2021 at 7:57

You can tell vim to open stdin:

ls -la | vim -

If your goal is simply to get the output into a text file then you don't need to invoke vim. Bash can do it alone with

ls -la > outputfile.txt

setlocal buftype=nofile

This is a good option if you are going to create an alias to replace less:

seq 100 | vim +':setlocal buftype=nofile' -

Now you don't need to type the ! to quit.

Another option is:

seq 100 | vim +'nnoremap q :quit!' -

so you can exit with just q<enter>.

  • Good idea. Definitely shorter than typing this long ! :)
    – kode
    Jun 21, 2017 at 15:04

I would like to add an option

ls -al | view -

view is a read-only version of vim(equivalent to vim -R)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.