gpakosz / .tmux

🇫🇷 Oh my tmux! My self-contained, pretty & versatile tmux configuration made with ❤️
MIT License
22.03k stars 3.37k forks source link

Difficulty adding variable to status bar #465

Closed artis3n closed 3 years ago

artis3n commented 3 years ago

I added a vpn variable in .tmux.conf.local (note the vpn function):

# -- custom variables ----------------------------------------------------------

# to define a custom #{foo} variable, define a POSIX shell function between the
# '# EOF' and the '# "$@"' lines. Please note that the opening brace { character
# must be on the same line as the function name otherwise the parse won't detect
# it.
#
# then, use #{foo} in e.g. the 'tmux_conf_theme_status_left' or the
# 'tmux_conf_theme_status_right' variables.

# # /!\ do not remove the following line
# EOF
#
# # /!\ do not "uncomment" the functions: the leading "# " characters are needed
#
# vpn() {
#   [ -d /proc/sys/net/ipv4/conf/tun0 ] && printf "VPN $({ ip -4 -br a sh dev tun0 | awk {'print $3'} | cut -f1 -d/; } 2>/dev/null)"
# }
#
# weather() {
#   curl -m 1 wttr.in?format=3 2>/dev/null
#   sleep 900 # sleep for 15 minutes, throttle network requests whatever the value of status-interval
# }
#
# online() {
#   ping -c 1 1.1.1.1 >/dev/null 2>&1 && printf '✔' || printf '✘'
# }
#
# "$@"
# # /!\ do not remove the previous line

Trying to replicate this output from my terminal to the tmux session, but am rather new at this:

└─$ [ -d /proc/sys/net/ipv4/conf/tun0 ] && printf "VPN $({ ip -4 -br a sh dev tun0 | awk {'print $3'} | cut -f1 -d/; } 2>/dev/null)"

VPN 10.10.14.112

I've tried adding the variable to tmux_conf_theme_status_left and right but I don't see my expected output:

tmux_conf_theme_status_left=" ❐ #S | ↑#{?uptime_y, #{uptime_y}y,}#{?uptime_d, #{uptime_d}d,}#{?uptime_h, #{uptime_h}h,}#{?uptime_m, #{uptime_m}m,} | #{vpn} "

I'm new to using tmux beyond basic commands and customizing my tmux session so I'm sure I'm doing something wrong - just not sure what.

gpakosz commented 3 years ago

Hello @artis3n 👋

Well first you can make sure your function works by doing

$ cut -c3- ~/.tmux.conf.local | sh -sx vpn

Then did you make sure you reloaded the tmux configuration? Finally, after reloading the tmux configuration can you please paste the output of

$ tmux show -gv status-left

or

$ tmux show -gv status-right

depending on whether you used #{vpn} in tmux_conf_status_left or tmux_conf_status_right?

artis3n commented 3 years ago

Looks like it does work:

└─$ cut -c3- ~/.tmux.conf.local | sh -sx vpn
+ :
+ vpn
+ [ -d /proc/sys/net/ipv4/conf/tun0 ]
+ printf VPN 10.10.14.112
VPN 10.10.14.112

I do not see it (I think?) in status-left, using the format I have in the description:

└─$ tmux show -gv status-left
#[fg=#080808,bg=#ffff00,bold] ❐ #S #[fg=#ffff00,bg=#ff00af,none]#[fg=#e4e4e4,bg=#ff00af,none] ↑#{?@uptime_y, #{@uptime_y}y,}#{?@uptime_dy, #{@uptime_dy}d,}#{?@uptime_h, #{@uptime_h}h,}#{?@uptime_m, #{@uptime_m}m,} #[fg=#ff00af,bg=#080808,none]
gpakosz commented 3 years ago

Which commit of Oh my tmux! are you using btw?

gpakosz commented 3 years ago

And does your ~/.tmux.conf.local file start with

# : << EOF
# https://github.com/gpakosz/.tmux
# (‑●‑●)> dual licensed under the WTFPL v2 license and the MIT license,
#         without any warranty.
#         Copyright 2012— Gregory Pakosz (@gpakosz).

? The important line is

# : << EOF

Then in your ~/.tmux.conf.local file you should have

# # /!\ do not remove the following line
# EOF
#
# # /!\ do not "uncomment" the functions: the leading "# " characters are needed
#
# vpn() {
#   [ -d /proc/sys/net/ipv4/conf/tun0 ] && printf "VPN $({ ip -4 -br a sh dev tun0 | awk {'print $3'} | cut -f1 -d/; } 2>/dev/null)"
# }
#
# "$@"
# # /!\ do not remove the previous line
artis3n commented 3 years ago

Yes - proof in description for how I added the vpn() function and the top of the file is unchanged, confirmed it has the # : << EOF line and the github license section.

artis3n commented 3 years ago

... I figured starting tmux new would be sufficient to apply the config. Misunderstood what you meant by reload the tmux configruation.

I ran tmux source-file ~/.tmux.conf and now the VPN function is applying correctly in my status bar. Thanks for your help :)

gpakosz commented 3 years ago

You can also use <prefix> + r to reload the configuration