6

In vim, all instances of an apple tree can be replaced by an orange tree with :%s/an \zsapple\ze tree/orange/g.
\zs and \ze mark the start and end of the passage to be operated on. Is there something equivalent in emacs? As far as I know, evil mode needs the much less elegant :%s/\(an \)\(apple\)\( tree\)/\1orange\3/g

Toothrot
  • 3,264
  • 1
  • 13
  • 32
  • The general answer is that Emacs regular expressions do not support these zero-width assertions. (For specific cases like this you can achieve the same result, as @Jan has shown.) – glucas Dec 23 '16 at 14:37
  • Please edit your post to explain what \ze and \zs are supposed to do. – Dan Dec 23 '16 at 16:15

1 Answers1

1

You could use M-x query-replace-regexpand use apple\( tree\) as search text and orange\1 as replacement.

query-replace-regexp will search for the first string and will ask you to replace the second string. You can use the exclamation mark ! to replace all of them without further questions.

Drew
  • 77,472
  • 10
  • 114
  • 243
Jan
  • 373
  • 2
  • 12