puk
puk

Reputation: 16802

Piping output of Vim command to shell command

To to find all modified files in Vim I type

:ls

This will give me a list like so

2  h   "index.html"                   line 98
3  h   "Category/Category.Bg_S.js"    line 1
4  h   "Category/Category.Box0_S.js"  line 1
5  +   "Category/Category.Box10_S.js" line 1
6      "Category/Category.Box11_S.js" line 1
7  +   "Category/Category.Box12_S.js" line 1

But if there are too many buffers this can be tedious. What I was thinking of doing would be something like:

:ls !grep +

to pipe the contents of vim's ls to shell's grep function. But it does not work. I therefore have 2 questions:

  1. How do I find out the list of all modified files?
  2. If there is an easier solution to 1), then how would I, for whatever reason, pass the output of a vim command to a shell command?

Upvotes: 2

Views: 598

Answers (1)

Herbert Sitz
Herbert Sitz

Reputation: 22266

The easiest way is probably to "redirect" the output to a vim variable, then filter it for modified buffers:

function! GetModifiedBuffers()
    redir => bufoutput
    buffers  " same as ls
    redir END
    return join(filter(split(bufoutput,'\n'),"v:val =~ '\\%8c+'"),'\n')
endfunction

Then do something like :echo GetModifiedBuffers() to show the list of modified buffers.

Upvotes: 2

Related Questions