在 Ubuntu 中配置 VIM 以便从系统缓冲区复制和粘贴键盘快捷键?

在 Ubuntu 中配置 VIM 以便从系统缓冲区复制和粘贴键盘快捷键?

如何配置 VIM 以在 Ubuntu 中使用Ctrl-c复制和Ctrl-v从系统缓冲区粘贴?

答案1

MS Windows 中的默认行为:-

以下是 mswin.vim 文件的摘录:-

" 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>

以及块模式剪切/粘贴所需的 paste.vim 脚本:-

    " 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

答案2

这只是最低限度,假设大多数都是默认设置**:

: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<
  • 第 1 行:使用 shift+箭头选择文本(以及执行更多操作*)

  • 第 2 行:使“+”(和“*”)成为默认寄存器(gui/term 剪贴板)

  • 第 3、4、5、6 行:使用 Ctrl-x/c/v 进行剪切/复制和粘贴

  • 第 7、8 行:使用 TAB/SHIFT+TAB 进行缩进/取消缩进选择

警告:*** [:set]tings 可以改变这种行为,并且可能需要进行许多调整才能满足您的需要,就像我说的,最少。 * [:behave] 更改了许多 [:set]tings,请阅读文档。

答案3

映射 Ctrl-V 以运行系统命令,该命令抓取系统剪贴板并将其放入寄存器中,然后将其粘贴到光标下的屏幕上:

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

来源:http://vim.wikia.com/wiki/In_line_copy_and_paste_to_system_clipboard

相关内容