5

When writing code in python-mode my preference would be to include underscores within a word boundary, so variable_with_underscores is treated as a single word.

How would I limit my word preference only to python-mode?

1
  • Depending on your use cases it might be enough to switch to balanced-expressions based stuff like mark-sexp instead of mark-word? Oct 6, 2015 at 4:40

2 Answers 2

7

Add a hook to modify the python-mode-syntax-table:

;; Keep underscores within a word boundary
(add-hook 'python-mode-hook
          (lambda () (modify-syntax-entry ?_ "w" python-mode-syntax-table)))
5

It's bird, it's a plane, it's superword-mode:

Superword mode is a buffer-local minor mode. Enabling it changes the definition of words such that symbols characters are treated as parts of words: e.g., in ‘superword-mode’, "this_is_a_symbol" counts as one word.

It's a minor mode, so you can set it in a hook the normal way: (add-hook 'python-mode-hook #'superword-mode). superword-mode has only existed since 24.4, so you'll need to hack the syntax table by hand in older versions.

I find it very convenient to bind this to a key so I can easily toggle it. It's the sort thing I want sometimes, but not others.

1
  • This might work for navigation but for "replace-string" does not work. Say you want to replace just one word C-u M-x replace-string this won't work. The accepted answer however, does the trick.
    – mfcabrera
    Aug 16, 2018 at 12:02

Your Answer

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

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