Why it is so difficult for vim to copy into system clipboard? - vim

According to official vim wiki copying any text into system clipboard is a something I've never expected to see in a text editor created by humans.
gg"+yG – copy the entire buffer into + (normal mode)
Copy/Pasting between the browser and a text file with 6 keystrokes is something I refuse to accept as normality.
Are there any sane alternatives?

It's not difficult, it's powerful. As the yank command takes a {motion}, you can copy arbitrary (and precisely selected) text areas, all with the same command. Likewise, "+ is just one destination of many, and Vim's named registers are very useful.
You may have noticed that most people (sometimes heavily) customize their Vim setup. On top of the powerful editing abstractions, that puts it on yet another level (at the expense of now being dependent on your Vim configuration).
So, if you need to copy the entire buffer to the system clipboard often, create your own shortcut, and persist it in your ~/.vimrc. For example:
:nnoremap <F2> :%yank +<CR>
There you have it: copying with a single keystroke (cp. :help key-notation for how keys are specified; as function keys are sparse, I would prefer <Leader>y instead).
If you often yank (various areas) to the system clipboard, making that the default register might also be worthwhile:
:set clipboard^=unnamedplus

To break it down:
gg
Move the cursor to the beginning of the buffer
"+
Set the target for copy to the system clipboard register
yG
Copy ("yank") everything from the cursor position to the end of the buffer (i.e. the whole file).
To give another example. If you want to copy just the current word to the clipboard you might perform:
"+yw
Since "w" is the command to move the cursor to the end of the current word.
If you want to shorten a regularly repeated action you could record a macro, or map a keyboard shortcut.

When I want to copy a whole file I just do :%y+
: Enter ex command
y[ank]
+ To clipboard register

Related

Select All + Copy/Paste

A common pattern I find myself doing in a non-vim text editor is:
Cmd a Cmd c
to copy all the content in the entire file, and then open a new file and
Cmd v
to then paste in all that content. When all is said and done, I can usually do this entire copy-paste in about 0.5s. What would be the most common/efficient way to do this in vim?
I do the same quite often by running the following commands: ggVGy.
The first command, gg, jumps to the beginning of the file. Then we select the entire first line with V. Still in visual mode, we jump to the end of the file with G, before copying (yanking) everything with y.
You can make this even faster and easier by mapping all of these commands to one key. With the mapping below, pressing the leader key and X will copy the entire file.
nnoremap <leader>X ggVGy
If you need the content to be available to the system clipboard, and not only in Vim, you should use "+y instead of y. This will yank to the "+ register used for the system clipboard. You can find more information on this topic in this question.

How to use different registers in Vim to copy/paste from/to OS clipboard -- how exactly?

I've read this How to make vim paste from (and copy to) system's clipboard? and I know what the hotkeys are. But I'm unable to execute any of those commands. For example, what exactly should I press to call
"* or "+? In which mode also? I've tried different things and none of them worked.
I am assuming you've double-checked that :echo has('clipboard') returns 1. If it returns 0, you're out of luck since vim isn't compiled with access to the system clipboard.
If you have clipboard powers, then yanking and pasting inside of vim is done with pressing just the letter y and the letter p, in normal mode. Start doing this first, to confirm that you can yank and paste inside of vim. For instance, yank a line: yy and paste it p.
Next, confirm that you can yank inside of vim to the system clipboard with "*y. (That means pressing the double-quote (shift-'), then an asterix (shift-8), then the letter y, all in quick-ish succession. Toggle over to another app and paste with the regular control-V. (You may be able to look at the bottom left of status line to see what you are literally typing, which might help).
If that works, then the clipboard functionality is "good to go." If it doesn't you might need to tweak your .vimrc to get things working. Try setting the clipboard to unnamed: set clipboard=unnamed and retest step 3 again.

Why does Vim always manage to clip off at least a couple of characters when copying and pasting?

