How to make Midnight Commander exit to its current directory

midnight commander skins
midnight commander tutorial
midnight commander compare directories recursively
midnight commander open terminal
midnight commander tree view
midnight commander vim keybindings
midnight commander ftp
linux mc compare files

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.

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

How to make Midnight Commander exit to its current directory , How to make Midnight Commander exit to its current directory. Edit .bashrc and add the following line: alias mc=". 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.

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'

Midnight Commander, 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  File location depends on the distribution you use (e.g. in Gentoo, Suse, … is /usr/libexec/mc/ ; in CentOS, Fedora, Ubuntu, … the path it is /usr/share/mc/bin/). Restart your shell to view the change. Make Midnight Commander to exit to its current directory Make Midnight Commander to exit to its current directory

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.

How to make Midnight Commander exit to its current directory, The design of Midnight Commander is based on a common concept in file managers: It is often useful to select a directory in the current panel and have its contents To exit the quick view mode, press Tab to return to the directory panel and  Restart your shell, launch mc, change to another directory, exit and your shell should be set to that new directory. Selecting files. Insert (Ctrl + t alternatively) - select files (for example, for copying, moving or deleting). + - select files based on a pattern. \-unselect files based on a pattern. * - reverse selection. If nothing was selected, all files will get selected.

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

Midnight Commander, MC it is a file manager based on the venerable Norton Commander. By default, MC does not remember its parent working directory. To correct  To achieve this you can issue this little script which will force Midnight Commander to exit in this directory. mkdir /etc/profile.d cat > /etc/profile.d/mc-chdir.sh << EOF #!/bin/sh if [ /usr/libexec/mc/mc.sh ]; then . /usr/libexec/mc/mc.sh fi EOF chmod 775 /etc/profile.d/mc-chdir.sh

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.

Make Midnight Commander to exit to its current directory, By using our site, you acknowledge that you have read and understand our Cookie Policy, Upon exit from midnight commander(MC), terminal returns to default directory(users/username) instead of working directory in MC. You can check which files are actually read in by midnight-commander using strace: How to make Midnight Commander exit to its current directory. 6.

Midnight Commander Exit from Working Directory doesn't return to , Oh, and make sure you're running a modern and UTF-8 friendly terminal Restart your shell, launch mc , change to another directory, exit and  Another tool that can save you time is Midnight Commander’s user menu. Go back to /tmp/test where you created nine files. Press F2 and bring up the user menu. Select Compress the current subdirectory (tar.gz). After you choose the name for the archive, this will be created in /tmp (one level up from the directory being compressed).

Use Midnight Commander like a pro, I have some Gentoo systems that have mc installed, but it's default behaviour is to exit to the directory it was run from. I wanted to change this,  Go to Command menu - Directory Hotlist - add by either typing it in, or if you are connected in a panel already, simply Add Current. Access the list with Ctl - \ . To disconnect ftp, type cd in the command line and it will return you to your home directory.

Force Midnight Commander to exit in current directory, Quit – To quite midnight commander, press Esc key followed by 0. Section 4 – Command Line prompt: Using midnight commander does not take away the command line from power user. You can start typing an Unix command anytime and press enter to execute it.

Comments
  • I'm not linux guru, but username is available in USER env variable, no need for MC_USER=, or?
  • works, just restart putty after this operation, and all will work
  • Yes, I did not mention that. You can also put the same line in your current terminal as a normal bash command and it will start to work there too.
  • Thanks. Just want to note its not always work to put in ~/.profile. E.g. on ubuntu it can be ~/.bash_aliases
  • @Babblo Well, the developers of mc did everything they could with their mc-wrapper.sh script. It would be up to the creators of the various distros to actually use the wrapper but adding 3 lines to the skeleton would take away from their precious time to rewrite the boot sequence scripts for the 17th time. Yes, your comment is justified, we shouldn't have to deal with this. 15 years ago I had a Mandrake that did it but since then distros focus their efforts on making their GUI tackier with every release.
  • I hardly ever use the ctrl+o option because it creates a lot of confusion if you forget that you are already running mc or with programs using curses or what I always ended up doing, mc then ctrl+o then ssh some.server.com then ctrl+o and exit because I though I was running mc on the server but instead I lost the ssh connection. IMO it's cleaner if you exit mc when you don't need it. What I use ctrl+o instead is to read the screen after a program has run then I go back to mc immediately.
  • ctrl+o is very usefull for me, to address the problem (forgetting I'm using mc) I'm changing my PS1 in mc to prefix it with [mc] ;-)