Emacs - Empty lines have no line numbers


Kirill Novikov

I am trying to set line numbers in Emacs.
Linum works great, but when I open both buffers, the numbering of empty lines disappears. I am using Manjaro Linux. Emacs works in the terminal.

Here is the screenshot.

Code in .emacs file:

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

How can I fix this?

Brian Malehorn
  1. You might be able to fix this by replacing all of the above code with

    (global-linum-mode 1)
    
  2. linum-mode should already do the variable size format for you. Not sure why the wheel is reinvented.

  3. Maybe your problem is that you are trying to stringify concattwo propertize-d objects . You can avoid this by formatting your format like this "%3d "instead "%3d"and concatting " "later:

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

Related


Emacs line numbers

flood I'm having trouble getting line numbers in an emacs buffer. I have the following numbers in my init file: (setq line-number-display-limit 2000000) and I have verified that it has line number mode turned on. When I first enter the (~500) line buffer, emac

Emacs line numbers

flood I'm having trouble getting line numbers in an emacs buffer. I have the following numbers in my init file: (setq line-number-display-limit 2000000) and I have verified that it has line number mode turned on. When I first enter the (~500) line buffer, emac

Add line numbers to GNU Emacs

Akma How to add line numbers in Emacs? Please explain, I am an absolute beginner. monster dragon If you run it in default mode, it's in the menu: Options > Show/Hide > Line Numbers. Then select Options > Save Options to save it for later sessions. If you run i

Add line numbers to GNU Emacs

Akma How to add line numbers in Emacs? Please explain, I am an absolute beginner. monster dragon If you run it in default mode, it's in the menu: Options > Show/Hide > Line Numbers. Then select Options > Save Options to save it for future sessions. If you run

Fill empty lines with first line

carol I have tab delim file like this: a 12 17 bac 30 42 a 15 18 a 18 22 I want to repeat bac 30 42 columns for the rest of the rows like this: a 12 17 bac 30 42 a 15 18 bac 30 42 a 18 22 bac 30 42 I tried the following commands aw

line numbers of explicit lines in r

Dheeraj Singh I need to get the row numbers of explicit rows grouped by ID. Suppose the data frame (df) looks like this: id a b 3 2 NA 3 3 2 3 10 NA 3 21 0 3 2 NA 4 1 5 4 1 0 4 5 NA I need to create one more column t

line numbers of explicit lines in r

Dheeraj Singh I need to get the row numbers of explicit rows grouped by ID. Suppose the data frame (df) looks like this: id a b 3 2 NA 3 3 2 3 10 NA 3 21 0 3 2 NA 4 1 5 4 1 0 4 5 NA I need to create one more column t

line numbers of explicit lines in r

Dheeraj Singh I need to get the row numbers of explicit rows grouped by ID. Suppose the data frame (df) looks like this: id a b 3 2 NA 3 3 2 3 10 NA 3 21 0 3 2 NA 4 1 5 4 1 0 4 5 NA I need to create one more column t

Concatenate 2000 lines into 1 line in Emacs

Yang Haik I need to concatenate 2000 lines of code which I hope can be combined into one line. I know that in Emacs, with the replace-stringcommand, I can add the for cascade to the end of each line in +one go. However, when I do this, I keep getting a syntax

Emacs line numbers are set to "??" in the status bar

bph I'm having problems with emacs(24), occasionally without line numbers. The settings in the status bar are ??, e.g. U:--- filename All (1000, 4) (Conf[Unix]) Switch to: U:--- filename All (??, 4) (Conf[Unix]) It's a bit annoying and I'd

Emacs line numbers are set to "??" in the status bar

bph I'm having problems with emacs(24), occasionally without line numbers. The settings in the status bar are ??, e.g. U:--- filename All (1000, 4) (Conf[Unix]) Switch to: U:--- filename All (??, 4) (Conf[Unix]) It's a bit annoying and I'd

Emacs line numbers are set to "??" in the status bar

bph I'm having problems with emacs(24), occasionally without line numbers. The settings in the status bar are ??, e.g. U:--- filename All (1000, 4) (Conf[Unix]) Switch to: U:--- filename All (??, 4) (Conf[Unix]) It's a bit annoying and I'd

Separating line numbers from text in Terminal: Emacs

username When I run emacs in terminal mode with the global-linum-mode option enabled, the line numbers are very close to my text, which really annoys the eyes. Is there a way to separate line numbers from text in Emacs? Anton Kovalenko Try setting linum-format

Separating line numbers from text in Terminal: Emacs

username When I run emacs in terminal mode with the global-linum-mode option enabled, the line numbers are very close to my text, which really annoys the eyes. Is there a way to separate line numbers from text in Emacs? Anton Kovalenko Try setting linum-format

Separating line numbers from text in Terminal: Emacs

username When I run emacs in terminal mode with the global-linum-mode option enabled, the line numbers are very close to my text, which really annoys the eyes. Is there a way to separate line numbers from text in Emacs? Anton Kovalenko Try setting linum-format

Why should there be whitespace for line numbers in Emacs?

caterpillar I (global-linum-mode t)use to display line numbers in Emacs. This worked fine until I used the ctrl+ up/ downcommand ( forward-paragraphand backward-paragraph) to navigate the buffer, at which point some line numbers were displayed incorrectly (see

Emacs line numbers are set to "??" in the status bar

bph I'm having problems with emacs(24), occasionally without line numbers. The settings in the status bar are ??, e.g. U:--- filename All (1000, 4) (Conf[Unix]) Switch to: U:--- filename All (??, 4) (Conf[Unix]) It's a bit annoying and I'd

Separating line numbers from text in Terminal: Emacs

username When I run emacs in terminal mode with the global-linum-mode option enabled, the line numbers are very close to my text, which really annoys the eyes. Is there a way to separate line numbers from text in Emacs? Anton Kovalenko Try setting linum-format

Does nano have line numbers?

Christy Is there a way to turn on line numbers for nano? Believe The only thing that comes close to what you want is an option to show the current cursor position. You can activate it with the --const(manpage: keep showing cursor position) option or by pressin

Does nano have line numbers?

Christy Is there a way to turn on line numbers for nano? Believe The only thing that comes close to what you want is an option to show the current cursor position. You can activate it with the --const(manpage: keep showing cursor position) option or by pressin

How to generate random numbers to fill empty lines?

Matt I have added two new columns on one of the database tables. They are called exam and assignment results. Currently, both columns are empty. I want them to be filled with random check results, but not more than 100. How to generate random numbers from 0 to

How to generate random numbers to fill empty lines?

Matt I have added two new columns on one of the database tables. They are called exam and assignment results. Currently, both columns are empty. I want them to be filled with random check results, but not more than 100. How to generate random numbers from 0 to

Emacs macro to fire only on non-empty lines

wen Often, I find myself writing short macros that, for example, add or remove line comments or correct indentation on lines. However, with whitespace mode enabled, I still have to be careful not to trigger these macros on empty lines. If a macro tries to remo

Emacs macro to fire only on non-empty lines

wen Often, I find myself writing short macros that, for example, add or remove line comments or correct indentation on lines. However, with whitespace mode enabled, I still have to be careful not to trigger these macros on empty lines. If a macro tries to remo