Midnight Commander HowTo |
(minor editing by Darrell May)
Abstract
The Midnight Commander is a directory browsing and file manipulation program that provides a flexible, powerful, and convenient set of file and directory operations. A sophisticated Virtual File System (VFS) supports directory-like browsing and file operations for a variety of common archive formats as well as FTP and network connections. Its basic operation is easily mastered by the novice while providing a rich feature set and extensive customization.
Without trying to be exhaustive I'd like to touch on the following topics:
[root@e-smith /root]# mc
At this point, you should see something like:
This main screen is similar to what you'd see at the console (the window has been sized to 80x24). It's main components are:
To activate one of the menus hit the F9 key and use the arrow keys to move the desired menu item. This brings up one of the important features of mc: almost all operations can be performed via keystrokes.
The directory panels are where most of the action takes place. mc is normally run in this two-panel mode although single panel mode is also supported. The panels provide a view of two directories at once with one of them being the "current directory." Most all file operations are performed on files in this current directory (although copy, rename, and move operations default to using the non-current directory as the "target" directory). Use the TAB key to switch the current directory from one panel to the other.
The command line at the bottom functions just as you'd expect: simply type the command to execute and mc runs it just as if you'd entered it at the shell prompt. Just above the command line is the hint line (at the console; in an xterm it uses the title bar) which displays a series of hints and suggestions.
Finally, the bottom line of the window display the function key mappings. Pressing F1 brings up the Help menu, F2 brings up the User Menu, F3 let's you view a file, and so forth. Note that clicking on any of these with the mouse results in the same action.
In addition to this main window there are a number of popup dialog boxes which are used for specific operations. I'll cover several of these shortly. For now, let's turn to issues of navigation.
One other navigation aid to keep in mind is that movement within the directory panels can be accomplished using a variety of keystrokes, many of which are modeled after the emacs text editor. These include:
Once you know how to move from one directory to another the next thing to do is learn how to perform basic file operations. But before doing that we'll need to take a quick detour.
First, we need to make a distinction between the currently selected file or directory and marked or tagged files. The currently selected file is simply the one that is highlighted in the current directory panel. If you want to delete foo.txt simply move the highlight bar to that file and hit F8 to delete it. However, if you want to delete a group of files then you'll need to tag them.
Tagging can be done by moving the highlight bar to the file and hit Ctrl-t (that is, hold down the control key and hit t). In this way you can tag any number of files for copy, deletion, moving, and so forth.
If the files you want can be specified by a shell pattern (such as *.tar.gz for all the gzipped tar files or foo_??.txt for foo_01.txt, foo_02.txt, foo_03.txt, etc., then you can use the following shortcuts:
Using pathname expansion (also known as filename globbing) is a fast and powerful way to select a group of similar files. Having now selected your files, let's see what you can do with them.
Below is a short summary of the file operations. In the next section we'll look specifically at file viewing and editing. Keep in mind that while the summary below indicates the keystrokes for the various operations, all of these can be accessed using the "File" menu.
If you want to use a different directory than the one in the other panel or if you want to change the file name then you can use the to: entry box to do so.
Also, when you perform a copy (and move or delete) operation mc displays a dialog box with a progress meter indicating the progress on the current file as well as the overall progress if a set of files has been selected.
Note that at any time you can cancel an operation by hitting the Escape key twice.
Note that an "Advanced Chown" facility is available under the "File" menu. Until you're rather sure of what you're doing, this is probably best left alone.
With these basic facilities you'll be able to do a good deal of day to day file system maintenance. To round things out, though, we'll need to add a couple other features.
For example, to view a manual page (even a gzipped page!) simply select the file and hit F3. If you have the mc source distribution handy, change to the doc directory and select the mc.1 file. Hit F3 to see:
You can do similar things with HTML or mail files. In the case of HTML files it is worth noting that "viewing" the file is probably not what you expect as mc will strip out the hypertext tags leaving just the text. If you want to view an HTML file it is best to select the file and hit the RETURN key. Doing so "opens" the file and automatically executes (by default) lynx if you are at a console or netscape if you are running under X.
The internal file view allows you to view files in one of two modes: ASCII or hex. When using the file viewer you'll notice that the function keys at the bottom change to a new set which are specific to the viewer. These include:
In addition Ctrl-s and Ctrl-r can be used for normal or reverse searches. Once you've started a search, hit the letter n to find the next match. Ctrl-l will repaint the screen; Alt-r will toggle the display of a ruler.
In terms of moving around the viewer, mc has a rather egalitarian attitude and will accommodate almost any set of movement keystrokes that you've gotten used including those for emacs, less, and even some vi. Some of these are:
One very handy feature is that, if you are in View mode and hit Ctrl-f then the viewer will move to the next file in the directory and display it. In this way you can easily move through a set of files, viewing one right after the other.
The internal file editor provides a full set of editing features and can be used to edit both text and binary files up to a size of 16 megabytes. As with the Viewer, function keys have been remapped to provide common file editing functions. In addition, a popup menubar provides extensive editing operations including file insertion, save, copy, and load; block operations (copy, move, etc); search/replace functions; command macro recording and execution; and the capacity to pipe selected text through various shell commands such as indent or fmt. When not active, the menubar is hidden and file information is displayed in the topmost line. Here's a screen dump of the editor in action:
Both the internal Viewer and Editor are designed to be fast and easy to use. You may, however, wish to use an external viewer (such as more, less, or most) or editor. To do so, you'll need to set your PAGER and EDITOR environment variables to the appropriate program and then use the Options->Configuration menu to unselect "use internal edit" or "use internal view". If you were using the bash shell and wanted to set the pager to "less" and the editor to "emacs", then use something like:
$ export PAGER=less $ export EDITOR=emacs
To make this change permanent you'd probably want to add these lines to your ~/.bashrc or ~/.bash_profile file. Having looked at the basic file operations let's return to mc itself and take a look at some of its other features.
As previously noted, you can quickly switch from one panel to the other using the TAB key (or Ctrl-i). You can also swap panels using Ctrl-u; note that the currently active directory panel does not change. Use Ctrl-r to refresh the directory display.
To change the sort order of the files being displayed, use the (Left|Right)->Sort Order... menu item. This allows you to sort files by name, size, various time stamps, inode number, and so forth. You can also specify whether sorting should be case (in)sensitive or reversed. Sorting by size is very useful when trying to cull out files to recover disk space; sorting by date is useful when you are searching for a recently installed, created, or modified file in a directory with many files or are looking for ancient files that can safely be warehoused.
As with sorting, use the (Left|Right)->Filter... menu item to filter the directory listing using shell patterns. For instance, suppose that you wanted a listing containing only files with a .c extension. In the Filter dialog simply enter "*.c" and all other files are removed from the listing. This is very useful when you wish to work with only a subset of files in a directory in an uncluttered setting.
You can also cycle from two-panel to single-panel modes using Alt-t. This is particularly useful when you need to see the full directory information for a particular file. Note that you can also use the (Left|Right)->Listing Mode... menu item to customize what file information the panel lists. In addition, resize the panels using the Options->Layout... menu item. This allows you to split the panels either vertically or horizontally as well as set the number of columns for each panel using the ">" and "<" keys.
One final shortcut to be aware of is Alt-o which makes use of both panels: by selecting a directory in the active panel and hitting Alt-o, its directory listing is displayed in the other panel. Hitting Alt-o repeatedly lets you quickly preview through a series of directories.
Another powerful feature of mc is its ability to handle a multitude of archive types: this feature alone makes it a "must have" utility!
VFS refers to the "Virtual File System" which mc implements. It is a powerful abstraction that allows you to view archives as though they were a directory: all the basic file manipulation operations can then be applied. The VFS file system handles an extraordinary number of archive types including tar, gzipped or compressed tar, RedHat's rpm package archives, Debian's deb package archives, gzip, zip, zoo, cpio, rar, and lha.
To use it either select the file and hit RETURN or double click on the file. It's contents are then displayed as a directory listing. Navigation through the archive is the same as you'd use for a directory. This is a very useful feature when you need a single file or set of files from an archive. Note that if the archive is a compressed single file -- i.e., a gzip, zip, zoo, or lha compressed file -- then it is uncompressed and displayed.
The VFS also supports its own FTP capacity which allows you to transparently manipulate files via FTP as though they were local to your machine. To log into an FTP server use the (Left|Right)->FTP Link... menu item and enter the URL or simply enter cd ftp://"URL" at the command line. For example, to ftp to the Linux Incoming directory at sunsite you would enter:
$ cd ftp://sunsite.unc.edu/pub/Linux/Incoming/The hintbar at the console or the title bar under X will display progress information (e.g., logging in, retrieving directory listings, and so forth). You can now view and copy files just as you would using ftp. On file transfer (use F5 to "Copy" the file to your local machine) a progress meter displays percent transfer completed, ETA (estimated time of arrival), transfer rate, and the now commonplace "stalled" flag. Use the Options->Virtual FS... menu item to customize the VFS features such as anonymous login name and so forth.
Note that mc also provides FTP service via a proxy server as well as network VFS. Having no experience with either of these I'll defer comment and simply refer you to mc's manual page if you are interested.
In this last section let's look at a few more shortcuts and suggestions for using mc effectively.
A number of popups are built into mc that considerably speed up various operations. These include:
If you use mc as an ftp client then you can use the directory hotlist to keep the URL's for your frequented sites! To edit (add, modify, or delete entries) the list type in Ctrl-\ and then use "New Entry" to create a new entry: enter the URL for the site, including the path to the directory that you're interested in and then fill in the alias. Now, anytime that you need to ftp just popup the hotlist and select the site!
Alternatively, if you were looking for all files with "announce" in the filename simply enter "*announce*" in Filename: (and leave the Content: entry box empty).
Another very handy feature which mc provides is subshell support. The way this works is by hitting Ctrl-o which creates a non-login interactive shell. This works for bash, tcsh, and zsh shells. Use this shell just as you would any ordinary shell. To immediately switch back to mc hit Ctrl-o once again, which allows you to toggle back and forth easily.
If you are using the bash shell, keep in mind that non-login interactive shells only source your ~/.bashrc file (and not the ~/.bash_profile file) which means that if you have aliases or other customizations that you want to use then you should put these in ~/.bashrc. For example, if you use color-ls and find that file listings are not colorized, then you'll need to add alias ls='ls --color=tty' to your ~/.bashrc.
One way to quickly create the "all-in-one-command-center" is execute mc and then start a subshell. From here, you can execute your favorite editor (emacs, xemacs, vim, etc.) and hit Ctrl-z to stop its execution and put it in the background. This returns you to the shell. Now, if you need to run mc then hit Ctrl-o; if you need to use your editor, type in fg which will resume the stopped program; and if you need to run any other program then use the shell as normal. This is a powerful means of keeping productivity tools readily available.
One last feature I'd like to mention is mc's ability to help you sync the contents of two directories. This is particularly useful if you are keeping a backup set of files on another partition, a floppy, zip drive, etc. To use this list the "source" directory in one panel and the "target" directory in the other then hit Ctrl-x d. This will pop up a dialog box that allows you to select the type of directory comparison: Size simply compares files by size; Quick compares files by size and date; and Thorough does an exhaustive byte-by-byte comparison. After the comparison operation is complete (and after ensuring that the source directory is in the active directory panel) hit F5 (Copy) to copy files from your source directory to the target (backup) directory.
While I've attempted to cover most of the important features which mc offers there are many more that I've not had time to cover that I'll leave for you to discover! One suggestion would be to print out a copy of the mc manual page:
man mc | col -b | lpr - -OR- man mc | col -b > mc.txtwill print a copy of the manual page or save it to a text file which can be further processed. Since the manual is quite long you might want to use a program such as a2ps which converts ASCII files into Postscript. As with most UNIX-type programs, a2ps has a slew of command line options including the -f option which lets you specify the font size: select something in the range of 7.0 to 9.0 to get a small font which cuts down on the number of pages and leaves large margins in the sides for you to scrawl notes in.
Also, while you are exploring, look through the Options menu for various items which will let you customize mc. Menus are a great place to poke around and see what facilities mc offers: most of the shortcut keystrokes I've mentioned are menu items (so you don't have to memorize the entire list of keystrokes!). For the adventurous:
Finally, enjoy mc! As with many powerful programs, you'll most likely learn it incrementally, often just through the process of exploration and "playing with it." I've found mc to be indispensable and, with a bit of experience, I suspect that you will as well. Have fun!
Date Last Modified: $Date: 1997/10/19 01:33:27 $