Vi is a default editor in Linux operating systems. Vim is basically the improved version of the Vi editor. In this tutorial, We will learn Vi commands.
If you are new to Linux operating system and you don’t know how to start with Vi, Vim editor then in this tutorial we’ll important commands and their usage .
I already mentioned Vi is a default editor in Linux , Ubuntu and all other flavors of Unix operating system. To use Vim editor you need to installed it first.
There are two modes available in Vi, Vim editor.
Command mode − In this mode you will perform the administrative task such as saving files, searching, executing some other command etc.
Insert mode − In this mode you will inserting and editing text into the file. Everything you type is written into the file.
How to Install Vim Editor in Ubuntu
To install vim go to command line (shortcut ctrl+Alt+T ) and type command
sudo apt-get install vim
How to Start with Vi, Vim Editor Commands
Let’s start with basic commands. To open any file using Vi, Vim editor, open the terminal and type following command.
# vi filename
# vi products.php
Similarly, The same command is used to open a file in vim, just you need to use vim instead of vi.
# vim filename
# vim products.php
A filename is the name of a file which you want to open using a terminal. For reference, I use products.php .
To edit this file you need to press i (i is a switch, By pressing i you enter into insert mode)
Now you can edit any text, insert a new line etc in an insert mode.
Vi, Vim Editor Save and Exit Command
You have done editing a file now you want to save this file, how you do it.
If you are in insert mode then you need to press Esc key and then a colon(:) .
Save and Exit Command
If you are not in insert mode then simply press colon and then type wq.
wq is used to save and exit.
Exit without Saving
q! is used to exit without saving the content.
Save File without Exit
To save the file without exit then use w!
Vi, Vim Editor Search Command
Now, We have learned the basics of Vi, Vim editor. Let’s see how to search words in Vi, Vim editor.
To search any word using Vi, Vim editor press forward slash (/) and then type the word you want to search.
For Example- I want to search a webrewrite using Vi, Vim editor.
It will search word webrewrite. To check it’s next occurrence press n key.
How to Navigate File in Vim
Once we open the file in Vi, Vim editor we need to navigate file. Here is the list of some very useful keys used for navigation.
Here are some of the very useful keys for navigation in Vim
h moves the cursor one character to the left.
j moves the cursor down one line.
k moves the cursor up one line.
l moves the cursor one character to the right.
0 moves the cursor to the beginning of the line.
$ moves the cursor to the end of the line.
w move forward one word.
b move backward one word.
G move to the end of the file.
gg move to the beginning of the file.