Reputation: 417
I have created a mapping in visual and normal mode to expedite moving around in a local region of code. if i press 1+direction key it is remapped to 10 instead of 1.
vmap 1j 10j | vmap 1k 10k | vmap 1h 10h | vmap 1l 10l
nmap 1j 10j | nmap 1k 10k | nmap 1h 10h | nmap 1l 10l
This works well. However when I am typing fast I inadvertently type 11 instead of 1 so '11j' insead of '1j'. This is moving me 110 lines down instead of 11.
I would like to only move 11 in a given direction instead of 110 when I make this mistake.
Vim is interpreting this as a 1 and then adding my mapping to get the 110. Similarly if I type '21j' it is interpreted as '210j'.
Upvotes: 0
Views: 73
Reputation: 31419
This should do what you. However I'm not really sure why it works the way it does. It seems that the old count doesn't get cleared when you change the count inside the mapping and the the new count is appended to the old count. (Notice that I only put a 0 in the mapping not a 10)
I also used v:count
to find the count of the mapping instead of overloading 1j
. v:count
returns 0 if no count was specified.
function TenMovement(type)
if v:count == 1
return '0'.a:type
else
return a:type
endif
endfunction
nnoremap <expr> j TenMovement('j')
nnoremap <expr> k TenMovement('k')
nnoremap <expr> l TenMovement('l')
nnoremap <expr> h TenMovement('h')
vnoremap <expr> j TenMovement('j')
vnoremap <expr> k TenMovement('k')
vnoremap <expr> l TenMovement('l')
vnoremap <expr> h TenMovement('h')
Upvotes: 2
Reputation: 172510
To fix this, you have to abort the previously typed count. <C-\><C-n>
works like <Esc>
in normal mode, but avoids the beep when there's no pending count:
nmap 1j <C-\><C-n>10j
For visual mode, the selection needs to be re-established with gv
:
vmap 1j <C-\><C-n>gv10j
Upvotes: 1