 I've been strictly a Linux user for about 15 years now and over those years the more and more I gravitated toward the terminal. These days I spend most of my time in a terminal and over the years I've learned some really useful tips and tricks for those of you that want to learn more about the command line, more about the shell, and one of the biggest things you can do to really improve your workflow using a terminal is to actually use some of the shell key bindings, these keyboard shortcuts that are actually built into things like the bash shell. So today I wanted to cover some of the most important built-in key bindings for the bash shell, but keep in mind these key bindings they will also work if you use ZSH or Fish that will work on all three of those shells. So let's switch over to my desktop and I've got a terminal opened and we are using the bash shell here. Let me zoom in a little bit so you can see what I'm doing here. So let me just start typing something maybe a command or I'm just going to type some random text here, I'll type this is a line of text, well I can't type today. Now one of the things about the shell that many people don't realize is that it has these built-in keyboard shortcuts, these key bindings and because the bash shell is built by the good folks at the GNU project and of course the GNU guys, they use GNU Emacs, the text editor, right? So typically what you'll find is your Emacs key bindings, your standard GNU Emacs key bindings also work in the shell. For example, a standard Emacs key binding is to go to the beginning of any line you would do control A. So if I do control A, I go to the beginning of the line to go to the end of the line and Emacs is control E. So once again, control A for the beginning of the line, control E for the end of the line. And I started with control A, control E because I think that is easily the most useful key binding for the shell because especially once you start writing very lengthy commands, very lengthy arguments, sometimes writing entire functions here at the command line, having to go back by using the arrow keys is very time consuming, very tedious on a very lengthy command. So that control A, control E thing, you're going to use it all the time. Now let's talk about navigating. Of course, you can use the arrow keys if you want. But again, the standard GNU Emacs key bindings work. So let's talk about moving forward and backwards with standard GNU Emacs key bindings. So control B for back will move back a character. So I'm just holding the control key and hitting B. If I hold the control key and hit F for forward, I'll go forward a character. Now does this really save you any typing from just using like the arrow keys for going forward and back? No, not really. It basically is the same. But where you get real speed improvements is by going forward and back by word instead of just by a single character. So instead of control B, do alt B. So alt B moves back by word instead of a character. So you can really move very quickly. And of course, alt F would move forward by a word. So that is how that works. Again, you can move very quickly using alt F and alt B. Now let's talk about deleting text. If I go to the middle of this Senate somewhere and I do control U, watch what happens. I just deleted everything from the beginning of the line up into the point, up into the cursor point, right? If I do control Y, that is an undo. So in Emacs undo is control Y. That's a little strange key binding. I always have trouble remembering that because of course in Vim, U is undo. But in Emacs, control Y to undo. So control U again, deletes everything from the beginning of the line up until the cursor point and then control Y to undo. Now watch what happens if I do control K. That deletes everything from the cursor point to the end of the line. So control U from the beginning to the cursor point, control K from the cursor point to the end of the line. And of course, control Y for an undo. Do you want to delete a word going backwards? You can do control W. Watch what happens. We just deleted the last word. If I do control W, it will delete the next word, control W. We just keep dropping off words, right? And again, control Y would undo everything. Now what if you want to do that same deleting words that's going the other direction, going forward rather than backwards? Well, let me do control A to go the beginning of the line. And instead of control W to delete a word going backwards, I'm going to do Alt D. So Alt D deletes a word going forward. And then again, control Y will undo all of that. Now let me show you another trick here. I'm going to control A to go to the beginning there. I'll do a control K to delete everything from the cursor point to the end of the line. And then what I'm going to do is I'm just going to do a random command here. I'm going to do echo, hello. And the reason I'm doing that is because oftentimes you will want to run a different command, but sometimes you will want to use the same argument. So in this case, the command is echo. The argument was hello. But maybe, you know, echo wasn't what I wanted to do. Maybe I really wanted to do printf, hello. And of course, if it was a very lengthy line for the argument, you know, do you really want to have to retype the whole line? Well, you don't have to do that if you're using the Emacs bindings that are the default key bindings on the bash shell. All you have to do is Alt period, Alt period. And magically, the last argument that you had used in the previous command appears here. So that way I don't have to retype, hello. And to see that in action again, maybe printf wasn't what I wanted. I wanted echo. So I type echo, space, and then Alt period, and I get the hello back. Now let's talk about going up and down in your history. So if I do control P for previous, we will get the previous command in our history. If I do control P again, we'll get the previous command, you know, the one before that. And if I do control N for next, we will go forward in the history. So control P goes back in the history. Control N goes forward in the history. So P previous in next. And finally, let's talk about clearing the terminal. If I do this as a line and I do control L, watch what happens. We cleared the terminal other than the line I was currently working on. But you know, if you had a whole lot of output that was kind of cluttering with the terminal, you want to clear it. And many people often complain that I type the word clear to clear the terminal. That works exactly the same way as control L. And why don't I just use control L? The reason I don't use control L in my videos and you always see me type the word clear is because I don't actually use the Emacs key bindings in my shell. I actually use the VI or VIM key bindings in the shell because that is an option. By default, Bash, ZSH, Fish, they all default to using Emacs like key bindings. But you can switch over to VIM like key bindings if you prefer. So we talked about control L to clear the screen instead of typing clear control L. Also instead of just exiting the terminal by typing exit, you could do control D with the Emacs key bindings and we just exited, right? So that actually just exited me out of the Bash shell and put me back into the Fish shell, which is my default control D again, would actually get me out of Fish and just kill the terminal. But let me open a new terminal here. And once again, I'm going to go back to Bash, I'm going to clear the screen. And in this Bash shell, I'm no longer using the Emacs key bindings because in my Bash RC, I have a line that says set-ovi. So by default, your Bash shell is set-oemacs to set it to use Emacs like key bindings. But if you want to change to VI, just put this in your Bash RC or just run this at the command line set-ovi. And you will now be using VI key bindings rather than the Emacs key bindings. Now is there any advantage using the VI bindings over the Emacs bindings? No. You can pretty much do all of the same stuff. You'll just use different bindings. For example, by default, you're in insert mode if you're using the VI bindings because this is modal, much like Vim, where you can be in normal mode and you can be in insert mode. But when you first launch the shell, you're in insert mode, meaning you can actually type something. So this is a line of text. Now to actually move to the beginning of the line, we did Control-A in the Emacs bindings. But of course in Vim, you would do zero, zero on the keypad. But in insert mode, it would just be zero, right? So you can't do that. You have to escape to get into normal mode, right? And then zero to go to the beginning of the line and dollar sign to go to the end of the line. So Control-A, Control-E were the Emacs bindings here. It's zero and dollar sign, zero and dollar sign. If you prefer, you can also use the carrot symbol instead of zero. For me, I find zero much easier than the carrot symbol. But that works, too, to get to the beginning of the line. And of course dollar sign always gets you to the end of the line. And while we're in normal mode, we can also move back by characters using H to go left and L to go right. So in the Emacs bindings, we were using what Control-F and Control-B. Here you're just using H and L as long as you're in normal mode. Now the Emacs bindings were Alt-F, Alt-B to move back and forward by word in VIM to go back by word. You just do B. So back is just B. To go forward by word is W, W, W, W, W. So again, you can do all of the same things with the Emacs bindings and the VIM bindings. They just are different bindings. And just like in VIM, you can give a number to these commands. For example, if I want to move back two words instead of just doing B, I could do 2B. If I hit 2, the shell tells me I passed 2 as an argument and B. You see, the cursor was on text and now it's on line, so it actually moved it two words back. If I do 2W, I will move two words forward. Let's talk about deleting from the beginning of the line and to the end of the line. So those were Control-K and Control-U and Emacs in VIM. It's a little different. In VIM, you do D and then either 0 for the beginning of the line or $ to the end of the line. So right now I'm in the middle of the line. If I do D, 0, it deletes everything from the beginning of the line up until the cursor point. U to undo in VIM. If I do D, $, it deletes everything from the cursor point to the end of the line. And once again, U to undo. And I showed you how to delete words going forwards and backwards using the Emacs bindings for VIM to delete words going backwards. D, B, D, B, D, B, right? Well, let's undo all of that. And if you wanted to do the same thing going forward, I would do DW, DW, DW for delete word U to undo. Now I showed you the Alt-Period trick earlier with the Emacs binding. So if I do Echo, Hello, and then Print, F, and then I did Alt-Period in the Emacs binding, so that would pass that last argument, Hello, to this next command. Now that's not available as far as I know of with the VIM bindings, but you don't really need it because that Alt-Period command, really, you could just simply type exclamation dollar sign in the shell. And that actually signifies the last argument, the previous argument. And if I do that, you see Print, F, Hello, that's actually what all that got expanded to. And then of course we get the output from Print, F, Hello, which is simply Hello. So that's really not part of the Emacs bindings or the VIM bindings, the exclamation dollar sign. Again, that's the last argument. Anytime you do that, that is just that last argument, in this case, Hello. The other really big thing you need to know about is exclamation exclamation. So the bang, bang. What is that? That signifies the last command you just ran, which the last command I just ran was not a real command, Hello, and quotations. But if I hit Enter, it would try to run it again. But let me do something that will work, Echo, Hi. And if I do bang, bang, you know, it just repeats that. And that's really interesting and really useful. If you try to run a command and, oh, you know what, I needed to run that with sudo privileges. Well, all you had to do is to rerun that last command sudo bang, bang. And now you rerun the last command itself now, hopefully with sudo privileges. That's really nice, especially if you typed a very lengthy command and then you ran it. Or, hey, you don't have permission and you don't want to have to retype that whole command just to add sudo to the beginning, right? Just do sudo space bang, bang. I showed you how to move up and down the history using the Emacs bindings, how you do this. And the Vim bindings is escape. Make sure you're in normal mode and you go J for down, which is actually the next in the history, K for up, which is actually previous in the history. So it's for me, it seems backwards, but K would go back in the history and J would go the other way forward in the history. Now, one thing when you use the Vim key bindings instead of the Emacs bindings for the shell, you don't have control L typically. So typically to clear the screen, you have to type clear, which is why you guys always see me type clear. It's because control L typically doesn't work. Now, if I do control L, it actually does work even in VI mode. Why is that? It's because I've configured my bash RC, my ZSH RC and my config.fish file. I actually wrote specific functions to clear the terminal and I have them binded to control L. So even though I'm not using the Emacs bindings, I've created this function that actually uses that binding, the control L binding to clear the screen if I want to use control L. Typically though, I just type the word clear. So that's the Vim bindings for the shell. So by default, bash, fish and ZSH, I'll use Emacs bindings, but you can switch all of them over to the VI bindings, the Vim bindings. If you prefer, I showed you in bash set dash O VI, get you the VI bindings. But what do you do in ZSH? Well, let me switch over to ZSH. I'll clear the screen here and ZSH, instead of set space dash O, you do this command here bind key and then you either give it dash E for Emacs, which is the default if you haven't changed it, or dash V for VI for the VI bindings. If I switch over to the fish shell, clear the screen there, fish, you use the following command. You just run this built-in function, fish, default. Well, you can see it kind of wanted to expand it for me. I love the fish shell because it's almost predictive. It kind of knows what you want to type as you're typing it. So the function is fish underscore default underscore key underscore bindings. And that is the Emacs bindings. If you want to switch over to the VI bindings, I'll just up arrow because the command is very similar. Instead, you want to do this function here, fish underscore VI underscore key underscore bindings. And that gives you the VI bindings. Now which bindings should you use, the Emacs bindings or the Vim bindings? It doesn't matter. You can do basically everything you want to do with either set of bindings, but I would definitely try to learn one of those sets of key bindings because it will drastically speed up your workflow if you're in a terminal all the time because you're going to be able to navigate around a lot better, especially if you're entering long strings of text at the terminal. And then you don't want to have to use the standard arrow keys to move the cursor back and forth. That takes so much time now that you can move forward and backward by word or even just hit a hot key and go to the beginning of the line or to the end of the line. Just that simple key binding, the end of the line and the beginning of the line, that saves you so much time. You will be just incredibly amazed at how that simple trick can save you so much time. Now, before I go, I need to thank a few special people. I need to thank the producers of this episode. I need to thank Dustin Gabe James, Matt, Max, and Michael Mitchell, Paul West. Why you bald homie Ellen, Norma Dragon, Chuck, Manderangri, Diokai, Dillon Gregg, Marsha, Mariana, Alexander, Paul, Peace, Archon, Vador, Polytech, Realities for the Red Prophet, Steven, Tools, Devler, and Willie. These guys, they're my high-steered patrons over on Patreon. Without these guys, this episode would not have been possible. The show is also brought to you by each and every one of these fine ladies and gentlemen, all these names you're seeing on the screen right now. These are all my supporters over on Patreon because I don't have any corporate sponsors. I'm just sponsored by you guys, the community. If you like my work and want to see more videos about Linux, free and open source software, the Bashill, right? Subscribe to DistroTube over on Patreon. All right, guys, peace. Control-wide to undo. That's such a weird binding.