How to set shell working directory after leaving Vifm

From Vifm Wiki
Jump to navigation Jump to search

Vifm can't do that on its own, since it's impossible to change working directory of another process (parent shell in this case). Still, it's possible to do this by relying on a shell to do part of the job.

Below is an example, how one can do that with bash shell.

Since 0.8[edit]

  • For bash add this to .bashrc:
vicd()
{
    local dst="$(command vifm --choose-dir - "$@")"
    if [ -z "$dst" ]; then
        echo 'Directory picking cancelled/failed'
        return 1
    fi
    cd "$dst"
}
  • For fish:
function vicd
    set dst "$(command vifm --choose-dir - $argv[2..-1])"
    if [ -z "$dst" ]; 
        echo 'Directory picking cancelled/failed'
        return 1
    end
    cd "$dst"
end
  • For Powershell add to $PROFILE:
function vicd {
    $path = $(vifm --choose-dir - .)
    if ($path) { Set-Location "$($path)" }
}

Then in Vifm started via vicd:

  • Use :q (:qall, ZZ, ZQ, etc.) to set the new current working directory.
  • Use :cq[uit] to leave the shell's current working directory unchanged.

Prior to 0.8[edit]

  • Add special command to vifmrc
command! Q :execute '!echo %d > ~/.vifm/lastdir' | quit
  • Add this function to .bashrc
vifm()
{
    if [ -f ~/.vifm/lastdir ]; then
        rm ~/.vifm/lastdir
    fi
    # "command" prevents recursive call
    command vifm "$@"
    if [ -f ~/.vifm/lastdir ]; then
        cd `cat ~/.vifm/lastdir`
    fi
}

Now running Vifm via that shell function and quiting it using :Q command (which can be to ZZ and/or ZQ) will change directory of shell to the directory of current active pane.

NOTE If your viminfo string contains 'savedirs', your next vifm session will start from the last vifm directory regardless of $PWD.