Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7022961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:38:59+00:00 2026-05-27T23:38:59+00:00

To replace all the occurrences of ‘foo’ in a text file with some other

  • 0

To replace all the occurrences of ‘foo’ in a text file with some other string, the usual Emacs command is M-x replace-string.

Off late, I have had to replace several such strings in my text files. Doing
M-x replace-string for every expression I want to replace is tiring. Is there any Emacs command to ‘batch replace’ a bunch of strings with their alternatives?

This might look something like,

M-x batch-replace-strings RET foo1, foo2, foo3, RET bar1, bar2, bar3 RET
where RET stands for hitting the return key.

So now foo1 has been replaced with bar1, foo2 with bar2 and foo3 with bar3.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:39:00+00:00Added an answer on May 27, 2026 at 11:39 pm

    This code does what you want, prompting for the string pairs pair-by-pair:

    (defun batch-replace-strings (replacement-alist)
      "Prompt user for pairs of strings to search/replace, then do so in the current buffer"
      (interactive (list (batch-replace-strings-prompt)))
      (dolist (pair replacement-alist)
        (save-excursion
          (replace-string (car pair) (cdr pair)))))
    
    (defun batch-replace-strings-prompt ()
      "prompt for string pairs and return as an association list"
      (let (from-string
            ret-alist)
        (while (not (string-equal "" (setq from-string (read-string "String to search (RET to stop): "))))
          (setq ret-alist
                (cons (cons from-string (read-string (format "Replace %s with: " from-string)))
                      ret-alist)))
        ret-alist))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a text, I would like to replace all occurrences of $word by [$word]($word)
How can I replace all the occurrences of the $keyword within a string without
Is it possible to search and replace all occurrences of a string in all
I got a string in which I replace all occurrences of [CODE]...[/CODE] . With
Possible Duplicate: Replace all occurrences of substring in a string - which is more
Possible Duplicate: replace all occurrences in a string I found this question/answer: Use JavaScript
Possible Duplicate: replace all occurrences in a string I have this string: 12-3-02 And
Regex dialect: Java The problem: given a string, replace all occurrences of a substring
I've to replace all occurrences of a specific macro inside some(only some amongst dozens)
I am trying to replace all the occurrences of \n in the Text property

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.