Right align line numbers with linum-mode?
Asked Answered
A

4

9

I would like that my linum-mode numbering is right aligned. The closest thing I've found is on emacswiki, but it doesn't work - it seems to left align the digits instead of right align it. The snippet is found here. Sorry for the horrible indenting, lisp is pretty alien to me :)

(setq linum-format                               
      (lambda (line)                                    
    (propertize                                  
     (format                                 
      (let                                   
      ((w (length (number-to-string (count-lines (point-min)         
                             (point-max))))))    
    (concat "%" (number-to-string w) "d ")) line) 'face 'linum)))

Any ideas?

Appendage answered 2/9, 2010 at 11:27 Comment(0)
G
4

Customize variable linum-format, for example to align on the right on 7 characters :

(custom-set-variables '(linum-format (quote "%7d")))
Goldeye answered 2/9, 2010 at 12:15 Comment(1)
I would have liked this a lot more if it'd been dynamically done, but once a file passes 1000 lines it doesn't matter anyways. Thanks! :) I settled for %4d.Appendage
C
21

You can just use the value 'dynamic so you don't have to choose an arbitrary amount of padding:

(custom-set-variables '(linum-format 'dynamic))

Or you can also customize it with: M-x customize-variable RET linum-format

Also, @asmeurer asked how to still add a space after the number with dynamic. There is no easy way to do that, but it can be accomplished using defadvice around the linum-update-window function that I adapted from the code for dynamic that is already in that function:

(defadvice linum-update-window (around linum-dynamic activate)
  (let* ((w (length (number-to-string
                     (count-lines (point-min) (point-max)))))
         (linum-format (concat "%" (number-to-string w) "d ")))
    ad-do-it))
Chism answered 12/12, 2011 at 5:1 Comment(3)
Thanks. You can also change this with M-x customize.Allergen
How can you use use dynamic and still add a space after the number? I'm sure it's straight-forward, but I'm still pretty new to lisp.Allergen
@Allergen I updated my answer to show how to use defadvice to still add a space after the number. And yes, you can use customize which is related to custom-set-variables.Chism
G
4

Customize variable linum-format, for example to align on the right on 7 characters :

(custom-set-variables '(linum-format (quote "%7d")))
Goldeye answered 2/9, 2010 at 12:15 Comment(1)
I would have liked this a lot more if it'd been dynamically done, but once a file passes 1000 lines it doesn't matter anyways. Thanks! :) I settled for %4d.Appendage
R
1

In 2022, for Emacs 28.1, the command is:

(setq linum-format 'dynamic)
Rickirickie answered 26/7, 2022 at 9:40 Comment(0)
S
-3

change the linum.el and byte-compile-file to .elc.

for emacs 23.3

line 143 of linum.el

                  (concat "%" (number-to-string w) "d" " | ")))))

I change the default fort to "xxx | ".

Som answered 15/12, 2011 at 14:48 Comment(1)
Changing the code directly is not a good long-term solution unless you are going to submit a patch upstream that you think will be accepted, otherwise when your emacs installation is upgraded you are going to lose those changes. Instead, in your init.el file you can add defadvice for the linum-update-window function, as the latest edits to my answer shows how to do.Chism

© 2022 - 2024 — McMap. All rights reserved.