VIM editor and its related commands

Linux Tutorial

Vim Editor is command line text editor, an enhanced version of ‘vi’. To open a file in vim use below mentioned command:-

$ vim myfile

There are 2 modes of vim we use.

  1. Command mode where we run commands.
  2. Insert mode where we do editing.

How to access these Modes of VIM?

ESC    - Command mode
INSERT - Insert mode and cursor remains at same position
i      - Insert mode and cursor remains at same position
I      - Insert mode and cursor moves to beginning of line
a      - Insert mode and cursor moves one character ahead
A      - Insert mode and cursor moves to hte end of line
o      - Create newline after current line and Insert mode
O      - Create newline before current line and Insert mode

Command to Save or Quit VIM editor

To Save or Quit vim editor use

ESC:w  - Save
ESC:q  - Quit
ESC:wq - Save and Quit
ESC:x  - Save and Quit
ESC:w! - Force Save
ESC:q! - Fore Quit

There are many other commands in VIM editor to help you in editing the files with ease.

x                      - Delete character under cursor
yy                     - Copy current line
4yy                    - Copy 4 lines starting from current line
dd                     - Deletes current line
4dd                    - Deletes 4 lines starting from current line
p                      - Paste after cursor
P                      - Paste before cursor
gg                     - Go to first line
G                      - Go to last line
ESC:12                 - Go to 12th line
/<str-to-search>       - Search string in file
n                      - Go to next occurance of search result
N                      - Go to previous occurance of search result
ESC:%s/<search-str>/<replace-str>   - Replace first occurance of string in all lines.
ESC:%s/<search-str>/<replace-str>/g - Replace all occurances of string in all lines.

There is an interactive tutorial for vim editor which can be started with vimtutor command.

$ vimtutor