Welcome to Vifm Q&A, where you can ask questions about using Vifm. Registration is optional, anonymous posts are moderated. E-mail and GitHub logins are enabled.
0 votes
in vifm by

I would like to set a global variable from vifm, to use it in a shell once exit from vifm, is that feasible?

1 Answer

0 votes
by
selected by
 
Best answer

Environment variables don't work like that. Process sets them for itself and child processes it creates afterward inherit them, you can't set environment variables of other processes explicitly.

It should be possible to write some variable to a file before exiting Vifm and then load it from your shell, but you'll have to use a wrapper function in the shell to then process the file. It's not clear if it fits your use case, what are you trying to achieve?

by

thank you xaizek for clearing that out to me, I'm trying to create a vifm command that exit vifm to parent shell and execute cd in that shell to the vifm path before exit. I would use a function in my .zshrc file with an if statement to start vifm. It's something similar to this but it fires only on command and not on every exit

by

"Prior to 0.8" section there shows how to do it. Need to check whether the file is empty and might make it work with multiple instances along the way.

Vifm side:

command! Q :execute '!echo %d %n > "$SHELL_CD"' | quit

Shell side:

vifm()
{
    local tmp_file="$(mktemp /tmp/vifm-shell-cd.XXXXXX)"
    # "command" prevents recursive call
    SHELL_CD="$tmp_file" command vifm "$@"
    if [ -s "$tmp_file" ]; then
        cd "$(cat "$tmp_file")"
    fi
    rm "$tmp_file"
}
by

that's flawless, really cannot thank you enough <3

by

how do I manage the exception where the directory name contains a single quote?

by

I wrote a bad command, it should have been this (to delay expansion of macros):

command! Q :execute '!echo %%d %%n > "$SHELL_CD"' | quit
by

no worries, your help is always top tier, thank you

...