2010-01-30

How to make Midnight Commander exit to its current directory

This blog post gives instructions how to make the current directory of the shell calling Midnight Commander be mc's current directory upon exiting from mc.

The quick answer is to add alias mc=". /usr/share/mc/bin/mc-wrapper.sh" (or something similar, with a different path) to your shell startup script. If you don't know how to do that, read on.

On Ubuntu (Hardy, Jaunty, Karmic etc.) or Debian (Lenny etc.), run this as root (copy-paste as a whole):

grep -l bash\.bashrc /etc/profile || (echo
    echo 'test "$PS1" && test "$BASH" && . /etc/bash.bashrc') |
    tee -a /etc/profile
echo 'type -p -a mc >/dev/null &&
    alias mc=". /usr/share/mc/bin/mc-wrapper.sh"' | tee -a /etc/bash.bashrc

On the Mac OS/X with mc installed from MacPorts, open a Terminal window, type sudo bash, press Enter, type your password, press Enter, and then run the following (copy-paste as a whole):

grep -l bashrc /etc/profile || (echo
    echo 'test "$PS1" && test "$BASH" && . /etc/bashrc') |
    tee -a /etc/profile
echo 'type -p -a mc >/dev/null &&
    alias mc=". /opt/local/share/mc/bin/mc-wrapper.sh"' | tee -a /etc/bashrc

After doing so, open a new terminal window (or SSH connection), and it should work as expected (try it by running mc /tmp, and exiting by pressing Esc, 0, Enter).

For your information, the underlying solution (in mc-wrapper.sh) makes Midnight Commander print its current directory when it exits, and then the caller shell code does a cd command to that directory.

5 comments:

Vidja said...

Thanks, saves a lot of frustration

boryn said...

Works perfect! If you could just describe what exactly the commands do?

Thank you!

pts said...

@MichaƂ: Read a book about Unix programming and/or Unix shell programming first so you can understand how these commands work and what they do.

Unknown said...

THANKS A LOT

nem said...

i haven't waited a second, and done exactly as suggested.
probably too fast, because... too bad, it does not work.
maybe, after 6 years, current version of OS X is substantially different.
now i can't even launch mc any longer, as i get:
-bash: /opt/local/share/mc/bin/mc-wrapper.sh: No such file or directory
instead.
would you be so kind and generous to suggest to the current newbie idiot, silly me, how to put things back to where they were, how they were, before i sent the command copied-and-pasted as described? so that I can use Midnight Commander again, please?
can't do anything else from within Terminal than deleting sparsebundle from TM backups via mc.
your help will be much appreciated.