2

In Dired Query and Replace, can we substitute in the filename being visited? For example, I want to substitute "solve" in all *.mod files with the filename *.mod.

  • 2
    I'm having trouble understanding what you want to do. Could you edit your post to elaborate, please? – Dan Aug 31 '15 at 18:35
  • Say I have 100 *.mod files, each of them contain the word "solve". I would like to replace "solve" with the corresponding filename. – Michael Aug 31 '15 at 19:42
  • sorry, that doesn't clarify it. Do you mean the filename contains "solve", or that the string "solve" occurs in the contents of the file? – Dan Aug 31 '15 at 19:45
  • 1
    I'm guessing I think that this is what you want to do: 1. Find all files whose names match *.mod and whose contents contain the word solve. 2. Replace the word solve in each of those files with the name of the file. – Drew Aug 31 '15 at 19:50
  • I think it would be helpful if the OP clarifies the question. – Name Sep 1 '15 at 12:49
2

I'm guessing that what you want to do is this:

  1. Find all files whose names match *.mod and whose contents contain the word solve.

  2. Replace the word solve in each of those files with the name of the file.

If so, one answer might be to use Q in Dired, searching for solve, and to use a replacement expression such as \,(buffer-file-name), to replace solve with the name of the file currently visited (where the solve occurrence is).

If you want, you can also issue a find command using find-dired.el, to open Dired with just the files that contain solve. And then use Q on all of them (just mark them all, or use C-u C-u Q with Dired+).

  • Great answer, no wait: A damn great answer! Thanks :-) – Dieter.Wilhelm Sep 1 '15 at 16:09
1

You are probably asking for writable dired (wdired) feature. In any open dired buffer, make it writable with

C-x C-q

then use search/replace as any normal buffer to substitute "solve" with whatever you want. Notice only the filenames are editable. Once you finish editing the filenames, you need to commit the changes with

C-c C-c

or if you want to undo or cancel the changes, use

C-c ESC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.