Underscore as part of word for forward-word not working
Asked Answered
B

4

13

I am trying to make underscores get treated as part of the word for the forward/backward-word function as described here and here. I am specifically trying to get this to work for nxhtml mode, but would really like it to work like this for all modes.

I have modified my site-start.el file a number of different ways but to no avail. But if I manually execute the command M-x modify-syntax-table in the buffer, it works just fine. I just can't get this to be the default behavior.

Here is what I tried putting in my site-start.el file:

;; 1
;; thought  this would apply it to all modes - no error, but did not work
(modify-syntax-entry ?_ "w") 

;; 2
;; thought this would automatically set it on a mode change - no error, but did not work
(defun change-major-mode-hook ()
(modify-syntax-entry ?_ "w"))


;; 3
;; thought  this would apply it to all modes - no error, but did not work
(modify-syntax-entry ?_ "w")

;; 4
;; this produced a symbol's value as variable is void error
(modify-syntax-entry ?_ "w" nxhtml-mode-syntax-table)

What am I missing?

Burette answered 19/5, 2012 at 14:47 Comment(1)
The docstring of modify-syntax-entry says "The syntax is changed only for table SYNTAX-TABLE, which defaults to the current buffer's syntax table". There's nothing there to suggest you could globally change all syntax tables by omitting that parameter.Samuelson
G
20

Have you tried modifying the nxhtml-mode-hook directly? Like so:

(add-hook 'nxhtml-mode-hook
          (lambda () (modify-syntax-entry ?_ "w")))
Grindery answered 19/5, 2012 at 14:52 Comment(2)
Thanks - this did just what I was looking for. And I added: (add-hook 'php-mode-hook (lambda () (modify-syntax-entry ?_ "w")))Burette
I would like for someone to clarify: Does this work for all Emacs modes, or only for the nxhtml-mode? (I am guessing the latter, but please clarify)Bialystok
C
6

Trey's answered your very specific question.

But Emacs already has a level of character organization that does what you ask: sexp. If you learned sexp commands instead of breaking word commands, then you can delete by "word" or by "symbol" as the situation fits.

Cassel answered 19/5, 2012 at 15:5 Comment(2)
When using variable names with underscores, I really want to treat the underscore as part of the word. The symbol-based movements are just too drastically different from what I am used to.Burette
I found treating an underscore as a separate word very inconvenient. I couldn't even remember when I actually was needed to move to half of a word foo_bar (btw, currently I have really many such a words). But conversely: every time I'm moving, or trying to do something with a text object in Emacs's Evil, and I have such a word around — this thing just interferes in my editing and slows me down.Somniferous
C
4

As event_jr, I recommend you learn to use the sexp-based commands like C-M-f and C-M-b. But if you really want to only jump over identifiers (and ignore things like parentheses), then rather than try and change all syntax tables in all major modes (which may end up having unforeseen consequences), I recommend you simply use new symbol-based movement commands. E.g. (global-set-key [remap forward-word] 'forward-symbol). For some commands (like kill-word) you'll need to write kill-symbol yourself, but it should be easy to do.

Chronometry answered 20/5, 2012 at 2:18 Comment(0)
V
3

Instead of adding hooks for each major mode, as suggested by Trey Jackson, you can modify the standard syntax table. The different modes inherit from the standard syntax table so your modification will hopefully apply to all modes:

(modify-syntax-entry ?_ "w" (standard-syntax-table))

This will affect regular text, XML, Python and others.

Some modes are more stubborn, and modifying the standard syntax table won't affect them as they override some symbols during initialization. One example is cc-mode. You'll still have to modify their tables explicitly.

Vaquero answered 29/4, 2016 at 18:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.