Previous in this series : Part 4 – Search
In my last post I discussed searching, frequently when searching you are looking for something to replace. As with search Emacs offers rich replace commands as well.
Unconditional and Conditional Replace
Unconditional – The command for basic replace is M-x replace-string. This command will prompt you for a target string and a destination string. When executed, it will unconditionally replace all occurrences of the target string with the destination string, from the cursor to the last occurrence of the target string. Two things should be noted. First this command does not have a reverse or backward mode. Second, a source string containing no uppercase letters will be treated as case insensitive.
Conditional – For the more paranoid and the case where only certain occurrences should be replaced, Emacs provides the command M-x query-replace or M-%. Similar to replace-string, this command prompts for a target string and a destination string. When executed the first occurrence of the target string is highlighted and the cursor is placed at its end. At this point the user has a few options to replace or not replace this occurrence, to move to the next occurrence, to unconditionally replace all remaining instance,or abort the command entirely. All of these options are specified by a single keystroke as described in the table below.
||Replace this occurrence and move to the next
||Skip this occurrence and move to the next
||Replace this occurrence and do not move on
||Unconditionally replace all remaining occurrences
||Backup to the previous matching occurrence
These two commands will work for most needs, however, we can get more power from the regexp versions.
Like the non-regexp versions, the regexp versions come in both conditional, M-x query-replace-regexp and unconditional, M-x replace-regexp flavors. Regular expressions are specified in the same manner as search regular expressions covered in the previous article. What comes in handy is the ability to use prior group construct to build your destination string. For example, suppose you want to change a string such as “Name: lastname, firstname” to “Dear firstname lastname,“. This can easily be accomplished with a target regexp of
Name: +([A-Za-z]+), +([A-Za-z]+)
and a destination string of
Dear 2 1,
Bonus: Narrowing and Widening
One particular method for restricting replace is to narrow the buffer. By narrowing a buffer you limit the portion of the buffer that you can currently view and edit. This can especially be useful when performing a replace operation. For example you may want to replace a variable name with in a particular function. You would first narrow the buffer to the function in question and then perform an unconditional replace operation. This would be much safer and possibly faster than a conditional replace operation.
Narrowing a buffer is comprised of two basic steps. First mark the region you want to narrow to using the M-x set-mark-command, C-SPACE or C-@, and then repositioning the buffer to the beginning or ending of the region of interest. Once a region is defined issue the M-x narrow-to-region or C-x n n command to narrow the buffer. Now any editing actions you take will be applied to the narrowed buffer only, with the exception of M-x save-buffer and like commands which will save the entire buffer. To continue editing the full document you must widen it with the M-x widen or C-x n w command.
Next in this series: Part 6 – Some Fun
Until next time.