I want to do filter through an external program without actually filtering the lines (but to keep them). For example :

%!grep aa > newfile

If I do this, all the lines are deleted and I need to do undo

There is a more neat way to run an external program with args without changing the current buffer?


2 Answers 2


I think you’re looking for :help :write_c. Try this:

:w !grep aa > newfile

@Rich answered your question very well. However, judging by your example you may want to look into :vimgrep/:grep.

:vimgrep /pat/ %
:grep 'pat' %

Both :vimgrep & :grep will put the results into the quickfix list. Here are a few quickfix list commands to get you up and running fast:

  • Use :cnext and :cprev to move between your errors/matches. I suggest you map these commands.
  • :cfirst and :clast to go to the first and last error/matches respectively.
  • :copen to open up the quickfix list in a window (:cclose to close)
  • :cwindow to open quickfix list window only if there are errors
  • :cc to display the current error/match.
  • Use :cdo {cmd} to run a command, {cmd} on each quickfix entry.
  • You can use % on the command-line to supply the current filename.

Vimcast episodes:

See the following Vimcasts episode:

For more help see the following:

:h quickfix
:h :cnext
:h :cfirst
:h :cope
:h :cwindow
:h :cc
:h :cdo
:h :_%
  • 1
    Thanks. It is just an example. But helpful answer. +1 for vimcasts :)
    – eyal karni
    Sep 4, 2018 at 18:29

Your Answer

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

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