Mapping Mystery

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Mapping Mystery

Andrew Stewart

Hi,

This is more of a pure Vim question than a MacVim one -- hope that's  
okay.

My keyboard has a UK layout and, because I often type # (with alt-3,  
which is awkward) and never type § (which is easy), I map the latter  
to the former in my ~/.vimrc:

map § #
map! § #

This mapping works in insert mode, replace mode, and on the command  
line.  But it doesn't work when replacing a single character (via r§)  
or moving forward to a # (via f§).

Can anyone explain why this is, and how to get § to be # all the time?

Thanks and regards,
Andy Stewart

-------
AirBlade Software
http://airbladesoftware.com





--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_mac" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Mapping Mystery

Ben Schmidt

Andrew Stewart wrote:

> Hi,
>
> This is more of a pure Vim question than a MacVim one -- hope that's  
> okay.
>
> My keyboard has a UK layout and, because I often type # (with alt-3,  
> which is awkward) and never type § (which is easy), I map the latter  
> to the former in my ~/.vimrc:
>
> map § #
> map! § #
>
> This mapping works in insert mode, replace mode, and on the command  
> line.  But it doesn't work when replacing a single character (via r§)  
> or moving forward to a # (via f§).
>
> Can anyone explain why this is, and how to get § to be # all the time?

Yes. Most maps work by mapping keys, which is OK, because commands are
based on keys, and maps are used to trigger commands.

But the arguments to f, t, r, etc. are not commands, but characters, and
are treated differently. In insert mode, it happens that the results of
your mappings are keys that by default insert characters, but that map
won't work for the f, t, r, etc. commands.

Using lmap will. See :help language-mapping

I think you will want

map § #
lmap § #

Ben.




--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_mac" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Mapping Mystery

Ben Schmidt

Ben Schmidt wrote:

> Andrew Stewart wrote:
>> Hi,
>>
>> This is more of a pure Vim question than a MacVim one -- hope that's  
>> okay.
>>
>> My keyboard has a UK layout and, because I often type # (with alt-3,  
>> which is awkward) and never type § (which is easy), I map the latter  
>> to the former in my ~/.vimrc:
>>
>> map § #
>> map! § #
>>
>> This mapping works in insert mode, replace mode, and on the command  
>> line.  But it doesn't work when replacing a single character (via r§)  
>> or moving forward to a # (via f§).
>>
>> Can anyone explain why this is, and how to get § to be # all the time?
>
> Yes. Most maps work by mapping keys, which is OK, because commands are
> based on keys, and maps are used to trigger commands.
>
> But the arguments to f, t, r, etc. are not commands, but characters, and
> are treated differently. In insert mode, it happens that the results of
> your mappings are keys that by default insert characters, but that map
> won't work for the f, t, r, etc. commands.
>
> Using lmap will. See :help language-mapping
>
> I think you will want
>
> map § #
> lmap § #
>
> Ben.

Furthermore, if you want to map these to each other so you can still
access both, use 'noremap' not 'map' (and 'lnoremap') to avoid recursion.

Ben.


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_mac" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Mapping Mystery

Andrew Stewart


On 6 Jun 2008, at 13:11, Ben Schmidt wrote:

>> I think you will want
>>
>> map § #
>> lmap § #
>>
>> Ben.
>
> Furthermore, if you want to map these to each other so you can still
> access both, use 'noremap' not 'map' (and 'lnoremap') to avoid  
> recursion.

Thanks for the reply.  Unfortunately this doesn't work for me.

I seem to need map! rather than map.  And neither lmap nor lmap! makes  
any difference as far as I can see.

Same story for noremap and lnoremap.

Maybe something else is conflicting?

Regards,
Andy Stewart

-------
AirBlade Software
http://airbladesoftware.com





--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_mac" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Mapping Mystery

Ben Schmidt

Andrew Stewart wrote:

>
> On 6 Jun 2008, at 13:11, Ben Schmidt wrote:
>>> I think you will want
>>>
>>> map § #
>>> lmap § #
>>>
>>> Ben.
>> Furthermore, if you want to map these to each other so you can still
>> access both, use 'noremap' not 'map' (and 'lnoremap') to avoid  
>> recursion.
>
> Thanks for the reply.  Unfortunately this doesn't work for me.
>
> I seem to need map! rather than map.  And neither lmap nor lmap! makes  
> any difference as far as I can see.
>
> Same story for noremap and lnoremap.
>
> Maybe something else is conflicting?

Maybe for lmap to work you need to do

set iminsert=1
set imsearch=-1
(or imsearch=1)

Yes. That seems appropriate after checking the help. I think that will
fix it.

:help 'iminsert'
:help 'imsearch'

I'm surprised the docs at :help language-mapping didn't mention that.
They mentioned *disabling* them, but not what you needed to *enable*
them.

map works for normal mode; map! works for insert/command modes, but with
keys; lmap works for insert/command/search/character arguments to
commands when it's enabled with appropriate options! So you want map and
lmap, or just lmap if you don't want to change the normal mode commands.
And actually probably want the noremap variants.

Cheers,

Ben.




--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_mac" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Mapping Mystery

Andrew Stewart


On 6 Jun 2008, at 14:34, Ben Schmidt wrote:
> Maybe for lmap to work you need to do
>
> set iminsert=1
> set imsearch=-1
> (or imsearch=1)
>
> Yes. That seems appropriate after checking the help. I think that will
> fix it.

Yes it does.  Superb!  This has been bugging me for ages.


> :help 'iminsert'
> :help 'imsearch'
>
> I'm surprised the docs at :help language-mapping didn't mention that.
> They mentioned *disabling* them, but not what you needed to *enable*
> them.

The docs are normally excellent, so I'm surprised too.


> map works for normal mode; map! works for insert/command modes, but  
> with
> keys; lmap works for insert/command/search/character arguments to
> commands when it's enabled with appropriate options!

Yes, lmap seems like the odd one out.

For the record, my ~/.vimrc now has:

set iminsert=1
set imsearch=-1
noremap § #
noremap! § #
lnoremap § #

Thank you for your help!  I am grateful.

Regards,
Andy Stewart

-------
AirBlade Software
http://airbladesoftware.com
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_mac" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---