Getting started with Vim: The basics – Opensource.com

I

remember the first time I came across Vim. I was a college student, and the computers in the lab of the computer science department were installed with Ubuntu Linux. While I had been exposed to different variations of Linux (like RHEL) even before my college years (Red Hat sold their CDs at Best Buy!), this was the first time I needed to use the Linux operating system regularly, because my classes required me to. Once I started using Linux, like many others before and after me, I started to feel like a “real programmer”.

Students could use a graphical text editor like Kate, which was installed on lab computers by default. For students who could use the shell but weren’t used to the console-based editor, the popular choice was Nano, which provided good interactive menus and an experience similar to Windows’ graphical text editor.

I

used Nano a few times, but I heard amazing things about Vi/Vim and Emacs and really wanted to try them out (mainly because they looked great, and I was also curious to see what was good about them). Using Vim for the first time scared me, I didn’t want to spoil anything! But once I got the hang of it, things became much easier and I could appreciate the powerful capabilities of the editor. As for Emacs, well, I gave up, but I’m happy I stayed with Vim.

In this article, I will go over Vim (based on my personal experience) enough that you can manage as an editor on a Linux system. This won’t make you an expert or even scratch the surface of many of Vim’s powerful capabilities. But the starting point always matters, and I want to make the initial experience as easy as possible, and that you can explore the rest on your own.

Step 0: Open a console window

Before jumping into Vim, you need to do some preparation. Open a console terminal from your Linux operating system. (Since Vim is also available on MacOS, Mac users can also use these instructions.)

Once a terminal window is activated, type the ls command to enumerate the current directory. Next, type mkdir Tutorial to create a new directory named Tutorial. Enter the directory by typing cd Tutorial.

That’s it for preparation. Now it’s time to move on to the fun part: start using Vim.

Step 1: Create and close an unsaved

Vim file

Remember when I said I was afraid to use Vim at first? Well, the scary part was thinking, “what if I change an existing file and mess things up?” After all, various computing tasks required you to work on existing files by modifying them. I wanted to know: How can I open and close a file without saving my changes?

The good news is that you can use the same command to create or open a file in Vim: vim <FILE_NAME>, where <FILE_NAME> represents the name of the destination file you want to create or modify. Let’s create a file called HelloWorld.java by typing vim HelloWorld.java.

Hello, Vim! Now, here’s a very important concept in Vim, arguably the most important to remember: Vim has multiple modes. Here are three you need to know to do the basics of Vim: Description of

the normal Default mode; for simple navigation and editing Insert To insert and modify text explicitly Command line For operations such as save, exit, etc.

Vim has other modes, such as Visual, Select and Ex-Mode, but the Normal, Insert and Command Line modes are good enough for us

.

It is now in Normal mode. If you have text, you can move around with arrow keys or other navigation keystrokes (which you’ll see later). To make sure you are in Normal mode, simply press the Esc (Escape) key.

Tip: Esc switches to Normal mode. Even if you’re already in Normal mode, press Esc just for practice.

Now, this will be interesting. Press : (the dot key) followed by q! (that is, :q!). Your screen will look like this

:

Pressing the colon in Normal mode switches Vim to command-line mode and the :q! command exits the Vim editor without saving. In other words, you are abandoning all changes. You can also use ZQ; Choose the option that is most convenient.

Once you press Enter, you should no longer be in Vim. Repeat the exercise several times, just to master it. Once you’ve done that, move on to the next section to learn how to make a change to this file.

Step 2: Make and save modifications in Vim

Reopen the file by typing vim HelloWorld.java and pressing the Enter key. Insert mode is where you can make changes to a file. First, press Esc to make sure you are in Normal mode, then press i to switch to Insert mode. (Yes, that’s letter i.)

At the bottom left, you should see – INSERT –. This means that it is in Insert mode.

Write some Java code. You can write whatever you want, but here’s an example for you to follow. Your screen will look like this:

public class HelloWorld { public static void main(String[] args) { } }

Very nice! Notice how text is highlighted in Java syntax highlight colors. Because you started the file in Java, Vim will detect the color of the syntax.

Save the file. Press Esc to exit Insert mode and enter command line mode. Type : and follow that with x! (that is, two colons followed by x and !). Press Enter to save the file. You can also type wq to perform the same operation.

Now you know how to enter text using Insert mode and save the file using :x! or :wq.

Step 3: Basic navigation in Vim

While you can always use its friendly up, down, left, and right arrow buttons to move around a file, that would be very difficult in a large file with almost countless lines. It’s also helpful to be able to jump within a line. Although Vim has a ton of impressive navigation features, the first one I want to show you is how to go to a specific line.

Press the Esc key to make sure you are in Normal mode, then type :set number and press Enter.

