 Here in the past few weeks, I've had a couple of people ask me questions about how to escape certain Emacs buffers. I've done my configuring Emacs series here over the past few months, so I've spent a lot of time configuring my own Emacs, if you will. I know a lot of you guys have been following along creating your own custom Emacs configurations, and sometimes you guys are complaining that you find yourself trapped in a certain Emacs program, an Emacs buffer, where your typical ways of exiting out of things, your typical key bindings to exit out of a particular buffer, is just not working for you. And this is a normal kind of problem with Emacs, because honestly there's several ways that you can get out of some of these situations. I'm going to cover those on camera right now. So let me switch over to my desktop, and I'll go ahead and launch Emacs, and let me jump into my Emacs config, because we need to talk about a few things. Those of you that followed along with the configuring Emacs series, one of the very first things we did was set up some key bindings using the general.el program, that's the general key bindings. And one of the things we did here at the top is we set two prefixes. So we set a prefix of space. So space is our leader key. So when we're setting key bindings later on, like this here, this key binding bb, switch to buffer, that's actually space, bb is the key binding for switching to another buffer. So the space is the leader key, but you can't always have space as a leader key, because if you're using evil mode within Emacs, obviously sometimes you're going to be in insert mode. And when you're in insert mode, a space is not a leader key. A space is actually a space. It's going to type a space right inside insert mode in evil. So you have to have a backup leader key. And that's where we set this prefix called the global prefix, and that is set to meta space, which is alt plus space. And that way you can still use your space related key bindings inside insert mode in evil. And I can show you this in action if I hit I on the keyboard to get into insert mode. Now let's imagine I want to switch to a buffer. Well, I can't type space bb for the switch buffer command because space inside insert mode is just going to be a space, right? So what I have to do is I need to do the meta space global prefix. I'm going to do alt space bb, and now you can see in the echo area, I'm running the switch to buffer command and I could give it a buffer to go to from that point. So that's always the first thing I would try if I'm in a buffer and for whatever reason my space related key bindings are not working, my space bk for space buffer kill, right? If it's not working in a particular buffer, try alt space bk for buffer kill, right? So always try the global prefix if your standard leader key is not working. Of course, sometimes you will encounter special buffers that really don't seem to respect either your standard prefix key or the global prefix key that will happen on occasion. I've noticed this especially with the documentation related key bindings for emacs or the documentation related programs such as all the described commands. For example, if I do space hv for described variable, I'll just pick any random variable to take a look at right here in the documentation. Let me get over into the split, well j and k work, so it does look like evil mode is working as far as j and k, but would space bk for buffer kill work? Space bk for buffer kill, it does work, but let's imagine that it didn't work. Let me do space hv and once again, I'll take a look at the documentation for a particular variable. Let's imagine this buffer didn't work with my evil related key bindings. Well, there is a standard key binding to get out of any kind of documentation or help related information and that is simply typing q on the keyboard, q for quit. A single q will actually get you out of that buffer and it will actually close the split that opens. So typically when you're doing the describe variable, describe functions, help, if you're doing info, if you're looking at the info information here inside emacs, a simple q for quit, get you out of that and put you right back in your previous buffer. I know that's kind of confusing because not every buffer works like that, but a lot of times anything that opens up especially in its own split typically just type q on the keyboard to get out of it. Now, one of the biggest pain points with being able to escape out of certain programs is terminals because if you're, especially if you're an evil mode user, which I know so many of you guys are and obviously I'm a big fan of the VM key binding. So I use evil mode in Emacs. Evil mode does present some real issues in the terminals inside Emacs. So I'm going to do space TV to toggle the term. So this is the term down here in this horizontal split. I'll make the split a little bigger. Now, let's imagine you're running a command line program or in curses program such as htop, htop standard in curses program. Now, how do you escape out of htop? Well, the good thing htop just needs q to quit. But some programs need you to type escape to quit. You often run into this with run launchers. I do a dm hub and give it the dash f flag. So dm hub is part of my dm scripts program. The dash f says run dm hub with fuzzy finder in the terminal rather than launching d menu. So this is the dm hub program in fuzzy finder. But let's imagine I don't want to actually select one of these options from the menu. You run into this all the time with d menu, rophy, fuzzy finder, any kind of run launcher. A lot of times you simply don't want to launch a program or choose something from a menu that you've launched. Sometimes you just want to escape out of it. Well, let me hit escape. Escape doesn't work. Now, in my case, I have configured my Emacs in a way to get around this problem if I hit escape again. It will get me out of that particular program. But that is not how Emacs is out of the box. Out of the box, you could hit escape a million times and you are stuck in that fuzzy finder script. Because by default, escape in evil mode, what does escape do? It gets you into normal mode, right? So escape is already binded to something. So what escape actually does in the program itself no longer works. Basically, you've had your escape key taken from you by evil mode. So if you have a serious need to use the escape key to actually escape a program, you can't actually do it. Now, I will show you my hack to get around the escape problem. One thing you could do if you run into this problem on a standard installation of Emacs where you've installed evil mode, but you haven't really configured it. If you run into this problem where you can't hit escape to get out of something, you can always Control C in a terminal to get out of something. Well, not necessarily in Emacs because Control C is a prefix for a lot of other commands. So it's the same problem. Except just remember this, Control C twice will always work. If I do Control C and then Control C again, it will always quit out of that terminal application. And what I've ended up doing, let me escape to get into normal mode, space BK to kill this buffer type yes for confirmation to get rid of the V term. In my config, what I've done, let me go to the top. And what I've done is it might be under the V term section where I added the escape key bindings or maybe it was under the actual key binding section itself, the general key binding section. And actually it's under the V term toggle section of my config. I have redefined the escape key. I've done a evil define key. So inside normal state in evil mode in a V term, because I'm using V term mode map, V term mode is the standard mode inside V term, obviously. And I'm rebinding escape, the simple one press of the escape key to run the command V term dash dash self dash insert. What that does, it tells V term to actually do a self insert of a particular key. And essentially what this does is when I hit escape twice now, it actually registers escape to actually do what escape should do in that particular fuzzy finder script or incurses program or whatever it happens to be, it'll actually register escape as an actual escape in that program rather than escape being standard escape in normal mode, which again really won't do me anything for us. One last key binding I would suggest always trying is if you're in a situation where some of your escape key bindings are not working by default, Emacs typically uses control G to kill a process to get out of a running process. So if I do meta X to get into basically our command here, right meta X, so we could launch some command, but imagine, you know what, I hit meta X, but I really don't want to run a program. Let me just get out of it while escape actually does work in meta X. But let's imagine it didn't. I could always use control G. So control G is just another key binding to try. So you've got a lot of ways to get out of things in Emacs evil mode. So remember, try your space related key bindings assuming you use space as a leader key and then try the alt space related key binding. So try your prefix leader key, try your global prefix leader key, or simply type Q on the keyboard to quit that works in a lot of Emacs programs or if you've done the escape trick where I've rebinded escape and something like vterm, if it's a terminal program, try escape twice or try control C twice or try control G. That often works as well. And finally, as the absolute nuclear option, if you find yourself in a buffer, you just can't get out of no matter where you're at in Emacs, meta X always works. So type meta X and you go to eye buffer. And once you're an eye buffer, you can select that buffer that you were in and you can actually kill it. You can just kill that buffer through eye buffer. And oh, and that's it. You can just meta X is always available for you. I know that's kind of a clunky way to do things. Typically though, at least one of those five or six key bindings will work to get you out of that Emacs program. Now before I go, I need to thank a few special people. I need to thank the producers of this episode. Daniel Gabe James, Matt Paul, Royal West, Armored Dragon, Commander Henry, George Lee, Methos, Nate Erion, Paul Peace, Archimdor, Riala, Teets were less red, Profit, Roland, Soul, Astrid, Tools, Devler, Ward Jento, and Ubuntu. And Willie, these guys, my highest tiered patrons over on Patreon. Without these guys, this episode about escaping Emacs. It wouldn't have been possible. The show is also brought to you by all of these fine ladies and gentlemen. All these names you're seeing on the screen. These are all my supporters over on Patreon. I don't have any corporate sponsors. I'm sponsored by the community. If you like my work, subscribe to DistroTube over on Patreon. These guys. Escaping Emacs is almost as hard as quitting Vim.