Vim Tips Wiki
No edit summary
 
(→‎Comments: Reply.)
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=1065
 
|id=1065
  +
|previous=1063
|title=Mighty Mouse
 
  +
|next=1066
|created=December 3, 2005 2:54
+
|created=2005
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Gerald Lai
 
|author=Gerald Lai
 
|version=6.0
 
|version=6.0
 
|rating=9/7
 
|rating=9/7
  +
|category1=Mouse
|text=
 
  +
|category2=Usage
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 tip [[VimTip743|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 <code>:version</code>) 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 '<code>:set mouse=a</code>', 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.
[ "Use Alt+Mouse to select blockwise (MS Word like)" ([[VimTip743]]) ] - for *GUI* Vim
 
   
 
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".
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!
 
   
 
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>
 
"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 &lt;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
  +
</pre>
   
 
==Comments==
This tip also attempts to resolve an issue that was brought up in the Vim mailing list:
 
 
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.
  +
<pre>
 
"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>
  +
</pre>
   
 
*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 &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
 
 
set nopaste
 
 
if has("gui_running")
 
 
"mouse visual block (ala MS Word)
 
 
nmap &lt;A-LeftMouse&gt; ms&lt;LeftMouse&gt;&lt;C-v&gt;`so
 
 
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;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
 
 
"paste toggle
 
 
nmap &lt;F7&gt; :set paste! paste?&lt;CR&gt;
 
 
imap &lt;F7&gt; &lt;C-o&gt;:set paste!&lt;CR&gt;
 
 
vmap &lt;F7&gt; &lt;Esc&gt;:set paste!&lt;CR&gt;gv
 
 
"xterm mouse with middleclick paste
 
 
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
 
 
=====
 
}}
 
 
== Comments ==
 
eeeeeeeeeeeeeeeeeeeee
 
 
eee
 
, December 7, 2005 22:34
 
 
----
 
----
 
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?
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
 
   
 
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).
eeeeeeeeee
 
, December 7, 2005 22:35
 
----
 
Well I don't know why, but your for the blockwise selection doesn't work for me.
 
   
 
I believe Vim 7 has native Visual Block mouse support.
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 &lt;A-LeftMouse&gt; &lt;LeftMouse&gt;&lt;C-V&gt;
 
vmap &lt;A-LeftDrag&gt; &lt;LeftDrag&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;
 
 
cu boesi
 
 
'''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?
 
   
  +
Does Vim 7 really have Visual Block mouse support? If so, how is it used? I don't see any documentation of this, and a little experimenting shows that <ctrl><left-mouse>, <alt><left-mouse>, and <shift><left-mouse> don't initiate block-selection. --February 11, 2014
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 &lt;A-LeftMouse&gt; 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).
 
  +
:The mouse can be used if options are set correctly. For example, after <code>:set mousemodel=extend</code>, you can left-click to set an initial position, then hold down Alt and right-click at another position to select a visual block ({{help|mouse-overview}}). Another method that is convenient for occasional use is to use <code>:set mousemodel=popup</code> then left-click to set an initial position, then hold down Shift and left-click at another position. That gives a character-wise selection. Then type Ctrl-V (or Ctrl-Q if Ctrl-V is mapped to paste) to change to block-wise. [[User:JohnBeckett|JohnBeckett]] ([[User talk:JohnBeckett|talk]]) 07:16, February 11, 2014 (UTC)
 
I believe Vim 7 has native Visual Block mouse support.
 
 
 
 
Gerald Lai
 
, December 20, 2005 23:41
 
----
 
<!-- parsed by vimtips.py in 0.486108 seconds-->
 

Latest revision as of 07:16, 11 February 2014

Tip 1065 Printable Monobook Previous Next

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

Comments[]

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.


Does Vim 7 really have Visual Block mouse support? If so, how is it used? I don't see any documentation of this, and a little experimenting shows that <ctrl><left-mouse>, <alt><left-mouse>, and <shift><left-mouse> don't initiate block-selection. --February 11, 2014

The mouse can be used if options are set correctly. For example, after :set mousemodel=extend, you can left-click to set an initial position, then hold down Alt and right-click at another position to select a visual block (:help mouse-overview). Another method that is convenient for occasional use is to use :set mousemodel=popup then left-click to set an initial position, then hold down Shift and left-click at another position. That gives a character-wise selection. Then type Ctrl-V (or Ctrl-Q if Ctrl-V is mapped to paste) to change to block-wise. JohnBeckett (talk) 07:16, February 11, 2014 (UTC)