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!
12 Answers
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. Note that there is only a distinction between "* and "+ for X11 systems. Under MS-Windows, use of "* and "+ is actually synonymous and refers to the gui-clipboard. So, on windows * (sharp) register can be used as well: "*y
2 Comments
"+y, the lines don't make it to the clipboard.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.
4 Comments
vim-gnome package instead of vim.vim --version | grep "+xterm_clipboard".clipboard=unnamed instead (vim.wikia.com/wiki/Accessing_the_system_clipboard)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).
Comments
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:
- Position your cursor to the first line you want to copy.
- Press Shiftv to enter visual mode.
- Press ↓ to select multiple lines
- Press "+y to copy the selected text to system clipboard.
- Now you can copy the selected text to browser, text editor etc.
- 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 :
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> "+pSave and reload your .vimrc to apply the new changes.
Position your cursor to the first line you want to copy.
Press Shiftv to enter visual mode.
Press ↓ to select multiple lines
Press Ctrlc to copy the selected text to system clipboard.
Now you can copy the selected text to browser, text editor etc.
Press Ctrlp if you want to copy system clipboard text to vim.
Note: This is for ubuntu systems.
1 Comment
sudo apt-get install vim-gtk3set 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.
1 Comment
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...
1 Comment
xclip -selection clipboardIf 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.
1 Comment
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.
1 Comment
After some helpful comments on this thread I decided to add my own setup, which works great so far:
vim.api.nvim_set_keymap('v', '<C-C>', '"+y', { noremap = true, silent = true }) vim.api.nvim_set_keymap('v', '<C-X>', '"+ygv<DEL>', { noremap = true, silent = true }) I just select in visual mode and press known to everyone Ctrl+C. If I want to also delete it, I press Ctrl+X. Basically that's how it works in all modern editors, and it's imo more performant than vim's modal commands, so why not?
Note that this config is in lua. I highly recommend using it over vimscript, it is much richer.
1 Comment
Here are steps that would work:
- Open source file in vim.
- Go to visual mode by pressing shiftv
- Select the number of lines by moving the up/down arrow keys.
- Once text to be copied is highlighted, then yank (copy) it to + register by pressing following keys: "+y
- Now open target file in vim.
- Paste the text copied in step 4 in to target file by pressing: Ctrlv
vim --version | grep clip, the only option is to uninstall vim and install gvim and use vim as you normally would.vim