简体   繁体   中英

vim - Ignore timoutlen for certain key-mapping macros

I have made two simple nmap s in Vim that moves the cursor 10 times right and left by pressing Alt - z and Alt - Z

nmap <ESC>z 10l
nmap <ESC>Z 10h

It works perfect.

Now I would like to add two similar nmap s that delete 10 times

nmap d<ESC>z 10x
nmap d<ESC>Z 10X

This works almost fine, and follows the vim logic of operation followed by cursor move.

Unfortunately it times out if I don't press Alt z or Alt Z quickly after d . I find this odd, because I have not mapped anything to just d . And if I press w or W after d , I can wait as long as I want.

I know there's a special thing about built-in maps such ad dw and dW .

To my question: Is there a way I can get my own nmap s to accept long delays between the key-presses?

You're right that by default, 'timeoutlen' (default 1 s) applies to mapped keys. I find this useful, but if you really want your started mappings to wait indefinitely, you have to define the map on the first key only, then query and handle the remaining keys yourself .

function! MapOrDefault()
    let c = nr2char(getchar())
    return c == "\<A-z>" ? '10x' : 'd' . c
endfunction
nnoremap <expr> d MapOrDefault()

Note: Though it's technically equivalent, I'd recommend to prefer the Vim key-notation ( <Az> ) instead of the cryptic <Esc>z .

Note: You should use :noremap ; it makes the mapping immune to remapping and recursion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM