2

Any advanced editor has it: Search and Replace to replace a chosen word with another, with search criteria "Match case" and "Match whole word only" in addition like in notepad++.

I am asking this for a selection of text and for the whole content in a file that is opened and in front, and it should be done in one go, without having to press Enter for each search hit.

How is that to be done in emacs?

questionto42
  • 147
  • 6

1 Answers1

5

The command M-x replace-regexp does this. It's not bound to a key by default, so if you use it regularly you may want to do that.

You can also use M-x query-replace-regexp, which is bound to C-M-% by default. It does require you to press enter and then y for each replacement. However, if you type ! instead of y, it will automatically replace all of the remaining matches in the buffer at once, so you only need to press the ! key once.

Tyler
  • 21,719
  • 1
  • 52
  • 92
  • From a beginner's view, you need to 1. press *and hold for the next three keys* `Alt`, 2. press [small letter m is enough], 3. `Shift`, 4. `5` (`Shift`+`5`= `%` of course). After the replace word, at the first search result asking you to replace, press `!` for all (as is said in the answer anyway). – questionto42 Feb 01 '22 at 16:24
  • 1
    `C-M-%` is a particularly convoluted keybinding, no question! It requires holding `control`, `alt`, and `shift` while pressing the `5` key. I think that's what you said, but the formatting is a bit hard to read. – Tyler Feb 01 '22 at 19:10
  • 1
    Ah no, it is a bit easier, no need for pressing `Ctrl`. I need to hold `Alt` and then press `m`, still hold `Alt` and press (at first) `Shift` and then still hold `Alt` *AND* `Shift` and then press `5` (for `%` of course). That means, in the beginning, you press two keys together, in the end you press three keys together. :))) – questionto42 Feb 01 '22 at 19:19
  • 1
    Tyler has indicated the standard way of typing it on a US keyboard layout. If `Alt+m` produces some kind of sticky `Ctrl` modifier for you, that's something specific to your system, and neither standard nor anything to do with Emacs (had that key sequence reached Emacs, it would normally invoke the command `back-to-indentation`, which I personally find very useful). As such, I'm not sure that comment was as useful for beginners as you thought. – phils Feb 01 '22 at 21:28
  • 1
    You might find it useful to investigate "sticky modifier keys" generally. I use them, and so I get a sticky `Ctrl` by typing.... `Ctrl`. Much simpler than typing `Alt+m` I think?! If I want to I can type `Ctrl`, `Alt`, `Shift` one at a time in order to then get `C-M-%` by typing `5`. Or I can type them all together. Or some individually, some together. I can also toggle any modifier to and from a "lock" position (caps lock style) which can be super useful for repeating certain sequences in Emacs. Sticky modifiers are great. – phils Feb 01 '22 at 21:41
  • @phils You are right, I had not seen that `Ctrl` would then replace `Alt`+`m`. Tyler's comment makes it easier indeed. – questionto42 Feb 02 '22 at 10:48