oiler_no11
oiler_no11

Reputation: 155

Underscore as part of word for forward-word not working

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?

Upvotes: 13

Views: 3736

Answers (4)

Nikita
Nikita

Reputation: 1143

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.

Upvotes: 3

Stefan
Stefan

Reputation: 28571

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.

Upvotes: 4

event_jr
event_jr

Reputation: 17707

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.

Upvotes: 6

Trey Jackson
Trey Jackson

Reputation: 74480

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

(add-hook 'nxhtml-mode-hook
          (lambda () (modify-syntax-entry ?_ "w")))

Upvotes: 20

Related Questions