Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically remove newline at end of yanked linewise visual selection?

Tags:

vim

Is there any way to yank a linewise visual selection without the newline at the end of the last line of the selection? More specifically, I'd like to be able to copy a line or lines from vim to the system clipboard and paste it elsewhere without the last command being executed without a chance to edit it.

I can get the desired effect by executing this command on the register in question after yanking:

:let @*=substitute(@*,'\n$','','g')

but is there any way to execute that command automatically? I am using MacVim at the moment and there doesn't appear to be a way to map an extra command to ⌘-C, so I'd have to have another mapping to remember to execute after copying if I can't find another solution.

like image 411
wilywampa Avatar asked Oct 03 '22 01:10

wilywampa


1 Answers

My original answer to myself has been working well enough that I pretty much forgot about it, but now I've decided to set clipboard=unnamed to always yank to the system clipboard. A side effect is that yanking to the clipboard also affects the unnamed register, from which I don't want the trailing newline to be removed.

The new (and more Vim-ish) answer is a mapping to convert the linewise visual selection to characterwise before yanking it:

vnoremap <C-c> <Esc>'<0v'>g_y

Now I copy with <C-c> if I intend to paste in a command line or just y any other time. Pretty much what user Explosion Pills suggested but some added stuff to include the first character of the first line and last of the last line.

like image 64
wilywampa Avatar answered Oct 13 '22 11:10

wilywampa