Script to copy text in vim and paste to different window - vim

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.

Related

VIM copying to clipboard and pasting to Notepad not working

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.

Avoiding paste content to be executed in normal mode in Neovim/WSL/ConEmu

I've started using Neovim on Windows/WSL through ConEmu. It all works pretty amazingly except one pretty major flaw:
When I paste content into Neovim and forget to enter insert mode before, Neovim will register the input as keyboard strokes and execute them as commands. Needlessly to say, this can get pretty ugly, pretty fast.
A contrived example for clarity: I need to copy some text from my browser. The text in question is "sp" (yes, this is completely stupid, but bear with me). As I paste this into ConEmu (CTRL-V), the terminal transfers the input to Neovim that parses it as commands because it's in normal mode. In this example Neovim will replace whatever character is at the cursor at the time with p.
Vim for Windows has this solved and will paste the content without changing mode (how does a pure Linux environment handle this?). The problem with that solution is that it is dependent on the Windows file system which is a lot harder to tweak/maintain compared to apt-get/Ubuntu/WSL as well other annoyances.
So, initially I thought I'd try to make Neovim detect if the input was directly from the keyboard and not any other source, if it was, switch to insert mode and handle it from there. But I'm frankly not sure if this is even possible (especially though WSL). I also tried to add a shortcut that makes Neovim enter insert mode on CTRL-V in the hopes that is would trigger first and then correctly paste the content.
Maybe I need another terminal than ConEmu?
Anyway, I'm out of ideas, hope you guys can help.
ConEmu shall not detect any "internal" modes of the application you run in the terminal. Actually, when you "paste" something, terminal does almost the same thing if you press same keys manually.
Well, except one major difference: ConEmu uses "bracketed paste mode", so the console application may determine if user paste something or press keys manually.
So, it's the question to Neovim how to process/configure bracketed paste mode inside it.
In ConEmu settings, Keys & Marco -> Paste
There is an option:
Multi-line paste: avoid unexpected command execution by < Enter >
keypress
That's what you need.

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

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.

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