In this guide, we have presented all basic commands that you will need to use VIM editor for doing all tasks efficiently. You will learn how to search for a word, how to replace the word, select, copy, paste, using various special features of VIM and much more. Vim is an editor to create or edit a text file. There are two modes in vim:
- Command mode
- Insert mode
- In the command mode, user can move around the file, delete text, etc.
- In the insert mode, user can insert text.
To create a new file named "opengenus.txt", use the following command:
This will create the file and open the file in VIM editor where you can add text.
If there is an existing file with the same name, the existing file will be opened in the VIM editor.
Changing mode from one to another
From command mode to insert mode, press anyone of the following keys: a/A/i/I/o/O ( see details below)
From insert mode to command mode, press Esc (escape key)
Some useful commands for VIM
a Append text following current cursor position
A Append text to the end of current line
i Insert text before the current cursor position
I Insert text at the beginning of the cursor line
o Open up a new line following the current line and add text there
O Open up a new line in front of the current line and add text there
The following commands are used only in the commands mode.
h Moves the cursor one character to the left
l Moves the cursor one character to the right
k Moves the cursor up one line
j Moves the cursor down one line
^F (CTRl F) Forward screenful
^B Backward screenful
^f One page forward
^b One page backward
^U Up half screenful
^D Down half screenful
$ Move cursor to the end of current line
0 (zero) Move cursor to the beginning of current line
w Forward one word
b Backward one word
To exit from VIM editor, use the following commands after entering the ESCAPE mode:
:wq Write file to disk and quit the editor
:q! Quit (no warning)
:q Quit (a warning is printed if a modified file has not been saved)
ZZ Save workspace and quit the editor (same as :wq)
Text Deletion Commands
Use these commands in ESCAPE mode:
To use these commands, you need to press the keys (for example for command dw, first press d key, then press w key):
x Delete character
dw Delete word from cursor on
db Delete word backward
dd Delete line
d$ Delete to end of line
d^ (d caret, not CTRL d) Delete to beginning of line
File Manipulation Commands
:w Write workspace to original file
:e file Start editing a new file
:r file Read contents of a file to the workspace
Go to end of the file
Esc + Ctrl + End – Jump end of file
Esc + Ctrl + Home : Jump start of file
Press Esc + gg : Go to top the file
Esc + G : Go to bottom of the file
Esc + G + A : Go to bottom of the file and in append text mode.
To search forward, use the command:
- Press Esc to make sure Vim/Vi is in normal mode.
- Type the command:
Searching Backward For a Word
Press Esc to make sure Vim/Vi is in normal mode.
Type the command:
search and replace
Find each occurrence of 'Search_Word' (in the current line only), and replace it with 'Replace_Word'.
Find each occurrence of 'Search_Word' (in all lines), and replace it with 'Replace_Word'.
Change each 'Search_Word' to 'Replace_Word', but ask for confirmation first.
select text, copy and paste
--> Position the cursor at the beginning of the text you want to cut/copy.
--> Press v to begin character-based visual selection, or V to select whole lines, or Ctrl-v or Ctrl-q to select a block.
--> Move the cursor to the end of the text to be cut/copied. While selecting text, you can perform searches and other advanced movement.
--> Press d (delete) to cut, or y (yank) to copy.
--> Move the cursor to the desired paste location.
--> Press p to paste after the cursor, or P to paste before.
Difference b/w Vi and Vim
-----> Editor Interface VIM vs VI
Both the editors are similar in their appearance.
-----> Modes in VIM vs VI
Both the editors are mode based, that is you need to enter INSERT mode to edit and COMMAND mode to perform actions like saving and undo.
The three modes are as follows:
Normal (Default): For navigation
Insert: For inserting and modifying text
Command: For Operations like saving, exiting, etc.
Insert mode allows you to make changes to your content.
The command mode is for specifying commands to exit, save your work, and perform other operations.
To enter command-line mode hit the “Escape” key and then type in the colon ‘:’. To enter insert mode, hit the “Escape” key and type in ‘i’. To enter Normal mode press Escape.
Navigating within the editor interfaces
Searching for text
Both the editors provide the option to search for text in the file.
To search in the file you must be in command line mode. So start by pressing colon (:).
To search for a word use forward slash followed by the word to search followed by enter.
Improvements in Vim – The differences between VIM vs VI
Vim editor differs from Vi editor since it provides a lot of improvements over the latter. The command to display the points of difference within the Vim editor is:
You can scroll through the list to go over the differences.
Multiple Undo in VIm
Vi editor only offers the option to perform one undo. This can be very limiting in the case of a large text file.
Vim editor on the other hand offers the ability to perform multiple undo.
With this, you must have the complete idea of using VIM editor. Enjoy.