Last active
December 5, 2022 23:14
-
-
Save nathanlippi/5923326 to your computer and use it in GitHub Desktop.
Emacs + Tmux integrated window movement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, was looking for a function like windmove-emacs-or-tmux