Linux operating system comes with a few text editors. The most commonly used editor among them is vi / VIM and nano.
In this tutorial, we learn about vi / vim text editor and some of its useful commands.
What is vi editor in Linux
Vi is a text editor which available preinstalled in Linux/Unix operating systems. Using vi editor you can create a new file and edit any existing files. Most commonly used to edit programs or configurations file in Linux. If you look for more advanced features you can use VIM which is a vi-like editor.
In Linux, visudo and crontab default editor is vi. You can change the default editor by adding shell configuration in ~/.bashrc
file.
The vi editor uses 3 operation modes: Command, Insert and Escape
Command mode:
In this mode vi editor accepts commands but won't display those commands on the screen. Commonly used to delete, copy or paste a line of text. Press ESC
key to enter into Command mode. When you open vi editor it starts in Command mode.
Insert Mode (input mode):
Press the letter i
from the keyboard to enter Insert mode. This mode is used to edit or insert text to the file.
Escape mode:
You can change to escape mode from command mode only. Press colon [:] key from the keyboard to enter Escape mode. Escape mode is used to type commands to save, quit and run commands etc.
How to open vi editor
The best way to open vi editor type vi filename
from the command line.
vi text1.txt
This opens the existing file named text1.txt and if text1.txt doest exist then it creates a new file.
Vi Create a new file
Let check here how to to create a new file name file1.txt:
The symbol '~' indicates unused lines.
You can now view the file content using the cat command.
Exit (quit) Linux vi editor without saving
To exit vi editor without saving the file content go through the following steps:
1. Press Escape
key from your keyboard to enter command mode
2. Press colon [:] key from your keyword, now you will see a colon prompt followed by a flashing cursor at the lower-left corner of the screen.
3. Type the command q!
and press Enter
key. This will close the vi editor and all changes you made will not be saved.
Exit (quit) Linux vi editor with saving
To exit or quit vi editor with saving the file content go through the following steps:
1. Press Escape
key from your keyboard to enter command mode
2. Press colon [:] key from your keyword, now you will see a colon prompt followed by a flashing cursor at the lower-left corner of the screen.
3. Type the command wq!
and press Enter
key. The w indicates that all changes will be written or overwritten to the file and q to close or exit the editor.
Useful vi editor Commands
Let's check some of the useful vi commands that you can execute in Command mode.
Commands | Description |
---|---|
i | Change to Insert mode and append text where the cursor points. |
a | Change to Insert mode and insert after the cursor. |
A | Change to Insert mode and insert at the end of the line. |
o | Change to Insert mode and insert at the new line. |
ESC | Change from Insert mode to Command mode. |
dd | Delete one line the cursor is on. |
dw | Deletes from the current cursor location to the end of blank delimited word. |
4dd | Delete 4 lines |
u | Undo last change |
x | Delete one character at the current cursor position. |
G | Go to the end of the file. ie the last line in the file |
4G | Go to the 4th line of the file. |
gg | Got to the first line of the file. |
b | The cursor goes to the beginning of the word. |
e | The cursor goes to the end of the word. |
yy | Copy the entire line. |
3yy | Yank 3 lines. |
p | Paste the copied text after the cursor. |
P | Paste the yanked text before the cursor. |
h | Move the cursor left by one character position. |
l | Move the cursor right by one character position. |
j | Move the cursor down by one line. |
k | Move the cursor up by one line. |
Using Control Key you can run some useful control commands for Window scrolling:
- CTRL+f - Scroll one screen forward.
- CTRL+b - Move one screen backward.
- CTRL+d - Scroll down half screen.
- CTRL+u - Scroll up half screen.
- CTRL+g - Display total number lines and position of the cursor in % at the bottom of the screen.
Vi can perform search and replace text very efficiently from Escape mode. Let's check some escape mode capabilities:
Search a text:
:s/tom
This will find forward for the string 'tom" and highlight when it finds the first match. This search matches both the whole word and the pattern within any words. You can use 'n' to search the next occurrence and for the backward press 'N'.
Search and Replace:
Search for the first occurrence of the string 'tom' in the current line and replace it with the string 'tomorrow'.
:s/tom/tomorrow
The string search in vi editor is case sensitive by default. You can use the i
flag to ignore the case.
Add %
symbol, in the beginning, to search and replace the string in the entire file.
:%s/tom/tomorrow
Show line numbers:
To display line number in vi editor:
:set number
To hide the line number:
:set nonumber
Display vi/vim color scheme:
:colorscheme [space] [crtl+d]
Change color scheme:
:colorscheme darkblue
Display the line number of the current line:
:.=
Conclusion
The vi editor is present on almost all Linux/Unix systems. It is very powerful as with a few short commands you can make changes to large files. In this tutorial, we learned about vi editor and its important commands.
Thanks for reading. Please leave your feedback and suggestions.
Comments