Skip to content

Instantly share code, notes, and snippets.

@mtwomey
Last active January 30, 2023 21:16
Show Gist options
  • Save mtwomey/629bb4b2163450117b7a4c8fdcc8eb4a to your computer and use it in GitHub Desktop.
Save mtwomey/629bb4b2163450117b7a4c8fdcc8eb4a to your computer and use it in GitHub Desktop.
Quickly run occur from your previous search regex
(defun occur-from-search ()
"Runs occur on the regex used in the last '/' search. This is useful for capturing
all the matches (just the matches, not the whole lines) into the occur buffer so
you can do something with them.
Note: Assumes evil search, so you may need to tweak for your needs."
(interactive)
(let ((search-pattern evil-ex-search-pattern))
(if search-pattern
(let ((capture-type (if (zerop (regexp-opt-depth (nth 0 search-pattern)))
"\\&" ;; No subexpression so collect the entire match.
"\\1"))) ;; Get the first subexpression
(occur (nth 0 search-pattern)
capture-type))
(message "First search with '/'."))))
@betaprior
Copy link

Most excellent hack!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment