Copy grep output from vim editor

vim

Assume you have some text file (e.g. a log file) and you open
it in a vim editor and hit the command

:g/aaa

It will output a result in which you can move with j and k keys
and when you move to the bottom a green sentence
Press ENTER or type command to continue will appear.

I understand it somehow, that I can use some commands with the result,
but don't know how to find what I can do with it.

One action I'd like to do, is to save the lines to the new file.

Of course you could use a command

$ grep aaa file.txt > new_file.txt

but is it possible from the vim editor directly?

Best Answer

It is possible to do this through a multi-step process.

Within vim:

:redir > new_file.txt
:g/aaa
:redir END

See :help redir from within vim.

The :redir command can also append to an existing file by modifying the first command.

:redir >> new_file.txt
Related Question