In BlogPost, vim

Vim Tutorial – Day 1

As a self-taught programmer, I got started in programming just fine without using Vim.

As a matter of fact, when I first encountered it, I was a bit intimidated.

vim logo

This intimidation reminded me of my martial arts classes

So as a kid I used to love martial arts. I took Taekwondo and Judo for a bit before life pushed me down a different path. One thing I noticed is that they never let you break boards on your first day of class. This is a good thing, as it’s a bit intimidating (“how am I supposed to punch through 5 bricks?”).

Nope. You’d start with basic things – learning to count in Japanese, how to bow, some basic blocking moves, and maybe a kick or punch if you were lucky.

Because sometimes going head first results in a cracked head

The more advanced martial artists would break boards and bricks. These were the folks who had been doing it a few years. The beginners (like myself) would probably have broken their hands if they tried (or at least not broken the board or bricks).

Learning Vim can be like that first day of martial arts class

If you’ve ever watched skilled Vim users zip around, you’ll realize it’s an amazing thing to behold. It’s also a little bit intimidating. When I started, there are things I wish I knew. So I’m writing this tiny Vim tutorial to help you get started.

Let’s be honest, it can be intimidating if you’re still used to using an editor like Sublime where you still use your keyboard and mouse.

And I’m calling it a kata after those simple karate exercises designed to train the mind and muscle for specific applications.

One of my favorite sites for learning more advanced Vim moves is Vimgolf. The other resource I’ve used is a book called Practical Vim.

But if you don’t know the below techniques, it’s going to be pretty hard to absorb anything from Vimgolf in my humble opinion. We begin with four basic moves.

Day 1 – Four Basic Moves

I’ll call this day 1. This is where you learn the basic moves.

Move 1 – The Two Modes Kata

A mode in Vim is an operational state that allows you to perform certain actions in Vim. There are more than 2 modes, but for now, I’m just going to walk you through two.

The first mode is normal mode. In this mode, you can do things like navigate in your editor and manipulate it with various commands that allow you to copy and paste text. Press the escape (ESC) key to ensure you are in normal mode.

The second mode is the insert mode. In this mode, you can insert new text as well as delete it. Press “i” to switch from normal to insert mode. Press the ESC key to get back to normal mode.

So, now let’s get on to the kata.

Kata Steps:
  • Open up Vim. You should be in normal mode by default.
  • Press i. You should see an “–INSERT–” or something similar in your terminal to indicate you are in insert mode. Type “abc” and you should see text in your terminal.
  • Now press ESC to get back to normal mode.

Move 2 – HJKL Kata

Now that you know the two modes, you are ready to navigate. Navigation in Vim is done in normal mode with the keys h, j, k, and l.

  • h moves the cursor left in normal mode
  • j moves the cursor down in normal mode
  • k moves the cursor up in normal mode
  • l moves the cursor right in normal mode

You can technically use the arrow keys, but you should learn to use h, j, k, and l because later on, it helps you navigate more quickly in Vim. Using the arrow keys is considered a bad habit.

And with that, you are ready for the kata.

Kata Steps:
  • Start Vim in normal mode.
  • Press l to see the cursor move right.
  • Press h to see the cursor move left.
  • Press j to see the cursor move down.
  • Press k to see the cursor move up.
  • Feel free to experiment a bit.

Move 3 – Yank and Paste Line Kata

Since you can now do basic navigation in Vim, you may be wondering how to copy and paste text. You do this with the yank command.

The yank command in normal mode is activated by pressing the y key. To copy a line of text, you navigate to it and press yy. You have now copied that text to the clipboard. You can paste it by pressing the p key.

So, now let’s get on to the kata.

Kata Steps:
  • Start Vim in normal mode (or open up a sample text file if you know how to do this).
  • Switch to insert mode and type some text. Now switch to normal mode and press yy.
  • Now press p.
  • You should see a duplicate line of text.

Move 4 – Top and Bottom Kata

Being able to navigate quickly is one of the best parts of Vim in my opinion. If you ever need to jump straight to the top of a file, you can type “gg” in normal mode. If you need to go the very bottom of the file, type SHIFT + g (I use the “+” sign to mean hit both keys at the same time.

And with that, you are ready for the kata.

Kata Steps:
  • Start Vim in normal mode with a sample text file that’s at least 10 lines long.
  • Use the j key to navigate down to the middle of a file.
  • Type gg to navigate to the top.
  • Type SHIFT + g to navigate to the bottom.

Move 5 – Quit Kata

Like a job gone awry, sometimes you just need to quit. In Vim, one way to do this is to type SHIFT + ; (this will output a colon). Then type q. This lets you quit your file.

Finally, you’re ready for the last kata.

Kata Steps:
  • Start Vim in normal mode with a sample text file that’s at least 10 lines long.
  • Type SHIFT + :. Then type q.
  • If you’ve made changes to the file, you may get a message such as “E37: No write since last change (add ! to override)”. Type SHIFT + ;. Then type q, followed by SHIFT + 1 (this outputs an exclamation point – you are effectively typing “:q!”).

Summary

The above 5 “katas” will get you started in Vim. Finally, you are ready to begin making Vim your personal text editor!

  • 2 of the modes you’ll use the most in Vim when starting out are normal and insert modes.
  • Use the h, j, k, and l keys to move around in Vim.
  • Yank a line (yy) and paste (p) to copy and paste a line.
  • Use gg to navigate to the top and SHIFT + g to navigate to the bottom.
  • Quit using SHIFT + : followed by q.
Recent Posts
picture of cat representing salesforce oauth for rest apioauth logo