How can I make Vim paste from (and copy to) the system's clipboard?
Asked Answered
O

38

1104

Unlike other editors, Vim stores copied text in its own clipboard. So, it's very hard for me to copy some text from a webpage and paste it into the current working file. It so happens I have to either open gedit or type it manually.

Can I make Vim paste from and to the system's clipboard?

Obstacle answered 15/7, 2012 at 4:28 Comment(12)
Possible duplicate of How to copy to clipboard using vim?Jubilant
Copy "+y Paste "+p Works just like yank and paste except you specify he registry before.Mehta
To use following commands, make sure you have done sudo apt-get install vim-gnome which will add that functionality to inbuilt vim of using system's clipboard.Act
@Harnirvair For many sets of readers here, vim-gnome is probably overkill or simply unavailable in their OS/distro, whereas I suspect vim-gtk and preferably vim-gtk3 are more likely to exist and pull fewer dependencies, while still providing clipboard integration (at least for those still on X11; I'm not sure how this all interacts with Wayland).Emersonemery
...hmm, my comment was based on finding Ubuntu's vim-gnome package, which seems to be an old GNOME 2 thing. However, on Debian proper, vim-gnome is just a dummy package that installs vim-gtk3 and vim-gui-common. My guess is it's the latter that provides the clipboard enhancements here.Emersonemery
See also vi.stackexchange.com/questions/84/…Bazluke
By default on Fedora - there is no clipboard access (you need gvim), superuser.com/questions/194747/…Bazluke
BTW, on windows system when running VIM in a cmd windows, the usual Ctrl+v, Ctrl+c windows shortcuts work to interact with the windows clipboard. The solutions provided below didn't for me.Stelmach
CTRL + C and then CTRL + SHIFT + V, work for copying and pasting in vim (even from a webpage), but problems arise when you want to copy text from vim to another application and you can't simply scroll down and highlight text, whilst using CTRL + SHIFT + COpsis
Checkout https://mcmap.net/q/54093/-how-to-copy-selected-lines-to-clipboard-in-vimGaunt
:) I have used the gedit method too, until I have found this solution below.Oriel
I just tested a superbly simple solution on a Mac, using the pre-installed vim. Just add "set clipboard=unnamed" to ~/.vmrcBixler
J
1159

TL;DR

Try using "*yy or "+yy to copy a line to your system's clipboard.

Full answer

Be aware that copying/pasting from the system clipboard will not work if :echo has('clipboard') returns 0. In this case, Vim is not compiled with the +clipboard feature and you'll have to install a different version or recompile it. Some Linux distros supply a minimal Vim installation by default, but if you install the vim-gtk or vim-gtk3 package you can get the extra features nonetheless.

The "* and "+ registers are for the system's clipboard (:help registers). Depending on your system, they may do different things. For instance, on systems that don't use X11 like OS X or Windows, the "* register is used to read from and write to the system clipboard. On X11 systems, both registers can be used. See :help x11-selection for more details, but basically the "* is analogous to X11's _PRIMARY_ selection (which usually copies things you select with the mouse and pastes with the middle mouse button) and "+ is analogous to X11's _CLIPBOARD_ selection (which is the clipboard proper).

If all that went over your head, try using "*yy or "+yy to copy a line to your system's clipboard. Assuming you have the appropriate compile options, one or the other should work.

You might like to remap this to something more convenient for you. For example, you could put vnoremap <C-c> "*y in your ~/.vimrc so that you can visually select and press Ctrl+c to yank to your system's clipboard.

You also may want to have a look at the 'clipboard' option described in :help cb. In this case, you can :set clipboard=unnamed or :set clipboard=unnamedplus to make all yanking/deleting operations automatically copy to the system clipboard. This could be an inconvenience in some cases where you are storing something else in the clipboard as it will overwrite it.

To paste you can use "+p or "*p (again, depending on your system and/or desired selection) or you can map these to something else. I type them explicitly, but I often find myself in insert mode. If you're in insert mode you can still paste them with proper indentation by using <C-r><C-p>* or <C-r><C-p>+. See :help i_CTRL-R_CTRL-P.

Vim's paste option (:help paste) is also worth mentioning: This puts Vim into a special "paste mode" that disables several other options, allowing you to easily paste into Vim using your terminal emulator's or multiplexer's familiar paste shortcut. (Simply type :set paste to enable it, paste your content and then type :set nopaste to disable it.) Alternatively, you can use the pastetoggle option to set a keycode that toggles the mode (:help pastetoggle).

