开发者

Automatically disable abbreviation expansion when pasting code in Vim

开发者 https://www.devze.com 2023-03-05 20:37 出处:网络
In a terminal instance of Vim, how can I make abbreviation expansion be aut开发者_JAVA百科omatically disabled when I am pasting code?

In a terminal instance of Vim, how can I make abbreviation expansion be aut开发者_JAVA百科omatically disabled when I am pasting code?

Note: The keyword here is automatically, not manually via :set paste.


There is no standard way for Vim to distinguish whether an input character was actually typed by the user or was generated for them by the X Window System through the clipboard or selection buffers. That is why there is the paste mode to manually switch Vim into the state when all input is not interpreted as interactively typed.

Thus, you have two alternatives:

  1. Paste from the "* or "+ registers (that represent the clipboard and X selection, respectively), if you are using GVim or your Vim is compiled with the X11 feature (or, at least, with the xterm_clipboard one).

  2. In case of Vim running in the terminal, you can manually synchronize one of the registers (for example, the unnamed one, "") with the clipboard or X selection. For instance, since I use Vim only in the terminal, I define the following mappings for myself:

     nmap <silent> <leader>y :call system('xclip', @")<cr>
     nmap <silent> <leader>p :call XClipRead()<cr>
    
     function! XClipRead()
         let s = system('xclip -o')
         if v:shell_error
             return
         endif
         call setreg('"', s)
     endfunction
    

    To paste the contents of the X selection buffer using these mappings, you need to type the leader key followed by p, and then an appropriate Vim paste command. If you agree to lose the opportunity of using different paste commands (such as P, gp, ]p, etc), you can define a mapping that does both things:

     nmap <silent> <leader>P :call XClipRead()<cr>p
    
0

精彩评论

暂无评论...
验证码 换一张
取 消