Increment decrement number under cursor?

Frank Fischer frank.fischer at
Wed Aug 17 12:29:49 CEST 2011

Am Mittwoch, 17. August 2011 schrieb Antono Vasiljev:
> Hello all!
> First of all, thank you for great Evil!
> Is it hard to implement increment/decrement number at point?
> C-a C-x in vim
> I'm emacs beginner but probably can implement it myself with
> some mentoring from gurus :)

Well, this is not really an evil issue. Just look at EmacsWiki 
for "increment number" and you will find examples, e.g.,

    (defun increment-number-at-point ()
      (skip-chars-backward "0123456789")
      (or (looking-at "[0123456789]+")
          (error "No number at point"))
      (replace-match (number-to-string (1+ (string-to-number 
(match-string 0))))))

Just bind it globally or, if you prefer, in evil-normal-state-map

(define-key evil-normal-state-map 
(kbd "C-a") 'increment-number-at-point)

If you prefer some evil-fun, you can define an operator which does the 
same except in visual-state, where it only increments the selected 

(evil-define-operator increment-number-at-point (beg end type)
  :motion evil-inner-word
  (let ((txt (buffer-substring-no-properties beg end)))
    (unless (string-match "^[0-9]+$" txt)
      (error "No number selected."))
    (delete-region beg end)
    (goto-char beg)
    (insert (number-to-string (1+ (string-to-number txt))))

I'm sure you can figure out decrementing or even count-arguments.


More information about the implementations-list mailing list