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
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
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
- save the changes made and exit:
:wq
alternatively you can use: :x
- forcefully exit without saving recent changes made to the file:
:q!
Saving changes
To save the recent changes made to the file:
:w
Copying lines
- 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.
Pasting
To paste content into the line the cursor is on.
P
Cutting
- 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$
Navigating cursor
- 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.
Undoing
- undo last change:
u
- undo multiple changes:
#u
Redoing
redo last change:
Ctrl + r
redo multiple changes:
#Ctrl + r
Deleting
- 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.
Top comments (5)
Heyo! I noticed ya got another post that is connected up to this one:
Command Line Editors Tutorial pt 1 : EMACS
sahra ใป Nov 18 ใป 2 min read
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!
A short guide on how to โmakeโ the series widget on DEV
Dzhavat Ushev ใป Sep 27 '19 ใป 2 min read
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!
Create a "Series" from Markdown Editor
Joel Louzado ใป Feb 2 '21 ใป 2 min read
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:Feature update: Rich Content Embeds
Arit Developer for The DEV Team ใป Feb 16 ใป 1 min read
Oh wow just getting to know about this....I would definitely do that, thank you ๐
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! ๐
Thank you for sharing my post ๐
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. ๐