How To Delete All Lines in VI
vi is a well-known screen-oriented text editor that goes back to the early days of Unix. Its most impressive feature is that it can operate in two distinct modes.
In Insert mode, the text becomes part of the file. In Normal mode, keystrokes are understood as direct commands. Therefore, various keyboard commands are used to reduce the amount of work needed to edit a file.
But if the commands are so different, is it really easier to remove lines or edit them using the keystroke commands? – Let’s find out.
Cutting a Line or Block
-
Position your cursor in front of the line of text that you want to remove
-
Press v and begin a character visual selection
-
Press V if you want to select entire lines
-
Press Ctrl + v or Ctrl + q if you want to select a block
-
Position the cursor at the end of the line of text
-
Press d to cut
If you now want to paste that line elsewhere, simply move the cursor to the new location and press p. Note that pressing p will paste the line or block after the cursor’s position. If you want to paste before the cursor’s position, you have to press P.
Editing Over a Line
You can also change the selected text if you press c in step 6. Pressing d will cut, pressing y will yank or copy, and c will open the insert mode in which you can add new text. This could help you replace an entire line or block without having to bother with cutting them first.
Deleting Multiple Lines
There are two good options if you want to delete more than one line in vi.
“:%d”
This command deletes all the lines. The ‘:’ puts vi in command mode. The ‘%’ character tells it to apply the ‘d’ (delete command) to all the lines.
“:1,$d”
This is the more popular alternative. Again, the ‘:’ introduces a command. ‘1,$’ tells vi which lines to target. In this example, it would be the lines starting with line 1 and the last one. ‘d’ stands for delete.
But what makes this command better than the first one? – This one is adaptable. You can use it to delete lines in bulk but without having to delete every one of them.
If you use something like “:4,$-2d”
then vi will delete all the lines between the third and second to last. It leaves the first three intact because the lines start with 1 and not 0. And, you specify the first line that you want to delete which in this example is 4.
The ‘-2’ basically represents how many lines starting from the last one and counting back to 1.
Using Characters, Words, and Lines in Normal Mode
First, you’ll want to make sure that you hit Escape to enter normal mode. After that, you can use the following vi commands to delete specific information.
dd
This command deletes an entire line. You can position the cursor anywhere on the line, whether it’s on a word or an empty space. The line and all the space it occupied will be erased.
x/X
If you want to remove just one character from a line you can use x. Position the cursor after a character and press x. This will remove the character and also the space it occupied. This also means that all the other characters near it will come together to fill in the gap.
You can also use x to delete a blank space in a line. If you hover over a character and you want to remove the one before it, press X instead of x.
dw
Pressing dw when your cursor is positioned at the beginning of a word will delete that word. This will also remove the space occupied by the word. Alternatively, you can remove just parts of a word.
Position your cursor to the left side of the part that you want to delete. Press dw to remove those characters and the space they occupied in the line. Keep in mind that doing this will bring the adjacent characters together to fill in the gap.
A Final Thought
As interesting and unique as vi is as a modal editor, it takes a while to get used to it. There’s very little feedback when it comes to switching between modes. This makes it very easy to accidentally input code text when you should be giving a command, and vice versa.