Python mode annoyingly sets underscore (_) to be part of a word, making editing very tedious.

How can one reset this to Emacs's original behaviour?

2 Answers 2


Simply call the function py-underscore-word-syntax-p-off in your config file:

(use-package python-mode
  :ensure t


Edited: use helper function instead of setting py-underscore-word-syntax-p-off programmatically.

  • it doesn't work in :config because despite is defined as variable it's later set by defcustom (look here). So it may not work in another configuration if its value taken from there differs from your :init value. You should dig what the setter does and configure the value properly (wherever it'll be, I don't write python).
    – Muihlinn
    Commented Jun 14, 2020 at 7:50
  • Thanks for your comment! I don't think that a user should read source code in order to configure a package. Anyway, I have found a helper function and changed my answer. :) Commented Jun 16, 2020 at 8:52

Change the syntax of character _ to symbol syntax ("_"):

(modify-syntax-entry ?_ "_" python-mode-syntax-table) ; "_" means symbol syntax

Or change it to some other syntax class (other than word syntax: "w"). For example, change it to punctuation syntax:

(modify-syntax-entry ?_ "." python-mode-syntax-table) ; "." means punctuation syntax

See the Elisp manual, node Syntax Table Functions.

  • Thanks! To do this automatically, add this to the .emacs file: (add-hook 'python-mode-hook (lambda () (modify-syntax-entry ?_ "_" python-mode-syntax-table)))
    – diemo
    Commented Apr 25, 2020 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.