Skip to content

Instantly share code, notes, and snippets.

@howardabrams
Last active December 10, 2016 11:36
Show Gist options
  • Save howardabrams/9244369 to your computer and use it in GitHub Desktop.
Save howardabrams/9244369 to your computer and use it in GitHub Desktop.
A simple approach to easily incrementing (or decrementing) the next number in an Emacs buffer. This seems to be helpful when editing odd XML data files.
(defun increment-decrement (amount)
(let* ((for-number "[0-9][0-9]*")
(end (re-search-forward for-number))
(start (match-beginning 0))
(str (buffer-substring start end))
(num (+ amount (string-to-number str))))
(delete-region start end)
(insert (number-to-string num))
(goto-char start)))
(defun increment (start end)
"Searches forward in the buffer and increments the first number
it finds."
(interactive "r")
(increment-decrement 1))
(defun decrement (start end)
"Searches forward in the buffer and decrement the first number
it finds."
(interactive "r")
(increment-decrement -1))
(global-set-key (kbd "C-c <up>") 'increment)
(global-set-key (kbd "C-c <down>") 'decrement)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment