Unicode in an Emacs regular expression

7

2

I'm using emacs 24.

How can I replace all occurrences of the non-printable Unicode bidi character RTL, whose hexadecimal number is 202e? I want to replace it with the non-printable Unicode bidi character LTR, whose hexadecimal number is 202d?

Could someone give me some guidance?

Evan Aad

Posted 2012-11-12T18:25:33.607

Reputation: 231

Answers

3

  1. Add the following piece of emacs Lisp code at the beginning of the buffer:

    (while (re-search-forward "\u202e" nil t)
            (replace-match     "\u202d"))

  2. Place the cursor just after the last parenthesis and type C-xC-e to execute the code.

Evan Aad

Posted 2012-11-12T18:25:33.607

Reputation: 231

5

This requires enable-recursive-minibuffers is set to non-nil:

M-x replace-regexp RET C-x 8 RET 202e RET RET C-x 8 RET 202d RET RET

npostavs

Posted 2012-11-12T18:25:33.607

Reputation: 151

I didn't know about C-x 8 RET! – None – 2012-11-12T20:54:01.907

Thanks, but unfortunately this solution doesn't work. I still get the message "Replaced 0 occurrences". – Evan Aad – 2012-11-13T06:45:44.863

1@EvanAad: Is it possible the point was after the character to be replaced? – npostavs – 2012-11-18T05:09:58.333

1

M-x replace-regexp RET \u202e RET \u202d

Oleg Pavliv

Posted 2012-11-12T18:25:33.607

Reputation: 336

You'd think, wouldn't you!? Unfortunately, running this command on a file that consists of a single unicode character, whose hex number is 202e, yields the following message: "Replaced 0 occurrences" – None – 2012-11-12T18:55:40.273

This one doesn't work for me either, and I don't see why it should be expected to work. – npostavs – 2012-11-18T05:15:53.423

1

Type the following:

M-% C-q 20056 RET RET C-q 20055 RET RET

C-q followed by an octal number and RET will insert the character represented by the octal number and discard the RET. To quickly convert hex to octal, type #x202e in a M-: prompt, which will print the resulting number in decimal, octal, and hex.

To enable easier input of decimal numbers, a la GTK's C-S-u binding, I use this in my .emacs:

(global-set-key [(control shift u)]
                (lambda ()
                  (interactive)
                  (let ((read-quoted-char-radix 16))
                    (call-interactively 'quoted-insert))))

user4815162342

Posted 2012-11-12T18:25:33.607

Reputation: 293

Thanks, but i still get "Replaced 0 occurrences". – Evan Aad – 2012-11-13T06:47:57.993

This does work for me. But I wonder why not just have read-quoted-char-radix always be 16? – npostavs – 2012-11-18T05:14:13.190

@npostavs I've retried and it still doesn't work for me. – Evan Aad – 2012-11-21T14:56:41.977