Linux provides a lot of useful tools from the command line. Vi or Vim is one of the very popular text editor used in Linux, Unix, and BSD operating systems. In this tutorial, we will look at different usage examples of the Vim. From up to now we will call Vim which is an improved version of Vi.
Open with File Name
We can open a file by giving its name to the Vim. In this example, we want to open a file named
test.txt . If this does not exist it will be created after saving operation.
$ vim test.txt
Vim and vi mainly work with commands. Vim has command mode in which these commands can be issued. We can use
: to enter command mode. In command mode, we will use the commands described below.
Exit From Vim
For beginners exiting from vim is very hard job. Vim have commands to manage Vim behaviour. We can exit from Vim with the following commands.
We can easily exit with the following command.
Exit Without Saving
If we have made some changes and try to exit vim or vi will prevent it unless we save it or exit without saving.
Move Cursor In The Vim Editor
Cursor can be moved with given direction keys. But Vim provides more shortcuts for this.
Down Single Line
Up Single Line
Left Single Character
Right Single Character
Start Of The Current Line
End Of The Current Line
First Line Of File
Last Line Of File
Move Cursor To The Given Line Number
One of the most popular command for Vim is moving cursor to the given line. We will use
:n for this but we will also provide line number. In this example we want to move cursor to the line number 500.
Insert/Edit mode is the situation where all given keystrokes are interpreted as text input. We will look how to enter insert/edit mode below.
We generally need to add some text to the files. There are different ways to use insert mode. Here some of them.
We can write the text and it will be inserted
Copy operating will operate at the given lines. Copy operation will copy lines to the buffer.
Copy Current Line
We will copy the current line where the cursor resides.
Copy Given Range
We can copy given range which is 4 lines in this example.
Copied text will be pasted from buffer.
Undo Last Change
We can revert changes easily with the following command
We can delete lines with the following command. Deleted lines also copied to the buffer where we can use this buffer to paste. This is same as moving.
We will delete the current line where the cursor resides
Given Line Range
We will provide the line count we want to delete which is 5 in this case.
We can search given string, text in the current file in different ways like forward search, backward search, next occurrence, previous occurrence, etc.
We will search the text
STRING to the forward which means top to down.
We will search
STRING to the backward which means down to top.
Jump to the next occurrence of the search to the top to down.
Jump to the previous occurrence of the search which means down to top.
Show Current Line Number
We can show current line number in the bottom with the following command.
Show Total Lines Number
If we need the total lines count we can use following
Current file can be saved with the following command.This will save to the same file.
Save File with Different Name
We have also option to save into different file name. We will provide the file name to the
:w command like below.
Save and Close
We can save and close current file easily with the following command.