5

You can place a pointer on a number, like 1, and increment it to 2. There are many alternatives on this. See also the Emacs Wiki:

https://www.emacswiki.org/emacs/IncrementNumber

7 alternatives to increment a number under the pointer! But there is a thing that I still miss. In Vim, you can increment the number/character under the pointer whether it's a character or a number.

Incrementing an A turns into a B.

Incrementing a 1 turns into a 2.

Emacs seems be to able to increment numbers only, from what I found on Emacs Wiki. Is there any way to dinstinct characters from numbers and increment them accordingly?

6

Characters are numbers (non-negative integers under some limit), in Emacs. If you want to increment a single character, including a digit character, then just increment it as a character:

(defun increment-char-at-point ()
  "Increment number or character at point."
  (interactive)
  (condition-case nil
      (save-excursion
        (let ((chr  (1+ (char-after))))
          (unless (characterp chr) (error "Cannot increment char by one"))
          (delete-char 1)
          (insert chr)))
    (error (error "No character at point"))))

But if you want to increment either a sequence of digits (a numeral) or a single character, then use something like this:

(defun increment-number-or-char-at-point ()
  "Increment number or character at point."
  (interactive)
  (let ((nump  nil))
    (save-excursion
      (skip-chars-backward "0123456789")
      (when (looking-at "[0123456789]+")
        (replace-match (number-to-string (1+ (string-to-number (match-string 0)))))
        (setq nump  t)))
    (unless nump
      (save-excursion
        (condition-case nil
            (let ((chr  (1+ (char-after))))
              (unless (characterp chr) (error "Cannot increment char by one"))
              (delete-char 1)
              (insert chr))
          (error (error "No character at point")))))))

If you want point to advance after the command, remove the uses of save-excursion.

The code inside the first save-excursion here is from the first increment-number-at-point definition at the wiki page you referenced. Choose different increment-number code for that if you prefer. Note that the increment-number code shown here takes no account of decimal or exponential format, or other bases besides base 10.

  • Great. It worked fine, and your posts gives me a valuable lesson into the inner workings of Emacs Lisp. I really deeply appreciate your contributions on this awesome site. – ReneFroger Feb 22 '16 at 12:31
0
(defun increment-number-at-point ()
  "increnment-number automatically search-forward to get integer in current  line"
  (interactive)
  (skip-chars-backward "0-9")
  (or (search-forward-regexp "[0-9]+"))  
  (replace-match (number-to-string
                  (1+ (string-to-number
                       (match-string 0))))))

if you want to increment a number in current line and you don't want to move your cursor to this number, this function will help you.

It automatically search-forward about number and move cursor to it and increment numbers on it.

  • I didn't expect to get any addition on this. Thanks for that! – ReneFroger May 19 '18 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.