Emacs 24 defines forward-whitespace
, which moves by whitespace-delimited words (i.e. anything but whitespace is considered a word constituent), but oddly not backward-whitespace
. It only treats space, tab and newline as whitespace, not other Unicode whitespace or characters defined as whitespace. When going forward, it moves to the beginning of the next word, not to the end of the current word.
(defun backward-whitespace (arg)
"Move point to the beginning of the current sequence of whitespace characters."
(interactive "^p")
(forward-whitespace (- arg)))
It isn't bound to any key by default.
(global-set-key "\M-B" 'backward-whitespace)
(global-set-key "\M-F" 'forward-whitespace)
If you prefer to rely on whitespace syntax, you can use these functions. The forward function moves to the end of the word. If you prefer to move to the beginning of the next word, swap the two calls to skip-syntax-forward
.
(defun forward-word-whitespace-syntax (arg)
"Move point forward ARG whitespace-delimited words.
Whitespace is defined as characters having the whitespace syntax,
plus newlines."
(interactive "p")
(with-syntax-table (make-syntax-table (syntax-table))
;; Always treat newlines as whitespace and not as e.g. comment ender
(modify-syntax-entry ?\n "-")
(while (> arg 0)
(skip-syntax-forward "-")
(skip-syntax-forward "^-")
(setq arg (1- arg)))
(while (< arg 0)
(skip-syntax-backward "-")
(skip-syntax-backward "^-")
(setq arg (1+ arg)))
t))
(defun backward-word-whitespace-syntax (arg)
"Move point backward ARG whitespace-delimited words.
Whitespace is defined as characters having the whitespace syntax,
plus newlines."
(interactive "^p")
(forward-word-whitespace-syntax (- arg)))
Current versions of Emacs don't have good support for Unicode categories, I don't know how to search for characters that are classified as whitespace in the buffer's encoding. I believe this is slated to improve in Emacs 25.
forward-same-syntax
instead offorward-word
.syntax-subword
package but it looks like it will get you close to what you want.