Carl Whalley
Carl Whalley

Reputation: 3103

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.

Upvotes: 37

Views: 28587

Answers (9)

gregers666
gregers666

Reputation: 1

Works even on exit in subshell

/usr/lib/mc/mc-wrapper.sh

#!/bin/bash

MC_USER=$(whoami)
MC_PWD_FILE="${TMPDIR-/tmp}/mc-$MC_USER/mc.pwd.$$"

cleanup() {

    if [[ -r "$MC_PWD_FILE" ]]; then
        MC_PWD=$(<"$MC_PWD_FILE")
        if [[ -n "$MC_PWD" && "$MC_PWD" != "$PWD" && -d "$MC_PWD" ]]; then
            cd "$MC_PWD" || echo "Unable to cd: $MC_PWD"
        fi
        rm -f "$MC_PWD_FILE"
    fi
    unset MC_PWD_FILE
    unset MC_USER
    unset MC_PWD
}

trap cleanup EXIT INT TERM HUP
/usr/bin/mc -P "$MC_PWD_FILE" "$@"
cleanup

Upvotes: 0

For Kubuntu 24.04

nano ~/.profile

Add this line at the end of file

alias mc='source /usr/share/mc/bin/mc-wrapper.sh'

Type this command to execute changes

source ~/.profile

Upvotes: -1

bomben
bomben

Reputation: 620

I want to add that this only works by existing with F10. If you exit by typing exit the path will not be preserved.

Upvotes: 0

soger
soger

Reputation: 1237

The other responses are fine, but I feel like they are unsatisfying, here is my solution, which I think is the simplest:

Put this line into your ~/.profile

alias mc='source /usr/lib/mc/mc-wrapper.sh'

Upvotes: 43

Mr. Smit
Mr. Smit

Reputation: 2532

Simple:

mcedit ~/.profile

Add this line at the end of file:

alias mc='source /usr/lib/mc/mc-wrapper.sh'

Type this command to execute changes

source ~/.profile

Then, to save both sides of mc windows, click at the top of MC

Options -> Panel options -> Auto save panels setup

Upvotes: 11

Yaroslav  Kornachevskyi
Yaroslav Kornachevskyi

Reputation: 1218

For Ubuntu put this to .bashrc:

alias mc='. /usr/lib/mc/mc-wrapper.sh'

then:

source ~/.bashrc

(or relaunch the console)

Upvotes: 2

Kolyunya
Kolyunya

Reputation: 6240

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

Upvotes: 9

nhkode
nhkode

Reputation: 1414

While it's not exactly an answer to your question: just use ctrl+o to drop to the shell. It doesn't really quit mc, but that has the benefit that you can just hit ctrl+o again to go back where you were in mc.

Upvotes: 1

VansFannel
VansFannel

Reputation: 45921

Here, in the article Use Midnight Commander like a pro, explains how to do it.

Basically, you have to create an alias for mc-wrapper.sh.

Upvotes: 5

Related Questions