I recommend using registers instead of these options, but if they are still too scary, this can be a convenient workaround while you're perfecting your Vim chops.

See :help clipboard for more detailed information.

Jesuit answered 15/7, 2012 at 4:31 Comment(29)
This is very informative, but can you summarize what keystrokes one needs to make to paste from the system clipboard ... for example what is one doing with "*p? Do I press Shift-', then Shift-8, then p, etc.?Bloc
Yes, type those three characters in sequence. Many commands in vim utilize what is known as "operator pending mode" where one key is pressed and then vim waits for more keys. Take dd for example. The first d enters operator pending mode, then the second d finishes the command and deletes the line. If you type dG, however, the G makes it delete to the end of the file instead of the just deleting the line. Registers use this same principal to allow various commands to use them.Jesuit
An emphasis: you need to install the vim-gtk package (under Debian, at least), to get vim that is compiled with +xterm_clipboard. Took me 40mn to find this out, and only a few seconds to sudo apt-get install vim-gtk.Detonator
@Kazark It is great, though C-r C-p + is a bit of a mouthful for what in Windows is just Ctrl-V. See my answer which makes a mapping. This command :inoremap <C-v> <C-o>"+p then in insert mode, Ctrl-VHosier
FYI, on windows and mac "* and "+ are equivalent.Longish
@DominykasMostauskis vim-gnome also works and has +xterm_clipboard enabled.Lymphadenitis
Thanks, this is very informative. I'd just like to add that if your distro doesn't have something like "vim-gtk", you're probably looking for a package called gvim.Halfprice
I typed ':help registers' and can't switch back to my buffer... Sad storyFluviatile
Usually the help window opens as a split but try :b#Jesuit
For Red Hat or Fedora distros, install vim-X11.x86_64 package and use vimx to get the copy-pate done. Without clipboard enabled, you won't be able to get this.Twum
Could someone clarify whether you need +xterm_clipboard or +clipboard only? I am working on a solaris terminal-only session and I have a pure vim 7.4 with +clipboard, but I find myself unable to copy to system clipboard using the above commands (I am on MacOS). It will not be possible for me to install gvim or the like.Saltatorial
Sometimes 'right click' in the editor pastes from system's clipboard.Underarm
Thanks for nice explanation. In general it works for me, except the case when I start vim in su session. In particular steps are the following: start xterm then su - root – now "*p does not insert from X clipboard, but rather from vim clipboard and prints Client is not authorized to connect to Server to console. In this case xhost + helped.Valuator
@Valuator not sure if sudoedit instead of sudo vim might fix your issueJesuit
@Chev not sure what it was like in 2014, but today in Debian sid, the package vim-gnome is just a dummy wrapper for vim-gtk3 and vim-gui-common.Emersonemery
I'm using the default Vim 8.0 on Fedora 25 and the echo has('clipboard') returns 0Bazluke
To switch the clipboard on in Fedora 20+, you need to install vim-X11 and run Vim with vimx instead of vim. See vi.stackexchange.com/a/2065/11986, or see @Twum 's commentBazluke
@Underarm I kept trying that to no avail, but I discovered that center-click does, thanks to Connor's parenthetical: "…X11's PRIMARY selection (which usually … pastes with the middle mouse button)…"Thekla
Too long an answer. If I wanted this description I could have read the docs, or a blog.Tristram
You might also need to enable X11 forwarding (in your terminal emulator: in PuTTY go to SSH > X11) AND install/run an X11 server on your Windows machine. You can test if X11 is forwarding properly by seeing if gvim opens up on the Windows side.Dorris
@DominykasMostauskis I don't have permission to install vim-gtk on a particular server. SHIFT+INSERT allowed me to paste content from system clipboard into vimFornicate
@Fornicate that's completely dependent on which terminal emulator you're using. See the last paragraph about :help pasteJesuit
Update for vim 8+ (Linux and Mac), just do system paste in Normal Mode. No more :set paste needed. #11489928Suttle
Downvoted because answer assumes this, which is not true for many use-cases: > Be aware that copying/pasting from the system clipboard will not work if :echo has('clipboard') returns 0.Affection
@Affection internal copying/pasting will not work if not compiled with system clipboard. Copying and pasting through your terminal emulator is not the same thingJesuit
That is a serious wall-of-text for copy n' paste instructions! tl;drRahal
"*p worked for me on Windows 10 - and "*y looks like copy.Grimace
Typed so much uninformative answer. Just :set paste is enough.Eldridgeeldritch
Ironically compared to the other comments, I appreciate the full context. Especially the check at the beginning for has('clipboard') since that saved me a lot of potential wasted time debugging. +1Ardoin
W
334

