Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying emacs forward-word / backward-ward behavior (to be like in vi/vim)

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.

like image 483
fikovnik Avatar asked Oct 14 '10 09:10

fikovnik


2 Answers

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)
like image 65
Trey Jackson Avatar answered Sep 27 '22 17:09

Trey Jackson


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.

like image 30
scottfrazer Avatar answered Sep 27 '22 17:09

scottfrazer