voila! You will see line numbers on the left side of each line.

OK, you

can say, “that’s great, but how do I jump to a line?” Again, make sure you are in Normal mode, then press :<LINE_NUMBER>, where <LINE_NUMBER> is the number of the line you want to go to and press Enter. Try moving to line 2.

:2

Now move to line 3

.

But imagine a scenario where you are dealing with a file that is 1,000 lines long and you want to go to the end of the file. How do you get there? Make sure you are in Normal mode, type :$ and press Enter.

You’ll be on the back line!

Now that you know how to jump between lines, as a bonus, let’s learn how to move to the end of a line. Make sure you are on a line with some text, such as line 3, and type $.

You are now in the last character of the line. In this example, the open brace is highlighted to show where the cursor moved, and the lock brace is highlighted because it is the matching character of the opening brace.

That’s it for basic navigation in Vim. Wait, don’t exit the file, though. Let’s move on to basic editing in Vim. However, feel free to have a cup of coffee or tea.

Step 4: Basic

editing in Vim Now that you

know how to navigate a file by jumping to the line you want, you can use that skill to perform basic editing in Vim. Switch to Insert mode. (Remember how to do that, by pressing the i key?) Sure, you can edit using the keyboard to delete or insert characters, but Vim offers much faster ways to edit files.

Move to line 3, where it displays public static void main(String[] args) {. Quickly press the d key twice in a row. Yes, that’s dd. If you did it successfully, you will see a screen like this, where line 3 is gone, and each subsequent line moved up by one (i.e. line 4 became line 3).

That’s the delete command. Fear not! Hit it to you and you will see the deleted line recovered. Go. This is the undo command.

The next lesson is to learn how to copy and paste text, but first, you need to learn how to highlight text in Vim. Press v and move the left and right arrow buttons to select and deselect text. This feature is also very useful when you show code to others and want to identify the code you want them to see.

Go to line 4, where it says System.out.println(“Hello, Opensource”);. Highlight the entire line 4. Fact? OK, while line 4 is still highlighted, press y. This is called yank mode and will copy the text to the clipboard. Next, create a new line below by entering o. Note that this will put you in Insert mode. Exit Insert mode by pressing Esc, then press p, which means paste. This will paste the copied text from line 3 to line 4.

As an exercise, repeat these steps, but also modify the text on the newly created lines. Also, make sure the lines are well aligned.

Tip: You must switch between Insert mode and command-line mode to perform this task.

Once you’re done, save the file with the x! command. That’s it for basic editing in Vim.

Step 5: Basic search in Vim

Imagine your team leader wants you to change a string of text in a project. How can you do it quickly? You may want to search for the line using a certain keyword.

The search functionality of Vim’s can be very useful. Go to command line mode (1) by pressing the Esc key, then (2) by pressing two dots: key. We can search for a keyword by entering :/<SEARCH_KEYWORD>, where <SEARCH_KEYWORD> is the text string you want to find. Here we are looking for the keyword string “Hello.” In the image below, the colon is missing, but they are mandatory.

However, a keyword may appear more than once, and this may not be the one you want. So how do you navigate to find the next match? Just press the n key, which stands for next. Make sure that you are not ‘t in insert mode when you do this!

Additional step: Use split mode in

Vim

That covers pretty much all the basics of Vim. But, as a bonus, I want to show you a cool feature of Vim called split mode.

Quit HelloWorld.java and create a new file. In a terminal window, type vim GoodBye.java and press Enter to create a new file named GoodBye.java.

Enter the text you want; I decided to write “Goodbye.” Save the file. (Remember that you can use :x! or :wq in command-line mode.)

In command-line mode, type :split HelloWorld.java and see what happens.

Wow! Look at that! The split command created horizontally split windows with HelloWorld.java top and GoodBye.java bottom. How can you switch between windows? Press and hold the Control key (on a Mac) or CTRL (on a PC) and then press ww (i.e. w twice in a row).

As a final exercise, try editing GoodBye.java to match the screen below by copying and pasting from HelloWorld.java.

Save both files and you’re done!

TIP 1: If you want to organize the files vertically, use the command :vsplit <FILE_NAME> (instead of :split <FILE_NAME>

, where <FILE_NAME> is the name of the file you want to open in split mode. TIP 2: You can open more than two files by calling as many additional split or vsplit commands as you want.

Try it out and see what it looks like.

Vim

Cheat Sheet

In this article, you learned how to use Vim enough to survive for work or a project. But this is just the beginning of your journey to unlock Vim’s powerful capabilities. Be sure to check out other great tutorials and tips on Opensource.com.

To make things a little easier, I’ve summarized everything you’ve learned into a handy cheat sheet.