Linux vim Text Editor Commands

Vim means Visual Interface Modal. Vim is a Text Editor.
There are three primary modes of vim editor.
  1. Command Mode: This mode is used for file navigation, cut and paste, and simple commands.
  2. Insert Mode: This mode is used for text exiting.
  3. Exit Mode: This mode is used to save, quit, and open files.

How to open file in Vim

The easiest way to open a file in Vim is to specify it as an argument on the command line. To open the file called /etc/hosts, we can execute following command.
Ex. vim /etc/hosts
After opening a file, vim will start in the command mode.

How to edit new file

To open file with named "linux". Use following command to create new file with name linux.

How to switch to insert mode

i is used to switch to insert mode.
In the following table, find some of the keys we can use from command mode to move your cursor.

Key
Result
h
Cursor left one position
l
Cursor right one position
j
Cursor down one line
k
Cursor up one line
^
Move to the beginning of the current line
$
Move to the end of the current line
gg
Move to the first line of the document
G
Move to the last line of the document

How to saving files in insert mode

Saving files in Vim is done from ex mode. We can enter ex mode by pressing : (a colon) from within command mode. After you enter ex mode, the ruler will display a colon (:) and wait for a command to be typed.
Following is a list of commands to save and quit your current file from ex mode.
Command
Result
:wq
Save and quit the current file
:x
Save the current file if there are unsaved changes, then quit.
:w
Save the current file and remain in editor
:w <filename>
Save the current file under a different file name.
:q
Quit the current file (only if there are no unsaved changes)
:q!
Quit the current file, ignoring any unsaved changes.
 

Most Reading