Introduction to Vim with the Spice Girls

Cup of Code
11 min readJun 5, 2020

It’s not clickbait if it is true

Vim is a highly configurable text editor built to make the creation and changing of any kind of text very efficient.

It is a clone, with additions, to the text editor from the early days of Unix, vi (vim~“vi improved”). It is a powerful text editor, a favorite of many system administrators and developers (for more information about vim’s abilities and the difference from vi, click here).
The only problem with vim is that it has a learning curve.

Vim is highly used among my team so I wanted to improve my skills at it. They like using it because it’s the fastest way to code and because not all our coding is done locally so using a “regular” IDE is not an option. I thought it would be fun to practice it with the famous song by the Spice Girls — that has over 467 million views on YouTube — Wannabe.

“If you wanna be my lover, you gotta get with my friends”♪♫

In this publication we will fix a file of the song’s lyrics, so it will be as the original lyrics are. In each stage we will be introduced to another basic command that you would usually use daily in a common IDE.

Here is a table of content for easy access:

Setup

0. If you don’t have already, download and installation instructions can be found here or here.

  1. Open a terminal window and navigate to the desired directory.
  2. Create a new file ‘wannabe.txt’: cat > wannabe.txt
  3. Copy the content below and paste it in the terminal

4. Start a new line: Press Enter.

5. Exit (control+C).

Important note (I could have just fixed it but then how could we learn?):

Control+C (^C) means “interrupt”, i.e., stop what you're doing. Technically, pressing ^C sends the INT signal, which by default terminates an application.

Control+D (^D) means end of file. It only works at the beginning of a line (I'm simplifying a little), and has no effect if the program isn't reading input from the terminal.

So I should’ve used Control+D (^D). Learned for next time!

  • If you made a mistake, no worries. Just delete the file by typing rm wannabe.txt and start over :)

It should look like this: (Notice the ^C in the end!)

Open

To open the file in vim, type in: vi wannabe.txt or vim wannabe.txt.
It doesn’t matter because the vi command opens vim editor (and if you’re curious why, click here).

Your terminal will look like this:

vim wannabe.txt

Make sure you have total of 22 lines! If you don’t, this is a good time to learn how to exit the editor ;)

Exit

To exit from the editor, type :q and hit Enter. (q for quit)

To save and exit, type in :wq and hit Enter.

Fun fact: “How to quit Vim” Is stack overflow’s most popular question.

  • If you opened the editor and saw you don’t have 22 lines, do not worry! Close the editor (:q), remove the file (rm wannabe.txt) and start the setup again :)

Important Notes

  • As you can see in the screenshot above, when the line is longer than the terminal window width, it will continue in the row below, but will not open a new line in the file.

An example for a line cut:

6 I wanna, (ha)                    -> Insert another three "I wan na, (ha)"
7 I wanna really, really, really wanna zigaziga.
  • All your commands will appear at the bottom of the terminal window.
INSERT

Mistakes You Will Make Even After Reading This

  • vim uses “modes”, and they can’t be mixed. You’ll quickly see, for example, that if you insert something and want to undo or exit the doc, you need to exit insert mode first. You exit a state with the esc button.
  • Aiming the cursor with a mouse click won’t work. You can only use the arrows and the scroller.

Ok, so now we have a text file with Wannabe’s lyrics, but in a bit of a mess and we need to fix it. Let’s get started!

Spice Girls

Save Changes

We will finish every phase with Save (saving changes often is good practice not just when using vim). To save your changes, type :w and click enter. (w ~ write)
Feel free to ignore this step. Moreover, if you do accidentally try exiting the editor without saving, you will be blocked:

If you do want to exit without saving, you can do it with :q!

Insert

In line 1 we need to add another “ha”.

1 Ha ha ha ha                                -> Insert another "ha"
  1. Enter INSERT mode: Click i (i~insert)
  2. Go to the end of the first lyric line using the arrows (because locating with the mouse will not work!)
  3. Insert “ ha”.
  4. Exit INSERT mode: esc
  5. Save: :w
A classic newbe mistake: typing ‘:w’ inside INSERT mode

The desired result:

1 Ha ha ha ha ha                             -> Insert another "ha"

Undo

  1. Enter INSERT mode: Click i
  2. Insert nonsense: “nonsense”.
  3. Exit INSERT mode: esc
  4. UNDO: Type u (u~undo)
  • Every u will undo another change (So if you undo one more time it will undo our “ha” fix!)

Delete Characters

