问题
I am using a Windows machine connected to CentOS 7
through MobaXterm
. I want the middle mouse button to work in Vim 7.4
(compiled without the clipboard option) as it does on the linux command line, which is to paste from my Windows clipboard.
In my ~/.vimrc
:
nnoremap <MiddleMouse> :set paste<cr>:startinsert<MiddleMouse><esc>
At first, I worried that this might not do what I expect because maybe I don't understand how Vim does recursion, so I tried it with Shift+Insert, since that is what MiddleMouse maps to in order to paste:
nnoremap <MiddleMouse> :set paste<cr>:startinsert<S-Insert><esc>
This gives an error about E488: Trailing characters
.
Edit: as Christian pointed out, I was missing a <cr>
after the :startinsert
command, but that still did not solve my issue.
nnoremap <MiddleMouse> :set paste<cr>:startinsert<cr><S-Insert><esc>
will no longer have an error, but it doesn't enter insert
mode before I paste.
回答1:
This is apparently expected behavior (ugh).
:star[tinsert][!] Start Insert mode just after executing this command.
Works like typing "i" in Normal mode. When the ! is
included it works like "A", append to the line.
Otherwise insertion starts at the cursor position.
Note that when using this command in a function or
script, the insertion only starts after the function
or script is finished.
This command does not work from :normal.
What this means is that all of my commands that I put after :startinsert
instead run immediately before :startinsert
and then :startinsert
runs and changes the mode to insert (Note: this appears to hold true for using i
instead of :startinsert
as well).
My next step was to try to make a nested function, where I had one function that calls another function, and the second function runs :startinsert
and then returns to the first function, which then completes the paste:
function! Paste()
call InsertMode()<cr>
:set paste<cr>
<S-Insert>
:set nopaste<cr>
<esc>
endfunction
function! InsertMode()
:startinsert<cr>
endfunction
nnoremap <MiddleMouse> call Paste()<cr>
But this did not work either. I also tried using the "+p
and "*p
registers without :startinsert
as Christian said in his comments with nnoremap <MiddleMouse> :set paste<cr>"+p:set nopaste<cr>
, but this again just pastes directly as if I were typing it in, it does not enter insert
mode first. I am willing to believe this would work on a version of Vim compiled with +clipboard, but that is not the version I have.
The best I have been able to do is to paste correctly with the MiddleMouse button after already entering insert
mode:
inoremap <MiddleMouse> :set paste<cr><S-Insert>:set nopaste<cr>
If anyone knows of a way to do this from normal
mode, please let me know. It seems like it should be possible, but everything I've read suggests that without the +clipboard
option (checkable with vim --version | grep clipboard
) it is not.
来源:https://stackoverflow.com/questions/49561616/make-middlemouse-in-vim-switch-to-insert-paste-paste-and-then-escape