C Programming Tutorial 17 – Vim Basics – Part 1

Don’t forget to read the last blog about comments! Are you new here? Start at the beginning, Intro to C!

In this series we have been using Vim! Now, you don’t have to use Vim if you don’t want. That’s fine. I too am new to Vim. I just started using it this year. When I first started using it, I found it to be very annoying, like it made everything more complex than it had to be.

If you feel that way, I understand. But once you learn some of the commands, you can see that it’s not so bad. One of the huge benefits of using Vim is that it is widely available. This means that if you have to connect to another computer through the command line, the chances are you can edit text files on the remote computer using Vim. Cool.

Let’s open a file in Vim.

The first thing I want to do with vim is make it not so ugly. The way we can do that is turn syntax highlighting on!

We can also add a line count:

This is great and all but watch what happens when we close and reopen Vim?

You can see that the syntax highlighting and line count goes away. To fix this, we have to edit a file for our Vim configuration. So let’s quit Vim.

Put this in the file:

It’s okay if it is a new file. It should still work as long as we are in the home directory, which we are from running cd.

The next thing I wanted to discuss is the proper way to move the cursor around in Vim. Let me open a small program to show you some commands. So far you’ve probably used the arrow keys to move your cursor around in Vim. This is fine starting out, but a lot of people actually do it a different way.

When you use the arrow keys, you often train yourself to stay in insert mode all of the time because you can move around while in insert mode. Well, you are actually not supposed to stay in insert mode, but are supposed to only be in insert mode when you are about to type.

The recommended way to move your cursor around is with h, j, k, l. Give it a try and see if you can get used to it. Now, when you see something that needs changed and you want to move there, enter into insert mode, and then exit insert mode. Let’s give it a try.

Now, whether you use the arrow keys or not is ultimately up to you, but either way you will probably want to try to get into the habit of only being in insert mode when you need to insert something and not all of the time. That’s because when you are not in insert mode there are other commands you can run to make coding a lot faster.

One of the easiest ways to move around is using G. Type a number followed by G to go to that line.

G by itself goes to the very end.

$ goes to the end of a line.

^ goes to the beginning of a line.

a will enter insert mode after the character you are on.

w is used to move to the beginning of the next word.

b is used to move to the beginning of the previous word.

In the next blog we are going to discuss how to copy and paste, undo and redo, and other cool stuff like that! Check it out!

Leave a Reply

Your email address will not be published. Required fields are marked *