emacsemacs23

Changing Emacs Forward-Word Behaviour


As the title says, how does one change the behaviour of emacs forward-word function? For example, suppose [] is the cursor. Then:

my $abs_target_path[]= abs_path($target);
<M-f>
my $abs_target_path = abs[_]path($target);

I know I could just use M-f M-b but as far as I'm concerned, that shouldn't be necessary and I'd like to change it. In particular, I want two things:

  1. When I press M-f, I want to go to the first character of the next word regardless of whether the point is within a word, within a group of spaces or somewhere else.
  2. Customize word-characters on a mode-by-mode basis. After all, moving around in CPerl mode is different than, say, TeX mode.

So, in the above example, item 1 would have the cursor would move to the 'a' (and the point to it's left) after hitting M-f. Item 2 would allow me to define underscores and sigils as word characters.


Solution

  • Try:

    (require 'misc)
    

    Then use M-x forward-to-word and see if it does what you want. You can then rebind M-f, etc.

    To make the _ not a word separator (i.e. make it a word constituent) for C & C++ mode, you would do this:

    (modify-syntax-entry ?_ "w" c-mode-syntax-table)
    (modify-syntax-entry ?_ "w" c++-mode-syntax-table)
    

    For more information on syntax tables, read this wiki page. Syntax tables are generally named like tex-mode-syntax-table and cperl-mode-syntax-table.