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".

plx
  • 347
  • 3
  • 11

4 Answers4

12

The package evil-numbers does exactly what you need.

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. (?)

Alvin
  • 101
  • 1
  • 4
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))))
legoscia
  • 6,012
  • 29
  • 54
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.)

Isti115
  • 111
  • 4