this nmap and imap work, but the vmap does not

2 views
Skip to first unread message

Bee

unread,
Dec 25, 2009, 10:54:55 PM12/25/09
to vim_use
These mappings work to put '\ ' at the start of the current line.
nmap <silent> <F5> :s/^/\\ /e<cr>
imap <F5> <c-o><F5>

visually select 4 lines
This vmap works, puts '\ ' at start of each line.
vmap <F5> :s/^/\\ /e<cr>gv

visually select 4 lines
This vmap does NOT work. How to make it work?
vmap <F5> <F5>gv

" ==============================================================
These mappings all work.
let sr5='s/^/\\ /e'
nmap <silent> <F5> :<c-r>=sr5<cr><cr>
imap <silent> <F5> <c-o>:<c-r>=sr5<cr><cr>
vmap <silent> <F5> :<c-r>=sr5<cr><cr>gv

Ben Fritz

unread,
Dec 27, 2009, 10:28:15 AM12/27/09
to vim_use

On Dec 25, 9:54 pm, Bee <200...@calcentral.com> wrote:
> visually select 4 lines
> This vmap does NOT work. How to make it work?
> vmap <F5> <F5>gv
>

This mapping does not (as you seem to intend) trigger the normal-mode
mapping for <F5>, because it never leaves visual mode. Instead, it
just continually calls itself recursively until it reaches the limit
defined in Vim, I think by a configurable option.

You probably want something like:

vmap <F5> <ESC><F5>gv

Bee

unread,
Dec 27, 2009, 10:46:02 AM12/27/09
to vim_use

Thank you, I understand what you are saying.

Adding <esc> did allow it to work, but only on the last line of the
selection.

How can the selection be passed on?

Ben Fritz

unread,
Dec 28, 2009, 11:01:10 AM12/28/09
to vim_use

On Dec 27, 9:46 am, Bee <200...@calcentral.com> wrote:
> Adding <esc> did allow it to work, but only on the last line of the
> selection.
>
> How can the selection be passed on?

Oops, I neglected that part.

Your explicit visual-mode mapping works on every line because pressing
':' in visual mode automatically enters a range of '<,'> to your Ex
command, which means to run the Ex command on the visual selection. I
do not think you will be able to use <F5> in your mapping after all,
without some logic to determine what mode you are in, in which case it
would just be easier to explicitly specify the command in a visual-
mode mapping.

Matt Wozniski

unread,
Dec 28, 2009, 11:11:08 AM12/28/09
to vim...@googlegroups.com
On Mon, Dec 28, 2009 at 11:01 AM, Ben Fritz wrote:

You could make the "implementation" mapping be defined for visual
mode, then map normal-mode <F5> to V<F5> and insert mode <F5> to
<C-o>V<F5>, or something like that, but then using <F5> in normal or
insert mode would clobber the '< and '> marks. You could split out
the important part of the implementation into a cmap, and have normal,
insert, and visual all be mapped to something that gets into
command-line mode with the correct leading range, and then invokes
that cmap, but that's almost certainly more trouble than it's worth.
It's probably best to just define the map multiple times.

~Matt

Tony Mechelynck

unread,
Feb 21, 2010, 5:53:47 AM2/21/10
to vim...@googlegroups.com, Ben Fritz
On 27/12/09 16:28, Ben Fritz wrote:
>
>
> On Dec 25, 9:54 pm, Bee<200...@calcentral.com> wrote:
>> visually select 4 lines
>> This vmap does NOT work. How to make it work?
>> vmap<F5> <F5>gv
>>
>
> This mapping does not (as you seem to intend) trigger the normal-mode
> mapping for<F5>, because it never leaves visual mode. Instead, it
> just continually calls itself recursively until it reaches the limit
> defined in Vim, I think by a configurable option.

No, because the {lhs} is at the start of the {rhs}, see ":help
recursive-mapping". In this case the F5 is not mapped again, and so does
what F5 does by default in Visual mode (i.e. nothing) followed by gv

>
> You probably want something like:
>
> vmap<F5> <ESC><F5>gv
>

Best regards,
Tony.
--
Eternal nothingness is fine if you happen to be dressed for it.
-- Woody Allen

Reply all
Reply to author
Forward
0 new messages