Show/highlight invisible characters like zero-width-space in Emacs

In my pursuit to migrate from Vim to Emacs, I have stumbled on yet another roadblock.

When working with files that contain special whitespace characters, Vim/Neovim would automatically highlight these. This saved me a lot of time during debugging or data analysis, and is a functionality that I struggled to get to work on more modern IDEs.

However, this does not work out-of-the-box neither on vanilla Emacs nor Doom Emacs. I am unable to find any working solutions online. I assumed whitespace-mode would have handled this, but it is not the case.

It would be really helpful if the community here can help solve my problem as I deal with such characters on a daily basis. Until then, I have to pause my pursuit and stick with the trusty Neovim.

::: spoiler U+200B in Neovim

Notice Neovim highlighting the character as <200B>.

:::

::: spoiler U+200B in Doom Emacs

Notice the think cursor between "hello" and "world".

:::


Thanks to the suggestion by @nmtake@lemm.ee, glyphless-display-mode allows me to view the characters. But it still doesn't play well with vim motions on Emacs.

Here is a demonstration, and below are the keystrokes.

  1. C-v to enable VISUAL-BLOCK mode.
  2. 9j to select all 9 occurrences.
  3. d to delete the selection.

The above vim-motion works on Neovim but not on Emacs with evil-mode.

If anyone wants to try out here is the text I am playing with:

hello ​ world
hello ​ world
hello ​ world
hello ​ world
hello ​ world
hello ​ world
hello ​ world
hello ​ world
hello ​ world
hello ​ world
11 points · 4 comments · view on lemmy.world

4 Comments

nmtake@lemm.ee · 3 pts · 2y (3 replies)
AusatKeyboardPremi@lemmy.world · 2 pts · 2y (2 replies)

Thanks. This helped me highlight the characters. But it still doesn't play well with vim motions on Emacs.

Here is a demonstration, and below are the keystrokes.

  1. C-v to enable VISUAL-BLOCK mode.
  2. 9j to select all 9 occurrences.
  3. d to delete the selection.

The above vim-motion works on Neovim but not on Emacs with evil-mode.

If anyone wants to try out here is the text I am playing with:

hello ​ world
hello ​ world
hello ​ world
hello ​ world
hello ​ world
hello ​ world
hello ​ world
hello ​ world
hello ​ world
hello ​ world
nmtake@lemm.ee · 2 pts · 2y (1 reply)

I don't know why the motion didn't work in Evil mode, but if the goal is deleting all invisible Unicode characters, I'd write a command like this:

(defun my/delete-invisibles-in-region (start end)
  "Delete invisible characters in the region specified with START and END."
  (interactive "r")
  (save-excursion
    (replace-regexp "\u200B\\|\u200C" "" nil start end))
    ;; (query-replace-regexp "\u200B\\|\u200C" "" nil start end))
  (deactivate-mark))
AusatKeyboardPremi@lemmy.world · 1 pts · 2y

Thanks again! I already have shell scripts to take care of such characters for me, which operate on entire files. Having a function like this would help for certain regions of a file. :-)

However, it does bug me a bit that some vim motions do not work exactly as intended. Going in, I knew evil-mode would have some gaps. But I assumed those would be some esoteric operations, and not something that I use daily.