How to get paragraph for certain option of command right away?

For example instead of whole manual of apt-get I want jump to -f option from terminal prompt right away, without using search through manpage.


Solution 1:

Type a forward slash, the thing you are searching for, and then press enter. You will jump to the first occurrence. Press N to move to next occurrence and B to go back. So, in this case:

/-f <enter>

Solution 2:

The default pager used by man is less. You can pass the ERE (Extended Regular Expression) search pattern that less understands directly to it via the LESS environment variable, in you case the following should do:

LESS='+/-f' man apt-get

This is exactly same as passing /-f after doing man apt-get.

Now, this would highlight all -fs in the man page, to jump straight to the desired one i.e. option -f, you can leverage ERE to match only the lines that start with spaces/tabs, followed by -f:

LESS='+/^[[:blank:]]+-f' man apt-get

Although this would do here but still might not be precise for all pages, as this will match anything that starts with -f after initial spaces/tabs. Tweak the pattern a bit to meet you need in those cases.

You can create a tiny function to pass the search pattern and the man page to look for as arguments, if you do this often.

Solution 3:

Use sed to show the entire paragraph of an option the begins with a hyphen. To show the entire paragraph of the -f option right away by running a single command use:

man apt-get | sed -n '/-f,/,/^$/p'
   -f-,--no-f, -f=no or several other variations. 

   -f, --fix-broken
       Fix; attempt to correct a system with broken dependencies in place.
       This option, when used with install/remove, can omit any packages
       to permit APT to deduce a likely solution. If packages are
       specified, these have to completely correct the problem. The option
       is sometimes necessary when running APT for the first time; APT
       itself does not allow broken package dependencies to exist on a
       system. It is possible that a system's dependency structure can be
       so corrupt as to require manual intervention (which usually means
       using dpkg --remove to eliminate some of the offending packages).
       Use of this option together with -m may produce an error in some
       situations. Configuration Item: APT::Get::Fix-Broken.  

This returns the entire paragraph for the -f option in man apt-get but the above command can be improved by eliminating the comma after -f to make it more generally useful as follows:

man apt-get | sed -n '/-f/,/^$/p'

This returns multiple paragraphs, most of which you don't want to read. By reading the first lines of the multiple paragraphs, you can see that you want to show only the paragraph containing the -f, --fix-broken option. Do this as follows:

man apt-get | sed -n '/--fix-broken/,/^$/p'
   -f, --fix-broken
       Fix; attempt to correct a system with broken dependencies in place.
       This option, when used with install/remove, can omit any packages
       to permit APT to deduce a likely solution. If packages are
       specified, these have to completely correct the problem. The option
       is sometimes necessary when running APT for the first time; APT
       itself does not allow broken package dependencies to exist on a
       system. It is possible that a system's dependency structure can be
       so corrupt as to require manual intervention (which usually means
       using dpkg --remove to eliminate some of the offending packages).
       Use of this option together with -m may produce an error in some
       situations. Configuration Item: APT::Get::Fix-Broken. 

This returns only the output that you want to read. This method works with any other options that begin with a hyphen, and it also works generally for searching for options that begin with a hyphen in other commands besides just apt-get too.

Showing additional information with sed

If the one paragraph description does not give enough information, the following command will show the first paragraph the same as the previous command and the next paragraphs after it too.

LESS='+/^[[:space:]]*-f' man apt-get  

The results of this command show that the next paragraphs are not very interesting, but for some options the next paragraphs are interesting too. That is why this is also a useful command to know.