Can vim use the system clipboard(s) by default?
Asked Answered
K

3

81

I am running into several problems because vim's tabs are, for the lack of a better term, god awful. I want to start using multiple Gnome tabs instead, each with a different instance of vim. Everything should work fine, however, only the system buffer + can be used to share text. This makes all the commands two key strokes longer:

y y becomes " + y y

y w becomes " + y w

d ' k becomes " + d ' k

This is especially so when one considers that a simply yank/paste operation like so

y y p

becomes

" + y y " + p

Is there anyway to instruct vim to always use the system clipboard(s)?

EDIT see Here for more information on using multiple instances of vim across Gnome Terminal Tabs

Kendrickkendricks answered 6/1, 2012 at 11:50 Comment(6)
whoever voted to close this, could you please clarify why you think this is not a good question.Kendrickkendricks
Can you explain why you don't like vim's tabs? Also, some people do not recommend to have more than one vim instance: One Vim ... just oneSondra
@Sondra I don't want 1 bufer/tab, but I still want to be able to group my buffers in different tabs. Vim does not allow this. All buffers are global. I discuss it here #8756959Kendrickkendricks
@puk: Why the need to call Vim's tabs "godawful" or "useless" just because they don't fit your idiosyncratic workflow? They're obviously useful; a huge number of people use and (enjoy using) Vim's tabs to make editing easier. Having said that, it would be an interesting project to write a plugin that adds functionality you describe in other SO question. E.g., each tab could have a t:buffers variable to hold buffer list for each tab and a tabbuffers navigation and other operations could be created with restriction to buffers only in a tab's t:buffers list.Youngstown
No it is true Vim does not just fit into what is now the standard workflow with out some nudging and the problems are in fact God awful to people who learned programming after things stopped being really really god awful.Stipend
possible duplicate of How to make vim paste from (and copy to) system's clipboard?Ruckman
K
107

I found a solution to my problem vim.wikia.com: Accessing the system clipboard. If you add the following to your .vimrc file

set clipboard=unnamedplus

for linux or

set clipboard=unnamed

for Windows.

Everything you yank in vim will go to the unnamed register, and vice versa.

Kendrickkendricks answered 6/1, 2012 at 12:36 Comment(8)
Note that this is not a perfect solution, if you copy a line outside of vim, it's not the same as yanking yy, so if you paste it p it will get pasted in place, not on the next p or previous P lineKendrickkendricks
@Kendrickkendricks you echo my sentiment. I have gotten used to just specifying the register. On occasion I even just :let @+=@" or similarHomothermal
@Homothermal what does :let @+=@ do?Kendrickkendricks
@Kendrickkendricks nothing! :let @a=@b assigns the contents of register b to register a, though. Next, register + is the X clipboard and register " is the unnamed register (used by default)Homothermal
I believe you can use both at the same time, like: set clipboard=unnamed,unnamedplus. Mind you VIM has to be build with +X11, +clipboard and +xterm_clipboard. On windows it goes like this by default, I suppose.Derouen
On Arch-derivatives, you get the clipboard enabled CLI vim when installing gvim or another GUI version of vim (see note).Visby
While this is the correct solution, it is confusing because it uses the term "unnamed register" wrong (or at least in a confusing way). The unnamed register is ", and that's where things you yank/delete go by default without any configuration. Setting the unnamedplus option makes it so that everything that goes to the unnamed register also goes to the + register (i.e., the system clipboard) and vice versa.Vieva
In order to get this working for me, I had to use unnamed for my vim in terminal and unnamedplus for my neovim in vscode.Gallantry
H
5

By the way, if you just want to use the terminal's native copy/paste handling, suggest setting

:se mouse-=a

and just doubleclick/rightclick as you're used to in your terminal.

That said, I love vim split windows and the fact that you can use the mouse to drag window dividers/position the cursor (heresy!). That requires mouse+=a... (and will work over ssh/screen sessions as well!).

I'm used to doing things like this instead:

:%retab|%>|%y+|u

and have commands like that on recall. Note that the "+ register is coded in the command line. To copy the last visual selection to the clipboard,

:*y+

or

:'<,`>y+ 
Homothermal answered 6/1, 2012 at 12:45 Comment(4)
can you please explain in a little more detail what :se mouse-=a does. Personally I'm not a big fan of the windows, I prefer the buffers. Buffers > windows >>>tabsKendrickkendricks
@puk: I've started to appreciate windows for diffmode + quickfix. I started to appreciate tabs for multiple diff-sets (e.g. :tabedit a|vert diffsplit b). I do use :set guioptions=agim switchbuf=usetab. To remove the UI suckiness and slownessHomothermal
having done what you suggest for a long time, I highly recommend puk's answer from above: set clipboard=unnamed[plus]Coligny
@Millimetric I'm set in my ways (I've come to rely on separate clipboards and hate non-standard settings in my vim - too many different systems). But: on your recommendation, I will give it a whirl for a good few weeks. CheersHomothermal
H
2

Possible workaround:

"Ctrl-c to copy in + buffer from visual mode
vmap <C-c> "+y

"Ctrl-p to paste from the + register in cmd mode
map <C-v> "+p

"Ctrl-p to paste from the + register while editing
imap <C-v> <esc><C-v>
Hoisch answered 6/1, 2012 at 11:54 Comment(3)
I thought about that, but then you'd need a new shortcut for yw, y{, y}, y%, D...Kendrickkendricks
I found a keyboard with just enough buttons rlv.zcache.com/…Kendrickkendricks
If you're going to use this solution, consider using vnoremap, noremap, and inoremap respectively.Glaucous

© 2022 - 2024 — McMap. All rights reserved.