Lesson 11 of 26

Useful Vim Commands Reference

Duration: 18:06 Watch on YouTube (external link, opens in new tab)

Markdown version

In this lesson

Roy walks through the ViEmu (external link, opens in new tab) Vim cheat sheet: the colors (green for motions, yellow for commands), the commands in red that end in insert mode, and the keys marked with a dot that need a character argument, such as f and t. He covers motions such as w and W, e, zero and dollar, star and hash, and commands that stay in normal mode, such as r, x and the dot command that repeats the last action. He also shows commands that end in insert mode, such as Shift-C, Shift-A and o, and compares typing Vim commands one key after another with Emacs chords.

Transcript

3,008 words

[00:09] (external link, opens in new tab) Vim shortcuts. I mean, we've we've discussed some of the basic stuff, but I wanted to give you a real overview of all the different types of shortcuts and and commands and the differences between them. And when you start just looking for Vim shortcuts and like basic maps of how they work, there is a bunch of stuff that comes up in Google. One of the best ones is this one which is from the ViEmu (external link, opens in new tab) website and it looks very very loaded. But what I thought I'd do in this video is to go through exactly what this contains and the different types of commands and all the information.

[00:46] (external link, opens in new tab) There's a lot of information here, but if you have this and you understand how to read this, this will be invaluable. You just print it out. You put it on the wall or right next to your keyboard. This will definitely get you a lot of the way there in in making sure that you feel more comfortable in Vim and understand what it is you can and cannot do. But this is too loaded. So what I'm going to do, I'm going to go through each part of this and explain what it is. So first of all, let's just clean things up.

[01:19] (external link, opens in new tab) One of the things that has nothing to do with everything else is kind of this little cheat sheet right here in the middle of this document. And this talks about the main command line commands, right? The ones that we use to write and save, edit files, etc. It also talks about substitution, help commands, redo, etc. So just out of the out of the gate, make sure that you understand all of these commands and practice them continuously until they kind of feel second nature.

[01:52] (external link, opens in new tab) The next part is the left part. Left part basically explains the different types of things that are mentioned in this map. And we have, you know, the main three things, the main three modes that we have in Vim are motion, command mode, and basically operations that we have. There's also visual selection, but that's not mentioned here. What's mentioned here are basically what the keys do on the keyboard. So, the keys can either move things around or they can execute commands or they can use operators that are basically

[02:26] (external link, opens in new tab) Metadata that is provided to a command. And then there's a few extra functions. And then the colors match so that when you see a green something on the keyboard, you know that's a navigation. And yellow is command. So usually what happens in Vim is that you press one of the yellow keys in and then after you'd press a green key. So you would say change word, right? you would say CW because usually commands will take

[02:59] (external link, opens in new tab) Parameters like you want to change but what it is do you want to change? So we have navigation and we have commands and we connect all these things together. So we have motions, commands and operators and we're going to go a bit deeper very very quickly. If we break down the commands, you'll see that some commands are different than others. For example, some commands, if you look at the full thing, some commands have red letters and some don't.

[03:30] (external link, opens in new tab) The commands with red red letters are commands that will actually switch you from normal mode into insert mode at the end of the command. So, if I type shift R, that brings me into insert mode. Then I can actually start typing. If I type shift I, it brings me to the beginning of the line and then I'm in insert mode. I can type. If I type shift O, it actually creates a line above, moves to it, and I'm now in insert mode. So these these two types of commands are very important.

[04:01] (external link, opens in new tab) There's commands that bring you into insert mode and ones that don't. And I'll have a separate slide for that. Then at the bottom left of the chart, you'll see that it mentions that some commands have the dot next to them. And the dot basically means that they require a parameter or a character argument. For example, if you press F, it means you're trying to find something. So L means find the next character and shift F L will find the next L character, etc.

[04:32] (external link, opens in new tab) Or if you want to move with a green right here to a specific just before a specific something, you say t and then that something and then it will move one character before that something. So everything with a dot for example I want to play a macro I press the add key and then I have to give it the parameter what is the macro location a letter or a number etc etc. And then there's one more thing here at the bottom which is explains words and uppercase words.

[05:04] (external link, opens in new tab) If you remember we have words navigations in in here you have the word navigation and word basically means you can have uppercase or lowerase. Lowerase word basically means alpha numeric word. So if you have a dot at the end of a car at the end of the word moving pressing small W will move to the dot and then uppercase W or uppercase words basically mean everything that's not whites space.

