How to export a function from tmux.conf

I wrote an addon for tmux, tmux-gitbar . It shows some git state information of the current directory in the tmux status bar. The status bar should be updated every time the user executes a command , for this I added a function $PROMPT_COMMANDcalled from a variable .

What am I doing now:

in file : .bashrc

source ~/.tmux-gitbar/tmux-gitbar.sh;

in file : tmux-gitbar.sh

update_gitbar() {
    #
    # update tmux status bar
    #
}
PROMPT_COMMAND="update_gitbar; $PROMPT_COMMAND"


I would like to achieve the same result without adding a line to.bashrc . Everything should be done in a file tmux.conf. I already managed to change $PROMPT_COMMANDby doing this in tmux.conf:

PROMPT_COMMAND="update_gitbar; $PROMPT_COMMAND"

Now my problem is exporting update_gitbarto Wednesday

I tried various ways for the source of the script, but no luck:

run-shell "source ~/.tmux-gitbar/tmux-gitbar.sh"
run-shell "eval $(~/.tmux-gitbar/tmux-gitbar.sh)"
run-shell "eval $(cat ~/.tmux-gitbar/tmux-gitbar.sh)"

I always get:

update_gitbar: command not found

Question:

tmux.conf ( ) ?

, github

+4
3

run-shell . , , run-shell, / tmux.

script

, script . , update_gitbar, . , , , , , . ~/bin, , PROMPT_COMMAND be "$HOME/bin/update_gitbar; $PROMPT_COMMAND".

, – , bash - , ( "ShellShock"; : , , ), , .

tmux

- , , tmux, . tmux script, , :

export -f update_gitbar
tmux

, , , update_gitbar PROMPT_COMMAND. , tmux update_gitbar , – script. , (, tmux), , .

, , - tmux setenv VAR=value .tmux.conf. Bash " ", .

bash - "ShellShock", , () , , , (b) , , .

, , update_gitbar :

env | sed -n '/^[^=]*update_gitbar/,/^}/p' |
perl -ne 'chomp; $a .= $a? "; $_" : $_; END{$a=~s/=(.*)/='\''$1'\''/; print "$a\n"}'

. Bash , , . , . , , . - , .

, , Bash ( update_gitbar hello). , , .

, , .tmux.conf setenv.

hello() {
    echo hi;
}
export -f hello
hello
>>>> hi
env | sed -n '/^[^=]*hello/,/^}/p' |
perl -ne 'chomp; $a .= $a? "; $_" : $_; END{$a=~s/=(.*)/='\''$1'\''/; print "$a\n"}'
>>>> BASH_FUNC_hello%%='() {  echo hi\!; }'

( >>>> , script .)

+2

tmux set-environment . man tmux:

set-environment [-gru] [-t target-session] name [value]
              (alias: setenv)
        Set or unset an environment variable.
        If -g is used, the change is made in the global environment; 
        otherwise, it is applied to the session environment for target-session.
        The -u flag unsets a variable.
        -r indicates the variable is to be removed from the environment before starting a new process.

, set-environment -g PROMPT_COMMAND update_gitbar tmux-gitbar.conf, .tmux.conf source /path/to/tmux-gitbar.conf

EDIT: , update_gitbar exectuable - PATH.

P.S.

!: -)

+1

While I must point out that there are already viable answers, I want to mention another solution to your problem.

There, the mod adds full scripts to tmux: http://ershov.imtqy.com/tmux/ (I am the author).

It not only allows you to create new teams:

proc update_gitbar {} { run-shell "source ~/.tmux-gitbar/tmux-gitbar.sh" }

(for your case)

... but also allows you to associate several actions with pressing the "mode" keys.

It can also be used to write complete plugins in this language without having to open the shell.

+1
source

Source: https://habr.com/ru/post/1629685/


All Articles