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.
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 thefont-lock-keyword-face
; and the8212
as---
with thefont-lock-function-name-face
. – lawlist Feb 27 '15 at 1:47
To quote the docs:
C-x = runs the command
what-cursor-position
, which is an interactive compiled Lisp function insimple.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
45EN DASH
8211EM 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.
-
1what 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 -
-
@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
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:
- individually
- using ranges
- using character classes (e.g.
[:digit:]
) - using character sets (e.g.
iso-8859-1
orlao
)
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.
--
and---
. – Kaushal Modi Feb 27 '15 at 0:53org-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