Configure VIM for copy and paste keyboard shortcuts from system buffer in Ubuntu?

14

12

How do I configure VIM for using Ctrl-c to copy and Ctrl-v to paste from system buffer in Ubuntu?

Rohit

Posted 2009-10-26T09:45:09.853

Reputation:

May I suggest using Cream ? http://cream.sourceforge.net/ It is a variant of vim specially designed for users who do not feel comfortable with vim's way of work.

– Rook – 2009-10-27T03:22:38.857

Answers

16

Default beheaviour in MS Windows:-

Here is an excert from the mswin.vim file:-

" CTRL-X and SHIFT-Del are Cut
vnoremap <C-X> "+x
vnoremap <S-Del> "+x

" CTRL-C and CTRL-Insert are Copy
vnoremap <C-C> "+y
vnoremap <C-Insert> "+y

" CTRL-V and SHIFT-Insert are Paste
map <C-V>       "+gP
map <S-Insert>      "+gP

cmap <C-V>      <C-R>+
cmap <S-Insert>     <C-R>+

" Pasting blockwise and linewise selections is not possible in Insert and
" Visual mode without the +virtualedit feature.  They are pasted as if they
" were characterwise instead.
" Uses the paste.vim autoload script.

exe 'inoremap <script> <C-V>' paste#paste_cmd['i']
exe 'vnoremap <script> <C-V>' paste#paste_cmd['v']

imap <S-Insert>     <C-V>
vmap <S-Insert>     <C-V>

" Use CTRL-Q to do what CTRL-V used to do
noremap <C-Q>       <C-V>

and the paste.vim script which is required for block mode cut/paste:-

    " Vim support file to help with paste mappings and menus
" Maintainer:   Bram Moolenaar <Bram@vim.org>
" Last Change:  2006 Jun 23

" Define the string to use for items that are present both in Edit, Popup and
" Toolbar menu.  Also used in mswin.vim and macmap.vim.

" Pasting blockwise and linewise selections is not possible in Insert and
" Visual mode without the +virtualedit feature.  They are pasted as if they
" were characterwise instead.  Add to that some tricks to leave the cursor in
" the right position, also for "gi".
if has("virtualedit")
  let paste#paste_cmd = {'n': ":call paste#Paste()<CR>"}
  let paste#paste_cmd['v'] = '"-c<Esc>' . paste#paste_cmd['n']
  let paste#paste_cmd['i'] = 'x<BS><Esc>' . paste#paste_cmd['n'] . 'gi'

  func! paste#Paste()
    let ove = &ve
    set ve=all
    normal! `^
    if @+ != ''
      normal! "+gP
    endif
    let c = col(".")
    normal! i
    if col(".") < c " compensate for i<ESC> moving the cursor left
      normal! l
    endif
    let &ve = ove
  endfunc
else
  let paste#paste_cmd = {'n': "\"=@+.'xy'<CR>gPFx\"_2x"}
  let paste#paste_cmd['v'] = '"-c<Esc>gix<Esc>' . paste#paste_cmd['n'] . '"_x'
  let paste#paste_cmd['i'] = 'x<Esc>' . paste#paste_cmd['n'] . '"_s'
endi

James Anderson

Posted 2009-10-26T09:45:09.853

Reputation:

Setting C-V to paste will break special character entry mode: http://vim.wikia.com/wiki/Entering_special_characters#By_character_value

– g33kz0r – 2014-10-03T15:00:33.440

3For windows users who accidentally SEO'd here (OP is Ubuntu), add source $VIMRUNTIME/mswin.vim at the top of your _vimrc file. Anything funky that prevents you from successfully including the file in Linux as well? – ruffin – 2013-05-01T13:26:25.653

1@ruffin Inspecting the source you'll see if statements for UNIX, so the creator had the use of mswin.vim under Linux in mind as well. – RoliSoft – 2014-06-11T17:54:57.650

0

This is just minimal, assuming most things are default settings**:

:behave mswin
:set clipboard=unnamedplus
:smap <Del> <C-g>"_d
:smap <C-c> <C-g>y
:smap <C-x> <C-g>x
:imap <C-v> <Esc>pi
:smap <C-v> <C-g>p
:smap <Tab> <C-g>1> 
:smap <S-Tab> <C-g>1<
  • line 1: makes the shift+arrows select text (and does more*)

  • line 2: makes "+ (and "*) the default register (gui/term clipboard)

  • lines 3,4,5,6: makes Ctrl-x/c/v Cut/Copy and Paste

  • line 7,8: makes TAB/SHIFT+TAB indent/outdent selections

Caution: *** [:set]tings can alter this behavior and that many tweaks may be needed to suit your needs, like I said, minimal. * [:behave] changes many [:set]tings read the docs.

osirisgothra

Posted 2009-10-26T09:45:09.853

Reputation: 331

0

Map Ctrl-V to run the system command that grabs the system clipboard and throws it into a register, and pastes it onto the screen under the cursor:

vmap <C-c> y:call system("xclip -i -selection clipboard", getreg("\""))<CR>:call system("xclip -i", getreg("\""))<CR>
nmap <C-v> :call setreg("\"",system("xclip -o -selection clipboard"))<CR>p

Source: http://vim.wikia.com/wiki/In_line_copy_and_paste_to_system_clipboard

Eric Leschinski

Posted 2009-10-26T09:45:09.853

Reputation: 5 303

1This is unnecessarily complex. You can just map to "+y and "+p – FliiFe – 2017-06-04T08:06:30.367