Vim always manages to clip off at least of couple of characters when I copy something from a text file, such as a public rsa key for example.
Generally, I open a text file in my computer, highlight the text, copy it to my clipboard. I go into my terminal (with Vim already open) and then:
i (for insert)
ctrl+shift+v
Am I doing something wrong?
I've double checked, and triple checked, this, and I am absolutely sure that I highlighted all of the text that I needed to copy. Other times it works fine. There does not seem to be any consistency in regards to when it happens.
Anyone experience this?
This usually happens because something in the pasted text triggers a key mapping unexpectedly. This is what paste mode is for.
Do this before pasting:
:set paste
And turn it off after:
:set nopaste
This can be annoying to type frequently, so there is a way to map it to a key combination. I use ,p for this, and have this in my .vimrc:
set pastetoggle=,p
(Yes, I use an actual comma here, not <Leader>. That's because <Leader> won't work here, as this isn't technically a mapping.)
This is a toggle, so you can use it to turn the mode on and off. It works in both normal mode and insert mode.
You do want to turn it off after the paste. Since paste mode disables your insert mappings, you don't want to leave it on all the time.
You can read more about the paste option with :help 'paste.
The annoyance of toggling to and form paste mode can be avoided by using "+p or "*p. (You're putting the quoteplus register).
More often than not, I prefer "+P, since this leaves my cursor at the end of properly formatted put. see :help quoteplus for details. So the whole workflow is:
outside of vim: copy to clipboard
inside of vim: "+P

Vim: Replace selection with default buffer without overwriting the buffer

Here is my problem:
I am in visual mode.
I select text and copy it to the buffer. ((y)ank)
I select another text which I want to replace and paste the buffer. ((p)aste)
Now the second selection has been replaced in the buffer, however I want the first one to still sit in there.
Any ideas?
Often, this behavior is useful. When you don't want it, you can instead do the usual yank, then paste (officially, 'put') with "0p. You can do this however many times you like.
See :help v_p for more.
If you want to avoid the overwrite, you need to delete first. You can use "_ to select the blackhole buffer, then delete d, then paste before P and you'll avoid the buffer being set.
While this does not technically answer the question (not using default buffer) it does solve the symptom of the problem so I thought I would still share. I get around this issue with a solution to a different problem.
I have mapped "Copy, paste" (yank, put) from the system clipboard to "Ctrl-Shift-C, Ctrl-Shift-V" (Ctrl-C, Ctrl-V if caps lock is on). This can be used in place of y with the same effect.
If I use the system buffer for copy it does not get overwritten when I paste.
I added this to my .vimrc
vnoremap <C-V> "*p
vnoremap <C-C> "*y
As a bonus it will give you easy access to the system clipboard.

Copy and paste from external source

I use vim (Actually gvim on windows) as my main text editor. In my work flow I have to copy sentences to/from various external sources, therefore I use clipboard=unnamed to save me key strokes (p instead of "*p).
I copy text from an outer source and I want to paste it over two different places in vim. I mark the first one (v) and then use p to paste over it. The problem is that at this point I lose the original buffer and can't paste it in the second place. It does not exist in the unnamed buffer, the * buffer or the numbered buffers. My guess is that pasting over selection is putting the "pasted over" text in the unnamed buffer.
How can I paste my original string in two locations? i.e. prevent it from getting lost from the buffers.
Thanks.
Try this:
:vmap p "_xP
vmap means to make a mapping that only applies in visual mode.
p is the key to create the mapping for.
"_ is the black hole register. This is used in any situation where you want to delete text without affecting any registers.
xP means delete the selected text, then paste before the resulting cursor position.
You could set up a mapping to ease your pain:
:vmap <F5> "zxP
This will delete the visually selected text, but put it in a different register, so the clipboard isn't affected. Change <F5> to whatever is easiest for you.
I don't know if I misunderstand you but I tried what you are doing and I have no problem in doing that with the + drop-register.
My workflow:
copy a sentence in an external application (ie. browser)
visual select a sentence in vim and replaced it with "+p or p (with clipboard=unnamed set)
visually select another sentence and replace it with "+p
Sadly when pasting the second time you have to explicitly paste from the + register. Therefore I would recommend a mapping for p/P instead of using clipboard=unnamed
nmap p "+p
Try using
:registers
to see the contents of the different registers.
I don't know how to do that on Windows. With KDE, the clipboard has a history that you can select from, so you could do the paste, select the previous selection from the clipboard, and paste in the new location.
That said, it sounds like it might make more sense for you to have it in only one location, then write a script to take that input and create the output you need. Can you elaborate more on what it is you are trying to accomplish?
Check the value of the 'guioptions' options. Make sure the 'a' flag is not set. Also, check that the 'clipboard' option and verify that neither the 'unnamed' or 'autoselect' flags are set.
:set go-=a
:set clipboard-=unnamed

Resources