9

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 ?

2
  • 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
    Commented Sep 26, 2014 at 17:59
  • I didn't knew about C-c C-j and C-c C-k! Thanks! Commented Sep 27, 2014 at 2:46

2 Answers 2

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"))
...
0
8

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)))
4
  • 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! Commented Sep 27, 2014 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
    Commented Sep 27, 2014 at 3:30
  • @Sigma you're right. I was in fact trying to quickly find a solution. Your answer is indeed more convenient.
    – Nsukami _
    Commented Sep 27, 2014 at 8:19
  • Good point on using term-raw-map instead of term-mode-map as it's the default keymap (alternatively, a user may put keybindings to both maps). It isn't obvious and for some reason I didn't find this answer till I wrote a question and almost sent it. Anyway, I sent fixes to the term-mode documentation, so hopefully it would be less of a problem for future users.
    – Hi-Angel
    Commented Mar 16 at 11:50

Your Answer

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

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