2

Question: using the less command in any linux shell (i'm using bash as probably most people do), is there a way to search a file only for it's commands or options?

So, to be more precise: if i want to quickly find the description for one special option in a man-page, is there a special search syntax to quickly jump to the corresponding line explaining that specific command?

example:

if i type:

man less

and i want to quickly find the description for the "-q" command, is there a search syntax to directly jump to that line? If I type /-q, it finds all occurences of "-q" everywhere in the file, so I get around 10-20 hits, of which only one is the one i was looking for..

So I'm just hoping there is a better/quicker way to do this.. (not to important though :D)

xhienne
  • 5,738
  • 1
  • 15
  • 34
  • Whoever is asking to close this question as general-software-related is probably not familiar with Unix. The `man` command is a first-class helper when it comes to developing (C and shell-scripting) under Unix. – xhienne Feb 22 '21 at 18:36

2 Answers2

2

In man, options are generally described with the option name in bold at the start of the line.

So, if you are looking for the option -q, then the search command would be /^\s*-q\>

The regex ^\s*-q\> reads as follow:

  • ^ start of a line
  • \s* any number of spaces (including none)
  • -q the option name you are looking for
  • \> the end of the word
xhienne
  • 5,738
  • 1
  • 15
  • 34
0

There're several possible solutions for the requested direct jump to the line with the option's description.

Strait-forward solution

Prepend your search query with a space(s): / -q will find option's description right away.

Piping to less

According to the less documentation, you can use -p or + option for jumping to the search string. Quoting the documentation:

The -p option on the command line is equivalent to specifying +/pattern; that is, it tells less to start at the first occurrence of pattern in the file.

So the following example commands will "jump" to the option's description too.

man ls | less -p "  -L"
man ls | less "+/  -L"

Extract into a function

To save us keystrokes, a good idea would be to write a shell (bash in this case) function that would call one of the above commands. For instance, see the basic example:

manopt()
{
    if [[ $# -ne 2 ]]; then
        echo "Enter command name and option to search for."
    else
        man "$1" | less "+/  $2"
    fi
}

After "sourcing" this function, search for less command -q option description with the following:

manopt less -q
Y. E.
  • 687
  • 1
  • 10
  • 29