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 ?

  • 6
    My keybindings work in term but only in line mode C-c C-j. I just toggle char mode C-c C-k whenever necessary. – Vamsi Sep 26 '14 at 17:59
  • I didn't knew about C-c C-j and C-c C-k! Thanks! – Renan Ranelli Sep 27 '14 at 2:46

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"))

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)))
  • 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 2: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 3: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 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.