gpakosz / .tmux

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

What is needed to extract specific functions from your config? #576

Closed CharlesARoy closed 2 years ago

CharlesARoy commented 2 years ago

Hello,

I was hoping to only add the _toggle_mouse() function from your config to mine.

I set this first in my .tmux.config file:

# : << EOF

and this at the end of it, but that doesn't seem to be enough to get the function working. What am I missing?

# toggle mouse
bind m run "cut -c3- ~/.tmux.conf | sh -s _toggle_mouse"

# EOF
# _toggle_mouse() {
#   old=$(tmux show -gv mouse)
#   new=""
#
#   if [ "$old" = "on" ]; then
#     new="off"
#   else
#     new="on"
#   fi
#
#   tmux set -g mouse $new
# }

Thanks for coming up with this, and for your help!

gpakosz commented 2 years ago

Hello @CharlesARoy 👋

You're missing a tiny bit to achieve transforming your .tmux.conf into a tmux/sh polyglot. You picked the first line of Oh my tmux! .tmux.conf: # : << EOF correctly. But you missed the last one: # "$@"

What does the first line do? For tmux, it's a comment and it's ignored, as everything after a # character on each line. Then when you do cut -c3- ~/.tmux.conf to remove the first 3 characters, and you pipe that into sh, the first line becomes : << EOF and it means "swallows and ignores the following heredoc up to the EOF string".

What does the last line do? The last line becomes "$@" which executes what's given to sh -s, in your case it will call the _toggle_mouse() function.

All in all you need

# : << EOF

... your own conf here ...

# toggle mouse
bind m run "cut -c3- ~/.tmux.conf | sh -s _toggle_mouse"

... and maybe also here ...

# EOF
# _toggle_mouse() {
#   old=$(tmux show -gv mouse)
#   new=""
#
#   if [ "$old" = "on" ]; then
#     new="off"
#   else
#     new="on"
#   fi
#
#   tmux set -g mouse $new
# }
# "$@"
CharlesARoy commented 2 years ago

@gpakosz Thank you so much for the response and for the detailed explanation, I really really appreciate it! This approach to managing the config is really ingenious, well done.

This function will make copying text inside tmux so much easier! Goodbye screenshot OCR to clipboard tools 😅

gpakosz commented 2 years ago

For the record you can also do

bind m run 'tmux set -g mouse $(tmux show -gv mouse | perl -p -e "s/off/on/g || s/on/off/g")'

And avoid the whole polyglot dance