2

I have a set of bindings for jumping to the (n*10)th column that are basically just sugar over the | command and somewhat nice for coarse horizontal movement.

nnoremap <silent> <leader>`      0
nnoremap <silent> <leader>1     10\|
nnoremap <silent> <leader>2     20\|
nnoremap <silent> <leader>3     30\|
nnoremap <silent> <leader>4     40\|
nnoremap <silent> <leader>5     50\|
nnoremap <silent> <leader>6     60\|
nnoremap <silent> <leader>7     70\|
nnoremap <silent> <leader>8     80\|
nnoremap <silent> <leader>9     90\|
nnoremap <silent> <leader>0    100\|
nnoremap <silent> <leader>-    110\|
nnoremap <silent> <leader>=    120\|
nnoremap <silent> <leader><bs> 130\|

For the most part, it works well. However, I'll sometimes hit a stray digit by accident, say 1 and then hit leader 2 and jump to column 120 by mistake.

Is there a way to modify the commands so that they clear numeric arguments before being executed?

I tried using the normal command to wrap one of the options

nnoremap <silent> <leader>1 :normal "10\\\|"<cr>

(and several variations of backslashing to prevent the | from being interpreted as a line separator in the body of the nnoremap)

But doing that seems to be equivalent to 0 for some reason.

2

You almost had it:

  • you need <c-u> to clear the count
  • and then either you use quotes with :exe, or you use none

In short:

nnoremap <silent> <leader>1 :<c-u>exe 'normal! 10\|'<cr>
" or
nnoremap <silent> <leader>1 :<c-u>normal! 10\|<cr>
|improve this answer|||||
3

One way to do it would be to carry out the movement with a function. The existing count will be converted to a range on the command line, but we can clear this with <C-U>.

This has the slight advantage of allowing you to compress the numerical mappings down a little:

for i in range(1, 9)
  execute 'nnoremap <silent> <leader>'.i.' :<C-U>call cursor(0, '.i.'0)<CR>'
endfor

nnoremap <silent> <leader>` :<C-U>call cursor(0, 1)<CR>
nnoremap <silent> <leader>0 :<C-U>call cursor(0, 100)<CR>
" etc

(hat-tip to Carpetsmoker and B Layer for use of cursor() instead of the function originally in this answer).

Further Reading

  • :help :call
  • :help v:count
  • :help cursor()
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.