8

Is there a command in Vim to move the cursor to the next number? I know I can do f2 to find the number 2 in the same line, but I'm looking for a more general number-jumping command.

3
  • :call search("\\d\\+")
    – Alex Kroll
    Commented Oct 5, 2017 at 23:12
  • 1
    :nmap <leader>nd :call search("\\d\\+")<cr>
    – Alex Kroll
    Commented Oct 6, 2017 at 0:16
  • 1
    Shorter: /\d. Commented Oct 6, 2017 at 6:06

2 Answers 2

13

Ctrl+A and Ctrl+X will both jump the cursor to the next number on the line.

However, the former will increment the number and the latter will decrement it. You don't want to make any changes, so you have the following options to quickly jump to the next number on the line.

  • Ctrl+ACtrl+X
  • Ctrl+XCtrl+A
4
  • 1
    +1 for unconventional thinking; this definitely is nifty when on a foreign system, but I wouldn't want to use this permanently. Commented Oct 6, 2017 at 14:11
  • My use for this question was to get to a number to increment/decrement, so definitely +1 for that. I didn't realize Ctrl+A and Ctrl+X also moved to the next number.
    – jpyams
    Commented Oct 6, 2017 at 14:40
  • 1
    For me "u" also moves the cursor back to the location before the jump, so the "u" part doesn't work for me. Commented Jun 28, 2021 at 15:36
  • @SebastianNielsen Huh. It does for me too. Not sure if Vim or my config has changed since I wrote this. I would have thought I would have tested that before adding it to the answer, but perhaps not. I’ll edit it out just in case. Thanks for letting me know!
    – Rich
    Commented Jun 28, 2021 at 21:26
11

To summarize the comments and make this a full answer:

The general search command is shortest: /\d<Enter>. You can then repeat the search with n / N, as usual. To skip to the next full number, not the next digit, use /\d\+ instead.

If you don't want to clobber your search pattern, you can use the lower-level search() function. As this is a lot to type, a mapping (defined in your ~/.vimrc) can be convenient:

:nnoremap <silent> <Leader>nd :call search('\d\+')<CR>

Unlike the built-in search commands, this doesn't yet support a [count] to skip some numbers. A robust implementation would need several lines of Vimscript. Alternatively, you can use my CountJump plugin to generate mappings:

:call CountJump#Motion#MakeBracketMotion('', 'd', '', '\d\+', '', 0)

This defines [d and ]d mappings that jump to the previous / next start of a number, with an optional [count].

2
  • 'this doesn't yet support': wow, I don't think I've ever seen those words regarding anything in Vim
    – jpyams
    Commented Oct 6, 2017 at 13:11
  • 1
    By "this", I mean the mapping implementation, not Vim's capabilities. As search() is a low-level function, it's also not supposed to handle a count by itself. Commented Oct 6, 2017 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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