Difference between revisions of "How to set shell working directory after leaving Vifm"

From Vifm Wiki
Jump to: navigation, search
m (Add ! to command definition)
m (there was a typo where you had Vifm when you defined the function to be vicd :) cheers)
 
(5 intermediate revisions by one other user not shown)
Line 3: Line 3:
 
by relying on a shell to do part of the job.
 
by relying on a shell to do part of the job.
  
Here is an example, how one can do that with
+
Below is an example, how one can do that with
[https://en.wikipedia.org/wiki/Bash_(Unix_shell) bash]:
+
[https://en.wikipedia.org/wiki/Bash_(Unix_shell) bash] shell.
 +
 
 +
==== Since 0.8 ====
 +
 
 +
* Add this function to <code>.bashrc</code>
 +
<source lang="bash">
 +
vicd()
 +
{
 +
    local dst="$(command vifm --choose-dir - "$@")"
 +
    if [ -z "$dst" ]; then
 +
        echo 'Directory picking cancelled/failed'
 +
        return 1
 +
    fi
 +
    cd "$dst"
 +
}
 +
</source>
 +
 
 +
Now running vicd via that shell function and quiting it will change directory of
 +
shell to the directory of current active pane.  Use <code>:cq[uit]</code>
 +
command to cancel directory picking.
 +
 
 +
==== Prior to 0.8 ====
  
 
* Add special command to <code>vifmrc</code>
 
* Add special command to <code>vifmrc</code>
Line 17: Line 38:
 
         rm ~/.vifm/lastdir
 
         rm ~/.vifm/lastdir
 
     fi
 
     fi
     /usr/local/bin/vifm "$@"
+
     # "command" prevents recursive call
 +
    command vifm "$@"
 
     if [ -f ~/.vifm/lastdir ]; then
 
     if [ -f ~/.vifm/lastdir ]; then
 
         cd `cat ~/.vifm/lastdir`
 
         cd `cat ~/.vifm/lastdir`
Line 24: Line 46:
 
</source>
 
</source>
  
Now running Vifm and quiting it using <code>:Q</code> command (which can be
+
Now running Vifm via that shell function and quiting it using <code>:Q</code>
mapped to <kbd>ZZ</kbd> and/or <kbd>ZQ</kbd>) will change directory of shell
+
command (which can be to <kbd>ZZ</kbd> and/or <kbd>ZQ</kbd>) will
to the directory of current active pane.
+
change directory of shell to the directory of current active pane.
 +
 
 +
[[Category:HOWTO]]

Latest revision as of 14:54, 3 July 2019

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]

  • Add this function to .bashrc
vicd()
{
    local dst="$(command vifm --choose-dir - "$@")"
    if [ -z "$dst" ]; then
        echo 'Directory picking cancelled/failed'
        return 1
    fi
    cd "$dst"
}

Now running vicd via that shell function and quiting it will change directory of shell to the directory of current active pane. Use :cq[uit] command to cancel directory picking.

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.