5

I'm writing some elisp code and would like to replace some string with some another string in a buffer. Which elisp function does that ? I did a search on documentation for the related function but all I'm getting is the interactive commands. Would love to see a sample code if possible.

9

Use functions re-search-forward and replace-match in a loop, or function perform-replace. See the Elisp manual, node Search and Replace.

Sample code:

(while (re-search-forward "hello" nil t)
    (replace-match "world"))

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.