How to delete in VIM text editor

Unix

How to delete characters in a line in vim?

Press Escape [Esc] to enter into command mode.
Press x to delete characters at the current cursor location

How to delete n characters from the current cursor location in vim?

To delete n characters from the current cursor location we can use nx where n is the number of characters to delete.

For example if we want to delete 4 characters from the current cursor location we can do the following

Press Escape [Esc] to enter into command mode
Type 4x to delete four characters from the current cursor location

How to delete the current line in vim?

To delete the current line in vim type dd in the command mode.

How to delete n line from the current line in vim?

Say, we have 5 lines in a file and the cursor is at the first line so the current line is 1. Now we want to delete 1st 2nd and 3rd line. In order to do this in vim we will do the following

Press Escape [Esc] to enter command mode
Type 3dd to delete 3 lines including the current line and the next two lines

How to delete from the current cursor position to the end of the line in vim?

For the earlier tutorial we saw that to move the cursor to the end of the line we type $ so to delete from the current cursor position to the end of the line we have to type d$

How to delete from the current cursor location to the beginning of the line in vim?

To delete from the current cursor location to the beginning of the line do the following

Press Escape [Esc] to enter into command mode
Type d0 to delete from current cursor location to the beginning of the line

How to delete from the current line to the end of the file in vim?


Press Escape [Esc] to enter into the command mode
Type dG to delete from current line to the end of the file

How to delete from the current line to the nth line of the file in vim?

To delete from the current line to the nth line we have to do the following

Press Escape [Esc] to enter into command mode
Type dnG to delete from current line to the nth line where n is an integer

For example, if we are at line 1 and type d3G it will delete line 1 to line 3 as n = 3 and we are instructions to delete from current line to the 3rd line.

Similarly, If we are at line 5 and want to delete from line 5 up to line 3 we type d3G

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT