开发者

In Vim, why can't I remap keys to `w`?

开发者 https://www.devze.com 2023-03-13 19:10 出处:网络
In my .vimrc: noremap z w When I examine the mappings, :map z shows only: z 开发者_运维百科* w When I press z, my cursor moves as expected (to the next word). However, when I try to use somethin

In my .vimrc:

noremap z w

When I examine the mappings, :map z shows only:

z 开发者_运维百科        * w

When I press z, my cursor moves as expected (to the next word). However, when I try to use something like diz or ciz, nothing happens. At the bottom of my screen, di andci appear as I'm typing them, but once I type z, Vim gleefully sits idle. diw and ciw still work as expected.

What else do I need to do? Is there a mapping mode I don't know about?


The problem is that iw is a single text object, it is not modifier i + motion w. You need to map iz and az in this case:

onoremap iz iw
onoremap az aw

. Note that this will wait for you to press z only for some amount of time (see :h 'timeoutlen'). To make it work like iw (e.g., wait for z forever), you should try the following:

function s:MapTOPart(tostart)
    let char=getchar()
    if type(char)==type(0)
        let char=nr2char(char)
    endif
    return a:tostart.((char is# 'z')?('w'):(char))
endfunction

onoremap iz iw
onoremap az aw
onoremap <expr> i <SID>MapTOPart('i')
onoremap <expr> a <SID>MapTOPart('a')

You will have to do the same for all i* and a* text objects you use because with the above code only iz and az works fine; for some reason iw must be either typed too slow or typed as iww.


The issue is not with trying to bind to 'w', but instead, you're using the 'wrong' mapping. Granted, :noremap z w is what you want when you're in normal mode, but after d or c, Vim isn't in normal mode anymore!

Commands such as d and c enter a new mode, called "Operator Pending Mode". Google for more information, or see :help Operator-pending within vim. Therefore, what you want is the following additional line in your ~/.vimrc:

onoremap z w

Afterwards, diz and ciz should work fine.


I think the reason is simple:

iw "inner word", select [count] words (see |word|). White space between words is counted too. When used in Visual linewise mode "iw" switches to Visual characterwise mode.

You can remap w to z, but the iw is one command, not i+w. Ex: dz works after:

:omap z iw


I don't think vim allows you to remap motions - and in your example, 'w' is a part of a motion ('iw'), not a command.

Remaping the 'z' key to the 'w' command worked because vim does allow you to remap commands.


That's probably because of the fact that 'z' is used for a series of commands itself. Check out :h z

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号