vim copy to clipboard with extra newline - vim

In order to copy to clipboard, I use "+y but one small thing is bugging me: if I copy it to some other program, like Sublime Text, sublime it entering to new line after i copy it.
For example, I have the following text:
this is sentences from vim
and I only select:
this is sentences
in visual mode then I press "+y.
When I paste in Sublime Text, I get a new line after it:
this is sentences
<this is where my cursor at>
Why does this happen? How do I prevent Vim from adding that newline when copying?

How to paste in a new line with sublime text 3?
this is somewhat similar; it shows that when you paste after 'line copying'
into Sublime, it will paste above your currently targeted line.
i.e. vim isn't adding a new line, Sublime is just pasting above your currently selected line

Related

Vim: Paste seems to be acting weird. Carrying out the keyboard shortcuts of characters that are copy pasted

My vim seems to be acting weird today.When I try to copy paste it is carrying out the vim keyboard shortcuts related to the characters I have copied and pasted. e.g. If i copy and paste two letter ks they will not paste but the cursor will jump up two lines. I think it has something to do with SHIFT+CTRL+V taking me out of insert mode and into normal mode. Then paste is putting the kk as input into normal mode.
How do I get back to the correct copy paste functionality/ stop making shift+ctrl+v exit insert mode? Thanks!
My cluster terminal went into bracketed paste mode while in vim somehow.
Typing reset into the terminal outside of vim fixed it.

How to copy a selected word to the clipboard using VIM?

I would like to copy a selected word that the cursor is on to the clipboard. Is there a way to do this.
If support for clipboard is built into your Vim, clipboard is mapped to register *, so you use it the same as any other register. So, for example, to copy the word under cursor to clipboard you do "*yiw. To copy current line "*yy. To paste from clipboard "*p
Perhaps you just want to do this:
viwp
which will visually select a new word, and paste over it.
Now, if you don't want to lose your register when doing this, you can also put in your vimrc:
xnoremap p pgvy
First at all you must check if the clipboard feature on your VIM is enable or not. For that use the --version parameter (vim --version)
If not (-clipboard), you can use a gtk vim edition or compile VIM manually.
If yes (+clipboard), in normal mode "+yiw on your word for copy it in your clipboard.
Follow instruction in https://stackoverflow.com/a/65666057/9384511
Once you have set up your vim as per above link, you will be able to copy single or multiple lines from vim to clipboard by pressing Ctrlc. And paste from clipboard to vim by pressing Ctrlp
Now to copy just a single word to clipboard press bveCtrlc

How do you select text in vim?

In most text editors, I can select text by clicking and dragging with my mouse, and then using Ctrl-C to copy that text, or Backspace to delete it.
However, since vim runs in the console, if I highlight some text with the mouse, my vim commands don't affect what I have selected.
What is the equivalent way to select text in vim?
In vim, text is selected by entering Visual mode. This can be done in multiple ways.
v (lower case v) begins regular Visual mode, and works similar to selecting text with a mouse. Use h and l to expand the selection left and right to include more words, and use j and k to expand the selection to the lines below and above.
V (upper case v) begins linewise visual mode. This selects entire lines of text at a time. Use j and k to expand the selection up and down.
Ctrl+v(lower case v) enters block visual mode. This selects text in a block format, allowing you to select parts of multiple lines without including the entire line. Use hjkl as usual.
As #FDinoff suggested, if your terminal emulator supports it, you can even specify visual selections with the mouse by enabling mouse input with :set mouse=a.
Once you have selected the text you want, you can use all sorts of commands on them. Some of the more useful ones are:
Escape visual mode
delete the text
yank (copy) the text
paste your clipboard onto the text, replacing it
change the text, which deletes it and sets your cursor for typing
replace the text with the next character you type
yq/p search for the text elsewhere in your document
You can learn more about Visual mode by typing :help v while inside vim.
Hightlight yanked text
First of all I would like to recommend highlight yanked text:
https://github.com/machakann/vim-highlightedyank (vim and neovim)
This is useful because it will give you a visual hint of what you have just copied.
For neovim:
augroup highlight_yank
autocmd!
au TextYankPost * silent! lua vim.highlight.on_yank({higroup="IncSearch", timeout=700})
augroup END
The vim philosophy goes way beond selecting, copying etc.
Start spending more time reading about vim/neovim and you will not going back to any other editor.
Nice to meet you dear "text-objects"
Read more about them here
Copy a whole paragraph to the clipboard:
"+yip
"+ .................... clipboard register
y ..................... copy
ip .................... inner paragraph
Copy the whole file to the clipboard
:%y+
Test some vim commands from the clipboard
:#+
The above command allows you to run functions and vim commands even if did not pasted them into your vimrc, there are some exceptions but in general it will work.
You can define your own text-objects
" vim line text-objects
xnoremap al :<C-u>norm! 0v$<cr>
xnoremap il :<C-u>norm! _vg_<cr>
onoremap al :norm! val<cr>
onoremap il :norm! vil<cr>
So you can use vil or dil
Sometimes you don't need to select to copy
If you wan to copy the second line to the end of the file you can do:
:2t$
If you want to move lines 4-7 to the beggining of the file you can do:
:4,7m0
copy from mark a to mark b:
ma .................. mark current line as mark a
Jump to a second place in your file and then
mb .................. mark current line as mark b
finally:
:'a,'by+
from mark a to mark b copy to the clipboard
Diving into advanced vim:
What is your most productive shortcut with Vim?

Paste in vim stripping newlines from the pasted text

I am copying large amounts of text from PDF documents into vim, but when you do a copy-paste from PDFs as opposed to say web browsers, everytime the PDF line breaks within a paragraph the copied text includes a newline. So when I paste into vim using 'p', I then have to press 'J' many times to collapse the newlines.
I'd much rather have a command that I can map to another letter on the keyboard that takes the contents of the copied text and pastes it removing the newlines. Any idea how I can do this?
This should do it:
map <leader>xx :let #* = substitute(#*, "\n", "", "g")<CR>"*p
Change the <leader>xx with the mapping of your choice.
What this command does, it substitues the end of line charater (\n) with nothing (""), inside the clipboard register (which is the star register). Then it pastes the text from the * register.
You can use my UnconditionalPaste plugin for that. It provides gcp / gcP mappings that force the paste to be characterwise, i.e. all newlines and indent are flattened to spaces. It also has other, similar mappings to force linewise mode, or paste with a custom separator, etc.

Why can't I copy paste this text in Vim?

When even i try to copy paste this text in vim it put half of it in the command line and half in text editor main window
This is the text
sub(/;;/," ",$0)
How can i copy paste that
When you paste in console Vim (not GVIM), Vim cannot detect whether what you've pasted is typed by you or an actual paste. Therefore, any (insert mode) mappings will apply. You probably have a mapping (maybe ;;?) that leaves insert mode, and that is triggered during the paste, wreaking havoc.
There are two ways to prevent that:
Either paste in normal mode via "*p (active selection) or "+p (system clipboard), provided that Vim is able to interact with them.
Or, set the 'pastetoggle' option, e.g.
:set pastetoggle=<F2>
and then press F2 (in insert mode) before pasting (note how the mode changes to -- INSERT (paste) --, and again after it. This way, you explicitly tell Vim "the next characters aren't typed by me, treat them literally".
If this manual management is too much of a hassle for you, you can alternatively use graphical GVIM.
I'm not sure which way you're copying and pasting (i.e. from an Xorg window (like gnome-terminal) to vim or vice-versa). Let's assume you want to copy and paste from an Xorg window into your vim window. There are a few ways to do it:
Select the text to copy using the mouse, put vim into insert mode, then press the middle mouse button.
If you are using vim with X support in a console (often started by typeing vimx) then you can select the text to copy using the mouse, then in the vimx window press "*p. The text selected by the mouse is placed in the * register.
If you are using vim with X support in a console (started by typing vimx) then you can select the text to copy using the mouse, right click the mouse, choose menu item Copy and then in the vimx window hit "+p to paste. In this case, because you used the right click menu item to copy, the text was placed in the in the + register.
Items 2 and 3 above also work when using gvim.
What you can do is make sure you're in insert mode and copy the code snippet with ctrl+v on the keyboard and then left click to the vim window to put it back in focus, then tap right click once and your text should paste in. I just tested this to make sure and it worked on CentOS 6.3 at least.
In vim you have modes, to enter text you have to be in insert mode. Press i (before current char) or a (after current char) before pasting your text.
If you don't go into insert mode, the first char s will erase the current char and enter insert mode putting ub(/;;/," ",$0) on your file.
Press ESC to leave the insert mode.
Also if you happen to have a shared clipboard with your GUI pressing p will paste.
To have a shared clipboard on OSX insert set clipboard=unnamed in your ~/.vimrc file.

Resources