Skip to content

Instantly share code, notes, and snippets.

@kallookoo
Last active December 26, 2023 14:57
Show Gist options
  • Save kallookoo/5a2870478c6e53ead4a6f431e0b98133 to your computer and use it in GitHub Desktop.
Save kallookoo/5a2870478c6e53ead4a6f431e0b98133 to your computer and use it in GitHub Desktop.
Solution for x-man-page when using a command installed with brew that already exists in the OS

After trying the proposed solution and seeing that it still doesn't work.

The original code says to define MANPATH as explained in the man page, but when the man command reads the file, it append to MANPATH. So any command that exists in system man path (/usr/share/man, /usr/local/share/man) and was installed with brew, will show the man page inside of the system man path.

I started testing until I found a solution.

sudo mkdir -p /usr/local/etc/man.d
sudo tee /usr/local/etc/man.d/homebrew.man.conf <<'EOF' >/dev/null
manpath "/opt/homebrew/share/man:$manpath"
# Remove duplicated paths and extra colons.
manpath "$(printf %s "$manpath" | awk -v RS=: -v ORS= '!arr[$0]++ {if (NR>1 && length>0) print RS; print $0}')"
EOF

As you can see in the code it uses the variable manpath that after inspecting the source code of the man command, I have been able to verify that it is used to add in the MANPATH the specified paths.

As the same command creates a variable when it reads the configuration files, I have used it to modify directly the MANPATH just before it is defined.

Obviously with this method you can specify several paths but I do not recommend using it unless you are sure that it will not affect.

For example:

sudo tee /usr/local/etc/man.d/homebrew.man.conf <<'EOF' >/dev/null
manpath "/opt/homebrew/opt/curl/share/man:$manpath"
manpath "/opt/homebrew/share/man:$manpath"
# Remove duplicated paths and extra dots.
manpath "$(printf %s "$manpath" | awk -v RS=: -v ORS= '!arr[$0]++ {if (NR>1 && length>0) print RS; print $0}')"
EOF

It is important to establish a correct order, since the man, as far as I know, reads the first file it finds.

This happens on Sonoma and the Apple chip, I don't know if it happens with other OS and chip combinations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment