8

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.

  • 2
    Possible duplicate of How do I search for `!` (exclamation mark) in a manual page? – M. Becerra Mar 31 '17 at 11:51
  • 1
    @M. Becerra my question is broader. – R S Mar 31 '17 at 12:34
  • @Becerra Not only is it a broader question, but it's also a different question, because R S wants to return the entire paragraph of an option in the results of man, instead of returning a jumbled list of separate lines. – karel Apr 4 '17 at 1:41
6

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.

9

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>
  • 4
    Try adding a couple of spaces before the dash. Most of the time, the options are indented and that should jump right to the option: / -f. This should bypass any inline uses of -f. – transistor1 Mar 31 '17 at 19:29
2

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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.