In line 4 I got too exited to tell what I really, really want that I accidentally added exclamation marks that do not appear in the original lyrics, so we need to remove them.

4 I’ll tell you what I want, what I really, really want!!!  -> remove exclamation marks
  1. Go to the last exclamation mark.
  2. Press x three times.
  3. Save :w

The desired result:

4 I’ll tell you what I want, what I really, really want  -> remove exclamation marks
  • I know it feels useless because we can enter INSERT mode and press Backspace, but it will be useful in the next commands!

Delete/Cut a Line

In line 10 we have a duplicate line we want to remove.

10 If you want my future, forget my past    -> Delete duplicate line
  1. Go to line 10.
  2. Double-click d (d~delete)
  3. Save: :w

The desired result (because the old line 11 is now line 10):

10 If you wanna get with me, better make it fast

Paste

Lines 11,12 need to be swapped, so we will cut line 11 and paste it after line 12.

11 Get your act together we could be just fine      -> Lines swapped
12 Now don't go wasting my precious time
  1. Go to line 11.
  2. Cut the line with double-click on d
  3. Paste by pressing p (stay where you are, it will paste under, don’t worry)
  4. Save: :w

The desired result:

11 Now don't go wasting my precious time
12 Get your act together we could be just fine
-> Lines swapped

Copy Characters

In line 6 we need to copy the content of the line and paste it three times.

6 I wanna, (ha)             -> Insert another three "I wanna, (ha)"

There are two ways to copy the content:

  1. Enter INSERT mode: Click i
  2. Mark the whole line (with the mouse!)
  3. Copy with the good old command+C .
  4. Exit INSERT mode with esc

or

  1. Go to the beginning of line 6.
  2. Enter VISUAL mode: Clickv
  3. Go with the right arrow to the end of the line.
  4. Press y (y~yank)
  5. Exit VISUAL mode with esc

— — —

For the non-native English speakers out there, I’ve got you covered:

Screenshot from Google search

And for those of you, like me, that are familiar only with the informal definition of the word “jerk”:

Screenshot from Google search

— — —

After copying:

5. Move the cursor to the desired location.

6. Paste with the good old command+v / with p (from the word paste)

7. Save: :w

  • If you are accidentally missing spaces, like this:
6 I wanna, (ha)I wanna, (ha)I wanna, (ha)I wanna, (ha)  -> Insert another three "I wanna, (ha)"

Don’t worry! Just enter INSERT mode and add those spaces :)

The desired result:

6 I wanna, (ha) I wanna, (ha) I wanna, (ha) I wanna, (ha)  -> Insert another three "I wanna, (ha)"

Copy a Block

In line 14 there is another chorus but it’s missing the lines. We can complete it by copying lines 5–7

14 I'll tell you what I want, what I really, really want
15 ..... -> Missing lines 5-7
  1. Go to the beginning of line 5.
  2. Enter VISUAL LINE mode: Click V (capital V)
  3. Go with the bottom arrow to line 7.
  4. Press y
  5. Move the cursor to the beginning of line 14.
  6. Paste with the good old command+v / with p
  7. Go to line 18 and delete the dots with double-click on d
  8. Save: :w

The desired result:

14 I'll tell you what I want, what I really, really want
15 So tell me what you want, what you really, really want
16 I wanna, (ha) I wanna, (ha) I wanna, (ha) I wanna, (ha)
17 I wanna really, really, really wanna zigazig ah
-> Missing lines 5-7
18

Make sure line 18 is empty!

  • You can also copy a block with command+c like we did before, but it is a bit annoying because it copies the line number as well (you don’t believe me? Try for yourself!).

Search

Oops! I wrote “cup of code” somewhere, but I don’t remember where! We should delete that, change to the original lyric.

If you wanna be my cup of code, you gotta get with my friends -> change "cup of code" to "lover"
  1. Type in /cup of code and press Enter. It will get you to the first occurrence of the phrase.
  2. Enter INSERT mode: Click i
  3. Erase “cup of code” (don’t worry, the search got your curser to the right location)
  4. Insert “ lover”.
  5. Exit INSERT mode: esc
  6. Save: :w

The desired result:

19 If you wanna be my lover, you gotta get with my friends -> change "cup of code" to "lover"

Replace All

Oops! We have “zigaziga” instead of “zigazig ah”! We need to search for all the occurrences and replace all!

7 I wanna really, really, really wanna zigaziga.  -> Supposed to be "zigazig ah"
  1. Type in: :%s/zigaziga/zigazig ah/g ( `%` means for all lines in the file)
  2. Press Enter.
  3. Save: :w

