The Problem
You use the Emacs commands:Command | Action |
---|---|
M-f | Move forward by one word |
M-b | Move backward by one word |
M-d | Delete word ahead of the cursor |
M-DEL | Delete word behind cursor |
M-t | Transpose words |
M-@ | Mark word |
M-u | Upcase word |
M-l | Downcase word |
M-c | Capitalize word |
They work beautifully with ordinary text but not so great in C or Java modes. For example, the arrows in the image below show where the cursor will skip to if you use M-f to skip forward starting at the first arrow.
The problem here is that C-mode doesn't recognize "_" as part of a "word".
The Solution
There is an easy fix for this: simply add the following lines to your
All word-related commands will recognize "_" as part of a "word" in C and Java modes.
Problem solved.
.emacs
file:
01 (add-hook 'c-mode-common-hook 02 (lambda () 03 (modify-syntax-entry ?_ "w" c-mode-syntax-table))) 04 (add-hook 'java-mode-hook 05 (lambda () 06 (modify-syntax-entry ?_ "w" java-mode-syntax-table)))The image below shows where the cursor will skip to after the hook is installed.
All word-related commands will recognize "_" as part of a "word" in C and Java modes.
Problem solved.