[05:35] (external link, opens in new tab) So if you press shift W you're going to move to the next whites space. You're going to move past the dot as well. So that's shift shift W is inclusive and W is uninclusive of non-alpha numeric characters. The only thing that matters for shift W is whites space basically. Let's see. The next thing that's mentioned in the chart is the the numbers here. And then and and I and I highlighted all the c all the keys on the keyboard that have these specific

[06:12] (external link, opens in new tab) Numbers next to them so that you can see exactly what I mean what they're supposed to mean. For example, in here number three means do this twice so that you affect the whole line. So if I press shift smaller than smaller than at the same time, it will affect the whole line. And here DD has number three here. So it affects the whole line. If I want to copy the whole line, again I have three here. And number one says use X before a yank paste if you want to use a register.

[06:43] (external link, opens in new tab) And I have a separate video on registers, but basically everything that has a one here will allow me to use a specific register. For example, I want to paste from a specific register or I want to yank to a specific register. And this is how I work with registers. We have number four here, which is basically these special commands that are Z related. They kind of have nothing else to do except Z. And then we have scrolling cursors. This is kind of like minutia, but just I want you just to understand what's going on here.

[07:19] (external link, opens in new tab) And we have extra commands with GG like go up and shift G to go down. It's not mentioned here. That's I I hid that because it didn't have a u a number associated with it. Now let's talk about motions. Remember that from the main part of the keyboard most of the keys and most of the time you're going to do a lot of motions. That should be your second nature. These are all the motions on the map. And here again we have words and the the we have motions with alpha numeric and we have motions with just everything that's non whitespace.

[07:55] (external link, opens in new tab) So it only applies to specific words. We have the e which basically leads us to the end of a word or to the end of a lowercase word. We have a two or till which brings us just before a specific character. We have an f which finds one character back. And this one, notice that uppercase in this case brings us back and lowerase moves us forward. Finding a specific character and moving to it. This is finding a specific character and moving right before it.

[08:28] (external link, opens in new tab) Moving to the top of the screen. These these should be very familiar by now. Moving to the previous word. Moving to the next find when we're searching with the slash or question mark. We can reverse our movements as well. And we have a couple of things we did not talk about yet in this course. So if I press dollar, I go to the end of the line.

[08:59] (external link, opens in new tab) And if I press POW, I go to the beginning of the line. So, B and E, end of line, beginning of line. And if I press star, I'm actually searching for the next instance of the word. If I press hash, I come to the previous instance of the highlighted word. These are movements that basically allow me to move full full paragraphs, full sentences.

[09:29] (external link, opens in new tab) And this also depends on how the syntax and the type of syntax in the file. Now, it's kind of hard to remember this. There's a different chart that I found online that that you can get in this URL at the bottom. And what it does is that it kind of shows you the general direction of where movement will take you and how far it will take you for. So, for example, GG will take you all the way up and shift G will take you all the way down. And the bigger the movement, the farther away it will be from the center.

[10:03] (external link, opens in new tab) So, L, J, K, H will move really, really close to just one character. And then E will move a word and W move award and B will move a word, but back. And then we have shift T versus T before and after. So, there's movement lateral up and down as well. Zero will bring you to the beginning of the line. Dollar will bring you to the end of the line. control pow will bring you to the beginning of the line etc etc. So it's I think it's much easier to memorize things this way.

[10:36] (external link, opens in new tab) It basically tells you how fast you can navigate with different motions. So it's a very very easy way to remember things. Feels very pragmatic to me but it's still the same thing as this. Now let's talk about commands. We have two types of command like I mentioned and in the chart the two types of commands are red ones and black ones. So regular commands are what I call full circle moves or kind of an air strike move in that you are navigating you have a motion right above the document.

[11:12] (external link, opens in new tab) You execute a command something changes but you go back into motion. You never go in and start typing in the document. you just say replace all the A's with B's or delete this sentence but you still are in motion mode after that. So you run a command and you go full circle and then basically you just continue moving. And I find that these are this is one of the powers of Vim is that you're able to just kind of look at the document kind of like a through a glass

[11:46] (external link, opens in new tab) Floor and you say just pull this out, change this, do whatever and without actually touching the document. It's very very fast. But sometimes, and this is another nice thing in Vim, is that you want to use a motion to change things and immediately start typing instead of them or on top or before. So motions plus commands that end in insert mode are also very powerful. And this is a very common theme. For example,

