This course covers vimrc files, which allow you to define various presets so that your favorite settings and options are already set up in Vim when you start using it. You'll also learn how to configure vimrc files with customization such as color schemes. A guided exercise will give you a hands-on tutorial of using vimrc files and you can follow along to put your skills into practice.
This course is ideal for anyone who needs to edit text files in a command-line environment.
If you want to follow along with the exercises in this course, you should have the Vim text editor installed on your computer.
If you want to follow along with the exercise(s) in this course, you can find the necessary resources here.
Okay, so we were just talking about color and making Vim easy to read. So along those lines you can specify a color scheme. By default Vim includes several color schemes. You can configure this with a color scheme command. You can also see the available color schemes by typing colon, color, space, and then just hitting Control + D. By the way, you can use colo or color for the shortened version of the color scheme command. Just try them out and see which one is your favorite. The default one is fine for most people really. Personally, I like slate. So I'm just gonna tab over here until I get to slate and then press enter to use that color scheme. And if you wanna configure this in your file here you can just do color, colo or color or color scheme, and then specify the name, for example, slate. If you wanna add a custom color scheme just search the internet for Vim color schemes. There are plenty of resources out there, and there are tons of different color schemes to choose from. How you install a custom color scheme is to create a .vim directory in your home directory and then a directory under that called colors. For example, on Linux, it might be something like this, home/jason/.vim/colors. And then on Mac it would be something like, users/jason/.vim/colors. Now what you would do is just place the color scheme file in the colors directory. And so now when you restart Vim you'll see the additional color schemes available to you as an option. So far we've covered the set command and the color scheme command. Next up is the map command. The map command is used to change the behavior of typed keys. Set another way, a mapping allows you to bind a set of Vim commands to a single key. The most common uses to define a series of commands for a function key. The function keys are typically on assigned by default. The format of the map command is map, followed by the key you want to map, followed by the key strokes that you want to be typed when that map key is pressed. For example, we could assign F2 to insert your name and address. To do that, you would use something like map, opening bracket F2 close bracket, lowercase i to enter insert mode, John Smith bracket CR and that represents a carriage return or an enter key, 123 main street, CR Anytown, New York CR, and then ESC close bracket. That is very similar to a macro. Here's a list of characters you can use with a map command. So if you need to use the backspace key in your mapping you would use bracket BS closed bracket and so on. If you wanted to map Control + D for example you would use open bracket or less than sign there followed by C, followed by a dash, and then D finally closed with a greater than sign or closing bracket. Mapping control E would be brackets C dash E closed bracket and so on. Let's say you happen to edit a lot of HTML documents and you want a quick way to get an unordered list started. Let's map that to F3. So we'd use the map command F3. The key we're going to press and now the commands that will be typed when that key is pressed. So we'll enter insert mode with I, use UL, CR is the enter key, space, space, LI for a list item and then a closing list item tag, another enter, and then we'll hit escape to get out of insert mode, zero to go to the beginning of the line. Already enter insert mode, and then the closing UL tag, and we'll hit escape again here. And then we'll do k to move up one line, c to start a change command. And IT, which is the text object for an enter tag. Now let me hit escape to get out of my insert mode here and write this change to the file. I actually want to demonstrate this and let you see what that looks like. So you already know that the vimrc stands for Vim run commands and what we can do is exit out of Vim, restart Vim, and these maps will be in place because all these commands are gonna be read in. However, you don't have to exit out and come back in if you want to. You can actually use a Vim command which is colon source or SO for short and then you can provide a file. And this source command will execute all those commands in that file. And of course, we want to do that with a file we're editing which is our dot vimrc file. So I'll do that here now, vimrc and press enter. Now all of our commands are read into our current configuration here. So I'm just gonna hit O and insert a couple of lines at the bottom here. And now I'm gonna hit F3 and show you what happens. Now you can see what pressing F3 does and it puts us right into insert mode right there in the middle of this list item. So I can say thing one, for example, press escape, and now I'm out of insert mode. I'm just gonna hit U to undo that. So now let's say you want to quickly add other list items. So now let's map F4 to something like this. Go up a couple of lines here and just put these together. We want the F4 command to do this. We want to make sure that we are in normal mode by pressing escape. O, which is going to start a line below us. And then we're just gonna type LI, closing LI and then we'll get hit escape here. And then we'll use the change command with CIT, which means change enter tag. Let me save this to the file. I'm gonna hit S and press up to use my history that I love so much and resource in this file, hit enter and now the F2, F3 and F4 commands are available to us. So I'm gonna move down here to the bottom of the file with J let me press function three or F3 and then let's type in thing one. And now when I'm done with that I'll just hit escape, then type F4. We get another line thing too. We'll hit escape there and do F4 again and now we get thing three and so on and so forth. So you can see how these mappings come in handy and how you can make them do pretty much anything you need or want it to do. I'll just hit you a couple of times here to undo that change. Now, it's important remember that when you're remapping keys you can actually override existing Vim commands. If you wanna do that, that's totally fine, you can do that. I don't typically recommend that, but again, Vim is powerful and lets you do pretty much anything you want. As you already know, Vim has many, many commands. So it can be hard to find a key that doesn't already have a purpose. In order to get around this challenge, Vim provides what it calls a leader key. By default the leader key is the backslash. What this does is gives you your very own namespace that won't collide with any existing Vim commands. So many people start their custom mappings with the leader key. For example, if you wanted a quick way to save a file you could create a mapping like this. We'll do map leader W two colon W exclamation mark enter. So let me do that. And also let me just source in this file, press enter. And so now if I were to type backslash W, colon W exclamation mark enter will be executed and your file will be saved. If you didn't use the leader in this example then you would have overridden the W command which you already know moves forward by a word. By the way if you don't like the default leader key at backslash you can change it with this, let map leader equal and then put whatever key you want a comma, for example and enclose that in quotes. Make sure you place this statement before the special leader variable otherwise mapping will use the default leader key. So let me do that. Let me just DD this line and do a capital P to paste it above the command there. And then I can use W here to write our changes and I will source this file again. And now when we type comma W, the command colon W exclamation mark enter was executed. You can use the map command by itself without any arguments to see the current mappings. That's a quick introduction to mapping but that takes care of most people's needs in that area. Of course, if you want to explore more on this topic then you can check out the help with colon H mapping and pressing enter there. So that's how you can create a vimrc file by hand. However, you can also just get everything configured the way you like it while you're using Vim and then use this make vimrc command to write out your settings to your vimrc file. The mkvimrc command writes out all the map and set commands in such a way that when these commands are executed the current key mappings and options will be set to the same values. Let's use it now with colon M-K-V-I-M-R-C and press enter. It gives you a warning that you already have a .vimrc file. If you want to overwrite that file then you have to use an exclamation mark at the end of that command to force it. By the way you can use this command make vimrc and provide another path so you can see what it's going to create before you overwrite your file. For example, I could just say this test vimrc and hit enter, and that file is created. But I'm actually gonna overwrite my vimrc file. So let me do this mkvimrc exclamation mark to force the overriding of my .vimrc file. Even though you didn't see anything change what happened is actually this .vimrc file that I'm currently editing was actually replaced. So I can reload it by typing colon E and pressing enter. Just gonna hit GG here to go to the very top of the file. And it's pretty obvious right off the bat that we didn't go over all the commands you see here. But of course you can always look up the help for each one of them if you want to. Some of them actually come from the system-wide vimrc file and other lines are created by the mkvimrc command itself. Quickly I'm just gonna go through a few of these. First off V map is key mapping for just visual mode. Meaning that that particular mapping is only valid in visual mode, not normal mode, not in command mode. In map is key mapping for just normal mode. By default key mappings are recursive. So if you use a mapped key in another key mapping it gets expanded there as well. What noremap does is disables that functionality. It essentially disables recursive key mapping for that map. So Vnoremap is the visual version of noremap. And nnoremap is the normal mode version of noremap. Let me jump to the bottom of this file with shift G. Here at the bottom of the file you can see several set commands. However, the very last line of this file is what's known as a mode line. What you can do is embed Vim settings within a file at the very bottom or the very top of that file. You place these mode lines as a comment. For vimrc files commented lines begin with a double quote. If you were doing this in another type of file, that could be a pound sign or something else. Here the mode line is at the very bottom of the file. And what it's telling Vimm to do is execute the set FT equals Vim command. FT by the way is short for file type. So it forces Vim to treat this as a vimrc file type. You might wanna do something similar to this. For example, if you're editing a C source code file but it doesn't end in the normal dot C extension, you can force them to treat it like a C source code file with set FT equals C. The syntax of the mode line is important. White space has to come before vim. Then you use a colon followed by the set command and then in the set command with another colon. If you wanna see more information on this you can type colon h mode line. I wanna point out that your vimrc file doesn't have to be anywhere near this complicated. Just use it to set whatever defaults you like and find useful. I like to make things as simple as possible. So my personal vimrc file is really short. Typically I use a dark terminal, one like I'm doing here to teach. So I set the background to dark. I also like the slate color scheme so I just set that there as well. I enjoy having the extra help wild menu provides and finally I like to see my cursor position at all times. Now that's a total of four lines. That's it. That's really all I need. Again, you don't have to get carried away with all these crazy options. Just keep it simple and use what makes Vim work well for you.
Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.