created 2005 · complexity intermediate · author Gerald Lai · version 6.0
As much as it is against the Vim-way of doing things, it would be nice to have the mouse do the right thing when it is used once in a while. This tip refines tip Use Alt-Mouse to select blockwise for gvim.
Holding down Alt + dragging the left mouse button will activate the Visual Block, much like how MS Word works. Holding down Shift + dragging will switch it back to Visual mode, and vice versa. Insert-Visual[Block] mode is called if mouse highlight is activated from Insert mode. This tip also fixes some issues with the default Alt/Shift + clicking to make it work more seamlessly.
This tip also attempts to resolve an issue that was brought up in the Vim mailing list:
- Console/terminal* Vims that are not compiled with the +xterm_clipboard option (see
:version) have trouble pasting from external sources. They are not able to access the clipboard, and have to make use of the middleclick paste function. Unfortunately, if the mouse option was set as '
:set mouse=a', the middleclick paste will not help. On the other hand, if the mouse option wasn't set, the middleclick paste would work fine, but instead, many of the console mouse functionalities such as positioning, wheel-scrolling and window sizing would not work.
This is fixed by only allowing middleclick paste to work in Insert mode. Extended mouse functionalities are sacrificed in Insert mode but will work in other modes like Normal and Visual modes.When a middleclick paste is issued in Normal mode, it switches to Insert mode to do the paste. If it is issued in Visual mode, it cuts the highlighted text, and switches to Insert mode to do the paste to simulate a "paste over selection".
To avoid indentation when middleclick pasting: hit <F7> first to enable paste mode, middleclick, and hit <F7> again to disable paste mode (see :help 'paste').
"place in vimrc set nopaste if has("gui_running") "mouse visual block (ala MS Word) nmap <A-LeftMouse> ms<LeftMouse><C-v>`so imap <A-LeftMouse> <Esc><C-v>`^ms<Esc>gi<LeftMouse><C-o><C-v>`so vmap <A-LeftDrag> <LeftDrag> vmap <A-LeftMouse> <C-v><LeftMouse>msgv`s vmap <S-LeftMouse> v<LeftMouse>msgv`s set mouse=ra else "paste toggle nmap <F7> :set paste! paste?<CR> imap <F7> <C-o>:set paste!<CR> vmap <F7> <Esc>:set paste!<CR>gv "xterm mouse with middleclick paste nnoremap <MiddleMouse> i<MiddleMouse> vnoremap <MiddleMouse> s<MiddleMouse> set pastetoggle=<F7> mouse=rnv "choose either one set ttymouse=xterm "set ttymouse=xterm2 endif
I don't know why, but your tip for blockwise selection doesn't work for me.
But it inspired me to look for a solution and the following lines do a blockwise selection for me.
"mouse visual block (ala MS Word) nmap <A-LeftMouse> <LeftMouse><C-V> vmap <A-LeftDrag> <LeftDrag> imap <A-LeftMouse> <LeftMouse><C-O><C-V> vmap <A-LeftMouse> <Esc><LeftMouse><C-V>
The tip makes use of mark s, so make sure it's not already used. Also, I tested it with Vim 6.3 and above. What version of Vim are you using?
It's good that you found a mapping that works for you. I tailored the mappings above so that it tries to be as faithful to the current mode that you are in. For instance, performing <A-LeftMouse> when in Insert mode block highlights, visually, from the position of the cursor to the position of the leftclick, invoking Insert-Visual mode (not Visual-only mode).
I believe Vim 7 has native Visual Block mouse support.