8

For some tasks like using ruby's pry and tmux both eshell and shell do not behave well. I tried to use term, but my innability to override the keybindings in term makes its use annoying.

How can I set, say, M-o as other-window and M-k as kill-buffer when using term ?

Renan Ranelli
  • 1,379
  • 10
  • 17

2 Answers2

7

In term-mode, any regular C-x whatever keybinding becomes C-c whatever instead. Courtesy of Josh Matthews

Now to define keybindings that will be active only in term mode, try this:

(add-hook 'term-mode-hook
  (lambda () 
    (define-key term-raw-map (kbd "M-J") 'other-window)
    (define-key term-raw-map (kbd "M-k") 'kill-buffer)))
Nsukami _
  • 6,341
  • 2
  • 22
  • 35
  • Nice, It solves my issue. I'm accepting @Sigma's answer because it is slightly more convenient to configure (no need to duplicate my global-map bindings in the term-raw-map). Thank you! – Renan Ranelli Sep 27 '14 at 02:48
  • 1
    @LeMeteore putting these calls in `term-mode-hook` is highly likely not useful: you will end up reinstalling those keys every time the mode is activated, instead of doing it only once. – Sigma Sep 27 '14 at 03:30
  • @Sigma you're right. I was in fact trying to quickly find a solution. Your answer is indeed more convenient. – Nsukami _ Sep 27 '14 at 08:19
7

maybe using ansi-term instead would be slightly less annoying, since it exposes a C-x prefix, allowing you to use, say, C-x o or C-x k without any special trick.

If that's not sufficient, you can use the following approach:

(defun expose-global-binding-in-term (binding)
   (define-key term-raw-map binding 
     (lookup-key (current-global-map) binding)))

(expose-global-binding-in-term (kbd "M-o"))
(expose-global-binding-in-term (kbd "M-k"))
...
Sigma
  • 4,510
  • 21
  • 27