You can paste into vim by gnome-terminal's shortcut for paste. Place the file in insert mode and use

Ctrl+Shift+v.

Remember beforehand to

 :set paste 

to avoid messing with the indentation.

Waligore answered 15/7, 2012 at 15:46 Comment(5)
this mess up with indentation if there are multiple line like python for loopCalash
Use a paste toggle for insert mode. E.g. set pastetoggle=<F2> Then in insert mode, hit <F2> and then insert your python code.Reck
if tabs/white spaces, eols etc. are shown as a visible character (via the listchars option), then those substitute chars will be copied instead of the hidden ones.Basilio
When the question seems to be about direct yanking/pasting from/to vim, then I'm not sure putting the text through the terminal's naive copy/paste layer really answers that question. The OP probably knew about this but didn't bother mentioning it because it's often so useless in practice. ;) To the problems others mentioned, I'd add that copying from vim naively into the system clipboard is also practically useless if you have line numbers on, which many of us find indispensable: they get dumped out too.Emersonemery
Update for vim 8+ (Linux and Mac), just do system paste in Normal Mode. No more Insert Mode or :set paste needed. #11489928Suttle
H
160

I believe that this question deserves a more objective and pictorial answer:

Entering Paste Mode

  • ESC
  • :set paste
  • press i
  • SHIFT + Insert (with a text copied on your clipboard)

Leaving Paste Mode

  • ESC
  • :set nopaste
  • press i

You pasted the text and you're able to type again.

Hedonism answered 20/4, 2018 at 3:35 Comment(11)
I got this to work, but :set nopaste does nothing. What is it supposed to do? Why do I need it?Flouncing
Well, actually, it does. When you perform :set nopaste, you're telling the editor that you don't want to paste anymore. And whe you hit i, you're returning to the edit mode :).Hedonism
Sorry I am a vi noob. Is there any reason for not permanently leaving :set paste enabled?Flouncing
To be honest with you, Marko, I don't know.Hedonism
@Marko One good reason to not leave :set paste enabled all the time is that in paste mode you don't get the automatic insertion of tabs and comment characters (compare putting your cursor on a line with a comment and pressing o in paste mode and not in paste mode).Zebrass
This worked like a charm for me. Every time I come back to vim I always end up having to look something like this up.Autosuggestion
This is the best answer that works perfectly without going too much deep to the details. Thx Ivanleoncs.Jimmyjimsonweed
You wonder why nobody else before could explain this so nice an simply.Topsyturvy
Perfect answer to explain how to paste what is already on the system clipboard ! Could you please add how to copy something TO the system clipboard ? Thanks !Holotype
Followed these steps, but the {SHIFT + Insert} step didn't work. However, right clicking with my mouse had the effect of pasting text from clipboard.Trudi
God bless you, it worked for me too.Brunei
B
57

For my that configuration works for copying and pasting

" copy and paste
vmap <C-c> "+yi
vmap <C-x> "+c
vmap <C-v> c<ESC>"+p
imap <C-v> <ESC>"+pa
Bedrabble answered 16/5, 2013 at 8:50 Comment(2)
into what did you paste?Porism
This IMHO is a better solution that setting clibboard to unnamed - not to mess with the OS clipboard with every x/dd/y etc actions. And on a Mac it may be a better idea to change the C(ctrl) modifier to D for mapping cmd+c/x/v combinations.Dosh
A
50

This would be the lines you need in your vimrc for this purpose:

set clipboard+=unnamed  " use the clipboards of vim and win
set paste               " Paste from a windows or from vim
set go+=a               " Visual selection automatically copied to the clipboard
Arriaga answered 15/5, 2013 at 21:2 Comment(5)
I like your solution. I would like a better explanation.Gobo
excellent. using registers always makes me hesitate and have to think. This is much betterApomorphine
My vim version has +clipboard and this option doesn't work for me. I'm using Byobu (which uses Tmux under the hood).Lohengrin
I had to add set clipboard+=unnamedplus for FedoraFloyd
To understand set go+=a read vimhelp.org/options.txt.html#'guioptions'Ethanethane
D
46

