This tutorial explains how to perform essential editing tasks in vi and vim editors.
Learn how to copy, paste, search, replace, insert, append, delete, undo and redo in vi and vim.
This tutorial is the second and last part of the article \”How to use vi and vim commands in Linux\”.
For this tutorial I assume that you know what the vi and vim editors are and how they work in Linux. To learn about these editors,
see the first part of this tutorial. First part of this tutorial is available here.
Vi and Vim Editor Explained with Examples
Inserting and appending text
The vi (vim) editor uses cursor position as the base point to calculate the editing location in file.
With proper command you can insert and append text exactly where you want from the cursor position.
Following are the some common positions, along with the required commands, where you can insert or append the text.
- To insert the text before the cursor, use the Escape + i keys combination.
- To append the text after the cursor, use the Escape + a keys combination.
- To insert the text in the beginning of line, use the Escape + I keys combination.
- To insert the text in the end of line, use the Escape + A keys combination.
Following image shows how to insert and append text in vi (vim) editor with practical examples.
Inserting new blank line
- To insert a new blank line above the line, use the Escape + O keys combination.
- To insert a new blank line below the line, use the Escape + o keys combination.
By default only one line will be inserted. To insert multiple lines, we have to specify the number of
lines before O/o key. For example to insert 3 blank lines above the cursor line, we have to use the Escape + 3 + O keys combination.
Following image shows practical examples of inserting new lines in vi (vim) editor.
Performing cut and paste
To cut a line, use the dd command. This command cuts the line in which cursor remains and puts that line in memory buffer.
To paste the copied line from buffer, use the p command. This command pastes the copied line in the current line of cursor.
By default, both commands perform their action only one time. It means, the dd command cuts a single line and the p command pastes
the copied line one time only.
To cut the multiple lines or to paste the contents from memory buffer multiple times, we have to specify the desired numbers before these commands.
For example to cut three lines, use the Escape + 3 + d + d keys combination. To paste the contents four times, use the Escape + 4 + p keys combination.
Following image illustrates the cut and paste operation practically.
Performing copy and paste
Copy and paste operation also works similar to the cut and paste operation.
To copy a line, use the yy command. This command copies current line in the memory buffer. To copy multiply lines, specify the
number of lines before the yy command. For example to copy 4 lines, use the Escape + 4 + y + y keys combination.
Just like line, you can also copy a single or multiple characters and words. To copy a single character from the cursor, use the
yl command. To copy a single word from the cursor, use the yw command.
To paste the copied contents, use the same p command.
Following image illustrates the copy and paste operation with example.
Finding text
To find a text string in forward direction, the Escape + / + [String] command is used. To find a text string in backward direction,
the Escape + ? + [String] command is used.
To navigate between search results, the n key is used with the Escape key. To move in backward direction, the Escape + N key is
used while to move in the forward direction, the Escape + n key is used.
Following image illustrates the search operation practically.
Finding and replacing text
To replace the first occurrence of text, use the following keys combination.
Escape + : + % + s + / + [old text] + / [new text]
Here old text is the text which you want to replace and new text is text from which you want to replace the old text.
To replace all occurrences of text, use the following keys combination.
Escape + : + % + s + / + [old text] + / [new text] + / +g
Following image illustrates the find and replace operation with example.
Deleting text
To delete a single character from the cursor in forward direction, use the Escape + x keys combination.
To delete a single character from the cursor in backward direction, use the Escape + X keys combination.
Following image shows the delete operation practically.
- To remove a single word after the cursor, use the Escape + d + w keys combination.
- To remove a single word before the cursor, use the Escape + d + W keys combination.
- To delete a line, use the Escape + d + d keys combination.
- To delete multiple lines, specify the number of lines before the dd command.
Reverting (undo / redo) last command or action
To undo last action or command, use the Escape + u keys combination. To undo all actions performed in current line,
use the Escape + U keys combination. To redo last command, use the Escape + Ctrl + r keys combination.
Following image illustrates the undo / redo operation practically.
I used dd command to perform the action for demonstration.
Setting line numbers
By default line numbers are not displayed. But if required, we can display them with the Escape + : +set +nu command.
Following image show how to set the line numbers in vi (vim) editor practically.
Save and exit
To save and exit, use the Escape + : + w + q keys combination. To exit without saving, use the Escape + : + q + ! keys combination.
If file is not changed, you can also use Escape + : + q keys combination to exit.
Following image shows how to save and exit from vi editor practically.
Vi / Vim Editor commands cheat sheet
Command | Description |
I | Insert text before the current cursor position. |
I | Insert text in the beginning of the current line. |
A | Append text after the current cursor position. |
A | Append text at the end of the current line |
o | Open new line below the current line and switch in input mode |
O | Open new line above the current line and switch in input mode |
/string | Search for string in forward direction |
?string | Search for string in backward direction |
n | Select next search result |
N | Select previous search result |
:%s/old/new | Replace string at first occurrence in forward direction |
:%s/old/new/g | Replace string globally |
yy | Copy single line and put it in memory buffer |
2yy | Copy two lines and put them in memory buffer |
yl | Copy single character from cursor point and put it in memory buffer |
yw | Copy single word from cursor point and put it in memory buffer |
p | Paste contains from memory buffer one time |
3p | Paste contains from memory buffer three time |
x | Delete single character after cursor |
X | Delete single character before cursor |
dw | Delete single word from cursor (in forward direction) |
dW | Delete single word from cursor (in backward direction) |
dd | Delete single line from cursor |
4dd | Delete four lines from cursor |
q | Quit from file if it is not changed. |
q! | Quit from file without save if it is changed |
wq | Save and quit form file |
u | Undo last command |
U | Undo all change in current line |
Ctrl+r | Redo last command |
Always press Escape key before typing any command otherwise whatever you typed will be inserted in file as a regular text.
That’s all for this tutorial. If you like this tutorial, please don’t forget to share it with friends through your favorite social channel.
Full Version EX300 Dumps
Try EX300 Dumps Demo