EmacsWiki: Replace Plus (original) (raw)

This page is about library Replace+, replace+.el. It extends functionalities in the standard Emacs library replace.el, enhancing commands ‘query-replace’ and ‘occur’. (For Emacs 20 it also enhances ‘keep-lines’, ‘flush-lines’, and ‘how-many’.)

Default Value for the Text Acted On

Common to the enhancements for commands ‘query-replace’ and ‘occur’ (and ‘keep-lines’, ‘flush-lines’, and ‘how-many’ for Emacs 20) is the handling of a default value when reading input. For ‘query-replace’ this applies to the text to be replaced and the replacement text. For ‘occur’ it applies to the regexp used to match buffer lines.

The defaulting is very flexible, so it might sound a bit complex. Defaulting is determined in the following order:

1. For ‘query-replace’ only, just as in vanilla Emacs, empty input means to repeat query-replacing using the same old and new text as the last time. This is not a change in behavior.

2. If the region is active and option ‘search/replace-region-as-default-flag’ is non-‘nil’, then the region text is a default.

3. Else, if option ‘search/replace-2nd-sel-as-default-flag’ is non-‘nil’ and the SecondarySelection is defined, then the secondary-selection text is a default.

4. Else, if option ‘search/replace-default-fn’ is non-‘nil’ then it is a function of zero arguments that is called to return a default.

5. Else the first entry in the minibuffer history is a default.

(For Emacs 23 and later, all defaults that can be defined as listed are available, in this order. For Emacs prior to 23, only the first default that can be defined is available.)

Note that ‘query-replace’, unlike the other commands enhanced here, normally uses the region not as default text but to restrict the scope of the command action. When option ‘search/replace-region-as-default-flag’ is non-‘nil’ you lose this feature and gain using the region for defaulting. In this case you can just narrow the region (‘C-x n n’) to restrict the scope of the command.

Because both uses of the region can be useful, you can easily toggles the value of option ‘search/replace-region-as-default-flag’ using command ‘toggle-search/replace-region-as-default’.

Wrt #4 above, consider using library thingatpt+.el, which provides plenty of functions that are useful for grabbing text at point to use as a default value. Among these is the default value of ‘search/replace-default-fn’ (if defined), ‘non-nil-symbol-name-nearest-point’. It finds the nearest symbol to the cursor and returns its name.

Query-Replace

Library replace+.el does not redefine either of the standard query-replace commands, ‘query-replace’ or ‘query-replace-regexp’. Instead, it defines a new command that combines them.

IciclesSearchContextLevelsScreenshot

See Also: IsearchPlus for more information about options ‘isearchp-set-region-flag’ and ‘isearchp-highlight-regexp-group-levels-flag’ and command ‘set-region-around-search-target’.

Occur Extensions

Library replace+.el provides the following extensions to Emacs’ standard ‘occur’ command:

See Also: OccurMode, for a screenshot and more information.

Extensions to `flush-lines', `keep-lines', and `how-many'

Library replace+.el provides the following extensions to commands ‘flush-lines’, ‘keep-lines’, and ‘how-many’. They are not needed for users of Emacs 21 or greater.


CategorySearchAndReplace CategoryRegexp