VIM copying to clipboard and pasting to Notepad not working - vim

I'm trying to select a block of text from the Unix VIM editor in console (I'm using Putty) and paste it into notepad. I've already read all the threads here and nothing is working.
What I've done so far:
sudo apt-get install vim-gtk (+clipboard +xterm_clipboard activated)
vimrc file: maped "+y to Ctrl-c (for convienience)
V to select block of text in visual mode (for example 132 lines)
Ctrl-c to copy text (the mapping above works, so it says "132 lines yanked")
PROBLEM:
When I go to Windows Notepad try to paste the text, nothing happens. Why and how to fix?
Thanks
Kind regards

What you are doing should get your yanked lines into the X11 copy buffer on the remote machine, but to get as far as Notepad on your local machine you need a couple more pieces.
An X server that will treat the X11 and Windows clipboards as
unified. I believe Xming will do this, probably others as well.
X11 Forwarding enabled in putty (and allowed by the ssh server).
All other things being equal, it's a lot easier to just copy lines in Putty and paste in the traditional way. If this breaks formatting you need to preserve, you might consider working on the remote file and copying it to your workstation using scp or some other means.

Related

vim paste in byobu fails if origin file is not closed

I connect my server(centos 7) with putty, and run vim in byobu.
Now I have 2 windows in byobu, and two files, a.py in window-1 and b.py in window-2.
I tried copy some text from a.py to b.py, but ran into some problems.
If I follow steps below, it works fine:
STEP1: In window-1, open a.py, press yy, then quit with :wq
STEP2: In window-2, open b.py, press p.
But if I keep a.py, and b.py open at first , or doest quit a.py after yy, paste fails.
It seems quite odd, did missed anything?
I have set clipboard=unnamedplus in .vimrc, and have +clipboard in vim --version, my vim version is 8.2.1438.
You are able to paste across Vim sessions because of the [viminfo] file. By default, Vim stores the contents of non-empty registers (including the default register) into the viminfo file. That's why when you put (with p) right after you start Vim, you get the contents you last yanked (with yy) before you quit Vim last time.
The viminfo file is written right before Vim quits, and it's read during Vim initialization. That's why this won't work automatically if you start the second instance of Vim before you quit the first instance. Then the viminfo file will be read too early by the second instance (or written too late by the first instance) to allow you to put the yanked text across instances.
You can explicitly write the viminfo file with the :wviminfo or wv command. And you can explicitly read it with the :rviminfo or :rv command. So you could use these two commands in the two separate instances to transfer the register information through the viminfo file without having to quit them.
I have set clipboard=unnamedplus in .vimrc, and have +clipboard in vim --version.
If you're using the X11 clipboard integration, you could use that to share copied contents between Vim instances. Note that setting the 'clipboard' option only affects yanks, so to put from the clipboard register you'd have to use "+p.
But this assumes you have a valid X11 server to connect to, which is quite unlikely in your situation. First, since you're connecting through SSH using PuTTY, you would need to use SSH forwarding to export an X11 server from your client. Since you're using PuTTY, I'm assuming you're on a Windows box, which doesn't really run X11 by default, so you'd have to install one and run it (easier said than done.) Also, since you're running Byobu, you will also have trouble reconnecting to an existing Byobu session, which will quite likely be pointing at an invalid X11 server, using the connection information at the time the Byobu session was created, which may no longer match the new X11 forwarding connection information of the new SSH session... It'spossible to make this all work, but if you want my opinion, it's probably too much trouble, especially for this particular use case of two Vim instances in the same remote box.

Copy text without including formatting characters that are not actually present

