Linux Vi and Vim Command Line Text Editor Tutorial
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.
1 |
$ vim test.txt |
Command Mode
Vim and vi mainly works with commands. Vim have command mode which these commands can be issued. We can use Escape
and :
to enter command mode. In command mode we will use 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.
Exit
We can easily exit with the following command.
1 |
:q |
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.
1 |
:q! |
Move Cursor In The Vim Editor
Cursor can be moved with given direction keys. But Vim provides more shortcuts for this.
Down Single Line
1 |
j |
Up Single Line
1 |
k |
Left Single Character
1 |
h |
Right Single Character
1 |
l |
Start Of The Current Line
1 |
End Of The Current Line
1 |
$ |
First Line Of File
1 |
: |
Last Line Of File
1 |
:$ |
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.
1 |
:n500 |
Insert/Edit Mode
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.
Add Text
We generally need to add some text to the files. There are different ways to use insert mode. Here some of them.
Insert
We can write the text and it will be inserted
1 |
i |
Append
1 |
a |
Copy Text
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.
1 |
yy |
Copy Given Range
We can copy given range which is 4 lines in this example.
1 |
y4y |
Paste Text
Copied text will be pasted from buffer.
1 |
p |
Undo Last Change
We can revert changes easily with the following command
1 |
u |
Delete Text
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.
Current Line
We will delete the current line where the cursor resides
1 |
dd |
Given Line Range
We will provide the line count we want to delete which is 5 in this case.
1 |
d5d |
Search Test
We can search given string, text in current file.
Forward Search
We will search the text STRING
to the forward which means top to down.
1 |
/STRING |
Backward Search
We will search STRING
to the backward which means down to top.
1 |
?STRING |
Next Occurrence
Jump to the next occurrence of the search to the top to down.
1 |
n |
Previous Occurrence
Jump to the previous occurrence of the search which means down to top.
1 |
N |
Show Current Line Number
We can show current line number in the bottom with the following command.
1 |
:.= |
Show Total Lines Number
If we need the total lines count we can use following
1 |
:= |
Save File
Current file can be saved with the following command.This will save to the same file.
1 |
:w |
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.
1 |
:w test2.txt |
Save and Close
We can save and close current file easily with the following command.
1 |
:wq |
2 thoughts on “Linux Vi and Vim Command Line Text Editor Tutorial”