The desired result (because we have two occurrences):

7 I wanna really, really, really wanna zigazig ah.  -> Supposed to be "zigazig ah"

17 I wanna really, really, really wanna zigazig ah. -> Supposed to be "zigazig ah"
  • If you don’t want to replace all, you can use
    :start,end s/search/replace/g. In that way, the replace will be performed only in the desired block (including end).

Comment a Block

I decided I don’t want the last three lines to appear, so let’s comment them out!

21 Make it last forever, friendship never ends  -> Comment (// ) and uncomment lines 21-23
22 If you wanna be my lover, you have got to give
23 Taking is too easy, but that's the way it is
  1. Go to the beginning of line 21.
  2. Enter BLOCK VISUAL mode: Click control+ v
  3. use the ↓ arrow key to select lines 22,23 as well (it won’t highlight everything like VISUAL LINE mode does, but don’t worry!)
  4. Shift + i (~capital I)
  5. Insert //
  6. Press Esc.
21 // Make it last forever, friendship never ends  -> Comment (// ) and uncomment lines 21-23
22 // If you wanna be my lover, you have got to give
23 // Taking is too easy, but that's the way it is

Uncomment a Block

You know what? I think we should take the marks off, and let’s ignore the fact that undo will have the same effect ;)

  1. Go to the beginning of line 21.
  2. Enter BLOCK VISUAL mode: Click control+ v
  3. Use the → arrow key to select the characters you want to delete (// )
  4. Use the ↓ arrow key to select lines 22,23 as well.
  5. Press x
  6. Save :w

Delete My Instructions

Our “wannabe.txt” file isn’t done, because it still has all my instructions.

1 Ha ha ha ha ha                            -> Insert another "ha"
...
4 I'll tell you what I want, what I really, really want
...
6 I wanna, (ha) I wanna, (ha) I wanna, (ha) I wanna, (ha)
-> Insert another three "I wanna, (ha)"
7 I wanna really, really, really wanna zigazig ah.
-> Supposed to be "zigazig ah"
...
12 Get your act together we could be just fine
-> Lines swapped
...
16 I wanna, (ha) I wanna, (ha) I wanna, (ha) I wanna, (ha)
-> Insert another three "I wanna, (ha)"
17 I wanna really, really, really wanna zigazig ah.
-> Supposed to be "zigazig ah"
...
19 If you wanna be my lover, you gotta get with my friends
-> Change "cup of code" to "lover"
...
21 Make it last forever, friendship never ends
-> Comment (// ) and uncomment lines 21-23

Going over the file, line by line, and deleting the instructions with x or VISUAL mode seems daunting. This is a good example for a task that will be performed quicker in vim rather than other text editors.

Let me introduce you to control+D, that deletes all the characters under the cursor until the end of the line.

For every occurrence of instruction:

  1. Go to the beginning of the arrow (or somewhere between the end of the lyric line and the arrow).
  2. Press control+D.

The desired output:

1 Ha ha ha ha ha
...
4 I'll tell you what I want, what I really, really want
...
6 I wanna, (ha) I wanna, (ha) I wanna, (ha) I wanna, (ha)
7 I wanna really, really, really wanna zigazig ah.
...
12 Get your act together we could be just fine
...
16 I wanna, (ha) I wanna, (ha) I wanna, (ha) I wanna, (ha)
17 I wanna really, really, really wanna zigazig ah.
...
19 If you wanna be my lover, you gotta get with my friends
...
21 Make it last forever, friendship never ends

We Are Done!

Today we learned how to do basic actions with vim:
Open, exit, save, insert, undo, delete characters, cut a line, paste, copy characters, copy a block, search, replace and replace all.

Vim has many more commands and it takes a while to gain the skills using it, but these are the basics, and it’s a good place to start. Mastering this editor requires practice that some of my colleagues would swear is worthwhile.

During my research I found a cute and useful cheatsheet:

To make sure everything went smoothly, I recommend listening to the song while reading the lyrics you created:

Another option is to compare your file to the lyrics:

And if you are really determined, you can use a text comparer.

I hope you enjoyed reading this article and found it useful. I would love to hear your thoughts, here in the comments or in a private message:
LinkedIn: www.linkedin.com/in/ifat-neumann
Facebook: https://www.facebook.com/cupofcode.blog/
Email: cupofcode.blog@gmail.com

--

--

Cup of Code

I write about things that interest me as a software engineer, and I find interest in various subjects :) || Come visit at www.cupofcode.blog