Linux

On my Linux system, the + and * registers map to an X11 selection, which can be pasted with the middle mouse button. When :set clipboard=unnamed and :set clipboard=unnamedplus are used, then the registers map to the clipboard, and can be pasted with CTRL-V.

The specifics seem to be somewhat configuration and system dependent, so your mileage will definitely vary. It should definitely get you pointed in the right direction, though.

See Also

http://vim.wikia.com/wiki/Accessing_the_system_clipboard

Dormancy answered 15/7, 2012 at 4:38 Comment(3)
Only * maps to X11 primary register (which is normally pasted by mouse). + maps to X11 clipboard register normally pasted by shortcuts.Absorbance
@Absorbance Not on my system, but your mileage may certainly vary. It's best to configure the behavior you want explicitly if you want a consistent experience across platforms and software versions.Dormancy
I believe it means you have clipboard manager like klipper/glipper, they are able to synchronize two buffers. It does not change the fact that vim uses * for primary and + for clipboard X11 registers, you can see this in the vim source: these values are set there and never modified, and clip_star and clip_plus are always used for * and + registers respectively.Absorbance
Q
34

Shift + Right Click -> Paste

did the trick for me

Quasar answered 27/5, 2022 at 16:42 Comment(6)
How about copy? (It looks disabled)Forge
@Forge if you highlight some text first and then shift + Right Click, it will be enabled (at least in my Ubuntu 20.04 terminal).Quasar
In my Ubuntu 22.04, I type "shift + v" to enter visual mode, then I highlight the lines I want to copy, but when I shit + right click, in the menu, the copy option is disabled. Why? What am I missing?Forge
@Forge I'm not using visual mode and it works just fine for me (inside VIM or otherwise).Quasar
I didn't need to hold the Shift key for this to work for me. I just right click within the terminal and click on paste and it puts the text where the cursor is.Colonel
This answer needs more love, as it especially works in vim-tiny, which has no clipboard functionality. Make sure you are in insert mode, but NOT in visual mode. However, I, too, can't seem to get copy to work.Fracas
P
28

clipboard

There is a special register for storing this selection, it is the "* register. Nothing is put in here unless the information about what text is selected is about to change (e.g. with a left mouse click somewhere), or when another application wants to paste the selected text. Then the text is put in the "* register. For example, to cut a line and make it the current selection/put it on the CLIPBOARD:

    "*dd

Similarly, when you want to paste a selection from another application, e.g., by clicking the middle mouse button, the selection is put in the "* register first, and then 'put' like any other register. For example, to put the selection (contents of the CLIPBOARD):

    "*p

registers E354

> There are nine types of registers:                      
> 1. The unnamed register ""
> 2. 10 numbered registers "0 to "9
> 3. The small delete register "-
> 4. 26 named registers "a to "z or "A to "Z
> 5. four read-only registers ":, "., "% and "#
> 6. the expression register "=
> 7. The selection and drop registers "*, "+ and "~ 
> 8. The black hole register "_
> 9. Last search pattern register "/

Paste from clipboard

1. Clipboard: Copy
2. Vim insertmode, middle mouse key

Check for X11-clipboard support in terminal

When you like to run Vim in a terminal you need to look for a version of Vim that was compiled with clipboard support. Check for X11-clipboard support, from the console, type:

% vim --version

If you see "+xterm_clipboard", you are good to go.

http://vim.wikia.com/wiki/Accessing_the_system_clipboard

The X server maintains three selections, called:

PRIMARY, SECONDARY and CLIPBOARD

The PRIMARY selection is conventionally used to implement copying and pasting via the middle mouse button. The SECONDARY and CLIPBOARD selections are less frequently used by application programs.

http://linux.die.net/man/1/xsel

Pegpega answered 21/7, 2013 at 8:55 Comment(0)
K
18

Copy to the OS clipboard

Select text in visual mode, and press "*y.

Paste from the OS clipboard

Press "*p.

Kinchinjunga answered 12/9, 2019 at 11:15 Comment(0)
P
14

Didn't have +clipboard so I came up with this alternative solution using xsel:

Add to your ~/.vimrc:

vnoremap <C-C> :w !xsel -b<CR><CR>

Pearman answered 6/1, 2014 at 23:55 Comment(2)
Thanks for this great idea! I ended up using: vnoremap <C-C> :w !xclip -i -sel c<CR><CR>Hyderabad
That works, both xsel and xclip, but visual selection extends to whole lines. However pressing C-C twice does the work properly! I do not why...Supereminent
C
13

