I've been using Vim for a while and now I've discovered Spacemacs and I found it great how it blends Vim ease of editing with Emacs superior power.

I really loved some parts of my Vim config, one of them was relative numbering.

In oder to have an idea, this is the Vim setup I'm trying to emulate (notice that relative numbering is off since Vim window doesn't have focus when taking the screen grab): vim setup

Trying to get the line numbering right aligned with a separator space in Spacemacs was quite complicated. But I'm finding it impossible to do the same for relative numbering. This is my code taken from emacs wiki for right aligned line numbering with a separator space:

    (unless window-system
      (add-hook 'linum-before-numbering-hook
            (lambda ()
              (setq-local linum-format-fmt
                  (let ((w (length (number-to-string
                            (count-lines (point-min) (point-max))))))
                    (concat "%" (number-to-string w) "d"))))))

    (defun linum-format-func (line)
       (propertize (format linum-format-fmt line) 'face 'linum)
       (propertize " " 'face 'mode-line)))

    (unless window-system
      (setq linum-format 'linum-format-func))

Any idea on how to achieve the same thing for linum-relative?

EDIT 1: I've tried the following code:

  (defvar my-linum-relative-format-string "%4s")
  (defun my-linum-relative-get-format-string ()
    (let* ((width (length (number-to-string
                           (count-lines (point-min) (point-max)))))
           (format (concat "%" (number-to-string width) "s")))
      (setq my-linum-relative-format-string format)))

  (setq linum-relative-format 'my-linum-relative-format)

  (defun my-linum-relative-format (line-number)
       (propertize (format my-linum-relative-format-string line-number) 'face 'linum-relative))

But it fails with the error: Error in post-command-hook (linum-update-current): (wrong-type-argument stringp my-linum-relative-format)

  • 2
    Welcome! You clearly have three distinct questions here, even though they all are related to line numbering. It would be best if you separated these into three specific questions with appropriate titles. – nanny Dec 7 '15 at 16:39
  • @nanny Oh, thank you Nanny, I'll do that then. Should I delete this question after? – jbssm Dec 7 '15 at 17:55
  • That would work. I think you'd be okay if you just edited this question so that it only asks one question, and then you can make two more questions. Same result either way. – nanny Dec 7 '15 at 17:59
  • Good idea. I'm doing that. – jbssm Dec 7 '15 at 18:07
  • 1
    When trying something that should work (but does not work), it is usually a good idea to try it with a minimal .emacs -- e.g., what we call Emacs -Q. If it works with a barebones configuration, then you know there is a conflict -- e.g., something is changing your settings. I have even gone so far as to set up a 3 second counter when starting Emacs with a prompt asking me if I want to start with nothing, or a full configuration -- it defaults to a full configuration after 3 seconds. – lawlist Dec 7 '15 at 18:42

Your Answer

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

Browse other questions tagged or ask your own question.