40

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.

46
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.

  • 1
    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
  • 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
  • 10
    @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 8:09
  • 1
    To add to this: run vimtutor from the commandline to get familiar with them @ripper234 – Bernhard Jun 13 '12 at 22:32
48

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

  • +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 at 19:49
8

Yet another method: :.,$d

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

7

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

  • 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
2
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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