Linux provides a lot of useful tools from 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 different usage examples of the Vim. From up to now we will call Vim which is 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 file named
test.txt . If this do not exist it will created after save operation.
$ vim test.txt
Vim mainly works with commands. Vim have command mode which these commands can be issued. We can use
: to enter command mode.
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.
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.
Copy operating will operate at the given lines. Copy operation will copy lines to the buffer.
Copy Current Line
Copy Given Range
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.
Given Line Range
We can search given string, text in current file.
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.