September 7, 2007

Secrets of the man command

Author: Shashank Sharma

The most referenced and most often used command on any Linux distribution is man, which lets users read the manual pages of other commands. Here are a couple of less well-known but useful commands that let you bookmark a position within a man page, and test a command you read from the man page without closing the page.

Trick #1: Bookmarking

Man uses the less utility to display the manual pages on your screen. When reading through a lengthy man page, I often forget an interesting fact if I don't make a note of it somewhere before moving on to reading the rest of the man page. In such a scenario you can create a bookmark within the man page.

While reading through a man page, you can press m followed by any letter to mark a position in the page. You can then return to this position in the page from anywhere by typing ' (single quote) followed by that letter. You can mark multiple positions within a man page by using a different letter to mark each position. You can use only use alphabet characters to mark a position within the man page, but contrary to what the man page for less suggests, you can use capital letters as well as lower case. For example, you can mark a position with the letter w and mark an entirely different one with W.

The marked positions are remembered for only the current session, which means that as soon as you close the man page, the marked positions are lost. Also, if you accidentally use the same letter to mark two positions, only the most recently marked position is marked.

Suppose you jumped from marked position a to marked position q, a distance of 10 lines. You next jumped from this position to another position t 30 lines away, then jumped to a position p 5 lines away. When you now type '' (two single quotes) you will jump to the position where you had traveled the most lines. That is, you'll end up at position t, since you had traveled the most, 30 lines, to get there.

Since man uses less to display man pages, you can also use the bookmarking trick to mark positions in any file you read using less.

Trick #2: Testing commands from within man

Another useful man trick is to press ! whenever you wish to test a command you've just read in the man page. This allows you to execute the command without closing the man page or opening another terminal window. Once you've tested the command, press the Enter key and you'll be dropped back to where you were in the man page.

Again, this works because man uses less to display the man pages. The commands used in less, as described in its man page, are based on both the more utility and the vi text editor. The exclamation trick can also be used from within vi. Try it yourself: open a file in vi, then press : followed by ! and type ls -l.

The exclamation mark trick makes life easier by reducing the number of clicks or key-presses it would require to otherwise test a command and then return to the man page.

Categories:

  • Desktop Software
  • Tools & Utilities