how to abort, quit and exit emacs

There is a long standing joke about the vim text editor which somehow depicts the complexity of the text editors when it comes to newbies……It usually goes something like this.

“I have been using vi editor for two years….mostly because I cannot figure out how to exit.”

Something that can be said about emacs as well, but it does have a window frame with a ‘X’ button…so maybe not quite. Anyways we will look at some of the commands that can get you out of some tricky situations that you will get into while working with the emacs browser.

quit the running command

If you want to quit the command that is currently running then the keyboard-quit command is the one to use. It will signal a ‘quit’ condition to the running command.

M-x keyboard-quit

This is bound to C-g. I am usually a fan of learning the commands rather than just the shortcuts, but this is probably one of the few shortcuts that you should remember. Hitting C-g repeatedly can get you out of many situations where you want to cancel what is running or when you do not want go ahead with what you have been typing.

It can cancel and get rid of partially typed commands and arguments. It is considered a safe way to get out of unresponsive commands or long running commands.

abort recursive editing levels

One of the common situations that you get into is called recursive editing levels. This happens when you perform arbitrary editing when you are in the middle of another command. For example, If you are performing a query-replace and you execute C-r to perform some editing to change the current buffer then you are in recursive edit.

The mode line shows you when you are in recursive edit. It will display a pair square brackets around the mode names, one for each recursive edit level.

Exiting a recursive edit will return you to the unfinished command which will continue to execute. Aborting a recursive edit will return you to the unfinished command and also quit the unfinished command. There are two different commands that will allow you to get out of recursive edits.

M-x exit-recursive-edit # This will exit out of the recursive edit but leave the command running.

The exit-recursive-edit command is bound to C-M-c.

M-x abort-recursive-edit # This will exit and quit the running command.

The abort-recursive-edit command is bound to C-] and is the more commonly used of the two. Again, remembering this shortcut is beneficial in getting out of tricky edits. When in recursive edit, the C-g command can cancel typed out command or arguments but will not always get you out.

Another command that is quite similar to C-] is top-level that gets you out of all the recursive editing levels. The C-] gets you out one level at a time, while the top-level command gets out of all the levels and the minibuffers all in one go.

M-x top-level # Exit all the recursive edit levels

get out of almost anything

Sometimes it is confusing as to what is happening and you just want to get out of the current situation. The keyboard-escape-quit command can be pretty handy. Depending on the situation, this command can clear a selected region, cancel argument, get out of a running command, get out of mini-buffer and recursive edits among many other things.

The keyboard-escape-quit command is mapped to ESC ESC ESC. That means you can hit the escape key repeatedly to get out of many tricky situations. However, this command cannot stop a running command.

exit emacs

Finally, you want to exit emacs itself. There are a couple of commands that lets you do that. The save-buffers-kill-emacs command offers to save each buffer and then kills the emacs process.

M-x save-buffers-kill-emacs # saves modified buffers and exits emacs

The save-buffers-kill-terminal command also works the same way and kills the current connection. It is bound to C-x C-c.

M-x save-buffers-kill-terminal # saves modified buffers and kill connection