6

How can one reverse the letters/characters in a selected region?

xyz --> zyx

ab cde --> edc ba

Name
  • 7,849
  • 4
  • 41
  • 87

2 Answers2

13

Emacs doesn't seem to provide any function to do this out of the box, but implementing one isn't that hard:

(defun my-reverse-region (beg end)
 "Reverse characters between BEG and END."
 (interactive "r")
 (let ((region (buffer-substring beg end)))
   (delete-region beg end)
   (insert (nreverse region))))
1

Since emacs can call the shell on a region of text, and since shell can reverse text I believe there's an alternative/elegant(?) solution to the OPs problem,.

That's a comment but not an answer

okay how about

    ;; select the region, then
    C-u M-|
    ;; Shell command on region:
    rev<enter>
NickD
  • 29,717
  • 3
  • 27
  • 44
american-ninja-warrior
  • 3,903
  • 2
  • 24
  • 44