108

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?

175

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 -
10
  • 3
    <(ls -la) is actually process substitution rather than command substitution. Sep 2 '14 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 '16 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 '20 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 '21 at 6:17
  • @EvanMorrison This is a good question, why not asking it?
    – chaos
    Jan 28 '21 at 6:27
47

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

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

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.

2
  • 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 '21 at 7:14
  • I meant, cleaner launch of gvim... And bash comes back with a carriage return. Mar 1 '21 at 7:57
13

You can tell vim to open stdin:

ls -la | vim -
1

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
1

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>.

1
  • Good idea. Definitely shorter than typing this long ! :)
    – kode
    Jun 21 '17 at 15:04
0

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, privacy policy and cookie policy

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