简体   繁体   中英

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-string 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: Mx 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))

自定义变量linum格式,例如在7个字符的右侧对齐:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM