68

Accidentally I managed to copy-paste a paragraph in vim a zillion times. How do I select and then delete the text from my current position to the end of file?

In Windows, this would be Ctrl-Shift-End, followed by Delete.

ripper234
  • 31,763

6 Answers6

90

d is delete and G moves to the end of the file, so dG will delete to the end of the file. It includes the entire current line though; if you're mid-line and want to preserve everything before the current position you need to use Mark's method

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
  • +1 The other way to preserve part of current line is D<enter>dG: D to delete to EOL, <enter> to move to next line, dG to delete to EOF. – Jim L. Jul 26 '19 at 19:49
78
VGx

Enter Visual Mode, go to End of File, delete.

Alternatively, you can do:

Vggx

To delete from the current position to the beginning of the file.

  • 3
    Ugh, I'll never remember all these vim shortcuts. The Windows shortcuts are just so much more intuitive. – ripper234 May 27 '11 at 14:45
  • 2
    Personally, I find vim's to be more intuitive. V, enter visual mode G, almost like "go", to the end of the file. And x, remove. – Mark Szymanski May 27 '11 at 14:46
  • 13
    @ripper234: don't try to remember "snippets" to do specific tasks, focus on the subcommands. That's part of the magic of vim: using these smaller commands to build up larger, more complex things. It's almost infinitely flexible. – Reid May 28 '11 at 16:38
  • And it really helps a lot to take the time and read through the related vim-help-sections with :h command-you-need-help-for – Gjallar Jan 26 '12 at 08:09
  • To add to @Reid's comment, when thinking about sub commands, think about motions and actions. There are (pretty intuitive if you ask me) shortcuts for each, and they can be combined in many intuitive ways. select a sentence, delete a line, yank to the end of the line change the text within the closest quotes, increment the first number on this line, etc. This is immensely powerful, you can't compare windows shortcuts with this. – Edd Steel Jan 26 '12 at 19:52
  • 2
    To add to this: run vimtutor from the commandline to get familiar with them @ripper234 – Bernhard Jun 13 '12 at 22:32
  • Isn't it just two characters? -> dG – zumalifeguard Mar 07 '15 at 06:24
  • v doesn't go to te end of file, but until the end of the last line, capital V does. –  Oct 19 '16 at 20:05
20

Yet another method: :.,$d

That means in ex mode, address current line to end, and delete.

Keith
  • 7,914
  • 1
  • 28
  • 29
8

You could also just type u to undo the changes that resulted in the errant copy-paste.

Kevin
  • 40,767
vimmar
  • 186
  • not always possible, see the "select". Imagine you have written some code at the end of the file (and want it somewhere else), dG allows you insertion with p|P – relascope Dec 30 '15 at 18:49
3
vGx

works fine for multi line selection(in this case paragraph), but if you want to select within one single line it acts mysteriously(selects from cursor till beginning of the line).

In case of selection within Single line you have to use:

v$x

Which enters to Visual mode("v") and goes to end of line("$") and deletes("x") from current position until end of line.

0

Vggx didn't work for me, but vG$d did. vG$ selects to EOF (last character of last line), then whatever command you want to executed.

=This approach (command after selection) seems to work better. Of course, I'm using Vim 8.1 on Windows 10, and a few years later.