When I reposition the cursor in term-mode or ansi-term with M-left or M-right, it always returns to its original position (see animation below). Is this behavior standard? Is there a way to change it so that it behaves as it would in other buffers?
The problem here is that Emacs is moving the cursor position in the buffer, but no input is being sent to the process running in the terminal, meaning that the apparent state does not match the actual state.
This should be reported as a
term-char-mode bug, but you can fix it by binding the Ctrl- and Meta-modified left and right cursor keys in
term-raw-map to send the same escape codes that are sent when you type
M-f, which is pretty analogous to the behaviour you'd expect from those bindings.
Tangentially I also have some safety net code to try to prevent any scenario where one of the
yank commands is executed in
term-char-mode, because that would cause a similarly broken/confusing state; so I've included that code here for good measure.
(eval-after-load "term" '(progn ;; Fix forward/backward word when (term-in-char-mode). (define-key term-raw-map (kbd "<C-left>") (lambda () (interactive) (term-send-raw-string "\eb"))) (define-key term-raw-map (kbd "<M-left>") (lambda () (interactive) (term-send-raw-string "\eb"))) (define-key term-raw-map (kbd "<C-right>") (lambda () (interactive) (term-send-raw-string "\ef"))) (define-key term-raw-map (kbd "<M-right>") (lambda () (interactive) (term-send-raw-string "\ef"))) ;; Disable killing and yanking in char mode (term-raw-map). (mapc (lambda (func) (eval `(define-key term-raw-map [remap ,func] (lambda () (interactive) (ding))))) '(backward-kill-paragraph backward-kill-sentence backward-kill-sexp backward-kill-word bookmark-kill-line kill-backward-chars kill-backward-up-list kill-forward-chars kill-line kill-paragraph kill-rectangle kill-region kill-sentence kill-sexp kill-visual-line kill-whole-line kill-word subword-backward-kill subword-kill yank yank-pop yank-rectangle))))