[12:16] (external link, opens in new tab) These are some of the commands that we can do that are combined of both. These are the full circle moves. So, for example, I can press R and then there's a dot here. So, I can say R I to replace anything that's underneath my cursor with the word I. I can P for paste. I can press shift K to get the help on the current word that I have. I can press U to undo, Y to yank, etc.

[12:47] (external link, opens in new tab) X to basically sorry small X to just delete the character and shift X to backspace without actually being in insert mode. V basically moves us into selection mode. Equals gives us auto format. This gives us indentation. Dot repeats the last command. So for example, if I did DD and then I move to a different line and I press dot again, it will just repeat DD on that line. So this is a very powerful way of just repeating something very very quickly.

[13:18] (external link, opens in new tab) It's kind of like a very very short macro that just does the last action you just did in Vim and always remembers only the last action. We can toggle case here. External filter actually allows us to run shell commands as if we're in the command line in in after the colon. So if I type colon exclamation mark, I can basically do ls or or move or anything as if I'm back in the command line and I'll see the output inside the shell and when I press escape or q I'll go back inside vim.

[13:55] (external link, opens in new tab) Escape moves us into normal mode. So all these things they basically keep us in motion mode right normal mode. And these actions are very powerful because they save us a lot of time and grief. For example, I use this one a lot. Sorry, this one a lot. I'll go to somewhere in the middle of a sentence or just before the end or just the last couple of words and I want to change them. I'll press shift C and then it will basically delete everything from this cursor to the end of the line.

[14:26] (external link, opens in new tab) Then I can just start typing instead. I don't have to press insert or anything. I'll just I'll be navigation say from this on just split everything just remove whatever you have to the right I just want to type there and C will take a specific movement so if I want to change the in the middle of a sentence I will just want to say change the the next three words I'll just say C3 and I immediately start typing and those words are already deleted if I want to start typing at the end of a sentence but I'm not at the end of it I'm in the middle or at the beginning, I'll just press shift A and I'll start and I'll be in insert mode at the end of the sentence.

[15:06] (external link, opens in new tab) If I want to just append one character after the character I'm currently on, then I'll just basically press A and S and shift S and S are substitute characters. And I might do a small video on that. I don't really use this too much. Shift I will bas basically bring you into insert mode at the beginning of the current line even if you're in the middle of it. And insert mode will bring you to insert wherever you are currently. Shift O and O opens lines and moves to insert mode above and below the current line that you're in.

[15:41] (external link, opens in new tab) So these are the commands that will bring you into insert mode from normal mode but also do something on the way. And then we have Z. A lot of people would use ZZ to actually scroll and and and come to a to the middle of the file. Scroll the file to the middle of it. ZT will score to the top and ZZ will center. And some people will just map J and K to also do J and then ZZ because

[16:12] (external link, opens in new tab) It makes the cursor always in the middle no matter if you're moving up or down. It's pretty awesome. and Z shift Z shift Z will actually save and quit just like saying write quit and ZQ will quit without saving. So that's it for understanding the basic commands and how they work and how they fit with each other. Again, you'll use yellow and then green and usually it's it's a it's a combination. And it the nice thing about Vim, a lot of people say is that if you're using stuff like Emacs, you're writing chords with your hands.

[16:47] (external link, opens in new tab) You have to use multiple fingers at the same time. With Vim, usually it's one finger and another. It's like kind kind of like strumming on a guitar. Maybe you do one one string at a time or it's kind of like typing only with two fingers on piano. So I'll find myself usually doing CW. It'll be like ta. And so you'd always feel like da da da da is it kind of feels like a very quick succession of very small commands that are connected to each other.

[17:18] (external link, opens in new tab) Kind of like the way that shell like bash works in in Linux and Mac is that there are these small tools and they're connected to each other and one takes the input of another and outputs something that another one takes. So they all work one after the other. So you type them one after the other as one key, one key and then another and then another. While in stuff like Emacs, you have to type all of them together which changes the meaning. So that's it for this section. And don't forget to print it out and and and try to repeat and con consistently look up if you're trying to do something with the mouse or with the arrow keys, stop, look at this, and say, "What?

[18:00] (external link, opens in new tab) How can I do what I'm trying to do using these kinds of commands?