Turn On Line Numbers For Python Automatically In Emacs

May 2022

I added this to my ~/.spacemacs file to turn on relative line numbers when in Python mode:

(add-hook 'python-mode-hook 
  (lambda () 
    (display-line-numbers-mode +1)
    (setq display-line-numbers 'relative)
    ))

I had to move this outside of the user (defun dotspacemacs/user-config () ... ) config section. It doesn't work if you put it in there.

This example uses python. It works with other modes as well. I confirmed the name of the hook finding the name of the variable via C-h v then typing hook and scanning the results to find what I'm looking for.

end of line