Reputation: 27925
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 copy to) the system's clipboard?
Upvotes: 1155
Views: 1202588
Reputation: 31110
Try using "*yy
or "+yy
to copy a line to your system's clipboard.
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.
Upvotes: 1208
Reputation: 10075
I believe that this question deserves a more pictorial answer:
Entering Paste Mode
Leaving Paste Mode
You pasted the text and you're able to type again.
Upvotes: 171
Reputation: 1581
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>
Upvotes: 3
Reputation: 557
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.
Upvotes: 1
Reputation: 5877
Copy to the OS clipboard
Select text in visual mode, and press "*y
.
Paste from the OS clipboard
Press "*p
.
Upvotes: 23
Reputation: 166
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.
Upvotes: 0
Reputation: 104
Windows. Google mswin-vim. Download it and make it your _vimrc file.
Upvotes: 0
Reputation: 289
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 ...
Modify text
Create text
command shortcut
buffer_modify.sh Ctrl+Alt+M
buffer_create.sh Ctrl+Alt+C
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.
" write current line
function! XCLIP_CURRENT()
:.w !xclip
:q!
endfunc
" write all lines
function! XCLIP_ALL()
:w !xclip
:q!
endfunc
nnoremap <Leader>xc :call XCLIP_CURRENT()<CR>
nnoremap <Leader>xa :call XCLIP_ALL()<CR>
Upvotes: 0
Reputation: 153
These key combinations work on any OS.
Select target Text using the mouse, and refer to the key sequences to copy, cut, and paste.
Upvotes: 12
Reputation: 2375
Mac OS X:
vim --version | grep clipboard
to check if the clipboard is enabled(with +)set clipboard=unnamed
to .vimrcUpvotes: 0
Reputation: 71
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:
Install parcellite
(a clipboard manager with a low memory footprint);
In your .vimrc
file, add the following:
command Clip r !parcellite -c
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.
Upvotes: 7
Reputation: 2712
Copying to system clipboard:
"+y
Paste something form system's clipboard:
"+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!
Upvotes: 8
Reputation: 9071
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.
Upvotes: 356
Reputation: 387
If you have it, try removing this from your vimrc: set mouse=a
It messes with the paste functionality.
Upvotes: 3
Reputation: 6952
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:
brew install vim
set clipboard+=unnamed
to my ~/.vimrc
fileiTerm2
Upvotes: 4
Reputation: 38033
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
Upvotes: 9
Reputation: 2480
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.
Upvotes: 4
Reputation: 2694
If you are using a mouse first do
:set paste
Then right click mouse
and the contents in buffer will be pasted
Upvotes: 7
Reputation: 2024
The simplest solution to this, that also works between different Linux machines through ssh
is:
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)
Add the following lines to your .vimrc
:
set clipboard=unnamedplus set paste
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.
Upvotes: 10
Reputation: 476
What you really need is EasyClip. It will do just that and so much more...
Upvotes: 1
Reputation: 2121
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.
v
:
(it will automatically expand to show :'<,'>
)y *
or y +
(depending on your system) to yank the selected text to the system clipboardUpvotes: 2
Reputation: 1490
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.
Upvotes: 3
Reputation: 10998
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 modev_CTRL-X
: subtract [count] from numberv_CTRL-V
: blockwise Visual modei_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 completionUpvotes: 2
Reputation: 51
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.
Upvotes: 0
Reputation: 7362
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
Upvotes: 30
Reputation: 11
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
Upvotes: 1
Reputation: 383
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
Upvotes: 1
Reputation: 4378
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.
Upvotes: 0