91

How to copy selected lines to clipboard in vim. I know how to do it for all text files, but I want to do in for selected lines. Thanks!

Community
  • 1
  • 1
ashim
  • 20,778
  • 27
  • 68
  • 89
  • related: http://stackoverflow.com/questions/7694633/copy-lines-using-visual-mode-in-vim, http://stackoverflow.com/questions/3961859/how-to-copy-to-clipboard-using-vim – eckes Feb 06 '12 at 21:03
  • see http://vim.wikia.com/wiki/Cut/copy_and_paste_using_visual_selection – laike9m May 28 '14 at 10:00

11 Answers11

112

SHIFTV puts you in select lines mode. Then "*y yanks the currently selected lines to the * register which is the clipboard. There are quite a few different registers, for different purposes. See the section on selection and drop registers for details on the differences between * and + registers on Windows and Linux.

Michael Berkowski
  • 253,311
  • 39
  • 421
  • 371
Spencer Rathbun
  • 13,398
  • 5
  • 46
  • 71
  • 10
    In Linux, + is global clipboard, * is selection clipboard, while in Windows both map to global clipboard. – Mohnish Feb 21 '17 at 05:26
76

If you're on Linux and are using a VIm version 7.3.74 or higher (the version that gets installed in Ubuntu 11.10 onwards satisfies this), you can do

set clipboard=unnamedplus

which will place yanked text into the global clipboard, and allow you to paste from the global clipboard, without having to use any special registers. Unlike ldigas's solution, this will also work on non-gui versions of VIm.

Suan
  • 27,428
  • 12
  • 44
  • 60
  • 8
    This only works if I install the `vim-gnome` package instead of `vim`. – dan-klasson Nov 27 '12 at 11:29
  • 5
    @dan-klasson: this option requires a vim that was compiled with clipboard support. You can check if this is the case by typing `vim --version | grep "+xterm_clipboard"`. – BenC Nov 28 '13 at 16:07
  • This seems to work to copy into other non-vim programs, but when I go into insert mode in vim and use "shift + insert" to paste from the clipboard, only the first two lines get pasted... I installed vim-gnome; am I missing something else? – t2k32316 May 12 '14 at 22:09
  • 2
    If you're on macOS you'll probably need to use `clipboard=unnamed` instead (http://vim.wikia.com/wiki/Accessing_the_system_clipboard) – streof Dec 07 '16 at 23:24
13
set guioptions+=a

will, ... uhmm, in short, whenever you select/yank something put it in the clipboard as well (not Vim's, but the global keyboard of the window system). That way you don't have to think about yanking things into a special register.

Rook
  • 54,867
  • 44
  • 156
  • 233
10

For GVIM, hit v to go into visual mode; select text and hit Ctrl+Insert to copy selection into global clipboard.

From the menu you can see that the shortcut key is "+y i.e. hold Shift key, then press ", then + and then release Shift and press y (cumbersome in comparison to Shift+Insert).

Mohnish
  • 1,511
  • 1
  • 14
  • 18
6

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 withyy and paste it system-wide.

ArK
  • 18,824
  • 63
  • 101
  • 135
anonymous
  • 1,033
  • 13
  • 16
  • // , I just tested this on OSX 10.12.3, and it worked properly after I installed both the latest Vim and the latets GUI version of Vim, as you recommended here. Thanks, @anonymous. – Nathan Basanese Mar 28 '17 at 19:45
4

Install "xclip" if you haven't...

sudo apt-get install xclip

Xclip puts the data into the "selection/highlighted" clipboard that you middle-click to paste as opposed to "ctrl+v"

While in vim use ex commands:

7w !xclip

or

1,7w !xclip

or

%w !xclip

Then just middle-click to paste into any other application...

user3512967
  • 141
  • 2
4

If vim is compiled with clipboard support, then you can use "*y meaning: yank visually selected text into register * ('*' is for clipboard)

If there is no clipboard support, I think only other way is to use Ctrl+Insert after visually selecting the text in vim.

dvk317960
  • 624
  • 4
  • 10
3

I have added the following line to my .vimrc

vnoremap <F5> "+y<CR>

This allows you to copy the selected text to the clipboard by pressing F5. You must be in visual mode for this to work.

aks
  • 352
  • 4
  • 14
2

Add the following code into your .vimrc:

if has('clipboard')
    if has('unnamedplus')  " When possible use + register for copy-paste
        set clipboard=unnamed,unnamedplus
    else         " On mac and Windows, use * register for copy-paste
        set clipboard=unnamed
    endif
endif
Pang
  • 8,605
  • 144
  • 77
  • 113
John Trump
  • 346
  • 2
  • 13
1

First check if your vim installation has clipboard support.

vim --version

If clipboard support is installed you will see:

+clipboard
+X11
+xterm_clipboard

If clipboard support is not installed you will see:

-clipboard
-X11
-xterm_clipboard

To install clipboard support:

apt-get install vim-gnome

Once you have verified that clipboard support is installed do the following:

  1. Position your cursor to the first line you want to copy.
  2. Press Shiftv to enter visual mode.
  3. Press to select multiple lines
  4. Press "+y to copy the selected text to system clipboard.
  5. Now you can copy the selected text to browser, text editor etc.
  6. Press "+p if you want to copy system clipboard text to vim.

Above steps might get tedious if you have to repeatedly copy from vim to system clipboard and vice versa. You can create vim shortcuts so that when you press Ctrlc selected text will be copied to system clipboard. And when you press Ctrlp system clipboard text is copied to vim. To create shortcuts :

  1. Open .vimrc file and add following text at the end of file:

     nnoremap <C-c> "+y
     vnoremap <C-c> "+y
     nnoremap <C-p> "+p
     vnoremap <C-p> "+p
    
  2. Save and reload your .vimrc to apply the new changes.

  3. Position your cursor to the first line you want to copy.

  4. Press Shiftv to enter visual mode.

  5. Press to select multiple lines

  6. Press Ctrlc to copy the selected text to system clipboard.

  7. Now you can copy the selected text to browser, text editor etc.

  8. Press Ctrlp if you want to copy system clipboard text to vim.

Note: This is for ubuntu systems.

ABN
  • 374
  • 5
  • 16
1

Here are steps that would work:

  1. Open source file in vim.
  2. Go to visual mode by pressing shiftv
  3. Select the number of lines by moving the up/down arrow keys.
  4. Once text to be copied is highlighted, then yank (copy) it to + register by pressing following keys: "+y
  5. Now open target file in vim.
  6. Paste the text copied in step 4 in to target file by pressing: Ctrlv
manish sehgal
  • 113
  • 2
  • 12