Show Only Matched Strings in a New Buffer
October 25, 2020
| By Haluk Dogan
(defun get-only-matched (query)
"Goto the beginning of the current buffer; perform the search and store the matched strings
Go back to the point of origin; create a new buffer; insert the matched strings into the new buffer
Switch to the new buffer; go to the beginning of the new buffer."
(interactive "sQuery:")
(let (matched-list)
(save-excursion
(goto-char (point-min))
(while (re-search-forward query nil t)
(push
(buffer-substring-no-properties (match-beginning 0) (match-end 0))
matched-list)))
(with-current-buffer (get-buffer-create "*MATCHED*")
(mapc
(lambda (matched)
(insert matched "\n"))
matched-list))
(switch-to-buffer (get-buffer "*MATCHED*"))
(goto-char (point-min))))