1. Overview

While using vi or vim, we may find the need to clear the entire buffer in which we’re working. In this article, we’ll learn a few ways to delete all the lines in a vi or vim buffer.

2. Setup

This tutorial covers vi and vim separately, as some of the methods differ between the two. vim is the editor on most current Linux flavors when using vi at the command line. Since we’re looking at vi-specific methods, we can do a couple of things to ensure accuracy.

Starting vim in vi-compatibility mode makes vim behave mostly like vi. That is, vi commands work as well as vim commands. We can start that mode by typing:

$ vim -C

But a more robust way to test vi commands would be in an environment in a Docker instance that has actual vi installed.

3. Delete All Lines in vi Buffer

This section assume we’re using the Docker instance with vi.

First, let’s check that we’re using vi:

$ ll $(which vi)
lrwxrwxrwx 1 root root 2 Apr 27  2020 /usr/local/bin/vi -> ex*

If vi is an alias for ex, then we’re using vi.

Now, let’s set up a simple buffer. We start vi:

$ vi

And now we have an empty buffer. Let’s fill it with some sample text. We type i to enter insert mode and then enter some text:

vi starting buffer

Finally, we hit Esc to exit insert mode to complete our buffer. Now that we have a buffer, let’s say we want to clear it.

3.1. Normal Mode

We can clear our buffer while in normal mode (as opposed to command mode, described in the next section). We can think of this method as “go to the beginning and delete through the end” and we can do it with a short set of key commands.

Let’s type 1GdG. Our buffer should be empty now:

vi empty buffer 1

Now let’s break down this command and see why it did what it did. G moves to a line, either the line number in front of it, or by itself it moves to the last line of the file. In our case, 1G moves our cursor to the start of the file. d followed by a movement command (like G) deletes up to wherever the movement command goes. In our case, since there is no number in front of G, it goes to the last line, and thus dG deletes all the lines in the file.

If we haven’t typed anything else, hitting u will undo the delete and we’ll have our full buffer again. Otherwise we can recreate it as described above.

3.2. Command Mode

Now let’s go through a couple of ways to clear the buffer using command mode. We enter command mode by typing :. We should see a prompt and a cursor at the bottom of the terminal:

vi starting buffer command line

Now, to clear our buffer, let’s type 1,$d and then Enter. Our buffer is clear now:

vi empty buffer 2

Breaking down the command, the 1 moves the cursor to the first character of the first line. The ,$ means all the lines to the end, and finally the d does the deletion of all those lines.

A shorter version of this command is %d. In that command, the % is short for 1,$, which essentially means the whole file.

4. Delete All Lines in vim Buffer

Now that we’ve discussedvi, let’s go through the same commands in vim. Notably, all the above commands should also work in vim.

First, let’s start vim by opening a terminal and typing vim. Similar to vi, we’ll enter insert mode by typing i, then enter our four lines of text, then hit Esc to exit insert mode. Our buffer should now look like this:

vim starting buffer 1

Now let’s try out some methods to clear the buffer.

4.1. Normal Mode

To start, let’s clear the buffer using this command, ggdG:

vim empty buffer 1

Similar to the command in vi, this is a “go to the beginning and delete to the end” approach. Breaking it down, the gg moves to the first character on the first line. The G without a number before it will go to the end. So dG deletes from the current position to the end.

Let’s type u to undo and move on to some other methods.

4.2. Command Mode

All the commands we discussed in vi command mode work in vim, so let’s move on to a third mode.

4.3. Visual Mode

vim has another mode, visual mode, where we can make text selections by navigating through the text. So to clear a buffer using visual mode, we can highlight all the lines in the buffer and delete them.

Let’s start with our text again:

vim starting buffer 2

v enters visual mode. Then we can use navigation to highlight all the words (ggG$ goes to the first character and highlights to the end of the last line):

vim visual mode

Now that we have all our text highlighted, we can delete all of the lines using D, which deletes to the end of each line:

vim empty buffer 2

5. Conclusion

In this article, we learned a few different ways to delete the contents of a buffer in both vi and vim.

We discussed deleting all lines in a buffer in normal mode and command mode. We also looked at deleting lines in visual mode in vim.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.