A Beginner Guide on How to Use the Vim Editor on Linux or Mac
Tags: linux, vim, python, devops
Published: 2022-10-07 14:43:49

*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](https://cdn.hashnode.com/res/hashnode/image/upload/v1661598229653/8Byh7m6_f.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;

![Vim](https://cdn.hashnode.com/res/hashnode/image/upload/v1661523157343/z_wl7fR5R.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1661597820040/suJ1R39Wx.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1661523691251/0FGDo4OGV.png)

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


![Vim](https://cdn.hashnode.com/res/hashnode/image/upload/v1661523781080/p2jdqdtcU.png)

Let's try to **undo** the characters we deleted.

- Undo: **u**

Keep pressing **u** till all the text appears.


![Vim](https://cdn.hashnode.com/res/hashnode/image/upload/v1661523870858/WIJ1qOsHm.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;


![Vim](https://cdn.hashnode.com/res/hashnode/image/upload/v1661524045110/dTmiGgef3.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. 


![Vim](https://cdn.hashnode.com/res/hashnode/image/upload/v1661524797222/I-JPUilaQ.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;


![Vim](https://cdn.hashnode.com/res/hashnode/image/upload/v1661524958840/CQoD3C68c.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.


![Vim](https://cdn.hashnode.com/res/hashnode/image/upload/v1661525412390/Ztp_47uOk.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.


![Vim](https://cdn.hashnode.com/res/hashnode/image/upload/v1661525666546/8sDdWRDTx.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**

![Vim](https://cdn.hashnode.com/res/hashnode/image/upload/v1661525871705/gTZQsEnAE.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.


![Vim](https://cdn.hashnode.com/res/hashnode/image/upload/v1661526045354/72IKO1RCo.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.

![Vim](https://cdn.hashnode.com/res/hashnode/image/upload/v1661526235673/gBAOHc0gQ.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: 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1661598117268/URZgmlnIq.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!