Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Fix an annoying apropos glitch UNIX
One of the most useful commands in the terminal is apropos(1). It prints out summaries of all man(1) pages related to a given keyword. However, an annoying interaction between certain improperly formatted man pages and the makewhatis(8) command may cause entire pages to be written out by apropos, which can make it very difficult to read. apropos serial produces an example of the glitch, which arose years ago in FreeBSD and which OS/X has inherited. However, since the apropos command is a shell script, there is a very simple workaround.

You can either make the change in place, in /usr/bin/apropos, or you can place a copy of the original somewhere earlier in your path and edit the copy. The modification is placed at the very end of the script, and involves inserting the command cut -c1-80 | into the pipeline, immediately before the $PAGER invocation. Here is the resulting line:
( printf -- "$line2"; echo "$line"; cat ) | cut -c1-80 | $PAGER
What the modification does is to truncate all characters to the right of column 80. The change will also truncate summary lines that are valid but long -- if you think more data should be written out, change the 80 to whatever you prefer.

[robg adds: I tested this by first backing up the original apropos, and then editing it with sudo vi apropos. I changed the last line and then saved the file (I had to use w! in vi to override the read-only flag). It worked as described; apropos serial now produces reasonable output...]
  • Currently 1.67 / 5
  You rated: 3 / 5 (3 votes cast)

Fix an annoying apropos glitch | 11 comments | Create New Account
Click here to return to the 'Fix an annoying apropos glitch' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Fix an annoying apropos glitch
Authored by: MaxMarino on Jan 21, '05 12:39:17PM

This is no fix, sorry. It does not scale (fixed length). What if you change the terminal size?

The real fix, of course, is to modify the very same line but NOT with 'cut -c1-80' or whatever column number you might have tried it with in order to have instead a DYNAMIC: want to read more? enlarge the window and you read more text. Text is not lost and it goes as far as the terminal size.

How? Just add, in the same line as the original hint(???) '-S' after $PAGER and you have the real fix. Have this since ages...

[ Reply to This | # ]
Where to put the less -S flag
Authored by: gshenaut on Jan 21, '05 01:33:00PM

Adding an -S flag after $PAGER is not a good way to do this, because it assumes that PAGER is set to less(1). Consider what would happen if PAGER were set in the environment to some other command, such as "xmessage -file -": if there is an -S flag after $PAGER, the program will simply fail to work.

If you look at the script, you will see that PAGER is defined to less plus some flags (PAGER="less -Rse"; the flags mean interpret control chars, squeeze multiple empty lines to one, and exit at EOF). It would make perfect sense to add the S flag to that definition ("PAGER=less -SRse") instead of the cut filter I suggested.

But, the -S flag is a good steer, thanks.

Greg Shenaut

[ Reply to This | # ]
Where to put the less -S flag
Authored by: arekkusu on Jan 21, '05 02:11:40PM

But, it is only set to less if it is undefined. What if you normally have it set to something else, like 'less -ERmX~'?

[ Reply to This | # ]
Where to put the less -S flag
Authored by: gshenaut on Jan 21, '05 04:12:09PM
Yeah, it's kind of an endless loop, isn't it? Well, you could put an "apropos" shim like this into your personal bin directory:
PAGER="less -xxxS"; export PAGER
exec /usr/bin/apropos $*
so that the PAGER definition is overridden. I still think the original file should be changed by adding the -S to its default PAGER definition. (The "xxx" gets replaced with whatever your other less preferences are.)

Greg Shenaut

[ Reply to This | # ]

Fix an annoying apropos glitch
Authored by: zs on Jan 21, '05 11:09:51PM
How about using something like:
cut -c1-`command that gets terminal width`
After some mucking around (using the "apropos" command, appropriately enough) I haven't found a command that can give me this information. Anyone know a way to do this?


[ Reply to This | # ]

Fix an annoying apropos glitch
Authored by: zs on Jan 21, '05 11:12:56PM

Of course this isn't dynamic, but it should work at whatever the terminal width is before 'apropos' is executed. It seems that there is no perfect solution to this problem.


[ Reply to This | # ]
shell width command
Authored by: gshenaut on Jan 22, '05 09:15:42AM
I think the -S flag is probably a better way to handle apropos, but you could alternatively use this (possibly somewhat fragile) cut command:
cut -c1-`stty all | head -1 | cut -d' ' -f6`

Greg Shenaut

[ Reply to This | # ]

shell width command
Authored by: perturb on Jan 25, '05 09:20:09AM
Less fragile stty:
stty size |cut -d' ' -f2

[ Reply to This | # ]
A better way to modify system files
Authored by: thadman08 on Jan 21, '05 12:51:15PM
Rather than changing the behavior of a sytem-wide program, I find that it's easier to have my own script directory and copy system scripts in to that and make changes.

Machine:~ username$ mkdir ~/bin
# ADD export PATH=/Users/USERNAME/bin:${PATH} to ~/.bash_profile
Machine:~ username$ . ~/.bash_profile
Machine:~ username$ cp /usr/bin/apropos ~/bin/
Machine:~ username$ chmod u+w ~/bin/apropos
# Make the changes to ~/bin/apropos
This assumes that you're running BASH as your shell. Make sure you substitute your short username for USERNAME. Here's what the commands do.
  1. Make a 'bin' directory in your home directory. You can call it something else if you want.
  2. You want your new 'bin' folder to be searched when looking for programs to run, so it must be added to the $PATH variable.
  3. Look closely, there's a period before ~/.bash_profile. This tells the shell to read ~/.bash_profile again, rather than closing your terminal and starting it back up again.
  4. Copy the systems apropos script to your new directory
  5. Give yourself Write permission on the new copy
  6. Edit the file like the hint told you to
If you're running C-shell or Tcsh, you'll need to use setenv in your .tcsrc or .login file.
Use this new directory for any command-line tricks and tips. It makes life so much easier and you don't have to do anything as a the superuser.

[ Reply to This | # ]
Fix an annoying apropos glitch
Authored by: mervTormel on Jan 23, '05 11:31:22AM
you want your less to behave; you want your apropos to behave.


$ alias mank='LESS=-iMsXS~ /usr/bin/apropos'     
what we have here is an alias definition that specifies an environment variable for the life of the following command only.

mank? see man -k

[ Reply to This | # ]
Or, more generally (for bash users)
Authored by: Mulligan on Jan 30, '05 10:56:50AM

You can keep your current LESS settings and only add the -S when running apropos:

alias apropos='LESS=${LESS:--}S /usr/bin/apropos'

The magic is in the variable expansion ${LESS:--} which expands to your current LESS value if you have one and - if not.

[ Reply to This | # ]