I tried the suggestions above and none of them worked in my environment. (Windows PuTTY clone over ssh)

Some additional googling turned up: https://unix.stackexchange.com/questions/110684/copy-paste-into-sshd-vim-from-local-windows-clipboard

One of the comments suggested using SHIFT+INSERT which did the trick for pasting from my desktop's clipboard into Vim's buffer. Ctrl-C was already working to copy to the desktop's clipboard from Vim.

Coleslaw answered 22/8, 2014 at 18:36 Comment(11)
i'm working on vim inside cmder tool [windows 8.1]. when you go into cmder settings > keys and macros > paste, there i got shift+insert command for multiline paste and ctrl+v for single line paste.Thirtytwomo
for me a simple right-click works like a charm!! Just a right-click!Millard
Because you're using a terminal and ssh vim cannot reach your native system's clipboard through SSL. SHIFT+INSERT works because it is the paste option for your terminal (or in your case PuTTY); however, indentation will likely be wrong. Check out the last paragraph of my answer about the paste option to correct this.Jesuit
@jinksPadlock shift+insert works well in this case, but I have yet to be able to copy from ssh vim to my client's X clipboard. Could you share what your <C-c> is mapped to please?Cornetcy
Have been struggling with this. Trying to understand what is your environment: Are you using Putty on Windows to log into a linux server via ssh, then starting VIM via putty terminal? Then in the VIM terminal you are pressing shift + Insert? I am using bash on windows (WSL) to do the same.. but Shift + Insert doesnt work?Lubricant
Any idea why this might be happening? I have also tried cntrol + Shift + V and a whole bunch of other commands..Lubricant
@lliseil, did you figure out how to paste from VIM to windows?Lubricant
@alpha_989, you are correct that was my environment at the time. Sorry I couldn't answer lliseil b/c I no longer have that environment to check the settings. Here is a question that might relate to your WSL configuration: #38832730Coleslaw
@jinksPadlock. thanks.. yeah.. I can copy/paste via mouse.. was trying to figure out how to do that by "keyboard only". Copying from windows to VIM is possibly by Keyboard-only using Putty, but not in the reverse direction. In bash for windows.. its not possible to do so in either direction ATM. Compiled a list of resources to point people in the right directions and also for us.. if you have anything to add..maybe you can add them here: #45272395Lubricant
@Lubricant later-better-than-never ;) I cannot add to jinksPadlock' as I presently don't have a Windows OS under my fingers. SHIFT+INS works from a linux host to ssh vim as well as does the mouse third click.Cornetcy
I am using PuTTY on Windows 10 over SSH to Linux Server 16.04.6 LTS and without System Clipboard in the compiled version of VIM. I liked the simple explanation given on advancedweb.hu/working-with-the-system-clipboard-in-vim see Plan B. In short, copy text from source with the usual CTRL-C and in the VIM window press (Left) Shift + Insert keys. In reverse, highlight text in the VIM window, press RETURN and in the destination use CTRL-VSuperstition
N
11

These key combinations work on any OS.

Select target Text using the mouse, and refer to the key sequences to copy, cut, and paste.

  • copy: Ctrl + Insert
  • paste: Shift + Insert
  • cut: Shift + Del
  • paste: Shift + Insert
Normannormand answered 8/2, 2022 at 2:5 Comment(5)
I haven't tested this answer, but (somewhat surprisingly?) it does not appear to be a duplicate.Thekla
copy: Ctrl + Del ?????? It deletes!Forge
@Forge Sorry, I fix it.Normannormand
Probably the fastest solution, works on Fedora Linux. Thanks!Comeuppance
ins???? my keyboard doesn't have that since the 90s. is there another key?Josephson
H
9

The simplest solution to this, that also works between different Linux machines through ssh is:

  1. Check whether vim supports X-11 clipboard: vim --version | grep clipboard. If it reports back -clipboard and -xterm_clipboard you should install either vim-gtk or vim-gnome (gvim on arch linux)

  2. Add the following lines to your .vimrc:

set clipboard=unnamedplus
set paste
  1. If you login on a different machine via ssh, use the option -Y: ssh -Y machine

Now copying and pasting should work exactly as expected on a single, and across different machines by only using y for yank and p for paste. NB modify .vimrc on all machines where you want to use this feature.

