A Beginner Guide on How To Use the Vi Text Editor on Linux or Mac

A Beginner Guide on How To Use the Vi Text Editor on Linux or Mac

It's no doubt that Vi is the most used and popular Linux text editor, it's pre-installed in almost every Linux distro and difficult to use.

ยท

6 min read

Table of contents

No heading

No headings in the article.

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.

DWVgnPdXcAArUU9.jpg

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

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

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

Screenshot 2022-08-26 at 3.12.15 PM.png

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;

Screenshot 2022-08-26 at 3.21.08 PM.png

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

Screenshot 2022-08-26 at 3.22.49 PM.png

Let's try to undo the characters we deleted.

  • Undo: u

Keep pressing u till all the text appears.

Screenshot 2022-08-26 at 3.24.20 PM.png

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;

Screenshot 2022-08-26 at 3.26.46 PM.png

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.

Screenshot 2022-08-26 at 3.39.45 PM.png

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;

Screenshot 2022-08-26 at 3.42.21 PM.png

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.

Screenshot 2022-08-26 at 3.50.00 PM.png

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.

Screenshot 2022-08-26 at 3.54.09 PM.png

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

Screenshot 2022-08-26 at 3.57.38 PM.png

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.

Screenshot 2022-08-26 at 4.00.33 PM.png

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

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

Screenshot 2022-08-26 at 4.03.41 PM.png

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

vi secondText.txt

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: cheatography.com/ericg/cheat-sheets/vi-edit..

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.

1*qM-Y5x3pcnzdl0R4uDPKNA.jpeg

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!

ย