Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right align line numbers with linum-mode?

Tags:

emacs

elisp

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?

like image 348
monotux Avatar asked Sep 02 '10 11:09

monotux


2 Answers

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))
like image 108
aculich Avatar answered Nov 14 '22 08:11

aculich


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

(custom-set-variables '(linum-format (quote "%7d")))
like image 23
Jérôme Radix Avatar answered Nov 14 '22 07:11

Jérôme Radix