tags:

views:

199

answers:

6

I am just starting out in in Emacs. Sometimes I'll be experimenting with something and when I'm finished experimenting, I want to just go back to the buffer I was editing, but I don't know how. Worse, sometimes I hit a key by accident and don't even know what I hit! In Vim I can just hit ESC to recover; is there any similar way to recover in Emacs?

EDIT: I am using the command line (non-GUI) Emacs.

+7  A: 

"C-g" is your command quit

"C-x o" with take you to the other window if you have multiple open

"C-h a" (apropos) is a great way to find out more commands and keys. Enter "C-h a buffer" to get a list of all the buffer related commands

Bryan Ash
Just to expand on this a bit, "C-h l" will display recent input, which can be useful if you hit something but don't know what it was.Furthermore, "C-h t" will get you to the built-in tutorial, which should be your first step for learning Emacs.
Huw Giddens
+5  A: 

There's also ESC-ESC-ESC (keyboard-escape-quit). From the manual:

The command ESC ESC ESC (keyboard-escape-quit) can either quit or abort. This key was defined because ESC is used to "get out" in many PC programs. It can cancel a prefix argument, clear a selected region, or get out of a Query Replace, like C-g. It can get out of the minibuffer or a recursive edit, like C-]. It can also get out of splitting the frame into multiple windows, like C-x 1. One thing it cannot do, however, is stop a command that is running. That's because it executes as an ordinary command, and Emacs doesn't notice it until it is ready for a command.

I find pressing ESC to be a natural response to the desire to "exit" a situation. Hammering it several times can also be emotionally satisfying when you are frustrated.

zetetic
Note that it works in multiples of 3: if you hit it 5 times, the minibuffer will display "ESC ESC" and you have to hit it one more time. :-)
ShreevatsaR
That's OK. When I'm fed up with Emacs I just keep hammering it until the minibuffer clears out. There are lots of numbers evenly divisible by 3...PS. Of course this requires `(setq visible-bell t)`, which IMHO is the one essential Emacs customization
zetetic
I love the bit "Hammering it several times can also be emotionally satisfying when you are frustrated."
Vivi
+3  A: 

Your subject line is a little confusing, because 'modes' (major and minor) are not the same thing as buffers (modes are applied to buffers, affecting the way you view and interact with them). It sounds as if you actually want to know how to find your original buffer after you have changed to another?

C-x b is the default binding for switch-to-buffer. It offers you a default buffer to switch to, which you can do so by pressing RET. Much of the time the default offered will be the one that you wanted. If not, you can instead press TAB to see a completion list of all the available buffers. Now just type enough of the name to make it unique, press TAB again to complete the name for you, and then RET to select it.

Alternatively, C-x C-b is the standard binding for the list-buffers command, which will show you a list of all buffers. You can then use C-x o to change to the other-window, move up or down to select the buffer you want to change to, and press RET to change to it. C-x 1 will then remove the other windows from your emacs display.

Finally, many major modes bind q to a 'quit' command, which generally buries or deletes that buffer, returning you to the previous one. This isn't guaranteed, but it works in many situations.

(As already mentioned by others, you may potentially need to C-g or otherwise abort an active command, before you can use these commands.)

That all said, I am thinking that what you really need to do is type C-h t or M-x help-with-tutorial RET and read and follow that document. It will answer all of these sorts of questions for you, and many more. Emacs can be complicated, so this would be very well worth your time.

phils
I changed "modes" to "messy situations". I am not sure if that clarifies things...
kerkeslager
A: 

M-x fundamental-mode will strip out all your modes in a given buffer.

Paul Nathan
This can be quite confusing if you don't know what mode you were in. Keys stop working and behaviour become weird.
Noufal Ibrahim
A: 

I keep an "experimental" buffer (usually my *scratch* buffer) and bind a key to switch to that (in my case, F4). I hit it, go to the scratch buffer, experiment to my hearts content and then use C-xb to switch back to the buffer I was in.

In any case the C-xb, should allow you to switch buffers and you can use that to "go back" to where you were. The iswitchb-mode makes the buffer switching a lot nicer but that's a personal opinion.

C-g is Emacs's quit command. It's roughly equal to vim's ESC key. You can hit it a few times to break out of what you're doing (e.g. halfway through a complex command etc.) and then get back to your regular workflow.

Don't worry. After a while, all this becomes muscle memory. Just like the ESC key for vim experts.

Noufal Ibrahim
I'm not worried. :)
kerkeslager
Well, I was when I first started using Emacs. I had to connect to the same machine from another one and kill the process because I didn't know how to quit. :)
Noufal Ibrahim
Haha, that's an awesome story. I already went through the vim learning process, so this time I knew better than to open emacs without knowing how to close it. :)
kerkeslager
+2  A: 

Cancelling, Undo/Redo

C-g will cancel any command. There is one time this will not work, when you interrupt yourself entering a command, for example:

  • You type C-xC-f (find-file).
  • While typing the file name you swap to another buffer C-xC-o.
  • Now you can't cancel the command without swapping back into the minibuffer (area at the bottom).
  • Just use C-xC-o again to get back there and continue.

C-_ is undo/redo, it has only 1 direction. If you C-_ 20 times, then make a change, those undos become your past and you can C-_ 21 more times to undo your change and your 20 undos, you'll be back where you started.

Finding Buffers

  • Use C-x<left> and C-x<right> in the beginning to swap between buffers.
  • Then get used to C-xb.
  • Try M-x ibuffer.

Splitting

  • C-x3 Split vertically
  • C-x2 Split horizontally
  • C-x1 Remove all splits

Info

  • M-x info is your best friend
  • Learn about C-h
Koroviev