Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Emacs Forward-Word Behaviour

Tags:

emacs

emacs23

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.

like image 636
gvkv Avatar asked Nov 20 '09 15:11

gvkv


3 Answers

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.

like image 86
scottfrazer Avatar answered Nov 19 '22 10:11

scottfrazer


See forward-same-syntax function also. probably this is you needed to base on.

like image 32
Alex Avatar answered Nov 19 '22 09:11

Alex


I have a minor mode that changes word-based commands to operate on syntax changes (and also CamelCaseSubwords). It may be a bit too fine-grained for some tastes, but I find I basically ever use single character movement anymore.

https://bitbucket.org/jpkotta/syntax-subword

like image 24
jpkotta Avatar answered Nov 19 '22 11:11

jpkotta