reddit / emacs

How can I replace something in the whole document?

zoliky · 2022-05-01 13:55
Hello, I would like to replace all occurrences of the string `t|` with `t̚`. The problem is that ` ̚` is a special character that I use for phonetic transcriptions, it's the "unreleased stop consonant" and it has the hex code U+031A. I can insert it by using `M-X insert-char` then typing `#x031A` but is there something that I can use to replace `t|` with `t̚` across all the document?
_viz_ · 2022-05-01 14:01
You can always use query-replace to accomplish this, maybe even save it as a macro. But if this is something you need to very often, then I would look into writing a custom input method. The built in compose method (from Emacs 28.1) serves as a good example.
zoliky · 2022-05-01 14:08
I just need to do it once however I need all the occurrences of `t|` to be replaced across all the document top to bottom. There are probably 1000 of `t|`.
_viz_ · 2022-05-01 14:12
M-< M-% t| RET t C-x 8 RET 031a RET RET !
flexibeast · 2022-05-01 14:14
So you could do `M-x query-replace t| RET t M-x insert-char` followed by the hex code and RET. (`query-replace` is also bound to `M-%` by default, and `insert-char` to `C-x 8 RET`.)
a-c-d-w · 2022-05-01 14:51
I *think* /u/zoliky might need to have `enable-recursive-minibuffers` set to t, if they don't already.
dpoggio · 2022-05-01 15:32
You can of course call `replace-string` or `query-replace`. Not for your case , but I like to load `evil-ex` for most of my text replacement because `:%s/a/b/g` is so powerful. Edit: I may even prefer this in your case.
oantolin · 2022-05-01 16:31
`%s/a/b/g` is very powerful, of course, but it is not more powerful that using `query-replace`, it is merely equally powerful.
dpoggio · 2022-05-01 19:28
Maybe I should’ve pointed out the convenience of using evil-ex. Regular expressions for query replace need to be escaped for the lisp reader. That’s awful.
xtifr · 2022-05-01 19:29
If you don't want to enable recursive minibuffers (which can be a bit confusing), you can use Yank (`C-y`) to paste text into the replace string! So, do the replacement manually once. Then copy the result, start the search-and-replace, and paste the result in when asked for the replace string.
oantolin · 2022-05-01 19:30
So evil does not use Emacs regexp syntax? I did not remember that! In any case, that may make `:%s` more *convenient*, but not more *powerful* in my opinion.
dpoggio · 2022-05-01 21:30
Evil-ex does not use Emacs regexp syntax. It’s vim (Unix) like. I find it curious that you have built an opinion around it and yet you ignore evil-ex’s basic behavior.
oantolin · 2022-05-01 21:45
I used evil for over a year, but it was a long time ago and I didn't remember that detail. In any case a superficial syntax change would seem to me likely to only modify the convenience of a tool, not it's power.
Alan_Shutko · 2022-05-02 01:08
Only if you do the replace in lisp. If you M-x query-replace-regexp or C-M-% you do not need to escape them.
dpoggio · 2022-05-02 01:46
Upvoted, you are right. Still Emacs regexps, but not so awful. Thank you.
washy99999 · 2022-05-02 06:48
i'm using visual-regexp. works perfectly. [https://github.com/benma/visual-regexp.el](https://github.com/benma/visual-regexp.el)
washy99999 · 2022-05-02 06:49
fwiw, i pasted in the replacement string at the prompt
SickMoonDoe · 2022-05-02 07:51
Yep the only notable change (improvement IMO) the comes to mind is `:%s/ \+/\n/g` actually works in evil-ex, while in Vim you'll have to use `\r` (and even then you'll fail to replace neighboring lines in some cases).
flexibeast · 2022-05-02 09:04
The command sequence worked on my system without me needing to change `enable-recursive-minibuffers`' default value of `nil`. Maybe because character input isn't considered a minibuffer command?
a-c-d-w · 2022-05-02 17:51
Maybe! No idea, lol
zoliky · 2022-05-03 18:27
> M-< M-% t| RET t C-x 8 RET 031a RET RET ! I used this but the problem is I need to confirm each change with Y manually and I have about a thousand entries that need to be replaced. Isn't it possible to do the replace globally on the whole file? Just pressing the last `RET` won't do anything.
zoliky · 2022-05-03 18:28
> M-< M-% t| RET t C-x 8 RET 031a RET RET ! I used this but the problem is I need to confirm each change with Y manually and I have about a thousand entries that need to be replaced. Isn't it possible to do the replace globally on the whole file? Just pressing the last RET won't do anything.
flexibeast · 2022-05-03 22:07
Pressing `!` when you're first asked to replace an occurrence should have just replaced all occurrences. (Pressing `?` at that time should display some help describing this and other possibilities.) This didn't happen on your system?
_viz_ · 2022-05-04 01:32
That's why you type ! In the prompt instead.
zoliky · 2022-05-04 23:41
Thank you. It works!
zoliky · 2022-05-04 23:41
I forgot to type the `!` It works. Thank you.