I have some formatting configured in vim for tabs, trailing spaces and line wrapping:
I have set mouse=v, I copy text by selecting it on the terminal and pasting it into another application. How can I configure vim so those formatting characters are not included into the copied text?
When you don't let Vim handle the mouse, it'll be the terminal that processes the text selection. For the terminal, any application is just driving the cursor and printing characters here and there. It doesn't have any semantic background for the terminal contents; it's just lines and columns of characters.
So, you cannot expect the terminal to be able to detect line wrapping inside Vim. Neither can the terminal discriminate between actual text characters and surrounding visual information (the same applies to statuslines, number and fold columns, etc.)
Therefore, if you want access to the actual text, do the copying through Vim; it has the registers * and + for the X selection and system clipboard, respectively. For these to work, certain preconditions need to be met. There are plenty of related questions here and elsewhere on the Internet (depending on your OS, terminal, and how and what version of Vim you've installed).
If you really can't get this to work, you can :write (parts of) the buffer to a temporary file, and do the copy from there. If you connect to a different host, use Samba, scp, or ftp to transfer the file. As a last resort, temporarily disable the visual embellishments inside Vim, copy from the terminal, and undo hard wraps in the target application after pasting.
I created a Vim plugin to toggle linenumbers, listchars, etc. using a mapping: https://github.com/timakro/vim-copytoggle
Ingo Karkat's answer to use the clipboard feature of Vim is an alternative. I decided against it since debian packages Vim without the feature (the vim-gtk package includes it) and I don't want to set up X11 forwarding everywhere, and I don't want to install X11 libraries on headless servers. Also just selecting text with the mouse is more convenient for me. That though can also work using the clipboard feature by setting set mouse=a and set clipboard=autoselect,autoselectplus, see :help 'clipboard'.

How to select all text in vim normal mode?

Is there "keyboard-only" way to select an entire vim document in a way that is equivalent to a left-click and drag with a mouse in normal mode? Please do not confuse this with selecting all text in visual mode (ggVG). I want to be able to follow this up with a right-click paste into notepad++ (ggVG/ggVGy followed by a right-click paste in notepad++ does not copy the document). Thanks
Again, the "ggVG" commands are not working, nor are the "+y" commands (which I should have mentioned in my original post). Perhaps it is worth noting that I am working on a Windows local machine (where I have notepad++ open) and am generating the vim file on a linux virtual machine (slurm cluster). Under these working conditions, if I left-click drag over the vim doc and right-click paste in notepad++, the selected text copies over. However, the process is cumbersome for large files, hence my inquiry. Thanks again.
You should have mentioned that, of course, as it is not a meaningless detail at all.
Manual selection in a terminal can only select the text currently displayed in the viewport, which is obviously cumbersome for larger files. The only practical way to copy on the remote machine and paste on the local machine (and vice-versa) is to enable X-forwarding and build Vim on the remote machine against X libraries. This will give you what you want: a shared clipboard.
You won't be able to reach your goal in a practical way if you can't or don't want to install the necessary stuff on the remote machine.
As a lightweight alternative, you could simply scp the remote file to your local machine.
Just use (esc) :%y+. This will copy the entire document to your clipboard. Then you can go to notepad++, or whatever else you want to use, and paste it with a right click.
Explanation:
%: Tells vim the next command will be applied to all lines.
y: to all 'yank' lines
+: Copies all lines to clipboard, You can also use Ctrl + C instead. Note: + is sometimes bound as *. And sometimes both are equivalent.
Or you can also use the slightly longer way: ggVG+.
If you really want to be fancy you can remap Ctrl + A to ggVG or %y by adding this line to your .vimrc:
map <C-a> <esc>ggVG<CR>
Try to use Xshell remote login software. in there is a option called "To Text Editor".
Just open the file using "vi filename.c" it will displayed on the screen after that just made a left click on the work area and choose "To Text Editor--->all" . then these all text moved to a notepad file then u can simply copy and paste in notepad++.

Script to copy text in vim and paste to different window

I've been trying to get a script working which would copy a line of text from vim, delete the line, then paste the line to another window. So far I've tried implementing the script with Autohotkey (on Windows 8) and Autokey (on a Ubuntu VM I run). Here's what I've put down:
On Autohotkey:
!^p::
SendRaw "*yydd
SendEvent !{tab}
SendPlay ^v
On Autokey (linked to Ctrl+Alt+p):
keyboard.send_keys("\"*yydd")
keyboard.send_keys("%{TAB}")
keyboard.send_keys("^v")
In both instances, the Vim commands (yank current line to system clipboard, delete current line) work properly, but the scripts fail to switch windows for the paste, and instead execute a Ctrl-v in vim.
Does anyone know what I would need to change in order to get the Alt-Tab functionality working?
Sending the raw Alt + Tab keys doesn't work, as application switching is a core Windows function. In AutoHotKey, there's the AltTab special command (look it up in the help), or better use the WinActivate command, as AutoHotKey seems to still have issues with Alt-Tab on Windows 8.

vim -- copy to system clipboard in OpenSuSE

I've tried the methods mentioned at Vim: copy selection to OS X clipboard, but neither the * or + register seem to be working for me. I'm on OpenSuSE 11.3, and have vim and vim-data installed (there is no vim-full package as mentioned in the link in SuSE). I've tried with Klipper enabled and disabled. (edit) I've also tried pasting with ctrl+v and middle click.
Thanks in advance.
See if you have something called vimx, an X enabled version of vim. You can alias vim to that, and then use * register.
This is a superuser question.
I presume that you are running VIM in terminal and talk about VIM's visual selection, not selection with the mouse in terminal window.
Since in your case VIM isn't aware of the X, it obviously cannot communicate your selection to the X Window System. If you select something in terminal window with mouse, then you are selecting not in VIM, but in the terminal emulator which is aware of the X and communicates properly the current selection to X's clipboard.
You can try to play around with xsel tool which allows one to access the X clipboard from command line. You would need to override the usual clipboard shortcuts to not only put the content of the visual selection into the usual VIM register(s), but also to pipe it to the xsel so that it becomes the new X selection. (Strangely enough, I do not have the xsel installed on the SLES10 system I have right now at hand.) That obviously would only work if (1) the terminal is running locally or (2) remote server you are connected to allows the X forwarding. If the X forwarding is disabled on the server where you edit the files, then you have no chance to do in VIM: only selecting with mouse in terminal window would work.
Check synchronize contents of the clipboard and the selection in the clipper configuration.
Highlight text with the mouse (will not work with vim key selections).
Your selection is in the clipper.

Resources