Modifying emacs forward-word / backward-ward behav

2020-02-26 07:56发布

What would be the easiest way to have the same kind of behavior that is in vim for the word back and forth navigation? In vim when you press "w" it moves a cursor forward one word, where word consists of a sequence of letters, digits and underscores, or a sequence of other non-blank characters, separated with white space (spaces, tabs, eol). In emacs on the other hand it skips until the end of the next word and the word is defined per mode in the syntax table.

For example: having a cursor at the beginning of the line following shows where vim put a cursor when you do forward-word ("w") operation:

opt1.arg = opt2.arg
^   ^^   ^ ^   ^^  ^

In emacs it is like:

opt1.arg = opt2.arg
^   ^   ^      ^   ^

It really depends on a one's preference, but I tend to like the vim style better and I was wondering what is the easiest way to have the same in emacs. I guess I'm not alone who switched from vim to emacs so perhaps someone already has a solution, ideally for the kill-word and backward-kill-word as well :)

I know you can get something similar by combination of M-f, M-b etc., but that is not the point. I also don't want to start a discussion which approach is better - the topis is well discussed in here.

3条回答
姐就是有狂的资本
2楼-- · 2020-02-26 08:35

I don't know why jpkotta's answer was deleted, but here it is again:

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

@ mods, I don't know why this answer would be deleted, so if you choose to delete this answer too, I'd appreciate an explanation.

查看更多
▲ chillily
3楼-- · 2020-02-26 08:41

Mostly a duplicate of this, which says:

(require 'misc)

Then bind whatever keys you want to forward-to-word and backward-to-word. For killing, create some simple functions that wrap these functions and do kill.

查看更多
Root(大扎)
4楼-- · 2020-02-26 08:50

You can actually use 'viper-forward-word

(require 'viper)
(global-set-key (kbd "M-f") 'viper-forward-word)
(global-set-key (kbd "M-b") 'viper-backward-word)
查看更多
登录 后发表回答