Hippopotamus answered 6/7, 2017 at 9:4 Comment(1)
I think, X server should be available in server as well for this to work. Yeah! it might be installed by vim-gtk. I think, xvfb will be an alternate option for running X server.Thorvald
S
9

With Vim 8+ on Linux or Mac, you can now simply use the OS' native paste (ctrl+shift+V on Linux, cmd+V on Mac). Do not press i for Insert Mode.

It will paste the contents of your OS clipboard, preserving the spaces and tabs without adding autoindenting. It's equivalent to the old :set paste, i, ctrl+shift+V, esc, :set nopaste method.

You don't even need the +clipboard or +xterm_clipboard vim features installed anymore. This feature is called "bracketed paste". For more details, see Turning off auto indent when pasting text into vim

Suttle answered 17/7, 2019 at 23:40 Comment(1)
This needs to be waaaay up there as the current correct answer.Moyna
O
8

What simply worked for me in Linux (Ubuntu 20.04)

  • Copying to system clipboard:

    1. Select what you want to copy in Visual mode.
    2. Type "+y
    3. Press Enter
  • Paste something form system's clipboard:

    1. Move to the place where you want to paste the copied text in vim.
    2. Just type
      • "+P to paste before cursor OR
      • "+p to paste after cursor.

To know more how this works: Copy and Paste to/from Vim from/to Other Programs!

Otisotitis answered 28/4, 2021 at 10:10 Comment(0)
W
7

A quick note for people whose Vim installation does not support the * and + registers. It is not necessary to download a new Vim installation to paste from the clipboard. Here is an alternative method:

  1. Install parcellite (a clipboard manager with a low memory footprint);

  2. In your .vimrc file, add the following:

    command Clip r !parcellite -c
    
  3. Restart vim.

Now when you type in :Clip as an ex command, the contents of the clipboard will be pasted in at the cursor. You can also map the new command to a function key so as to be able to do this with one keystroke.

Wamsley answered 8/12, 2013 at 0:1 Comment(0)
R
7

If you are using a mouse first do

 :set paste 

Then right click mouse and the contents in buffer will be pasted

Redpoll answered 2/3, 2018 at 13:58 Comment(0)
U
6

This works for me: Ctrl+Shift+V

Unstring answered 14/3, 2016 at 12:2 Comment(1)
@neel's answer already stated this. This should only be a comment, or better an up vote, if you want to say it also worked for you.Unifoliolate
H
4

Following on from Conner's answer, which was great, but C-R C-p + and C-R C-p * in insert mode is a bit inconvenient. Ditto "*p and "+p from command mode.

a VIM guru suggested the following to map C-v to what C-r C-p + does.

You could have :inoremap <C-v> <C-o>"+p for insert mode only

if you really wanted to override blockwise visual mode (not recommended by him as visual mode is good) you could have map <C-v> "+p

Hosier answered 25/5, 2014 at 14:10 Comment(0)
P
4

