Emacs - 空行没有行号

Emacs - No line numbering for empty lines

我正在尝试在 Emacs 中设置行号。
Linum 运行良好,但是当我打开两个缓冲区时,空行的编号消失了。 我使用 Manjaro Linux。 Emacs 在终端中运行。

Here's screenshot.

.emacs 文件中的代码:

(add-hook 'find-file-hook (lambda () (linum-mode 1)))

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

我该如何解决?

  1. 您可以通过将上述代码的 all 替换为

    来解决此问题
    (global-linum-mode 1)
    
  2. linum-mode 应该已经为 你。不知道你为什么要重新发明轮子。

  3. 也许您的问题是您正在尝试串接 concat 两个 propertize-d 对象。您可以通过将格式设置为 "%3d " 而不是 "%3d" 并稍后连接 " " 来避免这种情况:

    (add-hook 'find-file-hook (lambda () (linum-mode 1)))
    
    (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)
      (propertize (format linum-format-fmt line) 'face 'linum))
    
    (unless window-system
      (setq linum-format 'linum-format-func))