A Beginner Guide on How to Use the Vim Editor on Linux or Mac

kennibravo

Kehinde Alade

Posted on October 7, 2022

A Beginner Guide on How to Use the Vim Editor on Linux or Mac

OK, a quick Vi story.

It's 1am in the morning in the year 2060, you could faintly hear the sound of a mechanical engine whirring, struggling to understand the situation, you opened your eyes and found yourself in a spaceship making a free fall towards the earth, while panicking you began to find the controls, fortunately you saw one, but it's a ugly black screen, on it is a white cursor blinking as if it were laughing at you cos you're in real mess, the spaceship is about to crash and you can't control it, the only thing you know about Vi are the memes you've once laughed at, hahaha.

Vim Memes

Uhm, so back to business, Vi has two/three modes namely;

  1. Command Mode
  2. Insert Mode
  3. Last line Mode

Note: Any command or explanation here will also work for VIM, which is an improved version of Vi.

This is only a beginner guide so I won't be wasting your time with lot of stories, well, except that boring Vi story you read.

Command Mode:
This is often the most confused mode in Vi, you remember that time you mistakenly opened your Vi editor, then you start hitting all the keys on your keyboard, you were in the command mode, the command mode is the default mode in Vi, open up your terminal or CLI app then copy and paste this;

echo "Here is the first file in VIM" >> firstText.txt

Enter fullscreen mode Exit fullscreen mode

The above line just output the content "Here is the first file in VIM" into the firstText.txt file.

Now let's open up the file using Vi.

vi firstText.txt

Enter fullscreen mode Exit fullscreen mode

And then boom that ugly and unattractive screen spuns up and it looks like this, mine is light, yours could be dark;

Vim

As at this instance, we're currently in the Command Mode because it is the default mode, in command mode, as opposed to normal text editors, you can't type or insert text in your file, you can only issue commands like Undo, Redo, Quit, Save, etc.

You can move around in the command mode using your arrow keys.

arrow-keys.jpg

Let's try out some commands in the command mode!

  • Delete character: x

Press x, this deletes the current character you're on and it looks like this;

Vim

Keep on pressing x until it deletes all characters and looks like this.

Vim

Let's try to undo the characters we deleted.

  • Undo: u

Keep pressing u till all the text appears.

Vim

Instead of pressing x multiple time, you can just;

  • Delete whole line: dd

Press dd and it deletes the whole current line and looks like;

Vim

You can Undo the changes so all the text appears back.

There are also other commands like;

  • Copy: yy
  • Paste: p

Commands aside, let's continue to the other mode, we'd be seeing lot of commands, continue reading.

So far, we've played around in the command mode, but never for once did we actually insert data, uhm, can we not insert data like we will in a GUI text editor?

Yeah, you guessed right!

We can't!

Sorry, I mean we can, just kidding.

Insert Mode:
In this mode, you can insert data as you would in a GUI text editor, but how do we get to this mode? I mean we can't insert data in Command mode.

From command mode, there are lot of ways we can goto the Insert mode, one is pressing i on your PC.

Vim

We're currently in the command mode in the image above, you can try type stuffs here but it won't go through until you goto Insert mode, so press i (small letter 'i') to switch to Insert mode;

Vim

Bottom left in the terminal, you can notice a *-- INSERT -- *, this means we're currently in the Insert mode and can now start typing.

In Insert mode, you can type as you would type in a normal text editor.

Your backspace key would work, even Ctrl + C to Copy and Ctrl + V for Paste even the Enter key and others would work, so you can play around in here.

Let's try adding more data into the file and save it, erase all data in the file and type;

This is to test out the Insert Mode

Note: Type it yourself, don't copy it from here.

Vim

Let's try to save the file and exit Vi.

We need to go back to the Command Mode in order to save this file and exit the editor, since we can switch to Insert Mode from Command Mode by pressing i, how do we go back to the Command mode?

Very easy! just hit your ESC key(while in insert mode) on your PC.

Vim

Now we're back to the Command Mode.

  • Save: :w
  • Quit: :q
  • Save and Quit Vi: :wq
  • Quit without saving: :q!

Let's try to Save and Quit Vi, press :wq

Vim

Note:: Whenever you press : while in Command Mode, it takes you to the Last Line Mode

After you've typed in :wq, hit the Enter key.

Vim

And boom! We're Saved and exited Vi.

Let's verify the changes we made to the firstText.txt in our terminal.

cat firstText.txt

Enter fullscreen mode Exit fullscreen mode

Here is the output, cat is used to output the content of a file.

Vim

We can also use Vi to create a file and edit it immediately.

vi secondText.txt

Enter fullscreen mode Exit fullscreen mode

This creates the secondText.txt file and open with Vi.

This is the only basics you have to know to start using Vi, I attached a cheatsheet to this article so you can get more shortcuts from it.

Here is a link to a Cheatsheet: https://cheatography.com/ericg/cheat-sheets/vi-editor/pdf/

Now that you can use Vi, you can go on and brag to your friends and even pick up girls with it, I mean who doesn't love someone who can use Vi.

Vim meme

Takeaways from this article.

  • Command Mode is the default mode for Vi
  • Use i to switch to Insert Mode from Command Mode
  • Use ESC key to switch back to Command Mode from Insert Mode
  • Typing : in the command mode takes you to the Last Line Mode, you can then Save, Quit the file

So this brings us to the end of this article, I would making articles on Linux and DevOps generally, so keep close tab.

If you enjoyed the article, add a reaction for me. 😊

Keep Vi-ing high and Cheers!

💖 💪 🙅 🚩
kennibravo
Kehinde Alade

Posted on October 7, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related