Search code examples
vimunicodemingwarabicmsys2

How can I get vim to render Arabic text correctly?


I'm learning Arabic and I'd like to use Vim for taking notes. I tried a few different setups on Windows 10 and so far the closest to a good setup seems to be vim in MSYS2. MSYS2 renders Arabic text correctly, written from right to left, but when I open the file in Vim, while the text is still written from the right to left with Arabic characters, the letters are not connected.

Here are a few examples using cat:

سريع
اعتن بنفسك
بطيء
أن تقلق
لا تقلق

And the same ones from Vim:

ﺱﺮﻴﻋ
ﺎﻌﺘﻧ ﺐﻨﻔﺴﻛ
ﺐﻄﻳﺀ
ﺄﻧ ﺖﻘﻠﻗ
ﻻ ﺖﻘﻠﻗ

Looking up the first word of the terminal output on http://unicode.scarfboy.com/ gives me these:

Constituent codepoints:
   0633   ARABIC LETTER SEEN
   0631   ARABIC LETTER REH
   064A   ARABIC LETTER YEH
   0639   ARABIC LETTER AIN

And the codes for the same word in Vim are:

Constituent codepoints:
   FEB1   ARABIC LETTER SEEN ISOLATED FORM
   FEAE   ARABIC LETTER REH FINAL FORM
   FEF4   ARABIC LETTER YEH MEDIAL FORM
   FECB   ARABIC LETTER AIN INITIAL FORM

This looks like it's interpreting the text the wrong way around, the first letter should be in the initial form and the last one should be in its final form.

When I try entering :set arabic, the direction of writing changes for both Arabic and Latin text, making everything backwards, but the characters are rendered correctly (just at the wrong place).

Is there maybe a way to make vim output text the same way the terminal does? Maybe just change which characters are considered first and last, because MSYS2 is outputting the text correctly.

On a side note, my preferred setup would be to use vim in WSL, inside Windows Terminal, but that's not that important as long as I end up with something usable.


Solution

  • Unicode bidirectionality (bidi) offers the user the ability to view both right-to-left as well as left-to-right text properly at the same time within the same window. Vim, due to simplicity, does not offer bidi. Instead, it provides commands to make switching between the two directions easier.

    " switch to right-left mode (arabic)
    :set rl
    " switch to left-right mode (english)
    :set norl
    

    However, if your terminal supports bidi, you can tell Vim that the terminal is in charge of text direction with the termbidi setting:

    set termbidi
    

    If termbidi is set, changing the arabic setting will now only affect your keyboard mapping and the delcombine, which allows you to delete overlapping characters (fathah + letter) individually.