macosemacskey-bindingsmacos-monterey

emacs Terminal control-esc-% ("query-replace-regex") ignore the "control" and yields plain meta-% (query-replace)


macOS Monterey 12.2.1. Using the built-in emacs from Terminal, I often want to do query-replace-regexp. According to GNU docs, the shortcut is C-M %, so I type control-Esc %. But emacs doesn't notice the control key. Instead it gives me the non-regex query-replace, as if I had just typed Esc % (M %).

When I type control-Esc, the command buffer at the bottom of the emacs window shows just ESC, whereas I had expected to see something like C-ESC.

For other commands, like C-r for search, C-( to learn a macro, etc the control key works just fine.

Has anybody seen this behaviour, or could suggest how I can make C-M % work?


Solution

  • You can start a query-replace-regexp on the terminal by first starting a regexp isearch-forward-regexp (backward works too), there are two ways to do this:

    1. Start an isearch with C-s or C-r, followed by M-r to switch to a regular expression
    2. C-M-s or C-M-r to start a regexp isearch directly.

    If you then hit M-%, it'll start a query replace using your active isearch term (which was a regular expression) as the "replace" value.

    1. Hit M-r to switch to regular expression and enter your regular expression to be replaced
    2. Hit M-% to start a replace

    Hitting M-% from an active isearch uses your search term as the replace term, and it matches the current mode.

    Per Chris Johnson's answer, the reason you can't type C-M-% on the terminal is because C-% is not part of the standard control codes.