merlin2011
merlin2011

Reputation: 75565

Is it possible to always overwrite a specific built-in command with a particular mapped command?

I have the following mapping in my .vimrc.

" Scroll arbitrary
function ScrollToPercent(percent)
    let movelines=winheight(0)*a:percent/100

    if has("float") && type(movelines)==type(0.0)
        let movelines=float2nr(movelines)
    endif

    let oldso=&so
    execute ":set so=" . movelines
    execute "normal! zt"
    execute ":set so=" . oldso
endfunction

nnoremap zz :call ScrollToPercent(25)<CR>

This mapping causes the command zz to move the line under the current cursor to 25% of the way down from the top of the screen, instead of the usual behavior of moving it 50% of the way down.

I have observed that, if I type the second z with slightly greater delay after the first z, my mapping is ignored and the default behavior of zz kicks in. In particular, the line under my cursor will move to 50% of the way down instead of 25% of the way down.

Based on the answer to this question, I could set timeoutlen to some high value, but I do not want to do this because it will mess up other mappings which should not be kicked off if a small delay separates the keys.

Is it possible to make my mapping for zz always override the default zz, without changing some global option that will affect other mappings?

Upvotes: 0

Views: 173

Answers (1)

Ben
Ben

Reputation: 8905

You could map a single z to a no-op. Then if you quickly type zz then your cursor will jump, if you wait too long nothing at all will happen.

I.e. use this pair of mappings:

nnoremap zz :call ScrollToPercent(25)<CR>
nnoremap z  <NOP>

Upvotes: 1

Related Questions