Here's the situation. I start M-x ansi-term, which launches it in term-char-mode. I type the following:

echo "hello world"

and press RET, which outputs on the next line the expected output hello world. Now I press C-p which brings up the previous statement that I just typed (echo "hello world" in this example) and I place the cursor with repeated C-b just before the word world (in other words before w). Now I do M-x term-line-mode and I do some edits to the command. Weird stuff happens at this point. Sometimes the edits are not registered after I go back to M-x term-char-mode and execute the edited command, sometimes the text jumbles, etc. A screencast shows these possibilities: https://i.sstatic.net/wVGxk.jpg.

Here is my setup in ~/.emacs.d/init.el related to ansi-term:

;;;;;; Improving ansi-term
;; make that C-c t e launches an ansi-term buffer in the current window
(global-set-key (kbd "C-c t e") 'ansi-term)
;; avoid ansi-term asking always which shell to run (always run bash)
(defvar my-term-shell "/bin/bash")
(defadvice ansi-term (before force-bash)
  (interactive (list my-term-shell)))
(ad-activate 'ansi-term)
;; display of certain characters and control codes to UTF-8
(defun my-term-use-utf8 ()
  (set-buffer-process-coding-system 'utf-8-unix 'utf-8-unix))
(add-hook 'term-exec-hook 'my-term-use-utf8)
;; clickable URLs
(defun my-term-hook ()
(add-hook 'term-mode-hook 'my-term-hook)
;; make that typing exit in ansi-term (which exits the shell) also
;; closes the buffer
(defadvice term-sentinel (around my-advice-term-sentinel (proc msg))
  (if (memq (process-status proc) '(signal exit))
      (let ((buffer (process-buffer proc)))
        (kill-buffer buffer))
(ad-activate 'term-sentinel)
;; fix the problem of moving cursor left/right being captured by emacs
;; instead of underlying terminal, leading to jumbling when jumping
;; words then editing the middle of a command. Same for deleting
;; backward/forward whole words.
(eval-after-load "term"
     ;; rename buffer
     (define-key term-raw-map (kbd "C-c r") 'rename-buffer)

     ;; make sure typical key combos work in term-char-mode
     (define-key term-raw-map (kbd "M-x") 'nil)
     (define-key term-raw-map (kbd "M-&") 'nil)
     (define-key term-raw-map (kbd "M-!") 'nil)

     ;; make sure C-c t e launches a new ansi-term buffer when current
     ;; buffer is also ansi-term
     (define-key term-raw-map (kbd "C-c t e") 'nil)

     ;; move by whole words fix
     (define-key term-raw-map (kbd "C-<right>") 'term-send-Cright)
     (define-key term-raw-map (kbd "C-<left>") 'term-send-Cleft)
     (defun term-send-Cright () (interactive) (term-send-raw-string "\e[1;5C"))
     (defun term-send-Cleft  () (interactive) (term-send-raw-string "\e[1;5D"))
     (defun term-send-Mbackspace () (interactive)(term-send-raw-string "\e\d"))

     ;; word deletion fix
     (define-key term-raw-map (kbd "C-w") 'term-send-Mbackspace)
     (define-key term-raw-map (kbd "M-<backspace>") 'term-send-Mbackspace)     

     ;; switch between char and line mode with logical keystrokes
     (add-hook 'term-mode-hook (lambda () (local-set-key (kbd "C-c t c") 'term-char-mode)))
     (define-key term-raw-map (kbd "C-c t l") 'term-line-mode)

     ;; copy/paste native Emacs keystrokes
     (define-key term-raw-map (kbd "C-k") 'term-send-raw)
     (define-key term-raw-map (kbd "C-y") 'term-paste)

     ;; ensure that scrolling doesn't break on output
     ;;(setq term-scroll-show-maximum-output t)
     (setq term-scroll-to-bottom-on-output t)

     ;; max history (# lines) to keep (0 == keep everything)
     (setq term-buffer-maximum-size 50000)))
;; ansi-term bi-directional text support problem fix, which seems to
;; be the cause of text jumbling when going back commands in
;; ansi-term. This fixes it, yay!
(add-hook 'term-mode-hook 'my-term-mode-hook)
(defun my-term-mode-hook ()
  ;; https://debbugs.gnu.org/cgi/bugreport.cgi?bug=20611
  (setq bidi-paragraph-direction 'left-to-right))

Please help me to resolve this issue.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.