0

If I create a string from the output of a shell command, using shell-command-to-string, I sometimes see ^H characters in the string, when I guess those characters were supposed to result in backspacing.

For example, I can produce such a result on my Debian-stable machine by doing

(shell-command-to-string "fortune -m MacKay")

for that particular fortune, the result contains the following substring The subspace _^HW inherits (where I've replaced a "non-printing character" by the string "^H", because that is what I "see" in Emacs).

If I just do fortune -m MacKay in a terminal (e.g. in eshell) the result contains instead the substring The subspace W inherits.

If I do something like

(insert (shell-command-to-string "fortune -m MacKay"))

in the *scratch* buffer, and then do "M-x describe-string" with point on the non-printing character (which - as I said earlier - appears in emacs as ^H) it is (unsuprisingly) described as

             position: 1442 of 2745 (52%), column: 15
            character: C-h (displayed as C-h) (codepoint 8, #o10, #x8)
    preferred charset: ascii (ASCII (ISO646 IRV))
code point in charset: 0x08
               script: latin
               syntax: _    which means: symbol
             to input: type "C-x 8 RET 8" or "C-x 8 RET BACKSPACE"
          buffer code: #x08
            file code: #x08 (encoded by coding system prefer-utf-8-unix)
              display: no font available
       hardcoded face: escape-glyph

What should I do to make shell-command-to-string "treat these deletion characters correctly?"

I doubt this has much to do with the Emacs version, but anyhow I'm using Emacs 25.1.50.18

  • Actually, I think the intention of the C-h here is to backspace, not delete, so the W is printed on top of the _ character, leading to an underlined W. It is only on input that C-h is interpreted as “delete backwards”. – Harald Hanche-Olsen May 4 '16 at 15:17
  • @Harald: that seems credible (the "W" is supposed to be a mathematical symbol in the quote). But I'm not aware of "how to see the underlined W"... I don't see it in a terminal, e.g. – George McNinch May 4 '16 at 15:23
  • ... I've definitely seen this ^H in fortunes before. I admit, this is something of a frivolous question, but I thought I might learn something useful by asking it... – George McNinch May 4 '16 at 15:25
  • If you pipe the output into less, you'll see the underlined W. Of course, this emulates a printer that would print the _ on the paper, then backspace over it, and print a W in the same spot. – Harald Hanche-Olsen May 4 '16 at 15:29
  • nice! So can one coerce shell-command-to-string into imitating the behavior of the terminal window (which I guess mus treat the backspace as "delete backwards"? – George McNinch May 4 '16 at 15:34
2

On old teletypewriters, the BS control character, written ^H, \008 or \b, would backspace the print head by one character position. Underlining could be achieved by overstriking a character with an underline (_^HH_^Hi), and bolding could be achieved by overstriking a character with itself (B^HBy^Hye^He).

Teletypewriters no longer exist, but a number of Unix commands (such as nroff and man) still produce this kind of sequences, and they are embedded in old text files (such as the fortune data files). Unix pagers (more, less) interpret them by converting to the right terminal control sequences for underline and bold.

In your case, the simplest solution would be to discard the overstriking by removing the character preceding the BS and the BS itself:

(replace-regexp-in-string ".\008" "" string)

A better solution would be to interpret the overstriking and generate text properties for bold and underline. There are examples of that in the Emacs sources, e.g. Man-fontify-manpage in man.el or woman-man-buffer in woman.el.

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.