Command Line Editors Tutorial pt 2 : VIM
Tags: cli, tutorial, vim, programming
Published: 2022-11-21 12:56:34

---
title:Command Line Editors Tutorial pt 2 : VIM
published: true
tags: CLI, tutorial, vim,  programming
Description: 
canonical_url: https://dev.to/sarahokolo/command-line-editors-tutorial-pt-2-vim-hma
series: "Command Line Editors Tutorial"
---

In the previous article, we took a look at what Command Line Editors are, and one of them called EMACS.

**ICYMI**, You can check it out [here](https://dev.to/sarahokolo/command-line-editors-tutorial-pt-1-emacs-40mk). 

Today we are going to be taking a look at another one of the command line editors, which is **VIM**


![Vim logo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s5cks31jsmjj4v68uhwz.png)

##What is VIM?
Vim is acronym for **Vi IMproved**
It is a highly configurable Unix text editor. It can be found or included in any variant of the Unix operating system.
It was written by Bram Moolenaar and was first released in the year 1991.

VIM is a Successor of the popular VI editor. It still supports all of VI's keybindings just with more features.

So in simple terms, VIM is VI with extra features.

To open a file in the VIM editor,  we simply use the command:

```
$ vim filename
```

###VIM Modes
The VIM editor basically has two modes and they are:
**Command mode** and **Insert mode**

**<u>Command mode:</u>** This mode is used to input the different VIM commands that performs various tasks such as (undo, redo, delete, save, quit, etc).
To get into command mode , you simply press the `Esc` key on your keyboard.

**Note:** The editor always starts up in command mode.

**<u>Insert mode:</u>** In this mode, the user can insert text into the current opened file in the editor.
There are a few commands which we can use to get into insert mode in VIM, let's take a look at them:

 **i** : switch to insert mode before the cursor.

 **I** : switch to insert mode at the beggining of the line.

 **a** : switch to insert mode after the cursor.

 **A** : switch to insert mode at the end of the line.


**Note:** You cant write text into a file when you are still in command mode, and you can't also make use of the various VIM commands when you are still in insert mode.

The VIM editor provides a very long list of commands that are to be used to perform various tasks.  Because of the lenght of these commands, we can't simply cover them all in this article. So let's have a look at some of the most basic commands to get acquainted with in order to be able to use the editor.

###VIM Commands
**<u>Exiting Vim</u>**
Here are a few ways to exit VIM:
- exiting when no changes have been made to the file:  
```
:q
```
- save the changes made and exit:
```
:wq
```
alternatively you can use: `:x`

- forcefully exit without saving recent changes made to the file:

```
:q!
```

**<u>Saving changes</u>**
To save the recent changes made to the file:
```
:w
```

**<u>Copying lines</u>**
- To copy the content of the line the cursor is on:
```
yy
```
- To copy the a specified number of lines starting from the line the cursor is on:

```
#yy
```
Where **#** in the command represents the number of lines to copy. e.g *5yy*, 7yy* etc.

**<u>Pasting</u>**
To paste content into the line the cursor is on.

```
P
```

**<u>Cutting</u>**
- To cut out the current line:
```
dd
```

- Cut a specified number of lines:
```
#dd
```
Where **#** in the command represents the number of lines to cut e.g *5dd*, 7dd* etc.

- Cut everything after the cursor:
```
dd$
```

**<u>Navigating cursor</u>**
- move cursor to the beginning of current line:
```
^
```
- move cursor to the end of current line:
```
$
```
- move cursor left, right, up or down:
`h`, `I`, `k`, `j`. In that order.
Cursor movement can also be so with the arrow keys.

**<u>Undoing</u>**
- undo last change:
```
u
```
- undo multiple changes:
```
#u
```

**<u>Redoing</u>**
- redo last change:
`Ctrl + r`

- redo multiple changes:
`#Ctrl + r`

**<u>Deleting</u>**
- delete a single line:
```
D
```

- delete multiple lines:
```
d#d
```

-delete all lines:
```
:%d
```

- delete a single word:
delete word after the cursor
```
dw
```


We have taken a look a few easy commands that helps us navigate through the VIM editor. There are still a whole lot of VIM commands out there, but the ones we have looked at are the most basic and most frequently used commands.

That's it for this article guys🤝. I hope you found it helpful 😊...if you did, please don't forget to leave a like👍.
I would also love to hear from you if you have any questions, kindly leave them in the comment section😊. And of course...if you don't want to miss any of my contents, give me a quick follow 😁. See you in the next article guys.