How do I enable line numbers (on the left) every time I open Emacs?

Learn how do i enable line numbers (on the left) every time i open emacs? with practical examples, diagrams, and best practices. Covers emacs development techniques with visual explanations.

Enable Line Numbers in Emacs Permanently

Hero image for How do I enable line numbers (on the left) every time I open Emacs?

Learn how to configure Emacs to display line numbers automatically every time you open a file, enhancing your coding and editing workflow.

Line numbers are an essential feature for many developers and writers, providing quick navigation and reference points within a file. While Emacs offers powerful editing capabilities, line numbers are not enabled by default. This article will guide you through the process of permanently enabling line numbers in Emacs, ensuring they appear every time you open a file, without needing to manually activate them.

Understanding Emacs Configuration

Emacs uses a configuration file, typically named .emacs or init.el, located in your home directory (~/.emacs.d/). This file contains Emacs Lisp (Elisp) code that Emacs executes upon startup. By adding specific commands to this file, you can customize Emacs' behavior, including enabling global modes like display-line-numbers-mode.

Enabling Line Numbers Globally

The most straightforward way to enable line numbers for all buffers is to activate display-line-numbers-mode globally. This mode provides various options for how line numbers are displayed, including relative line numbers, which can be very useful for navigation. We'll focus on the basic absolute line numbers for now.

flowchart TD
    A[Start Emacs] --> B{Read init.el}
    B --> C{Find `display-line-numbers-mode` setting}
    C -->|Setting Found| D[Activate `display-line-numbers-mode`]
    C -->|Setting Not Found| E[Default: No Line Numbers]
    D --> F[Display Line Numbers]
    E --> G[No Line Numbers]
    F --> H[Ready for Editing]
    G --> H

Emacs startup process with line number activation

1. Locate your Emacs configuration file

Open Emacs and type C-x C-f ~/.emacs.d/init.el (or ~/.emacs if you use the older naming convention) to open your configuration file. If the file doesn't exist, Emacs will create a new, empty buffer for it.

2. Add the line numbers configuration

Add the following Elisp code to your init.el file. This line tells Emacs to enable display-line-numbers-mode globally for all buffers.

3. Save and restart Emacs

Save the file by typing C-x C-s. Then, restart Emacs. When Emacs reloads, you should see line numbers displayed on the left side of every buffer you open.

(global-display-line-numbers-mode 1)

Elisp code to enable global line numbers

Customizing Line Number Display

Emacs offers further customization for line numbers. For instance, you might prefer relative line numbers, which show the line number of the current line as 0 and other lines relative to it. This can be very helpful for commands like C-u N C-n (move down N lines).

;; Enable relative line numbers
(setq display-line-numbers-type 'relative)
(global-display-line-numbers-mode 1)

Elisp code for enabling relative line numbers