VIM Editor

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

Vim is an advanced and highly configurable text editor built to enable efficient text editing. Vim text editor is developed by Bram Moolenaar. It supports most file types and vim editor is also known as a programmer’s editor. We can use with its plugin based on our needs.

Also vim, short for Vi Improved is a command-line text editor. In this vim tutorial, you are going to learn useful tips in using the vim text editor. Without much ado, let’s jump right in.

Installation and Configure vim in Our Linux System

Install VIM Editor on Red Hat or CentOS run the command:

yum install vim

Also you can install vim on Debian based Linux like ubuntu run the command:

 sudo apt-get install vim

And To install vim on an arch-based distro run the following command:

sudo pacman -S vim

Now vim will be installed on your system. 

Vim modes

vim has 3 modes: insert mode, execute mode and escape mode

  1. Insert mode allows you to enter text into the text file like you would in a normal file editor.
  2. Exceute mode gives you the power to navigate and make modifications to the text.

Navigate between the 2 modes, use the ESC character or .

You can open vim by running vim command on the terminal.

vim 
VIM Editor Window

There are some commands given to use the vim editor. You can see all commands, and it’s documentation by help command as follows:

:help

Now to exit type command 

:!q

or

 :exit
 :x

-To save and exit where changes exist use:

 :e

Now, Let’s Start to use Vim

— To open a file in vim editor just write the file name after the vim command in the terminal as follows:

vim unixcop.txt

Then the file will be opened.

Write into file :

In the previous step we have opened the file now, Let’s write some content in to write data we need to go in insert mode. To go into write mode type ‘i’.  As follows:

 i

After going into insert mode you will see INSERT in the status bar. After that, we can write any data in it.

Save and Exit:

We have written the data into a file now the task is to save and close the file to do that first exit from insert mode by pressing the Esc key. To write a command first type semicolon  (  :  )  and then type the command wq!  And then hit ENTER.

 :wq!

Exit without saving the file:

So to exit from the file without saving the file just use the command q! As follows

 :q!

Vim also comes with its own tutorial. You can see this tutorial by command vimtutor into the terminal .

vimtutor

Moving the Cursor:

Till now, we are using the arrow keys to move the cursor into a file, but it is not recommended to use arrow keys in vim. Vim provides the special key to move the cursor in the vim editor following are the keys used to move into a file.

* k   ->   move up                                 ^
* j   ->  move down                                k
* h   -> move right                        <  h          l >       
* l   ->                                           j
                                                   v

Just use these commands in vim files and move the faster cursor into the files.

Exiting Vim:

Exit without doing anything go into command mode by pressing the Esc key and type the following command.

 :q

To exit from vim without saving changes type the following command in vim

 :q!

Quit and save changes type the following command

 :wq

Text Editing:

Text Editing: Deletion

We provide x key in command mode to delete the character under the cursor. Move the cursor to the character which has to delete and press Esc key  and then press the x key

 x

The character under the cursor will be deleted.

Deletion Commands: Always use the Esc key to go into normal mode and use the insertion, deletion keys, and other keys. 

 move the cursor to the beginning of the word and use dw command in normal mode. The word under the cursor will be deleted.

 dw

To delete more than one word in a single line use the following command.

for example to delete 2 words use the command

 d2w

Delete the line move cursor to the beginning of the line and use d$ command in normal mode. The line under the cursor will be deleted.

 d$

To delete or cut the entire line hit 

 dd

NOTE:
You can delete a number of lines by preceding the dd command with a number. For example, to delete 3 lines including the current line, run 3dd

Text Editing: Insertion

We have edited some text files before by using the ‘i’ key. There are Four keys used for insertion text. Just type the key into the normal mode in vim.

 i    ->   This key is used to put the cursor before the current position.
 a    ->   This key is used to put the cursor after the current position.
 o    ->   This key is used to put the cursor below the line.
 O    ->   This key is used to puts the cursor above the line.

Text manipulation:

Motion :

Motions provide context to your Operators. These execute the action in a particular way.

Here is a list of some motions

 w - until the start of the next word, EXCLUDING its first character.
 e - to the end of the current word, INCLUDING the last character.
 $ - to the end of the line, INCLUDING the last character.

We can use motion with the d key and with many more keys.

Count :

Count is the number for which replete the motion for count number. Here is a demonstration of the use of count and motion

To move courser 2 words forward use the following command

 2w

Here 2 is the number of counts and w is used for word

& To move cursor 4 line forward use the following command

 4$

Undo and Redo :

As we are programmers most time we are using undo and redo .vim to provide these to both features in it. To undo press u key in normal mode

 u

Redo use the ctrl+r key in normal mode in vim

  ctrl+r

Replace :

Replace the character under the cursor use rx command where ‘x’ is a character is to replace.

 r
Change Operator:

In vim c is used as a change operator. To replace the word use ce command

 ce

Replace the line use c$ command

 c$

This command will delete the contains from the cursor to the end of the line. Then this will automatically go into insert mode then you can put anything in that line.

Cursor location:

We can use the h,j,k,l key to move the cursor in the file but it will be difficult to move in big file vim provide more commands to move into the file.

.. Move the cursor at the start of the file use the gg command

 :gg

Move the cursor at the bottom of the file use the G command

 :G

View the current cursor location in the file using the following command:

 :ctrl+g

In place of 32 use can put any line number.

Search:

Search the word After the cursor uses the backslash key and then write the word and press enter.

 :/word

Use n to move on next matching word

 :n

-And use N to move on previous matching word

 :N

-Search backwards in the text file, again press the ESC key and use the ? followed by the search term e.g

 :?Linux

Search and Replace:

Replace the word in file use s/ command that refers to substitute in vim like

 :s/searchword/replaceword/

Do replace all occurrence of word use that refers to global replacement

 :s/searchword/replaceword/g

This command will replace the word globally.

Confirm before replacements of words use gc

  :s/searchword/replaceword/gc

Use this command in the whole file use % before the command

 :%s/searchword/replaceword/gc

Also if you want to search a string and replace it with another string in the file, use the syntax

 :[range]s/search/replace/

For example, if we want to search the string Linux and replace it with Unix from Line 1 to line 3 in code>linuxgeek.txt file , the command will be

 :1,3 s/Linux/Unix/g

Copy :

Copy a string and paste it at another location in a file follow the steps below

  1. Move the cursor to the beginning of the string or text
  2. Type 'v' on your keyboard and press cursor forward to highlight text
  3. Once you get to the end of text , hit 'y' short for yank , to copy the text
  4. Move the cursor to the location you want to paste the copied text
  5. Hit 'p' short for paste, to paste the copied content

Additionally, you can use the commands below for copying lines.

y$ â€“ Copies text from current position to the end of the line

yy â€“ Copies the entire line

4yy â€“ Copies 4 lines below

Vim configuration:

Configure vim, vim comes with the .vimrc file in the home directory if this file is not there then create one. Then open the file in vim by command 

vim ~/.vimrc

You can put your all configurations in this file.

In addition to you can set line number by using a simple command

set number 

Put this command in your .vimrc file

Enable syntax highlighter in vim use command

syntax on

And Put this command in .vimrc file then save file and exit after opening file again you will see the line numbers in vim.

Vim comes with many color schemes using the command:

colorscheme COLOR_SCHEME_NAME

Replace COLOR_SCHEME_NAME with any color scheme between Default, blue, darkblue, delek, desert, elford, evening, industry, koehler, morning, murphy, pablo, peachpuff, ron, shine, slate, torte, zellner then save file and exit then again open file to see changes.

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
MQ-Jr
MQ-Jr
unixcop Admin

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook