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?

Dan
  • 32,584
  • 6
  • 98
  • 168
Jeff Bauer
  • 861
  • 1
  • 10
  • 22
  • Depending on your use cases it might be enough to switch to balanced-expressions based stuff like `mark-sexp` instead of `mark-word`? – Dieter.Wilhelm Oct 06 '15 at 04:40

2 Answers2

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)))
Basil
  • 12,019
  • 43
  • 69
Jeff Bauer
  • 861
  • 1
  • 10
  • 22
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.

erikstokes
  • 12,686
  • 2
  • 34
  • 56
  • 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 '18 at 12:02