jcubic
jcubic

Reputation: 66488

How to send <C-left> into Emacs term?

I use this function to send raw commands to terminal:

(defun raw (str)
  (interactive "sKey: ")
  (term-send-raw-string (read-kbd-macro str)))

But read-kbd-macro for <C-left> return [C-left] which is not a string.

I've also try:

(term-send-raw-string "\C-\eOD")

and

(define-key term-raw-map (kbd "<C-left>") 'term-send-raw)

But those also doesn't work.

How can I send C-left then?

Upvotes: 4

Views: 390

Answers (1)

I have the following snippet in my setup file, for the exact same purpose as you: move by words on the bash prompt using C-<arrows>

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

I found the \e[1;5C and \e[1;5D codes using the following trick:

  1. run cat >/dev/null in a terminal
  2. type C-<left> and C-<right> and see what is echoed back in the terminal
  3. exit with C-d or C-c

Another way to find them would be to type in a terminal: C-vC-<left>

Upvotes: 4

Related Questions