Let's say, I want to search for email addresses and output them to a new file. What I can do so far with a help from another discussion is this:

g/pattern/.w! >> output.txt 

but that saves whole lines where search matches occur. Is it possible to extract just the matches (with possibility for them to be multiple on one line)? I would prefer a solution (if possible) without some additional plugins or macros.


This is a bit easier with grep and the --only-matching/-o option.

grep -o 'pattern' file.txt > output.txt

You can use Vim's substitution as well:


This approach will affect the current buffer though. It would probably be best to copy the file first.

Note: these approachs assumes only 1 match per line


Peter's solution with grep -o is the way to go IMO.

Yet, for the sake of the exercise here is a 100% pure vim script solution

" prepare the result variable to be of the right type
:let res = []
" join all lines in a big string, and use `substitute` to fill the result with matches -- this is the only tricky part
:call substitute(join(getline(1,'$'), ' '), 'THE PATTERN', '\=add(res, submatch(0))', 'g')

" You can remove duplicates BTW with
:let res = uniq(sort(res))

" And save into a new file
:call writefile(res, 'FILENAME')

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.