CLI magic: Midnight Commander

643
(Ed note: Please welcome Joe Bolin, who is standing in as guest CLI for noobies columnist this week. Joe Barr will return with next week’s edition.)

With the vast power of the GNU/Linux command line comes an equal amount of complexity. Trying to remember the multitude of commands can be overwhelming at times. As you sit staring at the blackness of your monitor, you wish for an easier way to tame this thing called CLI. Relax all of you GUI addicts, it is now time to introduce you to the wonderful world of Midnight Commander.

Midnight Commander (MC) is a clone of a DOS utility called Norton
Commander and belongs to a large family of console based file managers
known simply
as Commanders, or Orthodox File
Managers
.

Midnight Commander offers an intuitive interface, the ability to
view/edit files, mouse support, and built-in support for many Virtual
File Systems (VFS). The VFS supported by Midnight Commander include:
FTP, for browsing
remote FTP repositories; FISH, for manipulating files over shell
connections
such as rsh and ssh; and SMB, for accessing remote Windows shares. It
also includes the ability to browse many GNU/Linux archives such as
tar, tgz, bz2, rpm, deb, zip, cpio, lha, rar, and zoo files.

Getting Started.

As of this writing, the current version of Midnight Commander is
4.6.0.

After obtaining the source
code
for Midnight Commander, decompress the tarball
and issue the following commands from the CLI:

cd mc-4.6.0

./configure

make

su

<root password>

make install

To start using Midnight Commander type mc on the
command line.
At first glance you will notice that the interface consists of four
distinct sections.

  • The topmost line is the menu bar. This behaves much like the menu
    bar of GUI applications.The menu bar line may not
    be visible, but appears if you click the topmost line with the mouse or
    press the F9 key.
  • Two directory panels which take up most of the screen space. One
    of the panels is the active panel and is indicated by a horizontal
    selection bar. Almost all operations take place on the active panel.
    Some file operations like rename and copy use the directory of the
    inactive panel as the default destination. Switching from one panel to
    another can
    be done via the Tab key.
  • The second line from the bottom of the screen is a shell
    command line. You can execute system commands from the Midnight
    Commander by simply typing them. Everything you type will appear on the
    shell command line, and the typed command will be executed when you
    press the Enter key. If no command is typed then the Enter key will be
    applied to the active panel.
  • The bottom line shows the function key labels (F1 – F10). The
    functions keys map to commonly used commands in the browse, view, and
    edit modes.

Using the CLI is now as easy as typing on your
keyboard. Move the selection bar by using the up/down arrow keys. To
enter a directory simply move the selection bar on the directory name
and press the Enter key. Likewise, to move up one directory move the
selection bar to the “/..” line and press Enter. Browsing the contents
of an archive file can be performed by moving the selection bar to
highlight the archive and pressing Enter. Do you notice a pattern here?

Manipulating Files.

Click to enlarge

Since most configuration files on a GNU/Linux system
are plain text
files,
Midnight Commander’s most useful feature is it’s built-in file viewer
and editor.

Editing or viewing a file can be done by highlighting the file with
the selection bar and pressing either the F3 (view) or F4 (edit) key.
Once in
view mode you can navigate the file with the up/down arrow keys and the
Page Up/Page Down keys.

In edit mode, you can use the same keys as view mode along with the
left/right arrow keys to move the cursor through the file. Type as you
normally would in any GUI based text editor to make changes to the
file. To
save the file after editing, press the F2 key.

While in view or edit modes, the Home
key moves to the beginning of the file and the End key moves to the end
of the file. Press the F10 key to
exit either mode.

Menu and Shortcuts.

Both browse and edit modes contain a top menu area. The top menu may
be activated by the F9 key. Once activated, you can
traverse the menu area using the arrow keys. The Enter key will execute
the
current menu selection. The Esc key (twice) will deactivate the top
menu.

While within the top menu structure you may have noticed keyboard
shortcuts for most of the menu items. While using Midnight Commander,
you will find these shortcuts extremely useful. Some of the shortcuts
may appear unconventional, such as “C-x”. Here’s an explanation of
these shortcuts:

  • C-<chr> – Means to hold the Control (Ctrl) key while typing
    the character <chr>. Example: C-x
  • M-<chr> – Means to hold the Meta, or Alt, key while typing
    the character <chr>. Example: M-x
  • S-<chr> – Means to hold the Shift key while typing the
    character <chr>. Example: S-x
  • C-<chr> <chr2> – Means to hold the Control (Ctrl) key
    while typing the character <chr>, release the Ctrl and
    <chr> keys, and then press the last character <chr2>.
    Example: C-x d

The most common commands used in MC are assigned to the Function
keys (F1 – F10). The value of each key is different depending on which
mode that you are in. Here is a table of the commands assigned to each
of the Function keys in the various modes.

Browse mode View mode Edit mode
F1 Displays the main Help screen Displays the viewer Help screen Displays the editor Help screen
F2 Activates the User Menu Wrap/unwraps the text Saves the file
F3 View the selected file Exits View mode Selects text
F4 Edit the selected file Toggles ascii/hex view mode Find and Replace
F5 Copy the selected file Opens the Goto Line dialog Copy selected text to cursor position
F6 Rename/Move the selected file Regexp Search Move selected text to cursor position
F7 Creates a new directory in the active panel Search Search
F8 Deletes the selected file Toggle Raw/parsed mode Delete the current line or selection
F9 Displays the top menu Toggles Format/unformat view Displays editor’s top menu
F10 Exits MC Exits View mode Exits Edit Mode

Mouse Support.

Mouse support is available by default when using a terminal
emulator, such as rxvt,
under X. In order to enable mouse support on
the console, you will need to install and run gpm. The mouse cursor will
appear as a small, highlighted rectangular area when using MC from the
console.

The top menu, function labels, and cursor position can be
manipulated by single-clicking with the left mouse button. Entering a
directory may be performed
by double-clicking. Selecting multiple files (for Copy/Move/Delete) can
be done in X by right clicking each file, however, multiple selections
from the console must be done using the “C-t” keyboard shortcut.

Whether you are a noobie or a seasoned veteran, Midnight
Commander is probably on of the most useful tools for working with the
CLI. MC’s ease of use is equally matched by it’s vast functionality. To
learn more about using Midnight Commander, consult the man pages (man
mc
) and it’s built-in Help system.