Increment decrement number under cursor?

Timothy Harper timcharper at gmail.com
Wed Aug 17 19:06:59 CEST 2011


On Aug 17, 2011, at 4:29 AM, Frank Fischer wrote:

> 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 ()
>      (interactive)
>      (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 
> number:
> 
> 
> (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))))
>    (backward-char)))
> 
> I'm sure you can figure out decrementing or even count-arguments.
> 
> Frank

This version doesn't handle negative numbers. I should post my improved version on the wiki… but here's a paste (feel free to share)

http://paste.lisp.org/display/124090

There's obviously a HUGE keybinding collision with binding "c-x" to decrement number without some kind of prefix, so I decided that C-c + / C-c - would be a fine substitute.

Tim


More information about the implementations-list mailing list