How to copy text in a remote vim session into terminal - vim

I would like to copy the following function and paste it into a shell outside vim:
Once I have selected the text I have tried doing "+y and it yanks the text, and allows me to copy-paste that into another vim editor, but if I try copying it into another terminal shell, it doesn't do anything. Now how I have to do it is use my mouse, remove the line numbers, and highlight those lines 37-42. What would be the proper way to yank/copy the text so I can paste it into other programs. Note that this is on a remote ubuntu server.
Note that outside of vim, I would have to use Cmd p to paste in the text (my local machine is a mac).

Related

Vim default register overwrites clipboard (only when using Git bash console)

Yanking/deleting text overwrites my clipboard when I use vim on Windows 10 from git bash console. For example, after I use "yy" or "dw" my clipboard content is replaced with corresponding text.
This issue does not happen when I run vim from Windows command prompt or when I use GVim, it only happens when starting vim from git bash console.
My vim version is 8.2. My git version is 2.36.1.windows.1.
I reinstalled vim, updated git, but it didn't help.
I noticed that after using :set clipboard=unnamedplus, the paste command inserts clipboard text instead of default register text, unless register is specified. For example, this inserts the right text after using yy:
""p
And this inserts the clipboard:
p
I have no idea if it's related but this issue started occurring while I was playing with clipboard using PyQt6 script ran from git bash console containing a line like this:
QApplication.clipboard().text()
Edit:
:set clipboard? command ouputted clipboard=unnamed for some reason. Adding :set clipboard-=unnamed in .vimrc seems to "fix" the whole issue.
Hmm… the whole point of set clipboard=unnamedplus is precisely to synchronize register "" with register "+ so that y yanks to the system clipboard and p puts from the system clipboard.
Therefore,
For example, after I use "yy" or "dw" my clipboard content is replaced with corresponding text.
seems to describe a perfectly working setup that does what you ask it to do. I'm not sure what you want to fix, here.
What sounds strange, though, is this:
I noticed that after using :set clipboard=unnamedplus, the paste command inserts clipboard text instead of default register text, unless register is specified. For example, this inserts the right text after using yy:
""p
And this inserts the clipboard:
p
With set clipboard=unnamedplus, both p, "+p, and ""p should put the exact same text.
The command is "put", by the way, not "paste".
So, what do you want, exactly? That "" and "+ are always synchronized or that they are never synchronized?

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.

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.

copy a line/multiple-lines from vi editor to cmd line

Is there a shortcut key to copy a line from vi editor and then paste it into a terminal.
Currently I select the text with mouse and then press crtl+shift+c (I'm using default settings of KDE) and then do ctrl+shift+v on the konsole. The problem with this option is when the line is long enough to wrap. In that case copy-paste inserts extra spaces which I have to fix after pasting on terminal.
This is very annoying specially when the line copied is very long and contains file names (typically commands used to invoke compiler).
You can yank to the X clipboard by putting the contents in the * register.
To do this use "*yy on the line you want to yank. Then outside of vim you should be able to paste it.
If you don't have access to a clipboard via X or screen or tmux, you can write the contents of the buffer to a temp file, then go to the terminal and invoke $(cat temp-file) or open an editor for the command line and read in the file. Whatever shell you are using probably provides a mechanism for opening an editor on the command line. In bash with vi-style readline keybindings you can type v to get a vi session. If you shell does not provide that functionality, try a different shell.

selecting in visual mode to paste outside vim window

I need to paste some selected block in visual mode to outside of vim. Currently I need to select this block manually from mouse to paste outside of vim.
As selecting texts in visual mode is easier ,it would be efficient to select some text for pasting outside of vim.
You could yank the text into the + (plus) register, that is mapped to the system clipboard. Just select the text in the mode you like and then type "+y.
Disclaimer: Linux
So what I have noticed is you need clipboard support compiled in to your vim. I ended up compiling my own vim, which had the clipboard support. To check run vim --version and look for a +clipboard or a -clipboard, if it's + then yay you have it, if it's - then you need to compile vim yourself or download a version with clipboard support compiled in. Then the answers people have said seem to work. For me "*y copies into the buffer that is pasted by pressing the middle button, and "+y copies into the buffer which is the normal control + c or on the terminal control + shift + c so what I put into my vimrc was
map <C-c> "+y
that way doing control + c me paste it somewhere else by pressing the exact same command
:wq
If you are using GUI-based gvim, simply yank your text into the "clipboard register" by prefixing your yanking command with "+. That is, when you have finished selecting your text in visual mode, press "+y to yank your text then it will be in your system clipboard.
If you are using text-based vim and your vim has clipboard access to your current system, it's just the same as gvim. If your vim has no clipboard access, try to establish the clipboard connection as described in this page:
http://www.quora.com/How-can-you-copy-all-contents-of-a-text-file-opened-in-vim-through-Putty-on-a-Windows-desktop-to-Windows-clipboard
You can bind contents of the visual selection to system primary buffer (* register in vim, usually referred as «mouse» buffer) by using
set clipboard^=autoselect
You have to identify the register that vim is using to get the outside clipboard.
First copy any text outside vim and then inside vim do the command :registers and look for the text you copied, once you have identified the register simply use it every time you need to copy and paste from the outside:
for example:
Im using gvim in Windows7 and the register used by vim to get the external clipboard is
*"
then in vim select the text and do
*"y to copy (yank) and paste outside as usual
and to paste inside vim from outside do *"p
you can also do a map to the register to easy copy/paste

Resources