如何让无clipboard的Vim7.4通过中键粘贴Windows剪贴板?(Windows连CentOS7)
Hey there! The core issue here is that your Vim 7.4 build doesn’t have the clipboard feature enabled, so it can’t directly access the Windows clipboard. Your current mapping was trying to use Vim’s internal middle mouse handling, which doesn’t link to the system clipboard—instead, MobaXterm (your terminal) is the one managing clipboard sync between Windows and CentOS.
Let’s walk through two reliable solutions to get middle mouse paste working exactly how you want:
Solution 1: Let MobaXterm Handle Middle Mouse Paste (Simplest Fix)
By default, if Vim has mouse=a enabled (captures all mouse events), it intercepts the middle mouse button, stopping MobaXterm from using its native paste functionality. To fix this, tell Vim to ignore the middle mouse so the terminal takes over:
Add this line to your ~/.vimrc:
map <MiddleMouse> <Ignore>
Now when you press middle mouse, Vim won’t process the event—MobaXterm will paste directly from the Windows clipboard, just like it does in the regular terminal. If you still want Vim to handle other mouse actions (scrolling, text selection), keep your existing mouse settings (e.g., set mouse=nvi) and add the ignore mapping alongside it.
Solution 2: Map Middle Mouse to Paste with Paste Mode (For Clean Formatting)
If you want to automatically enter paste mode to avoid indentation or formatting issues when pasting, we can create a mapping that uses MobaXterm’s clipboard sync via system commands. First, install xclip (a tool to access the X11 clipboard, which MobaXterm syncs with Windows):
sudo yum install xclip
Then add these lines to your ~/.vimrc:
nnoremap <MiddleMouse> :set paste<CR>:execute "normal! i" . system('xclip -o -selection clipboard')<CR>:set nopaste<CR> inoremap <MiddleMouse> <C-O>:set paste<CR><C-O>:execute "normal! i" . system('xclip -o -selection clipboard')<CR><C-O>:set nopaste<CR>
This mapping does three key things:
- Enables paste mode to preserve the original formatting of your copied content.
- Pulls the clipboard content directly from Windows using
xclipand inserts it. - Disables paste mode automatically after pasting is done.
Why Your Original Mapping Didn’t Work
Your original line nnoremap <MiddleMouse> :set paste<cr>:startinsert<MiddleMouse><esc> was trying to recursively trigger the middle mouse button inside insert mode, but since Vim can’t access the system clipboard, this just ended up inserting nothing or using Vim’s internal register (not the Windows clipboard). The trick is to leverage MobaXterm’s built-in clipboard sync—either by letting the terminal handle the paste or using a tool like xclip to pull the content directly.
Testing with Shift+Insert was a smart call—since MobaXterm maps that to Windows clipboard paste, it bypasses Vim’s clipboard limitations entirely. The solutions above replicate that behavior for the middle mouse button.
内容的提问来源于stack exchange,提问作者jeremysprofile

