I have a text like this,

testStatus['id'] = elt.$.id^^''&&&&
testStatus$$['suite_id'] = ""

where ^^ is my cursor position. When I press M-f, it uses forward-word and end up in the place $$ in line number two.

How can I force M-f to treat newline characters as a word seperator and just stop at the end of line number one.

This works fine, when the last part of line one has some alphabets.

testStatus['id'] = elt.$.id^^''&&&&abcd$$
testStatus['suite_id'] = ""

where ^^ is my initial cursor position and $$ is my new cursor position.

  • & is not in a word syntax! When you want Emacs to go to the end of a line why don't you just command it with <kbd>C-C-e</kbd>? Sep 14 '15 at 4:59
  • 2
    What not using forward-sexp instead of forward-word?
    – Name
    Sep 15 '15 at 12:31

Look before you leap!

You can write a custom function which will look for non word characters followed by a new line before next word. On such condition go to end of line other wise run normal word forward.

(defun my-forward-word ()
  (if (looking-at "\\W+\n")
      (when (search-forward-regexp "\n" nil 'noerror)
        (goto-char (match-beginning 0)))

(global-set-key (kbd "M-f") 'my-forward-word)


This can be simplified futher using forward-sexp as mentioned by @Name

(defun my-forward-word ()
  (if (looking-at "\\W+\n")

(global-set-key (kbd "M-f") 'my-forward-word)
  • anand, one closing paren at my-forward-word's end is missing
    – Madhavan
    Sep 15 '15 at 14:35

Oh , if you are using evil-mode you can just add this line to your ~/.emacs or ~/.emacs.d/init.el

(setq-default evil-cross-lines nil)

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.