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

xyz --> zyx

ab cde --> edc ba


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))))
  • Perfect, works well.
    – Name
    Jan 17 '18 at 16:23
  • 1
    Mark my answer as an solution if it helped you.
    – user12563
    Jan 17 '18 at 16:38
  • Of course. Usually I wait for a while for alternative solutions, then mark it as accepted.
    – Name
    Jan 17 '18 at 16:43
  • 4
    You can also use delete-and-extract-region, i.e., (insert (nreverse (delete-and-extract-region beg end))).
    – xuchunyang
    Jan 17 '18 at 19:32
  • How do you use it with Meta + x? If I highlight a region (e.g. the abc string) and then execute it, I get the following error: Wrong type argument: listp, #("abc" 0 3 (fontified t)) Sep 14 '19 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.