emacs - Replace regexp / string / words in several / multiple files

In short:

M-x dired-do-query-replace-regexp

 

Longer version:

To replace the same regexp, string or word in multiple files go into dired mode

C-x C-f RET

Then you should see a list of files.

 

Mark the files you want to edit with m.

To mark all files press t (M-x dired-toogle-marks).

You can also mark only all files but some: First mark the files which you don't want to be marked (by using m) and then press t.

All the other files get marked.

To mark files using a regular expression, e.g. to mark all .tex files you can use following key combination in dired mode

*%

 

To replace a certain string or regular expression (regexp) in all marked files

M-x dired-do-query-replace-regexp

Options during replacing:

  • C-g  (stopps the replacing)
  • ! (replace the regexp string in the current file without asking)
  • N (do no further replacements in this file)
  • Y (replace all occurrences in all files, without asking)

Saving the modified buffers

1st way

C-u C-x s

This saves all modified files silently, i.e. without asking for confirmation.

2nd way

M-x ibuffer
*u (marks all modified buffers)
S (capital 'S', saves all marked buffers)
D (kills the marked buffers)

 

 

blog_tags: 

Add new comment

Filtered HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <img> <br> <p> <span>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Use [collapse] and [/collapse] to create collapsible text blocks. [collapse collapsed] or [collapsed] will start with the block closed.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Code snippets in <code>...</code> or <source>...</source> automatically will be pretty printed.
  • Use [collapse] and [/collapse] to create collapsible text blocks. [collapse collapsed] or [collapsed] will start with the block closed.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Target Image