VIM Editor – Visual Display Improved.

By | April 3, 2018

VI is a text-based file editor available on all the Unix based system. When you are connected to any remote server via SSH, you will not have access to another user friendly file editor like gedit, emacs etc. So here comes the extensive use of VI/VIM editor. It a little bit tricky and requires some effort and more practice to learn as it does not have any interface with mouse or menus. Once you will start with this editor you may find it a bit difficult and irritating, but once you do your hands dirty on this, you may find it one the powerful and easiest text editor in Unix Systems. 

VIM (VI Improved) is a superset of VI and both are identical with some improved supported functionality in VIM Editor.

  • VIM support syntax highlighting for several popular programming languages (C/C++, Python, Perl, shell, etc).
  • It allows multilevel undo and redo.
  • You can open multiple files for editing by splitting the screen using VIM.
  • Can edit compressed files.
  • It supports for many powerful programming languages and file formats.
  • Powerful Search and replace functionality.
  • Colorful text highlighting looks good as compared to VI.

VIM Editor has basically three modes 

  • Insert Mode
  • Command Mode
  • Extended Command Mode

In Order to edit any files in VIM Editor, we just need to type “vim samplefile.txt” (Commands in Linux/Unix is case sensitive). If we simply type “vi”, it will open the text editor without any filename and opens as an unnamed black file. Though we can edit and save it later at any time.

Note: When we open any files in VIM Editor, by default it stays in command mode.

1. Insert Mode

Insert mode is the mode in which we can start adding the content to the files. Unless and until the file is not in the insert mode we will be unable to add contents or type a word to a file. Below are the alphabets/options which you can use to land in insert mode.

  • i:    To get into the insert mode at the cursor point itself.
  • I:    To get into the insert mode and be at the beginning of a line.
  • a:    To move forward to the next word’s letter.
  • A:   To get into insert mode and be at the end of a line.
  • o:    To insert into a new line exactly below the cursor point.
  • O:   To insert a new line exactly above the cursor point.

 

2.  Command Mode.

In the command mode, you can take your cursor at any point of the file, can copy/paste, cut/paste and can do a lot of other this which we will see below.

  • h:    Works same as left navigation key.
  • i:     Works same as right navigation key.
  • j:     Works same as down navigation key. 
  • k:    Works same as up navigation key.
  • gg:   To move your cursor at the beginning of the page
  • G:    To move your cursor at the end of the page. (10G, at the 10th line)
  • b:     To move the cursor backward (word by word).
  • w:    To move the cursor forward (word by word).
  • nw:  To move the cursor forward to  “n” words, here “n” can be any number (ex: 10w: will  take 10 words forward)
  • nb:   To move the cursor backward to  “n” words, here “n” can be any number (ex: 10w: will  take 10 words backward)
  • u:     To undo last changes 
  • U:    To undo entire line (previous changes)
  • yy:    To copy a line
  • nyy:  To copy “n” lines, “10yy” will copy 10 lines.
  • p:      To paste the copied line exactly below the cursor point.
  • P:     To paste the copied line exactly above the cursor point. 
  • D:    To delete an entire line from the cursor point. (Same as D$)
  • ctrl+R: To redo the changes.
  • dw:  delete the word letter by letter, like a backspace.
  • x:    delete (cut) the word letter by letter like  Delete key. (nx)
  • dd:   To delete the entire line, it can also be used as “cut” (dd: cut, p: paste).
  • ndd:  delete “n” numbers of the line from the cursor point.
  • ^:      begining of the line.
  • $:      end of the line.
  • ^B:   back one page.
  • ^F:   forward one page.
  • dG:   Delete entire lines from the cursor point.
  • /: To Search a word in the file.  (/devopsage: This will Search for word “devopsage” in the entire file) – jump forward to the first occurrence
  • ?:  Search a word in the file (?devopsage: Will Search for devopsage) – jump backward to the first occurrence.
  • r:    replace 1 character under the cursor with a character immediately after “r”.
  • 10r: replace each of the next 10 characters with the character typed immediately after “r”.
  • R:   Overwrite, replace text typed input, ended with “esc”.
  • C:   Replace from the cursor to end of the line.
  • S:    Substitute entire line with input.
  • cw:   Replace Word.
  • xp:   exchange to character (its two command x=delete alphabet, p=paste)

 

3. Extended Command Mode.

Extended Mode can be used in various ways like It can be used to save, quit, save and quit the file using “esc” key with “:”. It can also be used to set line number, find and replace and a lot of other things can be done. Below are few examples.

Esc+:w  : To save the changes.   

Esc+:q   :   To Quit without saving.

Esc+:wq  :   To save and quit.

Esc+:x    :   To Save and quit.

Esc+:w!  :    To Save Forcefully.

Esc+:q!  :     To quit forcefully.

Esc+:wq!  : Save and quit forcefully.

Esc+:X     :  To Give password and to remove the applied password to a file.

Esc+:20    :   To go to line number “20”.

Esc+: se nu :  To set the line number.

Esc+se nonu:  To remove the line numbers

Esc+:s/linux/unix : Substitute the first occurrence of linux in current line by unix.

Esc+:s/linux/unix/g  : Substitute all occurance of linux in the current line by unix.

Esx+:1,20 s/linux/unix/  : find and replace only from line number 1 to 20.

Esc+:%s/linux/unix/g  : Substitute every linux to unix in the entire file (find and replace).

Esc+:20,30w devopsage.txt: write from line 20-30 to another file “devopsage.txt”.

 

To Open Multiple files in VIM Editor.

# vim -o file1 file2

Use ctrl+w to switch between the files.

That’s it for this post on VI/VIM Editor. It requires a lot of practice to keep a hold on this editor and of course, it requires some time.

 

One thought on “VIM Editor – Visual Display Improved.

Leave a Reply

Your email address will not be published. Required fields are marked *