Intro to Vim

Basic Vim Commands to Help Get You Up and Running

Meet Mikey:

Mikey

When we last saw Mikey he was just an aspiring businessman who used programming to help him keep track of the daily sales figures for his lemonade stand. You can read more about Mikey’s past adventures here.

It turns out that Mikey enjoyed his first project so much that he now uses all of his free time to write the code that manages virtually every task of his business. Today at school Mikey heard some cool 7th graders talking about something called Vim, but he doesn’t know anything about it. Mikey goes to Google and tries to find out some more information.

What is Vim?

Mikey is surprised by what he finds. It turns out that Vim is, for the most part, nothing more than a text editor, albeit an extremely powerful one. Unfortunately for Mikey using Vim comes with a bit of a learning curve, but Mikey found some basic commands on Google that he wants to try. Let’s code along with Mikey and see what this Vim is all about.

Entering Vim

Getting in to Vim is quite simple. Simply navigate to a directory via your terminal and input vim insert_file_name_here to the command line . This will open your file in Vim.

Insert/Command Mode

Vim is in Command Mode when it opens. Being in Command Mode will allow you to navigate and edit your text with Vim commands. It does not always act the way you would expect, assuming you were trying to use it like a standard text editor. There are a few points in this post where there will be text for you to copy/paste into your file. As you go to paste your copied text, be sure to enter Insert mode by clicking the i key. Let’s insert some text into our file:

Three words.
Two WORDs…!

You can copy and paste like normal when in Insert mode. To enter back into Command Mode, simply press the esc key.

h/j/k/l Commands

Vim doesn’t respond to the mouse the way we are accustomed to, inside or outside of Command Mode. As a result, navigation tends to be done via the keyboard. While Vim will respond to the keyboard directional arrows, it is best practice to get accustomed to the h/j/k/l commands:

h — moves the cursor one space left
j — moves the cursor one space down
k — moves the cursor one space up
l — moves the cursor one space right

Try using any of the h/j/k/l commands within the file you are currently viewing. Being able to move the cursor is a good start, but moving one space at a time is not very efficient. Let’s look at a few ways to to move the cursor more than one space at a time

‘WORD’ vs ‘word’

Before moving forward, it is important to point out that several of the following commands are designed to navigate between ‘words’ and ‘WORDS’. Take a moment with Mikey to try and distinguish the difference between words and WORDS, as defined by VIM Adventures:

  • A word consists of a sequence of letters, digits and underscores, or a sequence of other non-blank characters, separated with white space (spaces, tabs, EOL). An empty line is also considered to be a word.

Three words.

  • A WORD consists of a sequence of non-blank characters, separated with white space (spaces, tabs, EOL). An empty line is also considered to be a WORD

Two WORDS…!

‘w’ vs ‘W’

The w and W commands will move over words and WORDS, respectively.

The w command will move the cursor forward to the beginning of the next word. Let’s look at our word example from earlier:

- three w commands will navigate over each of the words below
Three words.

Now let’s look at our WORD example from earlier:

- two W commands will navigate over each of the WORDs below
Two WORDs…!

Test out the new w/W commands with Mikey.

‘e’ vs ‘E’

The e and E commands work in a fashion similar to that of w/W, except e/E will take the cursor forward to the end of the next word, rather than the beginning. Mikey adds the following to his text file to try out the e/E commands. Let’s do it with him:

sugar-free gum

‘b’ vs ‘B’

Remember how the w/W commands will take you to the beginning of the next word/WORD? The b/B commands will take you to the beginning of the previous word/WORD. Mikey is going to add this next example to his text file and try using each of the w/W, e/E and b/B commands. Will you do it with him?

Tuesday’s doctor appt.

‘0’ vs ‘$’

Leaping word to word will definitely come in handy, but what if we want to get our cursor from the beginning of a line to the end of a line? Alternatively, what if we want to get back to the beginning of the line while our cursor is at the end of a line? Jumping word by word can take several keystrokes. Luckily, we have the number 0 and $ commands. Do as Mikey does and copy/paste the following into your text file, then try navigating the file using the 0/$ commands along with any of the previous commands:

Fourscore and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.

‘x’ vs ‘X’

The x command enables us to delete the space following the cursor position. The X command deletes the space preceding the cursor position. Mikey goes to copy the the following into his text file to experiment with the x/X commands. What happens next will shock you…

You’ll never get away with this!!!

‘dd’

In the way that navigating word by word can take quite a bit of effort, the same can be said for the x/X commands. Deleting a single character is definitely something we want to have access to, but the command on its own is not very efficient at deleting. Executing the dd command allows us to delete an entire line with only 2 keystrokes (the first d invokes the delete command, the second applies it to the current line). Mikey copies and pastes the following into his text file, then tries experimenting with the dd command (don’t worry when your text goes missing. We will take care of that in second) :

All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy.

The ‘dd’ command will delete the entire line regardless of the cursor’s location.

undo/redo

Mikey got so excited he deleted all of his text. No biggie. Let’s undo our delete line actions. Mikey simply enters u (while in Command Mode) for each action he would like to undo. The redo command can be used with CTRL + r. Let’s undo our changes so we can get our text back.

Chaining Vim Commands

Mikey is feeling pretty good about his Vim skills. He decides to show them off to his friend Tommy as they play that afternoon, but Tommy is unimpressed. He tells Mikey that while the concept is cool, none of what he is doing is proving to be more efficient than using a text editor like Atom or Sublime. Mikey realizes Tommy was right and feels that all of his Vim practice was for nothing. Poor Mikey.

Mikey is crying, but doesn’t want anyone to see

By now the sun is setting and Tommy has to go home, but Mikey is still thinking about Vim. Mikey is just about ready to give up on Vim for good when he has a crazy idea. He opens his text file with Vim, finds a word in his file and enters this command:

2W

Mikey’s cursor now moves forward to the beginning of the word 2 WORDS ahead. Mikey tries out another command:

5x

Now Mikey has deleted the 5 spaces following of the cursor. Mikey tries something crazy now:

5dw

Now Mikey has deleted the 5 words following his cursor. He continues to fool around and takes note of some of the Vim methods he’s chained together:

  • [count], w/W : e/E : b/B : h/j/k/l — performs the command [count] number of times
  • d [count] w/W : e/E : b/B : h/j/k/l — deletes according to the command action [count] number of times
  • d 0/$- deletes everything between the cursor and the beginning/end of a line

He’s certain that there are plenty more to be found, but he’s confident that what he has here is enough to get him on his way to making Vim a viable option for editing his text files. This makes Mikey happy.

Mikey is happy again

Mikey logs out of Vim and rests assured knowing that he will eventually learn enough commands to efficiently use Vim. In order to quit Vim without saving any changes simply enter the quit command with :q to exit while in Command Mode (you may need to enter :q! if the file has been altered since opening it). To save your changes, input the write command before quit, like so: :wq.

Stay tuned for part two, where Mikey is going to implement a number of different methods that will make Tommy second-guess his assessment of Vim’s text editing power. Go Mikey!