4

When I move around specifically using M-f/b, I want to treat any symbol such as

", ', ., -, _, ?, !, *... 

as one word, so M-f/b will move around them instead of skipping them and jump to the next/previous word.

Is there any variable builtin to do that? If no, how can I do that?

Malabarba
  • 22,878
  • 6
  • 78
  • 163
CodyChan
  • 2,599
  • 1
  • 19
  • 33
  • This is somewhat similar to [this question](http://emacs.stackexchange.com/questions/10644/backward-kill-word-ignores-whitespaces-is-there-another-friendlier-version/10646#10646). Try using `forward-same-syntax` instead of `forward-word`. –  May 27 '15 at 08:42
  • Here is my own movement functions for left/right word/entity which use a custom syntax-table temporary for only the duration of the function: http://stackoverflow.com/q/18675201/2112489 Perhaps you might be able to modify it to suit your needs. – lawlist May 27 '15 at 08:45
  • 3
    I haven't tried the [`syntax-subword`](http://melpa.org/#/syntax-subword) package but it looks like it will get you close to what you want. – Kaushal Modi May 27 '15 at 13:20
  • Thanks @kaushalmodi [syntax-subword](http://melpa.org/#/syntax-subword) is great, it is exactly what I need. – CodyChan May 28 '15 at 02:01
  • I can confirm syntax-subword do what OP asked for. – Manuel Uberti May 30 '15 at 06:57

3 Answers3

5

Since the answer is picked from the comment from @kaushalmodi, I cannot choose the comment as an answer, so I post this as the right answer.

According to comment from @kaushalmodim, syntax-subword is great, it is exactly what I need. You can install it using package.el.

Here is my configuration in init.el:

(global-syntax-subword-mode)
(setq syntax-subword-skip-spaces t)
CodyChan
  • 2,599
  • 1
  • 19
  • 33
3

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.

Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
0

Try superword-mode. This won't take you inside symbols like long-function-name though, it will skip over the whole thing.

Kevin Holmes
  • 164
  • 3