开发者

Right align line numbers with linum-mode?

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-s开发者_运维技巧tring w) "d ")) line) 'face 'linum)))

Any ideas?


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


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

(custom-set-variables '(linum-format (quote "%7d")))


In 2022, for Emacs 28.1, the command is:

(setq linum-format 'dynamic)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