This course covers modes in Vim and looks at the command-line version of Vim. We then guide you through a demonstration of how to create and edit files in Vim, in order to provide with a practical understanding of the concepts and to give you the chance to try them out for yourself.
- Get a basic understanding of modes as well as the command-line version of Vim
- Learn how to create and edit files in Vim
This course is ideal for anyone who needs to edit 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.
For now, we're going to focus on the command line version of Vim. If you want to use the graphical version of Vim, you can do that, but just know that we're not going to get into any of the graphical only features of Vim just yet. The first thing you need to do is start a command line session on your computer. Now, this varies by operating system, so I'll quickly run down how to do this on Mac, Linux, and Windows. If you're using a Mac, you'll start the terminal application. An easy way to do that is to use Spotlight. To start Spotlight, click on the Spotlight icon in the menu bar, or type command space. Now just start typing the word terminal and it will appear. Now press enter to start it. By the way, you can also use the finder to start it as well. It's located in the /application/utilities folder. If you're using a Windows system, you can start a command line session by clicking in the search bar and typing command. Once the command prompt menu item appears, click on it to start it. There are a lot of Linux distributions out there, and a lot of various desktop environments for Linux, but in general, you just need to locate the terminal application. This might simply be called terminal, in might be called console, it could be called GNOME terminal. You might also find xterm, eterm, and rxvt. If you're running CentOS, like I am here, you can go to the applications menu, and then from there, go to utilities. And if you scroll down just a little bit, you'll see the terminal application. It's also located in the favorites by default. So just click on it to start the terminal application. Now that you have a command line session open for your operating system, you can start Vim by typing Vim and pressing enter. Here's what it might look like on a Linux or Mac system. Here's what it can look like on a Windows system. When you first start Vim, you'll see a welcome screen, and even a bit of help for you there as well. The first thing I want to teach you how to do is exit out of Vim. Remember that when you start Vim, you're in normal mode, but you're going to use command line mode to quit. So you'll type colon, which puts you in command line mode. Now you'll type q followed by an exclamation mark. When you press enter, your command will execute. This command quits Vim, so when you press enter, Vim terminates and you're back on the command line. Let's use Vim to create a brand new file. To do that, run Vim followed by the file name you want to create. Let's use abc123 as an example file to create. So you'll type Vim space abc123 and hit enter. If you wanted to edit an existing file, then you would have supplied that file name on the command line. At any rate, you're now looking at a new empty file. Again, when you start Vim, you're placed into normal mode. Let's say that you want to enter in some text. In order to start insert mode, you need to press lowercase I. After pressing lowercase I, anything you type will go into the file. So let's type a simple sentence here. Let's say something like, "Vim is fun!" Now let's save the file. To do that, you first need to return to normal mode by pressing escape. If you're not sure what mode you're in, you can always hit escape and you'll be placed back into normal mode. Now, from normal mode, you enter command line mode by typing a colon. To actually save the file and quit Vim, type wq and press enter. You can think of wq as standing for write and quit, because that's exactly what it does. It writes your changes to the file and quits the Vim editor. What you've learned up until this point is really the least amount of knowledge you need to actually use Vim as an editor. Obviously, this is only the beginning, and we're going to go really deep in this course, but I wanted to quickly get you to the point where you could actually use Vim in a very short amount of time. The next lesson is actually the first of many practice exercises in this course. You'll get a chance to use and reinforce what you've learned so far.
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.