Nicolas Petton

Nicolas Petton

Web developer, Lisper, Smalltalker & Emacs maniac.

Running multi-occur in many buffers

Multi-occur on projects

Update: Damien Cassou tricked me into extracting it in a package. You can get it here: github.com/NicolasPetton/noccur.el.

Occur-mode is one of the awesome modes that come builtin with Emacs.

Sometimes I just want to run multi-occur on all (or a subdirectory) of a project I'm working on. Used with keyboard macros it makes it a snap to perform modifications on many buffers at once.

I wrote a tiny package containing the following functions that does just that:

(require 'projectile)

(defun noccur-dired (regexp &optional nlines)
  "Perform `multi-occur' with REGEXP in all dired marked files.
When called with a prefix argument NLINES, display NLINES lines before and after."
  (interactive (occur-read-primary-args))
  (multi-occur (mapcar #'find-file (dired-get-marked-files)) regexp nlines))

(defun noccur-project (regexp &optional nlines)
  "Perform `multi-occur' in the current project files."
  (interactive (occur-read-primary-args))
  (let* ((directory (read-directory-name "Search in directory: "))
         (files (if (and directory (not (string= directory (projectile-project-root))))
                    (projectile-files-in-project-directory directory)
                  (projectile-current-project-files)))
         (buffers (mapcar #'find-file 
                          (mapcar #'(lambda (file)
                                      (expand-file-name file (projectile-project-root)))
                                  files))))
    (multi-occur buffers regexp nlines)))

(provide 'noccur)

It requires projectile for the `noccur-project` function, but `noccur-dired` is very similar and doesn't require it.

The way I use it is the following:

M-x noccur-project RET foo RET then I can edit the occur buffer's content with occur-edit-mode (bound to e). To save changes in all modified buffer and go back to occur-mode press C-c C-c.

comments powered by Disqus