In Neovim, C-a and C-x will increment/decrement a number under the cursor in Normal mode
Also works in Vim.
Thought this was neat. Wanted to share.
Thanks @spicyq. It turns out Emacs does have this feature built-in (via Org-mode) with the commands org-increase-number-at-point and org-decrease-number-at-point.
The commands:
- Work in any mode, not just
org-mode - Support prefix arguments with
C-u - Do not have a default keybind
I bound the commands to C-z <up> and C-z <down>, since I had previously unbound suspend-frame from C-z:
(keymap-global-unset "C-z" 'remove) ; suspend-frame
Keep in mind you can repeat your last executed command with C-x z (and then just keep pressing z to repeat the command however many times you want).
Of course, now that I've got this far, I'm realizing that typing out either C-u 10 C-z <up> or C-z <up> C-x z + z * 9 is probably a lot more keystrokes than just changing the number myself. (At least for a single number at a single point in the buffer.)
I don't think there is a built-in Emacs feature that does the same thing. You can find several custom Emacs Lisp solutions by searching online though.
There's also
g<C-a>, which increments a visual block in an order. For example, if you haveYou can block select it, press
g<C-a>and getIt also works with a count, so for example
3g<C-a>will give youDidn’t know that one, neat!
It's even better than that! You don't need to position your cursor on the number you want to increment/decrement. It'll apply to whatever the first number to the right of your cursor is. Most of the time it means you can just keep your cursor on the beginning of the line or wherever it happens to be.
Note that this will trip you up if trying to increment IP addresses. In my earlier days I resorted to removing the first three octets, incrementing the last one, then pasting the first three back in with a column block edit.
funky interactions when you've got stuff like classname-section-1 because it reads -1 as a negative. generally one of my more frequently used vim commands tho
In helix, which has this feature by default as well (and with the same key binds too I’m just realizing), this is just selection-dependent, and by default the selection is only one character wide, so this shouldn’t be a problem.
Edit; just checked and it seemingly doesn’t de- or increment anything if your selection contains letters, so you can’t even trip it if you were to apply it on the whole
classname-section-1word.Not quite the same, but using only built-in stuff in Emacs you can increment every number with an interactive regexp search and replace:
C-M-% -?[0-9]+ RET \,(1+ \#0) RETThe
\,(...)in the replacement takes an arbitrary Elisp expression to evaluate to get the replacement, and the\#0means the full match interpreted as a number. (\#1would be the first capture as a number, etc., while just\#is the replacement index: 0, 1, 2, ...). The1+is the Elisp function to add one. You can change that to1-to decrement instead.I tend to use this capability for doing simple math like that, rounding numbers, changing bases, changing capitalization selectively, renumbering indices, etc.
I never really got into Vim, but from what I can tell, it seems to have a lot of nice touches for micro-edits like this, whereas I feel that Emacs often requires more keystrokes of overhead but then favors flexible bulk edits.
also #'org-{increase,decrease}-number-at-point are build in, but they don't have global bindings
This also works in Helix :)