Command Line Editors Tutorial pt 2 : VIM

sahra 💫 - Nov 21 '22 - - Dev Community

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.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .