1

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)
      (concat
       (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)

jbssm
  • 233
  • 2
  • 7
  • 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 07 '15 at 16:39
  • @nanny Oh, thank you Nanny, I'll do that then. Should I delete this question after? – jbssm Dec 07 '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 07 '15 at 17:59
  • Good idea. I'm doing that. – jbssm Dec 07 '15 at 18:07
  • Using the fringe as a separator is the best option to create a separator: http://emacs.stackexchange.com/a/5310/2287 So you really just need to calculate the largest number of digits and add leading spaces to the digits that are less than the max-digit. http://stackoverflow.com/q/3626632/2112489 – lawlist Dec 07 '15 at 18:36
  • @lawlist I've already tried to apply this solution but nothing shows up. I think I'm missing something (probably something obvious to an experienced Emacs user) that is not specified in that post and that is needed to add besides the configuration code in there. – jbssm Dec 07 '15 at 18:40
  • 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 07 '15 at 18:42

0 Answers0