VI editor is a powerful command line based text editor that was originally created for Unix but has since been ported to various Unix & Linux distributions. In Linux there exists another, advanced version of VI editor called VIM (also known as VI Improved ). VIM only adds functionalities to already powerful VI editor, some of the added functionalities a
-
Support for many more Linux distributions,
-
Support for various coding languages like python, c++, perl etc with features like code folding , code highlighting etc
-
Can be used to edit files over network protocols like ssh and http,
-
Support to edit files inside a compressed archive,
-
Allows screen to split for editing multiple files.
Recommended Read: The (in)complete Guide To DOCKER FOR LINUX
Also Read: Informative guide to Gitlab tutorial for Beginners
Now let’s discuss the commands/options that we can use with VI/VIM i.e. VI editor basics. For the purposes of this tutorial, we are going to use VI as an example but all the commands with VI can be used with VIM as well. But firstly we will start out with the two modes of VI text editor,
Command Mode
This mode allows to handle tasks like saving files, executing a command within vi, copy/cut/paste operations, & tasks like finding/replacing. When in Insert mode, we can press escape to exit into command mode.
Insert Mode
It’s where we insert text into the file. To get into insert mode, we will press ‘i’ in command line mode.
Creating a file
In order to create a file, use
$ vi filename
Once the file is created & opened, we will enter into what’s called a command mode & to enter text into the file, we need to use insert mode. Let’s learn in brief about these two modes,
Exit out of Vi
To exit out of Vi from insert mode, we will first press ‘Esc’ key to exit into command mode & here we can perform following tasks to exit out of vi,
-
Exit without saving file- to exit out of vi command mode without saving of file, type : ':q!’
-
Save file & exit – To save a file & exit, type: ‘:wq’
Now let’s discuss the commands/options that can be used in command mode.
Cursor movement
Use the keys mentioned below to manipulate the cursor position
-
k moves cursor one line up
-
j moves cursor one line down
-
h moves cursor to left one character position
-
i moves cursor to right one character position
Note : If want to move multiple line up or down or left or right, we can use 4k or 5j, which will move cursor 4 lines up or 5 characters right respectively.
-
0 cursor will be at beginning of the line
-
$ cursor will be at the end of a line
-
nG moves to nth line of the file
-
G moves to last line of the file
-
{ moves a paragraph back
-
} moves a paragraph forward
There are several other options that can be used to manage the cursor movement but these should get the work done for you.
Editing files
Now we will learn the options that can be used in command mode to change our mode to Insert mode for editing the files
-
i Inserts text before the current cursor location
-
I Inserts text at the beginning of the current line
-
a Inserts text after the current cursor location
-
A Inserts text at the end of the current line
-
o Creates a new line for text entry below the cursor location
-
O Creates a new line for text entry above the cursor location
Deleting file text
All of these commands will be executed from command mode, so if you are in Insert mode exit out to command mode using the ‘Esc’ key
-
dd will delete the complete line of the cursor, can use a number like 2dd to delete next 2 lines after the cursor
-
d$ deletes from cursor position till end of the line
-
d^ deletes from cursor position till beginning of line
-
dw deletes from cursor to next word
Copy & paste commands
- yy to yank or copy the current line. Can be used with a number to copy a number of lines
- p paste the copied lines after cursor position
- P paste the copied lines before the cursor postion
These were some the basic operations that we can use with VI or VIM editor. In our future tutorials, we learn to perform some advanced operations with VI/VIM editors. If having any queries or comments, please leave them in the comment box below.
If you think we have helped you or just want to support us, please consider these :-
Connect to us: Facebook | Twitter | Google Plus
Become a Supporter - Donate us some of you hard earned money: [paypal-donation]
Linux TechLab is thankful for your continued support.
If you are interested in harnessing the power of Plug-ins and turning the all-powerful vim editor into a work-horse try this recent post: [Control Vim and Essential Plugins](https://www.companionway.net/post/control_vim_and_plugins/)