Next: , Previous: , Up: Replace   [Contents][Index]


12.7.1 Unconditional Replacement

M-x replace-string RET string RET newstring RET

Replace every occurrence of string with newstring.

M-x replace-regexp RET regexp RET newstring RET

Replace every match for regexp with newstring.

To replace every instance of ‘foo’ after point with ‘bar’, use the command M-x replace-string with the two arguments ‘foo’ and ‘bar’. Replacement occurs only after point: if you want to cover the whole buffer you must go to the beginning first. By default, all occurrences up to the end of the buffer are replaced. To limit replacement to part of the buffer, narrow to that part of the buffer before doing the replacement (see Narrowing).

When replace-string exits, point is left at the last occurrence replaced. The value of point when the replace-string command was issued is remembered on the mark ring; C-u C-SPC moves back there.

A numeric argument restricts replacement to matches that are surrounded by word boundaries.