16

A dash (-), an en-dash (–) and an emdash (—) are different but difficult to tell apart. This causes problems e.g. when writing programs. Is there some way to tell them apart easier in emacs? Thanks.

  • Can you talk about a use case about those dashes causing problem when writing code? The only dash I have come across while reading and writing code is the regular dash. I use n-dash and m-dash only in org-mode documentation using -- and ---. – Kaushal Modi Feb 27 '15 at 0:53
  • So the problem is not with the code in emacs but from something you copied from a web page. It's likely that the person who wrote that article did so in org-mode where -- will render as n-dash unless it is wrapped in verbatim or code formatting like =command --some-arg=. – Kaushal Modi Feb 27 '15 at 1:00
  • I believe some? all? flavors of markdown also support that which is much more prevalent than org mode for writing blogs. So that is a very likely issue. – Kaushal Modi Feb 27 '15 at 1:04
  • 1
    Surely that first one is a hyphen, not a dash at all. – TRiG Feb 27 '15 at 7:04
10

Here is a little fun with Unicode smiley faces. The en-dash visually becomes a black smiley face. The em-dash visually becomes a white smiley face. Note, some Emacs versions may not support the smiley face Unicode characters -- it is only meant to be a demonstration -- other Unicode characters can of course be chosen. Other faces can be used, and the user is free to compose his / her own faces.

(let ((glyph-en-dash (make-glyph-code ?\u263A 'font-lock-keyword-face))
      (glyph-em-dash (make-glyph-code ?\u263B 'font-lock-function-name-face)) )
  (when (not buffer-display-table)
    (setq buffer-display-table (make-display-table)))
  (aset buffer-display-table 8211 `[,glyph-en-dash])
  (aset buffer-display-table 8212 `[,glyph-em-dash]))

Here is an updated example that effectively concatenates the hyphen-minus and adds color -- i.e., the 8211 visually becomes -- with the font-lock-keyword-face as coloration; and, the 8212 visually becomes --- with the font-lock-function-name-face as coloration.

(let ((glyph-en-dash (make-glyph-code ?\u002D 'font-lock-keyword-face))
      (glyph-em-dash (make-glyph-code ?\u002D 'font-lock-function-name-face)) )
  (when (not buffer-display-table)
    (setq buffer-display-table (make-display-table)))
  (aset buffer-display-table 8211 `[,glyph-en-dash ,glyph-en-dash])
  (aset buffer-display-table 8212 `[,glyph-em-dash ,glyph-em-dash ,glyph-em-dash]))
  • Can the en-dash be rendered as -- and em-dash as --- with a distinct face like bold red so that the user knows that emacs has rendered those differently? – Kaushal Modi Feb 27 '15 at 1:35
  • 2
    @kaushalmodi -- I updated the example to include 8211 as -- with the font-lock-keyword-face; and the 8212 as --- with the font-lock-function-name-face. – lawlist Feb 27 '15 at 1:47
9

To quote the docs:

C-x = runs the command what-cursor-position, which is an interactive compiled Lisp function in simple.el.

It is bound to C-x =.

(what-cursor-position &optional DETAIL)

Print info on cursor position (on screen and within buffer). Also describe the character after point, and give its character code in octal, decimal and hex.

For a non-ASCII multibyte character, also give its encoding in the buffer's selected coding system if the coding system encodes the character safely. If the character is encoded into one byte, that code is shown in hex. If the character is encoded into more than one byte, just "..." is shown.

In addition, with prefix argument, show details about that character in *Help* buffer. See also the command `describe-char'.

Emphasis and formatting mine.

Try to remember these codes:

  • HYPHEN-MINUS 45
  • EN DASH 8211
  • EM DASH 8212

But if you forget them, you can easily do C-u C-x =, which pops up a buffer with extra info, including a name: field.

  • 1
    what is the difference between hyphen, minus and dash in terms of their glyphs? – Tim Feb 27 '15 at 2:08
  • HYPHEN-MINUS is the canonical Unicode name for the plain dash, which you can usually type by pressing the key to the left of = key. It's the "regular" dash used everywhere in programming. – PythonNut Feb 27 '15 at 2:29
  • when is it called a dash and when a hyphen? – Tim Feb 27 '15 at 2:30
  • @Tim As far as we are concerned, they are synonyms. The Unicode standards body calls it a hyphen. You call it a dash.... neither of you are wrong. – PythonNut Feb 27 '15 at 2:31
  • 4
    @Tim. In English orthography, hyphens connect things. There's a hyphen in twenty-three, for example. Dashes separate things. I wouldn't call that a dash at all. – TRiG Feb 27 '15 at 7:06
6

Use library highlight-chars.el to highlight any Unicode chars anyway you want. See Highlight Characters on Emacs Wiki.

You can use command hc-highlight-chars to do highlight characters in four ways:

  1. individually
  2. using ranges
  3. using character classes (e.g. [:digit:])
  4. using character sets (e.g. iso-8859-1 or lao)

hc-highlight-chars prompts you for the character(s) to highlight in a particular face and the face to use for them. (With a prefix arg it unhighlights.)

When you are prompted for the character(s) to highlight, you can use C-x 8 RET to choose Unicode characters using completion.

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.