tags:

views:

169

answers:

5

I don't know if there's something wrong with my settings but when I press M-f (forward a word) it doesn't matter where I am, it never place the cursor in the next word (just between words). This doesn't happen with M-b which place my cursor in the beginning of the previous word.

Is this a normal behavior? How do I place my cursor at the beginning of the following word?

+1  A: 

That is correct behavior. According to the Emacs manual, "[f]orward motion stops right after the last letter of the word, while backward motion stops right before the first letter."

Why is it this way? Perhaps to be consistent with kill-word (M-d).

Vebjorn Ljosa
+2  A: 

Ok, just so we're clear, Im going to assume you are talking about the commands forward-word and backward-word these are bound by default to Alt+f and Alt+b

eg string: "Hello dolly I am here"

If your cursor is on the "H" of "Hello", and you do forward-word the cursor will move to the space between "Hello" and "dolly", but it sounds like you want the cursor to be on the letter "d" of "dolly" instead of in-front of it.

So, do forward-word twice, then backward-word once.

That will put your cursor on the "d" of "dolly".

This can be automated with a macro.

;; = comments, do not type them

Ctrl+x ( ;;start macro

Alt+f Alt+f Alt+b

Ctrl+x ) ;;end macro

Then to run last defined macro do this:

Ctrl+x e

EDIT: as pascal mentioned in a comment, this can also just be done with

Alt+f Ctrl+f

You could put that into a macro as well, either way the result is the same.

AlexCombas
Thanks. I guess I'm still suffering from post-Vim trauma (w and b keys)
janoChen
+2  A: 

The macro solution described is a great way to get this behavior in a session, but it's a little inconvenient if that's the default behavior you want, since you have to define it every time you start emacs. If you want M-f to work like this all the time, you can define an elisp function and bind it to the key. Put this in your .emacs file:

(defun next-word (p)
   "Move point to the beginning of the next word, past any spaces"
   (interactive "d")
   (forward-word)
   (forward-word)
   (backward-word))
(global-set-key "\M-f" 'next-word)
John Nienart
A: 

Try something like following:

;; replace common word-operations on same-syntax-operations

(require 'thingatpt)

(global-set-key "\M-f" 'forward-same-syntax)

(global-set-key "\M-b" (lambda() (interactive) (forward-same-syntax -1) ))

(defun kill-syntax (&optional arg) "Kill ARG sets of syntax characters after point." (interactive "p") (let ((opoint (point))) (forward-same-syntax arg) (kill-region opoint (point))) ) (global-set-key "\M-d" 'kill-syntax)

(global-set-key [(meta backspace)] (lambda() (interactive) (kill-syntax -1) ) )

Alex
A: 

Moving forward twice and backwards once is fine unless you are at the beginning of a line with spaces in the front. Then going forward twice and back once will move you to the next word not the first word. The code below will mimic vi's "w" command perfectly. I've written this quite fast so this code can be cleaned up further.

(defun forward-word-to-beginning (&optional n)
  "Move point forward n words and place cursor at the beginning."
  (interactive "p")
  (let (myword)
    (setq myword
      (if (and transient-mark-mode mark-active)
        (buffer-substring-no-properties (region-beginning) (region-end))
        (thing-at-point 'symbol)))
    (if (not (eq myword nil))
      (forward-word n))
    (forward-word n)
    (backward-word n)))

(global-set-key (kbd "M-C-f") 'forward-word-to-beginning)
arcanereinz