Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Emacs + Tmux integrated window movement
;; Many thanks to the author of and contributors to the following posts:
;; https://gist.github.com/mislav/5189704
;; http://robots.thoughtbot.com/post/53022241323/seamlessly-navigate-vim-and-tmux-splits
;;
;; TODO: Make a script that generates tmux and emacs code without duplication
;;
;; NOTE: My keybindings are not the default emacs ones, using windmove
;; Try to move direction, which is supplied as arg
;; If cannot move that direction, send a tmux command to do appropriate move
(defun windmove-emacs-or-tmux(dir tmux-cmd)
(interactive)
(if (ignore-errors (funcall (intern (concat "windmove-" dir))))
nil ;; Moving within emacs
(shell-command tmux-cmd)) ;; At edges, send command to tmux
)
;Move between windows with custom keybindings
(global-set-key (kbd "M-P")
'(lambda () (interactive) (windmove-emacs-or-tmux "up" "tmux select-pane -U")))
(global-set-key (kbd "M-N")
'(lambda () (interactive) (windmove-emacs-or-tmux "down" "tmux select-pane -D")))
(global-set-key (kbd "M-F")
'(lambda () (interactive) (windmove-emacs-or-tmux "right" "tmux next-window")))
(global-set-key (kbd "M-B")
'(lambda () (interactive) (windmove-emacs-or-tmux "left" "tmux previous-window")))
;; Tmux code
;; bind -n M-B run "(tmux display-message -p '#{pane_current_command}' | grep -iq emacs && tmux send-keys M-B) || tmux previous-window"
;; bind -n M-F run "(tmux display-message -p '#{pane_current_command}' | grep -iq emacs && tmux send-keys M-F) || tmux next-window"
;; bind -n M-P run "(tmux display-message -p '#{pane_current_command}' | grep -iq emacs && tmux send-keys M-P) || tmux select-pane -U"
;; bind -n M-N run "(tmux display-message -p '#{pane_current_command}' | grep -iq emacs && tmux send-keys M-N) || tmux select-pane -D"
@daffes

This comment has been minimized.

Copy link

@daffes daffes commented Jan 13, 2014

Thanks, was looking for a function like windmove-emacs-or-tmux

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.