11

In spacemacs Ctrl-a is being used to move to the beginning of the line. I'm more used to 0 to move to the beginning of the line and $ to the end, which works in spacemacs. Ctrl-a increases numbers in vim and I'd like to use that key in spacemacs too. How can I do so? I tried to unbind the keys with the following in my .spacemacs:

(define-key org-mode-map (kbd "C-a") nil)
(define-key global-map (kbd "C-a") nil)

Now the key doesn't move to the beginning of the line, but it also doesn't increment numbers, it does nothing. When I use it, it simply shows "C-a is undefined".

4 Answers 4

12

The package evil-numbers does exactly what you need.

2
  • 1
    Why isn't this part of evil-mode?
    – ideasman42
    Jan 14, 2019 at 1:56
  • 1
    No idea, but it doesn't help that both C-a and C-x are bound by default in Emacs.
    – wasamasa
    Jan 14, 2019 at 7:14
10

I've stumbled upon the same issue of preference and as suggested before, evil-numbers is the package for that. But while trying to set it up, it was surprising that evil-numbers is already in spacemacs, just missing the keybinds.

To activate, just add

(define-key evil-normal-state-map (kbd "C-a") 'evil-numbers/inc-at-pt)
(define-key evil-visual-state-map (kbd "C-a") 'evil-numbers/inc-at-pt)
(define-key evil-normal-state-map (kbd "C-x") 'evil-numbers/dec-at-pt)
(define-key evil-visual-state-map (kbd "C-x") 'evil-numbers/dec-at-pt)

to your user-init and it will work with Ctrl+a and Ctrl+x combinations. (taken from the package's *.el file in git)

I don't get any conflicts with previous keybinds, i guess emacs redefines them, not overlays. (?)

2
  • 3
    Can you edit your question to have appropriate keybindings rather than telling to change them?
    – user12563
    Jan 13, 2019 at 23:43
  • 1
    Ofcourse i can :)
    – Alvin
    Jan 14, 2019 at 0:36
6

Emacs doesn't have a built-in function to increment numbers, so let's grab this one from EmacsWiki:

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

Now we can bind C-a to that function:

(global-set-key (kbd "C-a") 'increment-number-at-point)

Here's a version of that function that keeps point at the same place instead of moving to the end of the number:

(defun increment-number-at-point ()
  (interactive)
  (let ((old-point (point)))
    (unwind-protect
        (progn
          (skip-chars-backward "0-9")
          (or (looking-at "[0-9]+")
              (error "No number at point"))
          (replace-match (number-to-string (1+ (string-to-number (match-string 0))))))
      (goto-char old-point))))
2
  • After the increment the cursor moves to the end of the number. How can we avoid this behavior? Apr 6, 2021 at 17:28
  • 1
    I edited the answer and added another version of that function that seems to keep the cursor in place.
    – legoscia
    Apr 6, 2021 at 17:40
1

There are different bindings for this functionality in spacemacs.
You can increment with Space n + or Space n = and decrement with Space n -.

For more information take a look at the official documentation: https://github.com/syl20bnr/spacemacs/blob/master/doc/DOCUMENTATION.org#increasedecrease-numbers Or this issue on GitHub: https://github.com/syl20bnr/spacemacs/issues/9895

(On the develop branch these currently open a transient state in which you can further increment or decrement the values.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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