image
Vim Settings, Preferences, and Customizations
Vim Settings and the Vimrc File – Part 1
Difficulty
Intermediate
Duration
40m
Students
120
Description

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.

Intended Audience

This course is ideal for anyone who needs to edit text files in a command-line environment.

Prerequisites

If you want to follow along with the exercises in this course, you should have the Vim text editor installed on your computer.

Resources

If you want to follow along with the exercise(s) in this course, you can find the necessary resources here.

Transcript

- [Instructor] At various times throughout this course we've briefly touched upon Vim options when appropriate, for example, way back near the beginning of the course when you were introduced to the control G command, we talked about how the ruler option could affect what is displayed by that control G command. Now, in this lesson, you're going to learn how to make Vim, start with your favorite options and settings. This way you don't have to manually configure a setting you use often each and every time you start Vim thus saving you time and allowing you to make Vim behave the way you want it to behave. You store these options and settings in your vimrc file. By the way, having files that end in RC is a Unix and Linux convention. RC stands for run commands. So each time Vim starts it runs the commands in the vimrc file. There's a system-wide Vim vimrc file that will be executed each time anyone on the system starts Vim. Additionally each individual user can have their own vimrc file. This is where you wanna put your customizations. It's a good idea to just leave the system-wide vimrc file alone and let the Vim installation process put the default file in place. This also explains why sometimes you can see something different than I'm demonstrating. Each version of Vim has a slightly different set of system-wide defaults. If you're using Unix, Linux or a Mac your vimrc file is located in your home directory and is named .vimrc. The tilde you see on your screen expands to the current user's home directory. If you're using windows on the other hand then your vimrc file is located in your home directory but its name is _vimrc. You may or may not have a vimrc file already, by default you won't, but if you're working on a system where someone else set up your account, then maybe they've placed a vimrc file in your home directory as part of the user creation process. You can also use the version command to see where the system-wide and user-specific vimrc files are located. Let me demonstrate that here, I'll just type :version and press enter and we'll just hit the space bar to keep scrolling down. Now toward the bottom of your screen you can see where the system-wide vimrc file is, as well as the user vimrc file. We'll be focusing on our own user vimrc file so we can control our own Vim experience. Each line in the vimrc file is executed as a command. Set another way, it contains a list of commands as you would type Vim after a colon. For example, a line in a vimrc file that reads set ruler, is the same thing as you typing :set ruler enter when you're in Vim. However, the commands in the vimrc file are not proceeded with a colon. Before we jump into creating a vimrc file let's take a closer look at one of the commands we'll be using a lot in the vimrc file, and that command is set. When you type :set and press enter all the options that are set at something other than their default are displayed. If you wanna display the value of an option use :set optionname followed by a question mark. For example, let's say you wanna know if incremental searching is enabled or disabled. To do that you can type :set incsearch? and press enter. If it returns incsearch the option name, that indicates that it's turned on. If it was disabled, as in this example on your screen it would read no followed by the option name. And here you can see no inc search is displayed on our screen. Let's check another setting. Let's see if search highlighting is enabled with :set hls? and press enter. It displays no hl search indicating that the hl search option is disabled. By the way, hls is the shorthand version of hl search. So we can use it or the complete option name to manipulate it. Let's look at the help for this option by typing :h hls and pressing enter. The help says it's a boolean. This means it's an option that is either on or off. To turn on a boolean option, use :set, followed by the option name and press enter. So let's turn on search highlighting with :set hls and press enter. Now, when we query at status with :set hls?, it displays hlsearch because it's enabled. Let's disable it with set nohls and press enter. Now when we check it, it confirms that it's turned off. So when we do set hls?, it says no hl search. If you wanna toggle a boolean option you can simply append an exclamation mark to the end of it. So we now know that search highlighting is off, and if we type :set hls! then it toggles it on. Let's check it here with set hls?. Now, if we run that command again, :set hls! enter, it toggles it off. And of course we can check it with :set hls?. Sure enough it says no hl search. Some of Vim options are actually assigned a numeric or string value. For example, let's look at the history option with :set history? and pressing enter. Vim, tells us that it's set at 50. Let's look at the help for this option. I'm gonna type :h history and hit control D. You can see several choices here. The one that we want is actually surrounded in single quotes. Using single quotes jumps to the option documentation. For example, let's just hit enter on this line :h history. It says the command lines that you enter are remembered in a history table. You can recall them with the up and down cursor keys. So this has helped about history in general. What we want is the history option. So let's go there with :h 'history' and then press enter. Now we're looking at the documentation about the history option and this option tells Vim how many items to store in its history. So let's set it to 500 with :set history=500 and press enter. Now let's type a colon. And if you wanted to step back over the commands in your history, one by one you would just keep pressing the up arrow like I did here. You can actually narrow your search by typing a partial command line, and then pressing the up arrow key to go back through your history displaying only what matches your partial command. Let me hit escape here. For example, let's type :set h and press the up arrow key, that takes us to this match but we wanna keep going so we press up again. And now we're looking at the command we want so we just press enter to execute it. It confirms that we did in fact, set history to 500. If we look back at the history option help it says the default value of history is 50. If you wanna return an option to its default value upend an ampersand to it like so. So we can do :set history followed by an ampersand and press enter. Now, when we check the value of history :set h and backup a couple of here and press enter, it returns the value of 50, which is the default setting. Now, if we run the set command just by itself and hit enter you'll notice that history is missing from the list. And that's because the set command alone displays options that differ from their default value. So now what I'm going to do is create a vimrc file for my user. I'm gonna create it from scratch but later I'll show you how to create a vimrc file with a make vimrc command. You can follow along with me now if you like, or you can wait to practice this on your own. I'm on a Linux system so I'm going to create a .vimrc file. Now, if you're on windows you're going to use _vimrc. First I'm gonna close out of the help window here with :q and press enter. And now what I'm going to do is use the E command or edit command to open my .vimrc file. I'll just say :e. And if I'm in my current home directory I can just type .vimrc. Remember I'm on a Linux system that works, but I can also be more explicit. I can use ~/.vimrc. And by the way tilde represents your current home directory, and the forward slash is a directory separator. And of course .vimrc is the file we want to edit. And by the way, even though these are Unix and Linux conventions, these tilde and forward slashes this will actually work on a windows system. However, remember if you're on a windows system you wanna use _vimrc. Again, I'm on Linux. I'm gonna use .vimrc and I'm gonna press enter to open and create that file. You already know that you can use control G to display the file name you're working on. You can also supply a count that is greater than zero to control G, to force it to display the full path to the file. So if I type one control G, it shows ~/.vimrc. Again, remember that on Unix, Linux, and Mac the tilde represents your home directory and the forward slash is the directory separator. If you wanna be explicit, you can do something like :e space home, jason, in my example 'cause my account is Jason .vimrc for example. I'm just gonna hit escape though because we already have our file open for editing hit escape to abandoned that command. So at this point, I'm just going to start going over some commonly used Vim options. There are tons and tons of options and you can configure Vim to do almost anything you want. When I last counted the options there were something like 379 different settings. By the way, you can view those options by typing :h option-list and pressing enter. I'm just gonna hit control F here and go down a few pages and you can see that there are tons and tons of options. Again, I think there's something like 379 options in this particular version of Vim. I'm gonna close out of this with :q and hitting enter. And by the way there's another way you can view these options and that is by using the options command. So I'll do that now, :options and press enter. This opens up a window with the list of options and brief descriptions of each one. Now, don't worry I'm not gonna go over these 300 plus options here. I'm just gonna go over a few that I think you'll find really helpful. Of course use what you like and leave the rest alone. Now I'm gonna close this window here with :q and press enter. Let's start out with history. I personally like to keep a large command history. So I'm going to overwrite the default history value of 50 with set history=1000. So let me go into insert mode with I, type set history=1000. Now I'm gonna hit escape here to return to normal mode. Now remember that the commands in the vimrc file will be executed as if they had been proceeded with a colon. So if you want to change this option, while Vim is running we can use :set. In the vimrc file however, just use the word set without a colon. So let's check the value of the history option with :set history? and press enter. Notice that it doesn't contain the value that we specified in the vimrc file. This is because the vimrc file is read and executed when Vim starts. So to make the changes take effect, you have to exit out of Vim and start it again. So let's do that now with :wq do right and quit. And now we can start Vim again, just by typing Vim. And I'm going to open up the vimrc file which is .vimrc for me. Again, if you're on windows it will be _vimrc. So I'll hit enter. And so now when we check the value of history it should be set to 1000. So let's type :set history? and hit enter. And sure enough, it is set to 1000. By the way if you wanna use comments in your vimrc file to remember why you are setting these options in the first place, start a line with a double quote. So I'll insert a line above this one with shift O that's capital O command. I'm gonna type a double quote which indicates the beginning of a comment, and then I'll just type something like this, keep 1000 items in the history, and hit escape to return back to normal mode. I'm going to save my changes here with :w so that they don't get lost. Now let's move on to some other options. If you wanna show the cursor position at all time, use set ruler. I've talked about this in a previous lesson, so I'm just gonna go down here and type show the cursor position, type set ruler hit escape and write my changes to the file. You can also use set showcmd if you wanna display the incomplete commands in the bottom right hand of your screen. For example, if you were to type 2y then it would show up in the bottom of your screen, and when you completed the command with W for example, then it would disappear. Some people find that useful, and if you do, add set showcmd to your vimrc file. I'm just gonna add this here, set showcmd. And I'm gonna go up here and type show incomplete commands. And let's actually turn this on now. I'm just going to turn it on set showcmd, and press enter. Now that should be on. I'll just do this question mark and sure enough, it's on. And now let's do a command like I was talking about like 2y and you see in the bottom of your corner 2y is displaying because that is an incomplete command that we are in the middle of typing. Now, if I finish that command let's say with another Y, then the command completes, and that command goes away in the bottom right-hand corner. So that's what the showcmd option does. Let's go down here and let's say you wanna display completion matches on your status line. To do that, you would use set wildmenu. I know I've covered this option in another lesson but let's quickly review it here. Let's take an example. Let's do :h hist. And if I type the tab key, then it starts giving me options of things that match for tab completion. Let me hit escape here. Now what I'm going to do is actually set wildmenu and turn this option on. And now when I do, for example, help hist and press tab there is a menu at the bottom of your screen here and it shows you what is getting highlighted and what is coming up next. Now I'm just gonna hit escape to get out of that. So this shows a menu when using tab completion, I'll just leave that comment in there. Let me write my change to the file. Now let's move on to another option. Let's say you wanna show a few lines of context around the cursor. To do that, use set scrolloff equals something other than zero, five for example. This value is the minimum number of lines to keep above and below the cursor. By default, this is set to zero. So remember in the previous lessons where I would reposition the text on the screen using Z and enter, if this is set to zero then Z enter moves the texts all the way to the top of the screen. We'll do that here. Z enter, it goes all the way to the top of the screen. But if it said it's something like five, scrolloff=5, press enter, GG to go to the beginning of the file or J down here. And now when I hit Z enter it keeps five lines at the top of the screen. So if you wanna display a little bit of context at all times, then set this value to something like five. And we've already talked about this a little bit but if you wanna highlight your search matches use set hlsearch. Again you could use hls if you wanted to, for example because that's a shorthand version of the option. Either one works. Sometimes in my vimrc I actually like to use the longer names because they're more descriptive. But when I'm just typing them I like the shorter names because they're quicker. So here I'll just leave hlsearch because it's more descriptive. On a related note, if you want to enable incremental searching you can use set incsearch. This tells Vim to highlight the match string as you're typing it when it performs a search. Another search related option is ignore case. Now, if you want to ignore case when searching you can turn this on with set ignorecase. If you're going to use this I would suggest also setting something called smartcase. So I'll type that here set smartcase. Now smartcase tells Vim to overwrite the ignore case option, if the search pattern contains uppercase letters. You know the command to turn on line numbering in that set nu or the longer name as set number. Now, if you're the type of person that likes to see line numbers on each and every file that you open, then go ahead and use this option. Also, if you're the type of person who has backups for your backups, then you might be interested in the set backup option. It makes a backup copy of the file you're editing before saving it. Let me set this option now with :set backup and pressing enter. Now, let me save this file with :wq to write and quit. I'm just gonna get a directory listing here and this works on Unix type system so this particular command probably won't work on windows but here it is, ls .vim, and I'll just do R star to get everything that starts with .vimr followed by anything I'll hit enter here. And now you can see that there is a .vimrc file which is what we were editing and a .vimrc~ file. That is the backup file. By default, that tilde is appended to the end of the name, to create the backup. That's an unusual ending, and it's used to make sure that you don't overwrite something you care about. Now, if you wanna change that, you can use another option. Let me go back into editing the vimrc file here. Vim .vimrc, and I'm going go to the bottom of the file here with dollar sign and press enter. And that option is set bex=something whatever you want that to be. And by the way, BEX is short for backup extension. So I'll go ahead and write that change to the file. Actually, I'm gonna leave this as this. So I'm just gonna comment that out because I really don't want all of my files ending in capital something. So I'm just gonna comment that out for now. I'm gonna hit O to move down to the next line here. Normally Vim we'll wrap texts without regard to spacing. So if the end of your visible screen happens to be in the middle of the word cat, then you might see CA on the current line and T at the beginning of the next line. Set another way vim wraps at the last character that fits on the screen. Again, this is just wrapping, the contents of the file aren't being changed in any way. If you wanna make sure to not break mid word, then you can use set lbr. This makes for an easier to read line wrap. If you happen to do some coding and or scripting you might be interested in the next two settings. The first one is set ai or set autoindent. What this does is copies the indentation from the current line when starting a new line by pressing enter when you're in insert mode or by using the lowercase O or uppercase O commands, when you begin a new line below or above your current line. Let me quickly demo this for you. First I'm gonna turn it on with set ai press enter. Now let's start a block of code. For example, we can type log with an opening brace here and I'll just space over two times, just some random code here. And now when I hit enter, notice how the indentation is the same on this new line. So I can go ahead and keep coding right here. Now, let me go ahead and undo this hit escape and then press U and undo my edit. Another helpful setting is set si which is short for smart indent. This tells Vim to do what they consider smart auto indenting when starting a new line. This works for C like source code and other programming languages as well. For example, let's go ahead and turn it on. I'll do set si press enter and then type log and then an opening brace and press enter. Now notice how the new line is automatically indented over here. And let me go ahead and close this with the closing curly brace. And when I type that, notice how smart indent puts it back to the proper indentation. If you're going to use this smart indent setting you should also use the auto indent setting too. I'm gonna hit escape here and then hit U to undo my changes. Now, if you're coding, or even if you're editing a lot of configuration files that have syntax highlighting, you want to be able to clearly see that syntax highlighting. You can tell Vim if you're using a dark background or a light background with set bg=light if you're using a light background, or set bg=dark if you're using a dark background. So if you have a background that looks like mine here a white background, or a light gray background, for example then set it to light. If you're using a black or dark gray background then set it to dark. Now then we'll try to use colors that look good with your background. So if you're editing a file with syntax highlighting and you're having trouble reading the colors tell Vim what background you have, and more than likely you'll be able to clearly see all the texts. Let me go ahead and comment that out. Write my change here and let's just set it to dark and see what happens here. Now you can see on the left where the line numbering is, it's really hard to see, but if I had a dark background that yellow would really stand out and be easy to see. So I'm just gonna make sure Vim knows I'm using a light background so I'll just type light, press enter. And now those line numbers are easy to read on my light background. So that's kind of how that works. If you open up a file with Vim and you can't read anything, can't see anything then make sure to use the set bg command. So that's it for the set commands I wanted to cover in this lesson. Again, you can view these options by using :h option-list and pressing enter, or you can also use the :options command.

About the Author
Students
14217
Courses
61
Learning Paths
13

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.