Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

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?

like image 916
ClosureCowboy Avatar asked Jun 16 '11 02:06

ClosureCowboy


People also ask

How to map control key in vim?

On MS-Windows, if the mswin. vim file is used, then CTRL-V is mapped to paste text from the clipboard. In this case, you can use CTRL-Q or CTRL+SHIFT+V instead of CTRL-V to escape control characters. In the above command to enter a single ^V, you have to press Ctrl and v.

Why is J down in vim?

@geowa4, J was probably down because Ctrl+J is line feed in ASCII.


1 Answers

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.

like image 76
ZyX Avatar answered Sep 21 '22 18:09

ZyX