I should have written it years ago

You know when you write code and you’re doing a specific repetitive task and you think “I should write something that would make this much quicker, but not today, no time”. In this case the task is: finding any regexp in a directory tree and show the results and let me easily edit the matching files, or do a global search-replace across all results, or whatever.¬† The 10 lines of emacs lisp below do it almost perfectly. To use, just type M-x mwf-find, then a regexp, and you’ll get a nice dired view of the matches. Then you can click on any file to edit it, you can press “Q” and do an interactive search-replace across the files, etc.

(setq mwf-dir "~/Desktop/trunk")
(setq mwf-find-args nil)
(setq mwf-find-args-history nil)
(setq find-ls-option '("-exec ls -lh {} ;" . "-lh"))

(defun mwf-find()
"find a string in my code repository"
(setq mwf-find-args (read-string "MWF Find: " mwf-find-args '(mwf-find-args-history . 1)))
(find-dired mwf-dir (concat "-type f -not -path *.svn* -not -name .DS_Store -not -path */tests/* -exec grep -n \"" mwf-find-args "\" {} ;"))

Things that could be improved: the matching lines should be below the file name, and the output in general could be made more readable. I’m not sure I can do either without¬† changing find-dired.el, which I won’t do today, no time.
[tags]emacs, lisp, programming, search[/tags]

This entry was posted in General. Bookmark the permalink.