vi delete

You can delete or remove characters in different ways. r key is used to replace one single character. The r key will bring you in insert mode for just one key press and then you'll be directed to command mode.

x

The command x is used to delete current character.

Example:

Linux Vi Delete1

Look at the above snapshot, our cursor is at letter d and we want to delete it.

Linux Vi Delete2

Look at the above snapshot, after pressing x, letter d is removed.

x

The command X is used to delete character before the cursor.

Example:

Linux Vi Delete3

Look at the above snapshot, our current character is letter d, and we want to delete letter before current character that is c.

Linux Vi Delete4

Look at the above snapshot, after pressing X, letter before current character (c) is deleted.

r

The r key will bring you in insert mode for just one key press and then you'll be again directed to command mode.

Example:

Linux Vi Delete5

Look at the above snapshot, we want to replace 7 from 6 Linux Vi Delete6

Look at the above snapshot, after pressing r, type 6 to replace number 7.

xp

The command xp allows us to switch two characters. The character at the right of the cursor will come at the place of the cursor, and character at the cursor will switch to the right of the cursor.

Example:

Linux Vi Delete7

Look at the above snapshot, our cursor is at character e. Word teh is wrongly spelt. To correct it r can be used.

Linux Vi Delete8

Look at the above snapshot, both the characters that is character at the cursor (e) and character at the right of the cursor (h) are switched from their positions. Hence now word teh becomes the.

dd

The command dd deletes current line completely.

Example:

Linux Vi Delete9

Look at the above snapshot, our cursor is at line third. We want to delete this whole line.

Linux Vi Delete10

Look at the above snapshot, after pressing dd command, third line is deleted.

D

The command D is used to delete from current character to the end of the line.

Example:

Linux Vi Delete11

Look at the above snapshot, our cursor is at third line at letter a. We want to delete from the current character till the end of the line.

Linux Vi Delete12

Look at the above snapshot, after pressing D, all the characters are deleted from letter a to the end line.

dG

The command dG is used to delete from current line to the end line of the file.

Example:

Linux Vi Delete13

Look at the above snapshot, currently our cursor is at second line.

Linux Vi Delete14

Look at the above snapshot, all the lines are deleted from second line to the end line.





Latest Courses