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

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.

Related

SLES12 vim - background changes scrolling + copy paste issues

I am using gnome-terminal with SLES12 and I encounter an issue where I am scrolling down during showing file in vim, the background color is changing.
Using some exploration during the internet I got the following solution :
if &term =~ '256color'
" Disable Background Color Erase (BCE) so that color schemes
" work properly when Vim is used inside tmux and GNU screen.
set t_ut=
endif
But using this solution, it creates a new one.
When I copy paste a line from vim and paste it on other vim , the copy consider also the blank lines in as characters , and creates really long lines.
To emphasize, lets say I have the following line which contain 11 char (including the space)
the copy paste consider also the rest of the line (the blank ones) as characters.
I would like that the copy paste would stop at char 'd '
hello world
Any idea how to combine a solution for these two issues?
The two issues are completely unrelated.
When you "copy paste a line from vim and paste it on other vim", you are presumably using your terminal emulator's or system's copy/paste feature which have no idea about where what you consider as a line starts and where it ends. The terminal emulator's window is n characters wide so a line is n characters and that's all they care about.
A much better approach would be to use Vim's built-in :help y and :help p, which have the same idea of what a line is as you.
But this creates a second problem: the default Vim is generally not built with clipboard support so you can't really use yy to yank a line in one Vim and p to put it in another one. You will have to install a proper Vim for that: use your package manager for that.
Once you are set, you can yank to clipboard with "+y and put from clipboard with "+p. See :help registers for "+ and :help 'clipboard' if you would like to synchronise Vim's default register with the system clipboard.
That said, why don't you simply open those two files in a single Vim instance?

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

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

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

Undo a paste without undoing previous typing

In vim I frequently end up undoing more than I want to after a messed up paste from the system clipboard. Is there a better way?
To reproduce:
Open iTerm # or terminal
vi # open vim
i # enter insert mode
type some stuff
Cmd-V # paste the contents of the OS X clipboard
The paste is a mess because I haven't :set paste. So
Esc # enter command mode
u # undo
but that undoes my typing as well as my paste.
Is there a different undo I can use? Apart from remembering to :set paste before pasting is there a better way to do this in general?
This is not the answer you're looking for, but you really should stop using vim as a non-modal editor. If you want to paste then return to normal mode and use vim's paste commands.
Assuming that you have a vim version with clipboard access this should do the trick "+p. You can also facilitate this by setting your unnamed register to be the + (or *) register, such that pasting from clipboard becomes a simple p.
The following question has a great answer that includes all this information How to make vim paste from (and copy to) system's clipboard?.
I'm not sure there's anything you can do after the fact, but one solution is to hit Ctrl-G u in insert mode just before you paste. This breaks the undo block into two separate blocks - so the paste will be remembered as a separate undoable action.
Granted if you have to remember to do this, you might as well just use :set paste instead - but on the upside it's fewer keystrokes and you don't have to go to command mode first.
Take a look at this plugin, it can auto run :set paste! when you use Cmd-V(or Ctrl-V) to paste some text. And leave paste mode when you finished.
The following mappings create an additional undo point (via :help i_CTRL-G_u) before pasting in insert mode. This way, you can undo the paste separately.
inoremap <C-r> <C-g>u<C-r>
inoremap <C-v> <C-g>u<C-v>
Actually the solution is you have to go to command mode(e.g. Esc) first and re-enter the insert mode, but it only works if I type manually but it seems "randomly" stop working if I test it in ~/.vimrc. Google doesn't help at all.
I spends a lot of time try to fix this issue and I just figure out the reason in my case:
Don't map the paste key same with the terminal existing paste key
e.g. Ctrl+Shift+V will paste in my Konsole terminal, but if I assign this key <C-S-v> in ~/.vimrc, the "undo for only single paste instead of multiple pastes" will not working.
In my case, I have to use <C-v> instead of <C-S-v>:
inoremap <C-v> <Esc>"+pi<Esc>i<Right><Right>
Your case may difference, but the point is same: don't assign the same paste key conflicts with existing terminal emulator key.
I've 100% proved this conclusion by set my terminal paste key to Ctrl+V and now <C-v> stop working but <C-S-v> working.
Note also that the vim is too sensitive and strange. I figure out I have to use i and then 2 Right keys manually to make it works in the correct cursor position, that's means I have to put i and 2 Right keys in the ~/.vimrc too. Your case might difference, but the point is same, ensure the keys+order in ~/.vimrc 100% match with what you type manually.

How to copy all the text from vim editor using vim command line?

I want to select all the text from the vim editor, I tried the command :%y+ but getting error E850: Invalid register name. I get this command from this link. Please help me how to copy all the text from file which is open in vim. They are using yank, what is meaning of it..
I had a similar problem. Don't know why you got so many down votes.
The problem is that you haven't installed vim-gnome which takes about 24 MB and adds a feature to the inbuilt vim.
sudo apt-get install vim-gnome
then your command will work. :%y+ This command will copy all the text in system's clipboard.
TLDR: If you want to copy text in Vim to the system clipboard type ggVG"*y. Explanation below...
Vim runs in the terminal and, depending upon how you are using it and which type of Vim you are running, it's not really designed for you to select text with a mouse and copy and paste in the traditional way.
If you want to select all of the text using Vim then use ggVGy (note the uppercase VG in the middle). This command moves the cursor to the top of the file, enters visual mode, moves to the bottom of the file (thus, selecting all of the text) and then yanks (copies) it. You can then use p to put (paste) this code but only inside of Vim.
If you want to copy to the clipboard to use somewhere outside of Vim then try this:
First, select everything using the commands outlined above but without the final y: (ggVG). Then press "*y. This should now copy it to your operating system's clipboard and you can just paste (Ctrl/Cmd+v) anywhere you want outside of Vim. This can vary depending on what settings you have for Vim but it should work.
A brief explanation of the commands used. gg goes to the top of the file. V enters visual mode by lines. G goes to the end of the file. y yanks (copies) the text but not to the clipboard. p puts (pastes) the text.
The more advanced (i.e. cool) stuff:
" allows you to access registers. For example "a provides access to register a.
The * is the system clipboard so "* provides access to the system keyboard. Therefore, "*y yanks into the system clipboard.
While there's a great explanation of how to exploit the system clipboard in vim, it sounds like you're just having trouble getting your vim to access the clipboard in the first place. Try installing vim-gnome, it gives you the packages you need to get to the system clipboard.
For some reason, "* didn't work for me, but the exact same command with the "+ register did.
To select the whole file you can jump to the beginning, start visual mode, jump to the end:
ggVG
This question is a few years old now, but I had this same problem on Linux Mint 18. I found using xclip worked for me. You can map the command vmap <F7> :!xclip -sel c<CR><CR> in your .vimrc to have your current selection in visual mode copied to the system clipboard.
Here is a thread containing the above (and other) solutions.
You can use
Vggy/vggy or,
VGy/VGy
To visually select any number of text and then copy it, in your case it is gg / G as you want all text on the file,
gg is to copy while your cursor is at bottom of the file, gg for go to top
G is to copy while your cursor is at top of the file
Or even you can always use
Vk(as number of time)y to copy the selected lines of text.

Resources