Choosing Vim vs MacVim 0:00Before we start configuring our editor, you need to understand the basic movement keys. So I'm going to open up Vim. Or, once again, I like to use a tool called MacVim. It's basically the same thing, but it gives you a nice GUI wrapper where you can do things like CMD-S to save the file. Or, really, you can do a number of things that are a bit more difficult with regular Vim. Now, 95, even maybe more than that, 95% of what I will cover in this series applies to both. Alright, let's get started. Now, personally, for me, I prefer to use MacVim. Really, people kind of split down the middle on this one. Plenty of people would say, no, just use Terminal Vim, that's the way to go.Really, people kind of split down the middle on this one. Plenty of people would say, no, just use Terminal Vim, that's the way to go. And if so, just type Vim, and I almost guarantee it will be installed on your machine. But, yeah, I'm with a group of people that kind of prefer MacVim. It returns some common conveniences. For example, you've probably been hitting CMD-S to save a file for over a decade, right? Well, with Terminal Vim, you can't do that. With MacVim, those common keyboard shortcuts are just implied. And there will be those who tell you you're getting away from the philosophy or the idea of Vim. You know what? The key to being successful with Vim is to mold it precisely to your workflow.And there will be those who tell you you're getting away from the philosophy or the idea of Vim. You know what? The key to being successful with Vim is to mold it precisely to your workflow. It's not about any other person. This is all about what feels good to you. So if you're working along, you can give this a try. Otherwise, just use Terminal Vim. Now, a couple of setup notes here. I'm going to be using iTerm2. And what I did is, you may notice that this looks different from my usual videos. That's because I set up a new user on my system. Setting Up iTerm Look 1:29And what I did is, you may notice that this looks different from my usual videos. That's because I set up a new user on my system. And that way, I can configure Vim from scratch with you. So one thing I would like to do right off the bat is make this look a little prettier. So I like to use a tool called OhMyZshow, OhMyZSH. And if we scroll down, yeah, let's just get this set up very quickly. We'll use curl here. So I will switch to iTerm and paste that in. And you'll see that we have this ZShell RC file. This is very much like your Bash RC file.And you'll see that we have this ZShell RC file. This is very much like your Bash RC file. So you know what? Why don't we use Vim to review this? Or Mac Vim. Either one works. And we'll paste that in, ZShell RC. All right, so we can see a number of things here. We can set a custom ZShell theme. There's a number of plugins we pull in.We can set a custom ZShell theme. There's a number of plugins we pull in. There's really a lot here. And we're not focused on it too much. I just want it to look pretty. So let's go back and see what we can do here. We can enable plugins here. We can select a theme by updating the ZShell theme variable like we just saw. And it says if we open up a new terminal window, it should take effect. So let's try it.And it says if we open up a new terminal window, it should take effect. So let's try it. Command-T to open up a new terminal window. And yeah, now you can see that at least looks a bit better. Further, if I hit Command-Return, that'll make this full screen. Okay, finally, I'm going to go to my preferences and tweak just a few things. I like the text to be a bit larger. I'm going to change the font to something like 18, way too high, 14. All right, that'll do the trick. I actually configure mine quite a bit more, but this will get us going. Creating a Practice File 2:56All right, that'll do the trick. I actually configure mine quite a bit more, but this will get us going. So now I'm going to make a directory called practice. And let's cd in there. And now I want you to note if we open this up in Vim, this is what we get. Or again, you might get a white background. If I escape out and you do MacVim instead, it's going to give you some defaults out of the box, just so it's not incredibly overwhelming. For example, if I were to hit colon W, I'm going to save this as practice.php.just so it's not incredibly overwhelming. For example, if I were to hit colon W, I'm going to save this as practice.php. Now I can hit the letter I, and that's going to switch me to insert mode. And I can start typing PHP, and I at least get a little bit of syntax highlighting out of the box. Now really, though, we're going to configure every bit of that, so don't worry. Now once again, if we go back to Vim, I'm going to hit colon E for edit, practice.php, or I can hit PR tab, and we get some autocompletion there. Now notice in this configuration, we don't have any of those settings set, so we'll have to do that manually.Now notice in this configuration, we don't have any of those settings set, so we'll have to do that manually. So what I might do here is work at the beginning in Vim, and then once you figure out how to configure everything, I'll switch over to MacVim. And that way we don't get any confusing overlap there between how it looks on my computer versus yours. Now let's do a quick recap. We've already learned actually quite a bit in just these opening minutes. It looks like when I hit colon, that switches me to like a command mode where I can trigger any number of commands.It looks like when I hit colon, that switches me to like a command mode where I can trigger any number of commands. For example, colon W, write, that means write the file or save, colon Q to quit, and colon E to edit a file. And also we learned that we have tab completion in this mode, which is useful. And of course you can do things like colon PWD, and this will spit out the current working directory or present working directory. Now you might be familiar with colon WQ, and you probably do this all the time. If you're not familiar with Vim but every once in a while it opens and you want to escape out like this, well, now you know what that means. Opening and Using .vimrc 4:53If you're not familiar with Vim but every once in a while it opens and you want to escape out like this, well, now you know what that means. We're just saying write the file and then quit to the terminal. Okay, so try to memorize those. I'd even go as far to recommend keeping a little notepad by your laptop where you can write things down. Okay, so here's where you're going to live for probably the next month. I'm going to edit the file, and I'm going to go into my home directory and open up .vimrc. But you know what, before we do that, let me escape out,and open up .vimrc. But you know what, before we do that, let me escape out, and if we switch to my home directory, I'm going to list the files, and this will include hidden directories as well. Okay, I want you to note that you'll have this .vimrc file in your home directory. This is the only place you'll find it. This is where you can configure every ounce of Vim. So if we go back to our practice directory, type vim, colon E, open up .vimrc. Do note that I'm referencing the home directory here. Out of the box, it's going to be empty. Learning Modes and Movement 5:51Do note that I'm referencing the home directory here. Out of the box, it's going to be empty. We have zero configuration. Why don't we add a couple configuration lines here to start? Now, remember how if I type N or M, I don't see anything, and it looks like I have this block cursor here? That's because I'm within normal mode, and that's a very important thing to understand about Vim. You have several different modes. For example, if I type I, I'm now in insert mode, represented at the bottom.You have several different modes. For example, if I type I, I'm now in insert mode, represented at the bottom. Now if I hit escape, I've exited insert mode, and I'm now in normal mode, and this is good for maneuvering. So for example, hit the letter H, and the cursor goes left. Hit the letter L, and it goes right, and if I were to add more text here, I'm going to hit escape to go back to normal mode. If I hit K, it'll go up, and if I hit J, it'll go down. Now you might be thinking, well, why can't I use the arrow keys? It works when I do it. I'm hitting down here.Now you might be thinking, well, why can't I use the arrow keys? It works when I do it. I'm hitting down here. Well, the point is, with Vim, you shouldn't have to reach your hand all the way to the bottom right portion of your laptop, if you have one, to hit those arrow keys. Instead, you never have to move your right hand, and just take a look at your right hand as you do this. I'm maneuvering, but I never move my hand. I can keep them in resting position. All right, so once again, J to go down, K to go up, L to go right, H to go left.I can keep them in resting position. All right, so once again, J to go down, K to go up, L to go right, H to go left. H, J, K, L. Now what about visual mode? For example, if I want to select everything and delete it, hmm, that didn't work. Notice I select it, I hit delete, it doesn't work. And this is what drives people crazy about Vim, and it's because they don't really understand it. Don't try to apply everything you know from Sublime or TextMate over to this.and it's because they don't really understand it. Don't try to apply everything you know from Sublime or TextMate over to this. However, having said that, remember when I noted that MacVim returns some common things you're used to? Well, that would be one of them. With MacVim, I can select this and hit delete. Anyways, if I hit the letter V, this moves us to a new mode represented down here, visual mode. So now let's use L to go right, and notice this time we are visually selecting the characters.So now let's use L to go right, and notice this time we are visually selecting the characters. And if I want to undo that, I just hit escape. Now let's hit capital V. Well, we're in visual mode, but now we're selecting by lines. So right off the bat, capital V selects the entire line. Now, if I hit J, we will select down, and K, of course, to go up. Okay, so let's select all of this, and now I'm going to hit the letter D. It deletes it.Okay, so let's select all of this, and now I'm going to hit the letter D. It deletes it. So notice this is what I mean when I say it's a language, and it really makes good sense. If I want to visually select something, I hit V, and I select it. If I want to delete something, I hit the letter D to delete it. This may seem overwhelming and unnecessary, but trust me, if you stick with it, you're really going to love it. You don't find many people who took the time to learn Vim only to say, yeah, it was a complete waste of time. Adding Vimrc Configuration 8:47You don't find many people who took the time to learn Vim only to say, yeah, it was a complete waste of time. Most people become members of the converted very quickly. Okay, so we're in our VimRC file. How can we add configuration? Well, let's try this. I'm going to write syntax enable, and this means I want to enable syntax highlighting, and then I want to set a color scheme. But which ones are available out of the box?and then I want to set a color scheme. But which ones are available out of the box? Of course, we can download hundreds, but out of the box, I'm going to go to command mode by hitting colon color scheme, and now I'm just going to hit tab to cycle through the auto-completion. Why don't we just go through, how about desert? So I will type color scheme, desert. Let's save that, colon W, to write the file. And now here's the next important thing to note. We've updated this configuration file, but we haven't sourced it.And now here's the next important thing to note. We've updated this configuration file, but we haven't sourced it. We haven't really read the file. So I could type colon SO for source, and if I hit percentage, that just means the current file. You could even do this outside of Vim. So let's source our VimRC file, and instantly you can see we have syntax highlighting. It doesn't look that great to me, but we'll learn more about that later. So let's edit, practice.php.It doesn't look that great to me, but we'll learn more about that later. So let's edit, practice.php. Yeah, once again, somewhat ugly syntax highlighting, but it's way better than we had before. So let's hit I to go into insert mode, class foobar, escape, and that brings me back to normal mode. And it's important you do that, because if I hit I once again, I'm in insert mode. If I want to go up, well, K is going to insert the letter K.I'm in insert mode. If I want to go up, well, K is going to insert the letter K. So I want to switch to my normal mode, so that K actually means move up. Escape, K, and now I go up. But now what if we just want to delete? For example, I will return to insert mode. I'm going to hit the delete key, and nothing happens. Once again, this is another thing that just, it baffles people. And that's because really Vim doesn't include much configuration out of the box.Once again, this is another thing that just, it baffles people. And that's because really Vim doesn't include much configuration out of the box. All you have to do is prepare your VimRC file one time, and then you're good to go. So in this case, yeah, it's weird. Backspace doesn't work like I'm used to. Well, let's add some configuration for that. Now remember, for any given task you want to accomplish, just Google it, and there will be some kind of configuration you can reference. But I happen to know what it is.just Google it, and there will be some kind of configuration you can reference. But I happen to know what it is. So I will hit edit, VimRC file, and notice in this case it's not letting me because we haven't written the file. Okay, colon W to write, and now once again, I can type it manually, or I can hit up, and that will cycle through my previous commands. Now we're switching. Okay, so I'm going to set a configuration item. Set, backspace, and we'll set this to work with indentation, the end of the line, and the start of the line,Set, backspace, and we'll set this to work with indentation, the end of the line, and the start of the line, and that way it'll work with line breaks and things like that. Okay, so colon W to write the file. I'm going to source the file, and now if we go back to our practice file, and I type I, backspace, you can see that now it's behaving just like any other editor. And really that's how it goes with Vim. You find something that seems awkward, it doesn't do what you want it to do,And really that's how it goes with Vim. You find something that seems awkward, it doesn't do what you want it to do, you add a configuration item, and then you continue on. And then after a month or so, you will have configured this editor precisely to your taste. So in this case, I'll hit escape to go to normal mode, and U to undo. Once again, makes sense, right? It's a language. U to undo, W to write, Q to quit, E to edit.It's a language. U to undo, W to write, Q to quit, E to edit. But it goes way farther than that, and as you'll find, maybe lowercase V and uppercase V means different things, and that's where people get overwhelmed. But just take it one step at a time, learn a few new things each day, and you'll be there in no time.