how to reverse lines and characters in the buffer in emacs editor

There are several different commands to sort text in the buffer. Reversing lines, words or even paragraphs is just a variation of sorting text.

Reverse Lines in buffer

The command to reverse text is reverse-region, and as the command indicates works on the current active region of the buffer.

M-x reverse-region

The reverse-region command will reverse the order of the lines in the region. In order to reverse the lines in the whole buffer, select the whole buffer before executing the command. C-x h will select the whole buffer.

Reverse Characters in a line

There is no built-in command in emacs that will reverse the characters in a word or line in buffer. But emacs gives you the ability to run shell commands on a region in the buffer. If you are in Linux, you can very well use the rev shell command on a region….

First select the region that you want to reverse the characters. The characters are reversed with in each line but you can select multiple lines to perform the operation.

C-u M-| rev <enter>

After you enter M-| keystroke, you will get a prompt to enter the shell command. Enter rev at the prompt to execute the command on the region. Arguably you can use the sort command just the same way to sort the lines in the buffer.