RobKohr
RobKohr

Reputation: 6943

emacs - find-name-dired - how to change default directory

In emacs when you type M-x find-name-dired it will give you two prompts. The first is what directory path you want to search in, and the second is what the file pattern you would like to search for.

How do I change it (in my .emacs) so the first prompt is always a specific project directory?

Also, how do I make it so the file name search is case insensitive?

Upvotes: 1

Views: 2073

Answers (3)

RobKohr
RobKohr

Reputation: 6943

After I posted this I discovered another way of resolving this issue:

(add-hook 'find file-hook
 (lambda ()
  (setq default-directory command-line-default-directory)))

This left as a default the directory that I started emacs in (which is useful so you don't have to change your project directory in your emacs all the time).

In reality I think it is saying that every time you run find file, change the default directory to your starting command line directory.

Another thing could be to set some default directory: (setq my-default-directory "/home/rob/whatever")

and replace command-line-default-directory with my-default-directory/

Upvotes: 0

pajato0
pajato0

Reputation: 3676

I suspect Trey's answer is probably elegant and preferred for some reasons that hurts my brain whenever I try to grok (defadvice) but I would take the brute force simple approach and use the following:

(setq my-dired-default-dir "/home/fred/lib")

(defun my-find-name-dired (pattern)
  "My version of find-name-dired that always starts in my chosen folder"
  (interactive "Find Name (file name wildcard): ")
  (find-name-dired my-dired-default-dir pattern))

I'm guessing that I lose history with this approach so if that is important to you Trey's approach is better.

One of these days I have to wrap my head around (defadvice)

Upvotes: 1

Trey Jackson
Trey Jackson

Reputation: 74430

To get a different starting directory, you can advise the function to read the arguments using a different starting directory like so:

(defadvice find-name-dired (before find-name-dired-with-default-directory activate)
  "change the argument reading"
  (interactive
   (let ((default-directory "/some/path"))
     (call-interactively 'get-args-for-my-find-name-dired))))

(defun get-args-for-my-find-name-dired (dir pattern)
  (interactive "DFind-name (directory): \nsFind-name (filename wildcard): ")
  (list dir pattern))

And then you just call my-find-name-dired.

Regarding case insensitivity, you can customize the variable find-name-arg to be the case insensitive version:

(setq find-name-arg "-iname")

Upvotes: 7

Related Questions