How to indicate that shell session is running inside Vifm

From Vifm Wiki
Jump to navigation Jump to search

The idea is to make shell indicate whether it was run inside by Vifm and exiting it will bring Vifm back or not.

Variant #1. Demonstrated using zsh[edit]

Add the following to .zshrc:

if [ $(ps -ocommand= $(ps -oppid= -p $$)) = "vifm" ]
then
        printf "\e[48;5;21m"
        echo "-- VIFM SHELL SESSION --"
        export PROMPT="-- VIFM_SESSION --"$'\n'"${ret_status} %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)"
fi

It will look similar to this:

-- VIFM SHELL SESSION --
-- VIFM_SESSION --
➜  ~ uname 
Linux
-- VIFM_SESSION --

Variant #2. Demonstrated using bash[edit]

To be added to vifmrc:

let $INSIDE_VIFM = 'true'

In .bashrc :

if [ -n "$INSIDE_VIFM" ]; then
    PS1="[V]$PS1"
    unset INSIDE_VIFM
fi

Will look similar to this:

[V][~]$

Variant #3. Demonstrated using fish[edit]

To be added to vifmrc:

let $INSIDE_VIFM = 'true'

In .config/fish/functions/ create a file check_if_inside_vifm.fish and paste the following:

status is-interactive || exit
functions -c fish_prompt original_fish_prompt
function fish_prompt
# prepend fish_prompt with a custom message if running inside VIFM 
  if [ "$INSIDE_VIFM" = "true" ]
    set_color brgreen
    echo "Running inside VIFM, hit Ctrl-D to exit"
  end
original_fish_prompt
end

Drawbacks[edit]

Environment variable might "leak" to some child process, which will later spawn a shell and trick it into reporting as if it's inside Vifm.

Variant #4. fish shell[edit]

Make a fish function like this and name it whatever you want. In this case, the function is named and can be called with v. Define it using the following code, substituting vifmcd for vicd or whatever your change directory function is. This is (probably) not susceptible to the env leak mentioned in the previous solution.

function v --wraps=vifmcd --description 'alias v=vifmcd'
    set term_no (basename $(tty))
    set current_vifm_count (pgrep vifm -ct pts/$term_no)
    if test $current_vifm_count -ge 1
        echo "vifm is already running"
    else
        vifmcd $argv
    end
end

It pgreps the vifm instances running in the current terminal session and checks to see if the returned value is greater than 1. If it is, it will echo and refuse to start vifm. If not, it'll run vifmcd (or whatever wrapper you want).

Links[edit]

Vifm Q&A question