/ Mac OS X

Surviving Mac OS X Bash Terminal With Midnight Commander (MC)

Abstract : Midnight Commander (MC) is one of the few tools I'm still using since I've been inducted into software engineering more than 10 years ago (for comparison I've changed 5 integrated development environments (IDE) ). MC is classified as "a visual file manager" but for me is something much more, it's an engineering booster. In fact it's my fist IDE (MCedit supports color highlighting for various programming languages). In short, MC is one of those few things that changes your world. That's why I'm going to show you how you can painlessly integrate MC with Mac OS X bash terminal.

Goal : Configuring Midnight Commander under Mac OS X bash terminal

Acknowledgement : My gratitude goes to the open source community and especially to:

Miguel de Icaza - creator of Midnight Commander, Gnome, Mono, Gnumeric (and I guess other cool stuff)

Let's get started!

Installation: I'm a huge fan of homebrew (thanks Max!) and I advise you to install it and work with it. In short, Homebrew is "the missing package manager for OS X". Installing Homebrew is as easy as running the following command in the terminal prompt:

ruby <(curl -fsSkL raw.github.com/mxcl/homebrew/go)   

Once you have Homebrew, installing MC is exactly three words:

brew install mc

Side note: You may wish to install bash-completion and take advantage of the Tab key on the terminal prompt. If so, do the following:

brew install bash-completion

... and ensure that your .bash_profile has the following content inside:

if [ -f `brew --prefix`/etc/bash_completion ]; then
    . `brew --prefix`/etc/bash_completion
fi

Note that you have to perform:

source .bash_profile 

or re-open your terminal for the changes to take effect.

Mac OS X - fixing the shortcuts mess: No, I have no idea why the Apple guys have created such a big mess with the keyboard shortcuts. And yes, it's up to you, my dearest reader, to decide if you want to fix it as I suggest. Here is what I've done so that I can use my _F_unctional keys (the F keys):

Go to:

System Preferences -> Keyboard -> Keyboard and Shortcuts

... and make sure you have All controls checked

After that go to:

System Preferences -> Keyboard -> Keyboard

... and make sure you have Use all F1, F2 etc. keys as standard function keys checked

Next open a Terminal and go to:

Terminal -> Preferences ... -> Keyboard

... and make sure you have Use option as meta key unchecked.

Now, every F9 will enable MC menu bar and every F10 button press will exit MC (instead of doing a complex fingers split which may not work but which will certainly hurt your hand).

Playing with MC shortcuts: What about the cool MC shortcuts? Before I present you with some of the shortcuts mapping you should be aware that pressing two times the Esc button will close any MC pop-up or search box, and that there are three types of shortcuts:

  1. Pressing (and releasing) Esc and then pressing another key

  2. Pressing and holding Ctrl and then pressing another key

  3. Using the _F_unctional key

Here are some mappings:

----- Esc -----
Quick change directory: Esc + c
Quick change directory history: Esc + c and then Esc + h
Quick change directory previous entry: Esc + c and then Esc + p
Command line history: Esc + h
Command line previous command: Esc + p
View change: Esc + t (each time you do this shortcut a new directory view will appear)
Print current working directory in command line: Esc + a
Switch between background command line and MC: Ctrl + o
Search/Go to directory in active panel: Esc + s / Ctrl + s then start typing directory name
Open same working directory in the inactive panel: Esc + i
Open parent working directory in the inactive panel: Esc + o
Go to top of directory in active pane: Esc + v / Esc + g
Go to bottom of directory in active pane: Esc + j / Ctrl + c
Go to previous directory: Esc + y
Search pop-up: Esc + ?
----- Ctrl -----
Refresh active panel: Ctrl + r
Selecting files and directories: Ctrl + t
Switch active <-> inactive panels: Ctrl + i
Switch active <-> inactive panels content: Ctrl + u
Execute command / Open a directory: Ctrl + j
----- F -----
F1: help
F2: user menu
F3: read file / open directory
F4: edit file
F5: copy file or direcotry
F6: move file or directory
F7: create directory
F8: delete file / directory
F9: open menu bar
F10: exit MC

Keeping working directory after exiting MC: Now, I find this MC feature really cool but of course it's up to you to decide if you want to enabled it or not. In case you want, make sure you have the following (or similar, depending on the midnight commander version) line in your .bash_profile:

alias mc=". /usr/local/Cellar/midnight-commander/4.8.4/libexec/mc/mc-wrapper.sh"

IMPORTANT: As you can see from the line above, it includes the midnight commander version, so please modify accordingly to match your installed version.

Note that you have to perform:

source .bash_profile 

or re-open your terminal for the changes to take effect.

Ivan Hristov

Ivan Hristov

I am a lead software engineer working on software topics related to cloud, machine learning, and site reliability engineering.

Read More