Vim Tips Wiki
No edit summary
 
m (Fixed formatting and links)
Line 9: Line 9:
 
|rating=9/7
 
|rating=9/7
 
|text=
 
|text=
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 this previous tip:
+
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 this tip: [http://vim.wikia.com/wiki/VimTip743 "Use Alt+Mouse to select blockwise (MS Word like)"] - for *GUI* Vim.
 
 
 
[ "Use Alt+Mouse to select blockwise (MS Word like)" ([[VimTip743]]) ] - for *GUI* Vim
 
   
 
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. Give it a try!
 
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. Give it a try!
 
 
   
 
This tip also attempts to resolve an issue that was brought up in the Vim mailing list:
 
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 act funny. 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.
 
*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 act funny. 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".
 
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'}})
 
  +
<pre>
 
To avoid indentation when middleclick pasting: hit &lt;F7&gt; first to enable paste mode, middleclick, and hit &lt;F7&gt; again to disable paste mode (see :help 'paste')
 
 
 
 
=====
 
 
 
"place in vimrc
 
"place in vimrc
 
 
set nopaste
 
set nopaste
 
 
if has("gui_running")
 
if has("gui_running")
 
"mouse visual block (ala MS Word)
 
 
nmap &lt;A-LeftMouse&gt; ms&lt;LeftMouse&gt;&lt;C-v&gt;`so
"mouse visual block (ala MS Word)
 
 
imap &lt;A-LeftMouse&gt; &lt;Esc&gt;&lt;C-v&gt;`^ms&lt;Esc&gt;gi&lt;LeftMouse&gt;&lt;C-o&gt;&lt;C-v&gt;`so
 
nmap &lt;A-LeftMouse&gt; ms&lt;LeftMouse&gt;&lt;C-v&gt;`so
+
vmap &lt;A-LeftDrag&gt; &lt;LeftDrag&gt;
 
vmap &lt;A-LeftMouse&gt; &lt;C-v&gt;&lt;LeftMouse&gt;msgv`s
 
imap &lt;A-LeftMouse&gt; &lt;Esc&gt;&lt;C-v&gt;`^ms&lt;Esc&gt;gi&lt;LeftMouse&gt;&lt;C-o&gt;&lt;C-v&gt;`so
+
vmap &lt;S-LeftMouse&gt; v&lt;LeftMouse&gt;msgv`s
 
set mouse=ra
 
vmap &lt;A-LeftDrag&gt; &lt;LeftDrag&gt;
 
 
vmap &lt;A-LeftMouse&gt; &lt;C-v&gt;&lt;LeftMouse&gt;msgv`s
 
 
vmap &lt;S-LeftMouse&gt; v&lt;LeftMouse&gt;msgv`s
 
 
set mouse=ra
 
 
 
else
 
else
 
"paste toggle
 
  +
nmap &lt;F7&gt; :set paste! paste?&lt;CR&gt;
"paste toggle
 
  +
imap &lt;F7&gt; &lt;C-o&gt;:set paste!&lt;CR&gt;
 
nmap &lt;F7&gt; :set paste! paste?&lt;CR&gt;
+
vmap &lt;F7&gt; &lt;Esc&gt;:set paste!&lt;CR&gt;gv
 
"xterm mouse with middleclick paste
 
imap &lt;F7&gt; &lt;C-o&gt;:set paste!&lt;CR&gt;
+
nnoremap &lt;MiddleMouse&gt; i&lt;MiddleMouse&gt;
 
vnoremap &lt;MiddleMouse&gt; s&lt;MiddleMouse&gt;
 
vmap &lt;F7&gt; &lt;Esc&gt;:set paste!&lt;CR&gt;gv
+
set pastetoggle=&lt;F7&gt; mouse=rnv
 
"choose either one
 
 
set ttymouse=xterm
"xterm mouse with middleclick paste
 
 
"set ttymouse=xterm2
 
nnoremap &lt;MiddleMouse&gt; i&lt;MiddleMouse&gt;
 
 
vnoremap &lt;MiddleMouse&gt; s&lt;MiddleMouse&gt;
 
 
set pastetoggle=&lt;F7&gt; mouse=rnv
 
 
"choose either one
 
 
set ttymouse=xterm
 
 
"set ttymouse=xterm2
 
 
 
endif
 
endif
  +
</pre>
 
=====
 
 
}}
 
}}
   
 
== Comments ==
 
== Comments ==
eeeeeeeeeeeeeeeeeeeee
 
 
eee
 
, December 7, 2005 22:34
 
----
 
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
 
 
eeeeeeeeee
 
, December 7, 2005 22:35
 
----
 
 
Well I don't know why, but your for the blockwise selection doesn't work for me.
 
Well I don't know why, but your for the blockwise selection doesn't work for me.
   
 
But your tip inspired me to look for a solution and the following lines do a blockwise selection for me.
 
But your tip inspired me to look for a solution and the following lines do a blockwise selection for me.
  +
<pre>
 
 
"mouse visual block (ala MS Word)
 
"mouse visual block (ala MS Word)
 
nmap &lt;A-LeftMouse&gt; &lt;LeftMouse&gt;&lt;C-V&gt;
 
nmap &lt;A-LeftMouse&gt; &lt;LeftMouse&gt;&lt;C-V&gt;
Line 106: Line 57:
 
imap &lt;A-LeftMouse&gt; &lt;LeftMouse&gt;&lt;C-O&gt;&lt;C-V&gt;
 
imap &lt;A-LeftMouse&gt; &lt;LeftMouse&gt;&lt;C-O&gt;&lt;C-V&gt;
 
vmap &lt;A-LeftMouse&gt; &lt;Esc&gt;&lt;LeftMouse&gt;&lt;C-V&gt;
 
vmap &lt;A-LeftMouse&gt; &lt;Esc&gt;&lt;LeftMouse&gt;&lt;C-V&gt;
  +
</pre>
 
cu boesi
 
   
 
'''Anonymous'''
 
'''Anonymous'''
Line 117: Line 67:
   
 
I believe Vim 7 has native Visual Block mouse support.
 
I believe Vim 7 has native Visual Block mouse support.
 
 
   
 
Gerald Lai
 
Gerald Lai
 
, December 20, 2005 23:41
 
, December 20, 2005 23:41
 
----
 
----
  +
[[Category:Usage]]
<!-- parsed by vimtips.py in 0.486108 seconds-->
 

Revision as of 23:38, 21 September 2007

Previous TipNext Tip

Tip: #1065 - Mighty Mouse

Created: December 3, 2005 2:54 Complexity: intermediate Author: Gerald Lai Version: 6.0 Karma: 9/7 Imported from: Tip#1065

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 this tip: "Use Alt+Mouse to select blockwise (MS Word like)" - for *GUI* Vim.

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. Give it a try!

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 act funny. 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 

Comments

Well I don't know why, but your for the blockwise selection doesn't work for me.

But your tip 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> 

Anonymous , December 20, 2005 3:06


May I know what happens when it does not work for you? It 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.

Gerald Lai , December 20, 2005 23:41