DEV Community

Cover image for Command Line Editors Tutorial pt 2 : VIM
sahra ๐Ÿ’ซ
sahra ๐Ÿ’ซ

Posted on • Edited on • Originally published at dev.to

Command Line Editors Tutorial pt 2 : VIM

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.

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

Vim logo

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
Enter fullscreen mode Exit fullscreen mode

VIM Modes

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

Command mode: 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.

Insert mode: 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

Exiting Vim
Here are a few ways to exit VIM:

  • exiting when no changes have been made to the file:
:q
Enter fullscreen mode Exit fullscreen mode
  • save the changes made and exit:
:wq
Enter fullscreen mode Exit fullscreen mode

alternatively you can use: :x

  • forcefully exit without saving recent changes made to the file:
:q!
Enter fullscreen mode Exit fullscreen mode

Saving changes
To save the recent changes made to the file:

:w
Enter fullscreen mode Exit fullscreen mode

Copying lines

  • To copy the content of the line the cursor is on:
yy
Enter fullscreen mode Exit fullscreen mode
  • To copy the a specified number of lines starting from the line the cursor is on:
#yy
Enter fullscreen mode Exit fullscreen mode

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

Pasting
To paste content into the line the cursor is on.

P
Enter fullscreen mode Exit fullscreen mode

Cutting

  • To cut out the current line:
dd
Enter fullscreen mode Exit fullscreen mode
  • Cut a specified number of lines:
#dd
Enter fullscreen mode Exit fullscreen mode

Where # in the command represents the number of lines to cut e.g 5dd, 7dd* etc.

  • Cut everything after the cursor:
dd$
Enter fullscreen mode Exit fullscreen mode

Navigating cursor

  • move cursor to the beginning of current line:
^
Enter fullscreen mode Exit fullscreen mode
  • move cursor to the end of current line:
$
Enter fullscreen mode Exit fullscreen mode
  • move cursor left, right, up or down: h, I, k, j. In that order. Cursor movement can also be so with the arrow keys.

Undoing

  • undo last change:
u
Enter fullscreen mode Exit fullscreen mode
  • undo multiple changes:
#u
Enter fullscreen mode Exit fullscreen mode

Redoing

  • redo last change:
    Ctrl + r

  • redo multiple changes:
    #Ctrl + r

Deleting

  • delete a single line:
D
Enter fullscreen mode Exit fullscreen mode
  • delete multiple lines:
d#d
Enter fullscreen mode Exit fullscreen mode

-delete all lines:

:%d
Enter fullscreen mode Exit fullscreen mode
  • delete a single word: delete word after the cursor
dw
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (5)

Collapse
 
michaeltharrington profile image
Michael Tharrington

Heyo! I noticed ya got another post that is connected up to this one:

If ya wanna, you might consider connecting them up together more formally by using our series feature.

Here's a really great guide from @dzhavat that shows ya how to do it!

If ya happen to be using our basic markdown editor, then you'll need to follow a slightly different process... thankfully @jlouzado has created a guide on how to do so!

By the way, if you're wondering how I keep embedding posts in this comment, the markdown is as follows {% embed https://... %} ... you can learn more about embedding posts and other things courtesy of this article written by @aritdeveloper who also updated this feature earlier on this year:

Collapse
 
sarahokolo profile image
sahra ๐Ÿ’ซ

Oh wow just getting to know about this....I would definitely do that, thank you ๐Ÿ˜Š

Collapse
 
michaeltharrington profile image
Michael Tharrington

Of course!! Happy to help out. And as you can see, I leaned heavily on other community members who have written really great guides. So props to them for making my job easy! ๐Ÿ™Œ

Collapse
 
dzhavat profile image
Dzhavat Ushev

Thank you for sharing my post ๐Ÿ™

Collapse
 
michaeltharrington profile image
Michael Tharrington

No probs at all! I really appreciate you writing this resource for the community. I'll probs lean on it again anytime I'm looking to show people how to create a series! So yeah, major thank you for making my job easier. ๐Ÿ™Œ