3

To clear a file I find myself doing something like:

  • 1G
  • 1000cc (or some large number)

Is there a simpler way to do this for clearing a file? On a normal text editor I normally would do Cmd-A and then start typing.

1

2 Answers 2

5

ggdG

gg: Move to the first line in the file

d: Delete

G: [until] last line in file

From help:

G           Goto line [count], default last line, on the first
            non-blank character linewise.  If 'startofline' not
            set, keep the same column.
            G is one of the jump-motions.
...
gg          Goto line [count], default first line, on the first
            non-blank character |linewise|.  If 'startofline' not
            set, keep the same column.

Substitute d with c if you want to be in insert mode instantly.

4
  • thanks for this. Is there a way to do this without copying the text into the buffer (I have stuff in the buffer I'd like to 'keep')
    – David542
    Apr 23, 2020 at 19:19
  • @David542 What do you mean? How can you clear all contents of a file and keep some of the stuff in your buffer? Also, what do you mean by "copying the text into the buffer"?
    – filbranden
    Apr 24, 2020 at 4:14
  • 2
    Ah I think you mean the register! The default register, right? So that if later you paste with p or P it will put the previously yanked text... Well, for that, use gg"_dG, which deletes it into the black hole register.
    – filbranden
    Apr 24, 2020 at 4:17
  • 1
    @filbranden yes exactly, thanks for the tip! And thanks again for all your helpful feedback/answers/etc on my questions.
    – David542
    Apr 24, 2020 at 4:18
10

Fastest is

:%delete

Which can be abbreviated to

:%d

The % is a range standing for “whole file.”

If you want to use a particular register (e.g., black hole):

:%d_

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.