Since vim 8 right click enables visual mode by default. This prevents the "normal" copy & paste (call it a "defect by design" https://github.com/vim/vim/issues/1326). Fix it by doing:

echo "set mouse-=a" >> ~/.vimrc .

Exit and restart vim.

Psychasthenia answered 2/5, 2019 at 12:4 Comment(0)
S
4

I ran into this issue on a mid-2017 Macbook Pro running vim within iTerm2 as my primary development environment.

As other answers have suggested, I ran vim --version and noticed that it returns -clipboard, which means that the version of vim that shipped with my machine hasn't been compiled with the clipboard option.

The homebrew package for vim appears to compile with the clipboard option, so the fix for me was to:

  1. Run brew install vim
  2. Add set clipboard+=unnamed to my ~/.vimrc file
  3. Close and reopen iTerm2
Sanctus answered 25/7, 2019 at 12:57 Comment(0)
B
3

If you are using vim in MAC OSX, unfortunately it comes with older verion, and not complied with clipboard options. Luckily, homebrew can easily solve this problem.

install vim:

brew install vim --with-lua --with-override-system-vim

install gui verion of vim:

brew install macvim --with-lua --with-override-system-vim

restart the terminal to take effect.


append the following line to ~/.vimrc
set clipboard=unnamed

now you can copy the line in vim with yy and paste it system-wide.

Beaumarchais answered 16/8, 2016 at 8:0 Comment(0)
C
3

If you have it, try removing this from your vimrc: set mouse=a

It messes with the paste functionality.

Clergyman answered 1/9, 2020 at 19:55 Comment(0)
E
2

On top of the setting :set clipboard=unnamed, you should use mvim -v which you can get with brew install macvim if you're using vim on Terminal.app on Mac OS X 10.9. Default vim does not support clipboard option.

Escurial answered 20/11, 2013 at 20:26 Comment(0)
U
2

Based on @lis2 answer, I use a simpler configuration that will not force Insert mode at the end:

" Copy and paste
if has('clipboard') && !has('gui_running')
  vnoremap <C-c> "+y
  vnoremap <C-x> "+d
  vnoremap <C-v> "+p
  inoremap <C-v> <C-r><C-o>+
endif

Mind that all these override default Vim mappings:

  • v_CTRL-C: stop Visual mode
  • v_CTRL-X: subtract [count] from number
  • v_CTRL-V: blockwise Visual mode
  • i_CTRL-V: insert next non-digit literally, which is also mapped to i_CTRL-Q

As an alternative, one can use keys inspired in the "yank", "delete" and "put" Vim verbs: <C-y>, <C-d> and <C-p> respectively. These would only override one default mapping:

  • i_CTRL-P: backwards search keyword for completion
Unifoliolate answered 23/7, 2016 at 21:48 Comment(0)
C
2

The other solutions are good if you want to change your vimrc, etc... However I wanted an simple way to copy from vim to my system keyboard. This is what I came up with.

  • Select the text you want to copy with visual mode v
  • Press : (it will automatically expand to show :'<,'>)
  • Type y * or y + (depending on your system) to yank the selected text to the system clipboard
Columbuscolumbyne answered 21/12, 2016 at 0:0 Comment(0)
C
2

Yet another way to paste is just to 'r'ead 'cat' output:

:r!cat

This will run cat in the foreground, so you can send to the terminal what you want to insert/have in your clipboard. After you are done, press Ctrl + D to end the stream.

The benefits is you do not need to mess with indentation. The text is inserted right as is. Note in cat's place, there could be any command, like fold (I used to use this until I discovered gq) or any other filtering utility.

Although the '+' register seems to be more vimic, it does not work on all platforms, while r!cat just works.

'w'rite can be used pipe selection/range to an external utility's standard input. An example for an X system to copy to the clipboard:

`<,`>:w !xsel -ib

(I.e., make a visual selection and type :w !xsel -ib)

My bindings:

" Ctrl + K, Y while in visual mode
vnoremap <silent> <expr> <C-K>y ':w !xsel -ib<CR><CR>'
" copy from yank register while in normal mode
nnoremap <C-K>y :call system("xsel -ib", getreg("@0"))<CR>
nnoremap <C-K>r :r!cat<CR><CR>
Catullus answered 7/2, 2023 at 1:47 Comment(1)
A couple more cross platform variations that will also work across supported VNC sessions: :r!pbpaste for OSX and :r!powershell -command "Get-Clipboard" for Windows.Carpeting
N
1

It may also be worth mentioning, on OSX using Vim, you can select text with the mouse, Cmd-C to copy to OSX system clipboard, and the copied text will be available in the clipboard outside of Vim.

In other words, OSX treats it like it were a regular window, and this is where the much-maligned Apple "Command" button comes in handy.

B-30

Neomineomycin answered 15/6, 2015 at 0:50 Comment(1)
This is also true for any half-decent terminal emulator on any other OS. Apple have no advantage here whatsoever.Emersonemery
E
1

There are two simple ways to do this. Make your file in insert mode and 1) press the middle button (the scroll wheel) in your mouse, or 2) Ctrl + Shift + V

Endaendall answered 9/7, 2015 at 2:30 Comment(0)
L
1

What you really need is EasyClip. It will do just that and so much more...

Leadership answered 25/2, 2017 at 5:59 Comment(0)
B
1

After entering the Vim window, press I to enter into insert mode. Then move your cursor to the desired location and press the Ctrl + Insert buttons simultaneously to paste from the clipboard.

Blackboard answered 5/9, 2020 at 16:35 Comment(0)
S
0

