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.
2 Answers
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
-
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
andCtrl+X
also moved to the next number.– jpyamsCommented Oct 6, 2017 at 14:40 -
1For 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!– RichCommented Jun 28, 2021 at 21:26
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– jpyamsCommented Oct 6, 2017 at 13:11
-
1By "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
/\d
.