Search code examples
mc

How to make Midnight Commander exit to its current directory


I've been using, in equal amounts, Fedora and Ubuntu for well over a decade now, and there's one minor but irritating difference I noticed from their installs of midnight commander. When you change dirs inside it using Fedora, then exit, it has done the chdir for you but in Ubuntu it keeps it at the place you started. Googling threw up a solution for older Ubuntus here: http://ptspts.blogspot.co.uk/2010/01/how-to-make-midnight-commander-exit-to.html but trying that fails on 16. When I say fails, I mean the commands are accepted without complaint but it doesn't change mc's behaviour in Ubuntu.


Solution

  • Create an executable with the following content:

    MC_USER=`id | sed 's/[^(]*(//;s/).*//'`
    MC_PWD_FILE="${TMPDIR-/tmp}/mc-$MC_USER/mc.pwd.$$"
    /usr/bin/mc -P "$MC_PWD_FILE" "$@"
    
    if test -r "$MC_PWD_FILE"; then
            MC_PWD="`cat "$MC_PWD_FILE"`"
            if test -n "$MC_PWD" && test -d "$MC_PWD"; then
                    cd "$MC_PWD"
            fi
            unset MC_PWD
    fi
    
    rm -f "$MC_PWD_FILE"
    unset MC_PWD_FILE
    

    Then define an alias pointing to that executable:

    alias mc='. ~/.config/mc/exitcwd'
    

    Don't forget to apply the alias:

    source ~/.bashrc