From 4c1fbe01f64b18736e499c7600e6a0776c2c3436 Mon Sep 17 00:00:00 2001 From: William Carroll Date: Wed, 3 Apr 2019 17:19:46 +0100 Subject: Fix $DISPLAY issues with Tmux Ensure that DISPLAY is set correctly so that commands executed in Tmux that require X windows don't disappear. --- configs/shared/zsh/.zshrc | 6 ++++++ configs/shared/zsh/functions.zsh | 15 +++++++++++++++ 2 files changed, 21 insertions(+) (limited to 'configs') diff --git a/configs/shared/zsh/.zshrc b/configs/shared/zsh/.zshrc index db60e3841059..b4ccd66a16c6 100644 --- a/configs/shared/zsh/.zshrc +++ b/configs/shared/zsh/.zshrc @@ -47,3 +47,9 @@ source "$DOTFILES/configs/shared/zsh/variables.zsh" source "$DOTFILES/configs/shared/zsh/aliases.zsh" source "$DOTFILES/configs/shared/zsh/functions.zsh" source "$DOTFILES/configs/shared/zsh/zle.zsh" + +preexec() { + # `preexec` runs before every command is run. + update_x11_forwarding +} + diff --git a/configs/shared/zsh/functions.zsh b/configs/shared/zsh/functions.zsh index 645426cd9044..c0f6e21e4613 100644 --- a/configs/shared/zsh/functions.zsh +++ b/configs/shared/zsh/functions.zsh @@ -202,6 +202,21 @@ kush() { } # Misc +update_x11_forwarding() { + # Sometime Tmux misbehaves with X11 applications (e.g. Emacs). This is because + # the DISPLAY variable is not set properly to `:0`. This function w + # Cache the DISPLAY when outside of Tmux. When inside of Tmux, use the cached + # value for DISPLAY. + # + # This cooperates with my `preexec` function, which runs before every command. + # Adapted from here: http://alexteichman.com/octo/blog/2014/01/01/x11-forwarding-and-terminal-multiplexers/ + if [ -z "$TMUX" ]; then + echo $DISPLAY > ~/.display.txt + else + export DISPLAY=$(cat ~/.display.txt) + fi +} + monzo_balance() { # Return the balance of my Monzo bank account. Intended to be used in my i3 # status bar. -- cgit 1.4.1