2

I have myfile.txt with some text. If I type vim myfile.txt it opens the file. Is there some parameter for vim to delete the old file and create an empty new one? For example

vim -recreate myfile.txt

Or is such functionality not supported?

4
  • Everything is supported in Vim. You do know you could delete all the text in the file with like 5 keystrokes once you got into vim, right? Dec 10, 2014 at 6:31
  • 3
    After starting vim, just type dG - this deletes from the cursor position to the end of the file, actually emptying it. If your vim is configured to put the cursor to where it was the last time you edited the file, instead of the first row, prepend 1G to get to the first line. 1GdG is still less to type than -recreate. Dec 10, 2014 at 9:32
  • @GuntramBlohm I was gonna post that as an answer, but I see you beat me to it. You should answer with your comment. :)
    – user50849
    Dec 10, 2014 at 9:39
  • It's a workaround, not an answer, so i made it a comment. Also, if the file is really huge, loading it into vim will take a lot of time. @JakeGould's answer, which i would accept if i was the OP, solves the problem much faster. Dec 10, 2014 at 9:56

2 Answers 2

3

Not really. You could do something like this; a compound command line instruction:

> myfile.txt && vim myfile.txt

The > myfile.txt would wipe the contents of the file—but leave the actual file in place—and the && then ties that command to vim myfile.txt.

2

To wipe file you may use:

$ vim +%d +w file

But that’s not a recreation (removing file and creating new one), a file remains the same (same inode). I guess, there is no difference for you, right?

If I’m wrong and you need a new file, I’m afraid, you have to use shell and rm:

$ rm file && vim +w file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .