How can I delete all lines in a file using vi?
At moment I do that using something like this to remove all lines in a file:
echo > test.txt
How can I delete all lines using vi
?
Note:
Using dd
is not a good option. There can be many lines.
In vi
do
:1,$d
to delete all lines.
The :
introduces a command (and moves the cursor to the bottom).
The 1,$
is an indication of which lines the following command (d
) should work on. In this case the range from line one to the last line (indicated by $
, so you don't need to know the number of lines in the document).
The final d
stands for delete the indicated lines.
There is a shorter form (:%d
) but I find myself never using it. The :1,$d
can be more easily "adapted" to e.g. :4,$-2d
leaving only the first 3 and last 2 lines, deleting the rest.
ESC+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd+dd...
Commented
Nov 3, 2016 at 19:38
:%d
and enter, its way too many strokes. I am trying to get into vim but without vim ctrl + a and del button is much more simpler than what vim provides for this. Just two strokes(ctrl+a, del) unlike :%d
which has 4 strokes including the enter command
Commented
May 2, 2020 at 10:04
In vi I use
:%d
where
:
tells vi to go in command mode%
means all the linesd
: deleteOn the command line,
> test.txt
will do also.
What is the problem with dd?
dd if=/dev/null of=test.txt
where
/dev/null
is a special 0 byte fileif
is the input fileof
is the ouput filedd
i need pess for every line on file.
dd
the vi command (which the OP meant) and dd
the utility, which you give an example of. Also, > test.txt
may not work as expected in non-bash shells (e.g. zsh).
I'd recommend that you just do this (should work in any POSIX-compliant shell):
> test.txt
If you really want to do it with vi, you can do:
G
represents last line. If you are on the first line (gg
), dG
tells vi to remove all the lines from current line (first line) to the last line. So, you do it in one shot.
>| test.txt < /dev/null
is somewhat more robust as it will always clobber the file and avoid waiting for input on interactive terminals.
I'm a lazy dude, and I like to keep it simple. ggdG
is five keystrokes including Shift
gg
goes to the first line in the file, d
is the start of the d
elete verb and G
is the movement to go to the bottom of the file. Verbosely, it's go to the beginning of the file and delete everything until the end of the tile.
:$d
or :1,$d
is the faster way (I write less characters) kkk
:%d
is good alternative, since you only use 5 keystrokes that way too.
Commented
Oct 15, 2014 at 15:56
If your cursor is on the first line (if not, type: gg
or 1G
), then you can just use dG
. It will delete all lines from the current line to the end of file. So to make sure that you'll delete all the lines from the file, you may mix both together, which would be: ggdG
(while in command mode).
Or %d
in Ex mode, command-line example: vim +%d foo.bar
.
Related: How I can delete in VIM all text from current line to end of file?
dG
is what I always use.
ggdG
) the easiest method in vim. The reason this answer isn't upvoted as others is that gg
is non-existent in pure vi?
1GdG
or Gd1G
. But if you're using vim
, then ggdG
is the easiest to type.
I always use ggVG
gg
jumps to the start of the current editing fileV
(capitalized v) will select the current line. In this case the first line of the current editing fileG
(capitalized g) will jump to the end of the file. In this case, since I selected the first line, G
will select the whole text in this file.Then you can simply press d
or x
to delete all the lines.
d
vertically, it automatically applies linewise. dl
deletes a character to the right, dj
deletes a line down, for example.
Commented
Oct 15, 2014 at 15:49
Another solution:
truncate -s 0 file
note that in your question, echo > test.txt
creates a file with a single line break in it, not an empty file.
From the shell, consider using echo -n > test.txt
or : > test.txt
.
While I'd generally use a vi editing command (I use ggdG
), you can also call out to the shell with a reference to the current file like so:
:!:>%
It's nearly as concise as ggdG
, but harder to type, and you also have to confirm that you want to reload the modified file, so I don't particularly recommend it in this case, but knowing how to use shell commands from vi like this is useful.
breaking it down:
:
initiate a vi command!
initate a shell command:
this is a shell builtin command with empty output>
redirect the output%
vi substitutes this with the name of the current fileThe suggested :1,$d
is also a good one of course, and just while I'm at it there's also 1GdG
:!:>%
is also dangerous as vi
doesn't escape the file name for the shell (don't do it when editing a file called $(reboot)
for instance). You'd need something :call system(":>" . shellescape(expand("%")))
Commented
Feb 7, 2019 at 11:51
I do a dgg
, which deletes all lines from cursor (which is typically EOF when you open a file) to the top, which is the quickest.
I type gg
to reach the top of the lines and then do a 100 dd
. This brings the pointer to the top line of editor and clears 100 lines from there. You may have to adjust the number to add more lines if the file is lengthier.
> file.txt
You don't even have to open vi unless you really want to.
1GdG
would work from anywhere.echo | test.txt
is not a valid command, unlesstest.txt
is an executable script. I'm guessing you meanecho >test.txt
instead?vi
,>test.txt
is sufficient to truncate it to zero length - noecho
needed.