Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Next: , Previous: Replace, Up: Replace

20.9.1 Unconditional Replacement

M-x replace-string <RET> string <RET> newstring <RET>
Replace every occurrence of string 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 happens only in the text after point, so if you want to cover the whole buffer you must go to the beginning first. 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). In Transient Mark mode, when the region is active, replacement is limited to the region (see Transient Mark).

When replace-string exits, it leaves point at the last occurrence replaced. It sets the mark to the prior position of point (where the replace-string command was issued); use C-u C-<SPC> to move back there.

A numeric argument restricts replacement to matches that are surrounded by word boundaries. The argument's value doesn't matter.

What if you want to exchange ‘x’ and ‘y’: replace every ‘x’ with a ‘y’ and vice versa? You can do it this way:

     M-x replace-string <RET> x <RET> @[email protected] <RET>
     M-< M-x replace-string <RET> y <RET> x <RET>
     M-< M-x replace-string <RET> @[email protected] <RET> y <RET>

This works provided the string ‘@[email protected]’ does not appear in your text.

  Published under the terms of the GNU General Public License Design by Interspire