1

Refer to can-i-get-individual-man-pages-for-the-bash-builtin-commands:

bashman () { man bash | less -p "^       $1 "; }

This function will jump to the desired bash manual parameter section directly.

I want to make it accept any Manual Name and Manual Parameter on the fly, so i change this function to:

function superman () {
    man "$1" | less -p "^[ ]+[-]*$2[ ]"
}

It works perfectly with:

$ superman bash type

And jump to the desired man page section:

enter image description here

But it doesn't work in section ending with newline. I need change it to:

function superman () {
    man "$1" | less -p "^[ ]+[-]*$2$"
}

Then run $ superman gcc Wall will able jump to Wall parameter:

enter image description here

How to combine [ ] and $ to be [ ] OR $? i.e. endswith at "atleast one space" OR "newline".

I tried $ man gcc | less -p "^[ ]+[-]*Wall[ |$]" but it doesn't works.

Please note that i accept for this undesired jump, which is endswith [ ] but not really a beginning of section, because i believe it's unsolvable if regex is [ ] OR $:

enter image description here

[Thanks for answers]

I want to share my final form here:

function superman () {
    if [[ "$1" == "-I" || "$1" == "-i" ]]; then
        man "$2" | less -I -p "^[ ]+-*$3( |=|,|$|\[)"
    else
        man "$1" | less -p "^[ ]+-*$2( |=|,|$|\[)"
    fi
}

This will work if i run superman -i gcc wl which means case-insensitive to jump into -Wl,option section. -Wl,option from gcc require ,. scaletempo from mplayer require [, and scale from mplayer require =

I wrote for info version too:

function superinfo () {
    if [[ "$1" == "-I" || "$1" == "-i" ]]; then
        info "$2" | less -I -p "^ *‘*'*-*\** *$3’*( |=|,|$|\[|:|')"
    else
        info "$1" | less -p "^ *‘*'*-*\** *$2’*( |=|,|$|\[|:|')"
    fi
}

Tested with superinfo gcc _HPPA, superinfo -i gcc werror, superinfo -i gcc wl, superinfo -i mplayer scaletempo, superinfo -i mplayer stats, superinfo -i ls -f, and superinfo -i bash -a(with minus sign make big difference here). Unicode's LEFT SINGLE QUOTATION MARK used by -f of ls.

  • 2
    Nice. My similar function is just b () { LESS="+/^ *$*" man bash;} so I can do, for instance, b history expansion. – Wildcard Oct 22 '16 at 18:35
  • 1
    How about using info where the data is properly indexed. Like info bash type. – Stéphane Chazelas Oct 22 '16 at 19:38
  • @StéphaneChazelas Thanks for reminding me, I wrote for info version too. – Fruit Oct 22 '16 at 20:48
1

I believe you are mistaken about the meaning of [ square brackets ] in a regular expression. Note your pattern:

^[ ]+[-]*Wall[ |$]

[ ] is just exactly the same as (a single space) and [-] is just exactly the same as -. And in the final part of the pattern, [foo|bar] does not signify "either foo or bar".

What you're looking for is ( |$). That's the syntax for matching one thing OR another thing. (You'll also need to quote that part of the pattern with single quotes or a backslash instead of double quotes, because of the dollar sign).

[thing] denotes a character class: it matches either t, h, i, n, or g.

| improve this answer | |
  • Thanks. But what do you means by quote that part of the pattern with single quotes or a backslash ? I put man "$1" | less -p "^[ ]+[-]*$2( |$)" and works fine, neither have single quotes nor backslash. – Fruit Oct 22 '16 at 18:46
  • 1
    You have a literal $ in there, so it should be quoted. I guess it "happens" to work by luck in this case because the $ is followed by something that cannot refer to a variable name. – Celada Oct 23 '16 at 6:07
1

How to combine [ ] and $ ?

([ ]|$) or ( |$)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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