14

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

wonea
  • 1,817
  • 1
  • 23
  • 42
  • 1
    http://superuser.com/questions/10588/how-to-make-cut-copy-paste-in-gvim-on-ubuntu-work-with-ctrlx-ctrlc-ctrlv/10604 – innaM Oct 26 '09 at 10:18
  • 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 Oct 27 '09 at 03:22

3 Answers3

18

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 <[email protected]>
" 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
  • 3
    For 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 May 01 '13 at 13:26
  • 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 Jun 11 '14 at 17:54
  • Setting `C-V` to paste will break special character entry mode: http://vim.wikia.com/wiki/Entering_special_characters#By_character_value –  Oct 03 '14 at 15:00
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
  • 361
  • 2
  • 7
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
  • 6,828
  • 6
  • 45
  • 51