2

In emacs, you split a window with C-x 2 (one above the other) or C-x 3 (one next to the other).

How can I get the same key bindings in tmux?

Also, once a window is split, in emacs you cycle through them with C-x o. Can I configure tmux to use the same key?

2
  • Have you read the man page for tmux? It should be obvious what to do. Unfortunately if you do it, then C-x stops working as a prefix key in Emacs because tmux will interpret it instead.
    – Kyle Jones
    Nov 7, 2013 at 23:42
  • It would be nice if there were a tmux command I could use to disable those keys just for the one window in which I run emacs. Alternatively, I'd be happy to use C-b C-x 2 instead of the normal C-x 2 to split windows.
    – Frank
    Nov 8, 2013 at 23:42

3 Answers 3

2

Now, dreams become true: https://github.com/tmux/tmux/issues/2904

My use case is:

  • If a tmux pane is focused and emacsclient is not focused (eg. on termial prompt), use [C-x o] to switch to the next tmux pane on the same window.
  • If a tmux pane is focused and emacsclient is focused as well, then let emacs handles [C-x o].

tmux.conf:

is_emacs='echo "#{pane_current_command}" | grep -iqE "emacs"'
bind -Temacs-keys o if-shell "$is_emacs" "send C-x; send" "select-pane -t :.+"
bind -Temacs-keys Any { send C-x; send }
bind -Troot C-x switch-client -Temacs-keys

init.el:

;;; Tmux integration (See tmux.conf for details).
(defun id/other-window ()
  "Switch to the next window if opened, otherwise select next tmux pane."
  (interactive)
  (if (eq (next-window) (get-buffer-window))
      (call-process "tmux" nil nil nil "select-pane" "-t" ":.+")
    (other-window 1)))

(global-set-key [remap other-window] #'id/other-window)
0

I am using this tmux conf to emulate the same behavior with splits. Is uses the same shortcuts to split into different panes the window I set the prefix to C-x, and the behavior to select the panes is similar than dealing with buffers in emacs. Give it a try and modify it yourself to feel more comfortable.

# remap prefix from 'C-b' to 'C-x'
unbind C-b
set-option -g prefix C-x
bind-key C-x send-prefix

# split panes using 3 and 2
bind 3 split-window -h
bind 2 split-window -v
unbind '"'
unbind %

# switch panes using Alt-arrow without prefix
bind Left select-pane -L
bind Right select-pane -R
bind Up select-pane -U
bind Down select-pane -D

# Enable switch session similar than emacs selecting buffer
unbind s
bind b choose-tree -w

# Kill window
bind k confirm kill-window

# Source the conf file
bind r source-file ~/.tmux.conf

# Use C-x 0 to close panel, similar than emacs when closing the splitted window
unbind 0
bind 0 kill-pane
-1

Have a look at the must read at http://pragprog.com/book/bhtmux/tmux; you'll get anything you need Further on, try out this:


# Setting the prefix from C-b to C-d 
# START:prefix
set -g prefix C-d 
# END:prefix 

# Ensure that we can send Ctrl-A to other apps 
bind C-d send-prefix 

# Free the original Ctrl-b prefix keybinding unbind C-b
unbind C-b

bind | split-window -h
bind _ split-window -v

And this for moving between panes:

bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

Both from the previous reference and the man page.

4
  • Much better now, thanks @Jason, I'll do my best next time.
    – csantosb
    Nov 23, 2013 at 21:58
  • Sorry, I don't understand how this answers my questions and would give me C-x 2 and C-x 3 for splitting?
    – Frank
    Nov 24, 2013 at 6:16
  • Put this code in your config file along with whatever you need
    – csantosb
    Nov 25, 2013 at 16:23
  • 2
    @csantosb - I don't think those keys are what @frank is looking for. Emacs splits with Ctrl-x 2, and switches with Ctrl-x o. Aug 10, 2015 at 21:52

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.