how to show line numbers in emacs editor

When editing a file in emacs, you can choose to display the line numbers just like the other text editors.

show which line number

First lets see how you can find the current line number that the point is in the buffer. This is useful if you don’t have line numbers enabled in the buffer.

M-x what-line

This will print the line number of the point in the current buffer to the mini-buffer. If you have the display line number feature on in the buffer, then this command is unnecessary.

enable line number display on mode line

If you want to enable line number display in the buffer, then you have a couple of options to do that. You have the option to display the line number in the mode line or with in the buffer itself.

M-x line-number-mode

This will toggle the line number display in the mode line. This will work on almost all buffers, but it is bound by couple of variables: line-number-display-limit and line-number-display-limit-width. Depending on the values of these variables, the line numbers may not show on large buffers or buffers with long lines.

In addition to the line numbers, you can also display the column number of the point in the buffer. In order to do that use the command column-number-mode.

M-x column-number-mode

Both of the commands are toggle commands. That means you can disable them by executing the command again. To make it the default, you can add it to the emacs configuration file.

display line numbers in gutter

If you want to display line numbers in the buffer, then you can use the command display-line-numbers-mode to do that.

M-x display-line-numbers-mode

This will toggle the display of line numbers in the current buffer. If you want to enable the display of line numbers across all of the buffers, then use the command global-display-line-numbers-mode.

M-x global-display-line-numbers-mode

Again these commands are toggle commands and you can use the same command to enable and disable the functionality.

If you want to make these modes permanent and not have to type them in every time then you should add them to the emacs configuration file. An example would look something like this.

(line-number-mode t)
(global-display-line-number-mode t)
(column-number-mode t)