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. – DoMiNeLa10 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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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