Skip to content

Grepping in Emacs under Windows

ack logo

One thing that bothered me for quite a while was that I had to resort to (beloved, but nevertheless) TotalCommander in order to search for files or contents of files even though I’d rather search directly from within my Emacs-session. The crux was: I couldn’t get the /find-combo (e.g. via M-x find-grep) working under in .

This beautiful weekend I finally got it working – even though in a different way than one might suppose…

You can get lots of Linux/-Tools for Windows from http://gnuwin32.sourceforge.net/. For grep/find you’ll have to get grep, findutils, and CoreUtils (for ls – which is used by find).

The problem under windows (at least for me) was that there is an executable also called find.exe – residing in Windows‘ system-root – which interferes with the one from GNU by the same name.

So what I did yesterday was to put the \GnuWin32\bin\-directory on my PATH and rename find.exe in that directory to find-gnu.exe.

(I realize I could have fooled around with the order of directories in PATH, i.e. giving the GNU-directory before c:\Windows – but I didn’t want to depend on that order. BTW tinkering around I learned there is an limit to the length of the content of PATH (I couldn’t believe it being true even for Vista – lovely legacy).)

To let emacs know of the renaming I finally added (setq find-program "find-gnu.exe") to my .emacs.

Now find is found, ls can be accessed and a command starting with find-gnu.exe . "(" -path "*/CVS" -o -path "*/.svn" was generated – but I got the message: find-gnu.exe: paths must precede expression.

I fiddled about even more and finally got it working – not knowing how. In the meanwhile I stumbled over another – better? – solution:

to the rescue

…to stumble over Customizing Emacs grep-find to ignore Subversion files which suggests another – mightier – tool: ack.

ack is a Perl-script, so I got Perl (once again).

Searching for an emacs-mode, Google came up with several:

After having tried the former two (the latter seemed too ‚big‘) I decided to go with the first one – the minibuffer-interface was more pleasing. E.g. it provides directory-selection with tab-completion.

To get this going one simply has to:

  1. Put the Perl-script (after adding the .pl-extension to get the association to the Perl-executable established under Windows) in your Emac’s plugin-directory.
  2. Place the ELisp-source (maybe as ack-emacs.el) also there (or someplace else that should be on the PATH – or – under Perl\site\bin\ (which was added to the PATH by Perl’s installation)).
  3. Add (require 'ack-emacs) to .emacs.

Invoking the freshly activated ack-mode with M-x ack will give you something like the following:

-*- mode: ack; default-directory: "~/hacking/clojure/scripts/" -*-
Ack started at Sat May 23 19:07:19

ack.pl -i filter ~/hacking/clojure/
C:\Users\Steffen\hacking\clojure\clojure\branches\1.0\build.xml:44:  
C:\Users\Steffen\hacking\clojure\clojure\branches\1.0\build.xml:45:  
C:\Users\Steffen\hacking\clojure\clojure\branches\1.0\build.xml:46:    
C:\Users\Steffen\hacking\clojure\clojure\branches\1.0\build.xml:47:  
C:\Users\Steffen\hacking\clojure\clojure\branches\1.0\build.xml:71:    	
C:\Users\Steffen\hacking\clojure\clojure\branches\1.0\src\jvm\clojure\lang\LispReader.java:746:			//filter line numbers
C:\Users\Steffen\hacking\clojure\clojure\branches\20081217\src\jvm\clojure\lang\LispReader.java:723:			//filter line numbers
C:\Users\Steffen\hacking\clojure\clojure\branches\20090320\src\jvm\clojure\lang\LispReader.java:746:			//filter line numbers
C:\Users\Steffen\hacking\clojure\clojure\branches\lazy\src\jvm\clojure\lang\LispReader.java:737:			//filter line numbers
C:\Users\Steffen\hacking\clojure\clojure\branches\streams\src\jvm\clojure\lang\LispReader.java:733:			//filter line numbers

(the locations will teleport you directly to the occurrence in the given file – once clicked)

ack excels grep in that it ignores Subversion’s metadata-directories and does recursive search by default. There are more advantages – have a look at ack’s homepage.

{ 4 } Comments

  1. Kim van Wyk | 2009/6/8 at 01:16 | Permalink

    For file finding under Windows, I find w32-find-dired an awesome tool. It uses the normal Windows search functionality and places the results into a dired buffer: http://www.emacswiki.org/emacs/w32-find-dired.el Also, ack is a terrific tool – and great for finding things on Windows boxes. A friend built a wrapper around it to make it behave a little like grep (putting all found results into a compilation buffer, effectively), which can be found at http://rooijan.za.net/?q=ack_el (sorry to point to my own website, but that’s where I keep it)

  2. Steffen | 2009/6/15 at 07:39 | Permalink

    I couldn’t get w32-find-dired.el working – I got (translating from german): „System cannot find given path.: doesn’t exist or is inaccessible“.

     

    And: I am already using „your“ ack.el – as I’ve noted in the post. 🙂

     

    I’d rather have something more „out of the box“, though, without having to install Perl (in that case) – w32-find-dired.el would have been a better fit in that regard.

  3. Kim van Wyk | 2009/6/23 at 12:43 | Permalink

    w32-find-dired not working is a bit odd – as far as I recall I installed it and it worked with no fussing required. I actually completely missed the fact that you were using ack.el – I just assumed you’d found something from someone who actually knew what they were doing 🙂 It’s quite flattering, thanks.

  4. John Connors | 2009/6/26 at 04:17 | Permalink

    I have this in my emacs to get find.exe working. (setenv „PATH“ (concat „C:\\Program Files\\Emacs\\EmacsW32\\gnuwin32\\bin“ path-separator (getenv „PATH“))) It just makes sure that Emacs’s own version of the PATH has the gunuwin32 tools ahead of everything else, thus masking Windows’s find.exe. Works for me.

Post a Comment

Your email is never published nor shared. Required fields are marked *