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?

  • This is somewhat similar to this question. Try using forward-same-syntax instead of forward-word. – rekado May 27 '15 at 8: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: stackoverflow.com/q/18675201/2112489 Perhaps you might be able to modify it to suit your needs. – lawlist May 27 '15 at 8:45
  • 3
    I haven't tried the 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 is great, it is exactly what I need. – CodyChan May 28 '15 at 2:01
  • I can confirm syntax-subword do what OP asked for. – Manuel Uberti May 30 '15 at 6:57
4

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)
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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.