When I use my Debian vim that is not integrated with Gnome (vim --version | grep clip # shows no clipboard support), I can copy to the clipboard after holding the Shift key and selecting the text with the mouse, just like with any other curses program. As I figured from a comment by @Conner, it's the terminal (gnome-terminal in my case) that turns off its mouse event reporting when it senses my Shift press. I guess curses-based programs can receive mouse events after sending a certain Escape sequence to the terminal.

Selfreliance answered 20/1, 2015 at 21:24 Comment(1)
This is using your terminal, not vim.Jesuit
W
0

If you are on windows and you want to paste contents of system clipboard using p then type this command.

:set clipboard = unnamed

This solved my problem.

Whatley answered 6/5, 2015 at 9:30 Comment(0)
F
0

For some international keyboards, you may need to press "+Space to get a ".

So in those case you would have to press "Space+y or "Space*y to copy.

And "Space+p or " Space*p to paste.

Fiddling answered 23/8, 2015 at 18:50 Comment(0)
O
0

Mac OS X:

  1. run vim --version | grep clipboard to check if the clipboard is enabled(with +)
  2. add set clipboard=unnamed to .vimrc
  3. restart terminal & vim
Octahedrite answered 16/8, 2022 at 6:24 Comment(0)
R
0

Windows. Google mswin-vim. Download it and make it your _vimrc file.

Ramie answered 20/7, 2023 at 20:3 Comment(1)
Please link to it instead.Octennial
R
-1

I used the answer of NM Pennypacker and installed Vim via Homebrew for an early 2011 MacBook Pro:

brew install vim

Now I can also use the "* register to copy and paste text within Vim. I even didn't have to change something within my ~/.vimrc file or the $PATH. Homebrew added symlinks in /usr/local/bin, e.g., vim -> ../Cellar/vim/8.1.2350/bin/vim.

The alternative, which worked before, is to copy some lines of text within Vim by marking it with the mouse and using copy and paste (Cmd + C, Cmd + V) on a Mac. This option only works if the text you want to copy and paste is less in size than the window size of Vim. If you want to copy all text within Vim by marking the whole window or using Cmd + A, this will copy other parts of the console, written before starting Vim, which is very annoying.

So, I am happy having found the new method using the clipboard register.

Ruelu answered 4/12, 2019 at 12:22 Comment(0)
F
-1

Simple for Linux and Vim

Wanted to do something like this for a while but everything out there seemed to be rather complicated and/or did not work (for me).
So ...

Work Flow:

Modify text

  1. Want to edit text in xclip.
  2. Call buffer_modify.sh with Ctrl+Alt+M
  3. Modify text in popup Vim
  4. Either copy line or all text into xclip using Vim keybindings
  5. Popup Vim exits
  6. Modified text now in xclip and can be pasted using middle mouse button

Create text

  1. Want to create some text
  2. Call buffer_create.sh with Ctrl+Alt+C
  3. Enter text into popup Vim
  4. Either copy line or all text into xclip using Vim keybinds
  5. Popup Vim exits
  6. Created text now in xclip and can be pasted using middle mouse button

Keyboard Settings:

command              shortcut
buffer_modify.sh     Ctrl+Alt+M
buffer_create.sh     Ctrl+Alt+C

Commands:

buffer_modify.sh:

#!/bin/bash
xclip -o | /usr/bin/gvim -geometry 60x5 -bg lightgrey -fn 'FreeMono Bold 16' - ; wmctrl -r :ACTIVE: -b toggle,above

buffer_create.sh:

#!/bin/bash
/usr/bin/gvim -geometry 60x5 -bg lightgrey -fn 'FreeMono Bold 16' ; wmctrl -r :ACTIVE: -b toggle,above

Note: Commands require wmctrl so that the popup gvim window stays on top which is generally what one wants.

Addition to Vim .vimrc file:

" write current line
function! XCLIP_CURRENT()
  :.w !xclip
  :q!
endfunc
" write all lines
function! XCLIP_ALL()
  :w !xclip
  :q!
endfunc

nnoremap &lt;Leader>xc :call XCLIP_CURRENT()<CR>
nnoremap &lt;Leader>xa :call XCLIP_ALL()<CR>

Comments:

  • I found that using xclip put the captured text in some clipboard which could be pasted (using middle mouse button) into my xterms and both Firefox and Thunderbird.
  • Creating shell scripts for both keyboard copy and paste settings allowed me to modify/tweak the desired action (in the script files) without having to go into the Settings application over and over again.
  • Sometimes one wants a single line of text and sometimes one wants the whole file of text. Hence, the two Vim functions and map definitions.
  • It would be easy to add an additional Vim function/keybinding allow for the writing of only selected text to the xclip.
Furl answered 5/5, 2023 at 0:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.