1

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.

  • :call search("\\d\\+") – Alex Kroll Oct 5 '17 at 23:12
  • 1
    :nmap <leader>nd :call search("\\d\\+")<cr> – Alex Kroll Oct 6 '17 at 0:16
  • Shorter: /\d. – Karl Yngve Lervåg Oct 6 '17 at 6:06
4

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
  • Ctrl+Au
  • Ctrl+Xu
  • 1
    +1 for unconventional thinking; this definitely is nifty when on a foreign system, but I wouldn't want to use this permanently. – Ingo Karkat Oct 6 '17 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 Oct 6 '17 at 14:40
6

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].

  • 'this doesn't yet support': wow, I don't think I've ever seen those words regarding anything in Vim – jpyams Oct 6 '17 at 13:11
  • 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. – Ingo Karkat Oct 6 '17 at 14:09

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.