2

Here is a typical workflow I would like to follow:

  1. copy some data from Excel spreadsheet into the clipboard
  2. in vim , delete the data in the file using ggdG
  3. then paste the data into the file from the clipboard (I use CTRL-V as I am under Windows)

This does not work because step 2 copies the deleted data into the clipboard.

Is it possible to delete without affecting the clipboard ?

3 Answers 3

4

When deleting the buffer contents in Vim, you can send it to the black hole register "_. From :help quote_:

When writing to this register, nothing happens. This can be used to delete text without affecting the normal registers.

So simply use this command in your step 2:

gg"_dG

Or you can also use the :delete command with a range:

:%d _

This Ex command deletes every line into the black hole register.

2
  • 1
    Simpler is of course :%d _
    – D. Ben Knoble
    Apr 9, 2020 at 15:16
  • Tnhanks @D.BenKnoble! Great call! Incorporated it into the answer.
    – filbranden
    Apr 9, 2020 at 15:22
2

Of course, everything is possible in Vim!

There is a register system in vim, and the clipboard is the register + (or *, it depends)

By default, the text you cut is put in the clipboard register, but you can put it in a black hole register (:h quote_) like so:

gg"_dG

The "_ here tells vim to put what you're about to cut in a black hole register, so your + register (a.k.a. your clipboard) stays untouched.

And the magic doesn't stop there, if it's a task you do often, you can set up a mapping to do that, for example:

nnoremap gd gg"_dG

So that next time you need to delete a whole file, you just need to hit gd

I personally have a mapping on my \ key like so:

nnoremap \ "_

So when I need to keep my clipboard, I begin my keystrokes with \

7
  • In fact, the deleted text should not be put in the clipboard, not without tweaking some options!
    – D. Ben Knoble
    Apr 9, 2020 at 13:56
  • Ahaha okay then you might want to disable the xterm_clipboard option. When you type vim --version, you should see the option with a + before it right now, which you don't want.. but I'm not sure how to do that on windows
    – Zorzi
    Apr 9, 2020 at 14:06
  • Out of curiosity, why don't you want to do that with buffers?
    – Zorzi
    Apr 9, 2020 at 14:07
  • 1
    You're mixing up buffers with registers. "+, "* and "a are all registers. Furthermore your command doesn't fix the issue, since you're using "a as the register for gg and not for dG which is the part that writes to registers. Also, there's no reason to use "a or a letter register which would be overwritten too... you can simply use "_, the black hole register (see :help quote_).
    – filbranden
    Apr 9, 2020 at 14:41
  • 1
    I corrected my answer, thank you for your comments!
    – Zorzi
    Apr 9, 2020 at 15:12
2

You could visual select with v or select the whole line with V and then press p or P to replace the selected text with the one in clipboard (I think it will work with yours Ctrl+v as well).

Your Answer

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

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