27

I am using Emacs -nw in Ubuntu. I like to turn on linum-mode to see line numbers on the left margin, but the numbers are put right next to my code.

I would love it if there could be some 'padding', like 1-character long, between line number and code. sorry I can't post an image since they are asking for 10 reputation, which I dont have:(

How can I do this?

Diaz
  • 957
  • 2
  • 13
  • 22

2 Answers2

67

You can use the variable linum-format to achieve this. Its value can either be a format string or a function which is called with line number as an argument. emacswiki has a couple of example of setting it to a format string

1) The following adds a space after the line-number

(setq linum-format "%d ")

2) You can also add a solid line separator

(setq linum-format "%4d \u2502 ")

I guess the above are sufficient for your needs. You can also find an example of using a function as linum-format here. Add whichever format suits your needs to your init file

  • I installed nlinum mode instead as apparently it's more efficient, this means that the above variable name becomes `nlinum-format` instead but I can confirm that after installation and adding the second option I now have the coolest line numbering I've ever seen :) `(setq nlinum-format "%4d \u2502 ")` – Emacs The Viking Sep 18 '18 at 12:53
  • It's my favorit answer and it works fine to me. The only thing that for org mode it can put several numbers to one line. Seems like a little bug. – Rostyslav Druzhchenko Feb 22 '19 at 08:38
  • I don't see the solid line separator – alper Aug 13 '20 at 01:27
16

In addition to the other answer(s) in this thread, options for putting distance between the line numbers and the text include, but are not limited to, adjusting the fringe width (and also set the color if you so choose).

The fringe is like a vertical ruler that runs from the top to the bottom of the buffer -- the left fringe is sandwiched between the line numbers and the text. It can be invisible if it is the same color as the default background of the user, or it can be a different color.

(setq-default left-fringe-width  10)

(setq-default right-fringe-width  0)

(set-face-attribute 'fringe nil :background "black")
lawlist
  • 13,099
  • 3
  • 49
  • 158
  • Exactly what I was looking for:) The fringe is by default present in X11 version of emacs, but not in terminal. Thanks! – Diaz Feb 18 '14 at 20:12
  • Glad to help. If you are comfortable with the answer, go ahead and place a check-mark please to mark this thread as being answered. – lawlist Feb 18 '14 at 20:27
  • After serious consideration I decided to mark the above one since it came before this, and did do the trick. I WAS looking for your way to do this, though:) Thanks again! I'm new to stackoverflow:) – Diaz Feb 19 '14 at 03:53
  • 1
    Is it possible to add a solid line separator as well? @lawlist – alper Aug 13 '20 at 01:27
  • @alper -- Commencing with the introduction of Emacs 26, Eli Z. wrote a built-in version of line numbers that essentially deprecates the Lisp versions that were popular prior thereto. The Lisp versions are easily modified, and "yes" it is possible to put in something like a `|` symbol. The built-in version can be modified, but requires building the modified version from source. For the deprecated version of Lisp, see the wiki page for some examples of how to modify the format: https://www.emacswiki.org/emacs/LineNumbers – lawlist Aug 13 '20 at 02:51
  • @alper -- If you are feeling bold and want to play around with modifying the Emacs source code and building your own build, see the following link containing some ideas: https://emacs.stackexchange.com/a/52271/2287 – lawlist Aug 13 '20 at 02:56