How to switch to a different buffer from a terminal buffer
Asked Answered
M

5

72

I've been using emacs for few weeks and it's been great so far - coming from vim was easier than I expected (actually - emacs' keyboard shortcuts feel more... natural).

I have added few customizations like moving between buffers using M-Left/Right/Up/Down because C-x o felt a little bit too slow when I had four files opened at once.

So far - so good :-)

One thing bugs me though:

  1. I open some splits using C-x 3 and C-x 2
  2. I open the terminal in one of them using M-x term ENT
  3. How do I switch to different split using keyboard?

Usual shortcuts obviously don't work - terminal is intercepting every emacs command, and I have to click on different buffer to activate it.

Mathis answered 31/1, 2010 at 21:12 Comment(7)
Upvoted, a saved sinner is especially welcome to the Church of Emacs.Means
You should consider using iswitch-buffer to change buffers rather than use multiple windows. It's vastly more productive.Mandie
@Noufal - by "windows" you mean actual application windows? Emacs terminology is sometimes quite confusing :-)Mathis
Emacs "windows" are the things you get when you do C-x 2 etc. The top level application windows are called "Frames" in emacs speak.Mandie
@NoufalIbrahim why not both? very often to work effectively, I need to see two files or pieces of code at the same time, two windows is very good for that. And then icomplete-mode to switch buffers (which replaced iswitch-buffer in 24.4: emacswiki.org/emacs/IswitchBuffers)Rustcolored
@lukaszkorecki, could you add your customization to use M-[left|right|up|down]?Bullbat
@ramy I asked that question almost 10 years ago :-) These days I use ace-window package for quickly jumping between visible buffersMathis
D
174

In term-mode, any regular C-x whatever keybinding becomes C-c whatever instead.

Dap answered 31/1, 2010 at 22:59 Comment(5)
This should get more upvotes - there are times when the difference between m-x term and m-x shell are required - for example, using the watch command to keep an eye on changing data (unless there is a better emacs way to do so).Carbineer
Very true, my current issue is that I've bound window switching to C-f/b/n/p but when I'm in term I can't switch out!Stancil
Does anyone know why this is the way it is? Why not just use C-x like usual (the stty -a command doesn't show a binding for C-x).Collectivize
Apologies for the downvote. I misread the answer and thought it was wrong. Unfortunately I realised it too late and the downvote is now locked.Cane
C-c shift-o did the trick for meCulminant
R
12

I'm not sure I understand your question. If you run M-x terminal, most of the key events are sent to the underlying terminal, so the standard C-x o binding and your M-Left are not available in the terminal.

Try using M-x shell to get a shell in one of the windows, and the navigation bindings you set up should still work.

Reckford answered 31/1, 2010 at 21:35 Comment(1)
Thanks - that solved my problem. I didn't know that there is a difference between M-x shell and M-x term.Mathis
Y
10

In term-mode, type C-c b RET to switch to some other buffer.

That does what C-x b RET normally does.

Yankeeism answered 31/1, 2010 at 23:18 Comment(0)
F
6

This should do the trick to get C-x b working. You may have to add bindings for any custom move commands.

(add-hook 'term-mode-hook
   (lambda ()
     ;; C-x is the prefix command, rather than C-c
     (term-set-escape-char ?\C-x)
     (define-key term-raw-map "\M-y" 'yank-pop)
     (define-key term-raw-map "\M-w" 'kill-ring-save)))

BTW, there is a big difference between shell-mode and term-mode. The former integrates better with emacs (e.g. cd command). The latter is a full terminal emulation and can handle curses programs. They both have their place.

Fog answered 27/2, 2010 at 21:7 Comment(0)
A
2

For a more generic answer dealing with emacs' windows, you can look at windmove, which started shipping with Emacs circa Emacs 22, I believe:

;;; Commentary:
;;
;; This package defines a set of routines, windmove-{left,up,right,
;; down}, for selection of windows in a frame geometrically.  For
;; example, `windmove-right' selects the window immediately to the
;; right of the currently-selected one.  This functionality is similar
;; to the window-selection controls of the BRIEF editor of yore.
;;
;; One subtle point is what happens when the window to the right has
;; been split vertically; for example, consider a call to
;; `windmove-right' in this setup:
;;
;;                    -------------
;;                    |      | A  |
;;                    |      |    |
;;                    |      |-----
;;                    | *    |    |    (* is point in the currently
;;                    |      | B  |     selected window)
;;                    |      |    |
;;                    -------------
;;
;; There are (at least) three reasonable things to do:
;; (1) Always move to the window to the right of the top edge of the
;;     selected window; in this case, this policy selects A.
;; (2) Always move to the window to the right of the bottom edge of
;;     the selected window; in this case, this policy selects B.
;; (3) Move to the window to the right of point in the selected
;;     window.  This may select either A or B, depending on the
;;     position of point; in the illustrated example, it would select
;;     B.
;;
;; Similar issues arise for all the movement functions.  Windmove
;; resolves this problem by allowing the user to specify behavior
;; through a prefix argument.  The cases are thus:
;; * if no argument is given to the movement functions, or the
;;   argument given is zero, movement is relative to point;
;; * if a positive argument is given, movement is relative to the top
;;   or left edge of the selected window, depending on whether the
;;   movement is to be horizontal or vertical;
;; * if a negative argument is given, movement is relative to the
;;   bottom or right edge of the selected window, depending on whether
;;   the movement is to be horizontal or vertical.
Ansilme answered 1/2, 2010 at 18:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.