Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save unicornist/453c024a4941865073717492032099b2 to your computer and use it in GitHub Desktop.
Save unicornist/453c024a4941865073717492032099b2 to your computer and use it in GitHub Desktop.
SublimeText Macros to convert some Arabic chars (yeh, keh, digits) and convert English digits to their correct Persian alternatives.

What is this?

There is a popular demand with developers dealing with Persian text to fix some incorrect characters which is often caused by bad/old Persian keyboard layouts. It is a time consuming find and replace operation, which would be great if recorded into separate macros. But unfortunately SublimeText lacks the ability of recording find and replace operations in its macros. These SublimeText macro files, use the insert_snippet command instead, with some regular expression substitution, to replace the following incorrect Persian characters with their correct alternatives.

  • Replace Arabic yeh and keh with Persian yeh and keh. (ي to ی and ك to ک)
  • Replace Arabic digits with Persian digits. (٠١٢٣٤٥٦٧٨٩ to ۰۱۲۳۴۵۶۷۸۹)
  • Replace English digits with Persian digits. (0123456789 to ۰۱۲۳۴۵۶۷۸۹)

How to install

  1. Download the macro files.
  2. In SublimeText's menu go to PreferencesBrowse Packages...
  3. In the opened folder go to the User directory.
  4. Move the downloaded files here.
  5. For using with shortcuts, from SublimeText's menu go to Preferences → Key Bindings - User.
  6. In the opened file, add the content from the following OS-User.sublime-keymap files in between the brackets. (Make sure to add the required comma, if you already have any key-bindings)
  7. You could obviously change the shortcuts to your desire.

How to use

  1. Have the Persian text containing errors in front of you (either as a saved file or as an unsaved text).
  2. In SublimeText's menu go to ToolsMacrosUser → and select the one you need.

Notes

  • Tested only on SublimeText 3 (So please report about ST2 if it has any problems or works as desired).
  • Not tested with huge data, It might become slow with big files since it is using insert_snippet command internally.
[
{"command": "insert_snippet", "args": {"contents": "${SELECTION/(?<1>\\x{661})|(?<2>\\x{662})|(?<3>\\x{663})|(?<4>\\x{664})|(?<5>\\x{665})|(?<6>\\x{666})|(?<7>\\x{667})|(?<8>\\x{668})|(?<9>\\x{669})|(?<10>\\x{660})|(?<11>\\x{64A})|(?<12>\\x{643})/(?{1}\\x{6F1}:(?{2}\\x{6F2}:(?{3}\\x{6F3}:(?{4}\\x{6F4}:(?{5}\\x{6F5}:(?{6}\\x{6F6}:(?{7}\\x{6F7}:(?{8}\\x{6F8}:(?{9}\\x{6F9}:(?{10}\\x{6F0}:(?{11}\\x{6CC}:\\x{6A9})))))))))))/gm}"}}
]
[
{"command": "select_all", "args": null},
{"command": "insert_snippet", "args": {"contents": "${SELECTION/(?<1>\\x{661})|(?<2>\\x{662})|(?<3>\\x{663})|(?<4>\\x{664})|(?<5>\\x{665})|(?<6>\\x{666})|(?<7>\\x{667})|(?<8>\\x{668})|(?<9>\\x{669})|(?<10>\\x{660})|(?<11>\\x{64A})|(?<12>\\x{643})/(?{1}\\x{6F1}:(?{2}\\x{6F2}:(?{3}\\x{6F3}:(?{4}\\x{6F4}:(?{5}\\x{6F5}:(?{6}\\x{6F6}:(?{7}\\x{6F7}:(?{8}\\x{6F8}:(?{9}\\x{6F9}:(?{10}\\x{6F0}:(?{11}\\x{6CC}:\\x{6A9})))))))))))/gm}"}}
]
[
{"command": "insert_snippet", "args": {"contents": "${SELECTION/(?<1>1)|(?<2>2)|(?<3>3)|(?<4>4)|(?<5>5)|(?<6>6)|(?<7>7)|(?<8>8)|(?<9>9)|(?<10>0)/(?{1}\\x{6F1}:(?{2}\\x{6F2}:(?{3}\\x{6F3}:(?{4}\\x{6F4}:(?{5}\\x{6F5}:(?{6}\\x{6F6}:(?{7}\\x{6F7}:(?{8}\\x{6F8}:(?{9}\\x{6F9}:\\x{6F0})))))))))/gm}"}}
]
[
{"command": "select_all", "args": null},
{"command": "insert_snippet", "args": {"contents": "${SELECTION/(?<1>1)|(?<2>2)|(?<3>3)|(?<4>4)|(?<5>5)|(?<6>6)|(?<7>7)|(?<8>8)|(?<9>9)|(?<10>0)/(?{1}\\x{6F1}:(?{2}\\x{6F2}:(?{3}\\x{6F3}:(?{4}\\x{6F4}:(?{5}\\x{6F5}:(?{6}\\x{6F6}:(?{7}\\x{6F7}:(?{8}\\x{6F8}:(?{9}\\x{6F9}:\\x{6F0})))))))))/gm}"}}
]
[
{"command": "select_all", "args": null},
{"command": "insert_snippet", "args": {"contents": "${SELECTION/(?<1>1)|(?<2>2)|(?<3>3)|(?<4>4)|(?<5>5)|(?<6>6)|(?<7>7)|(?<8>8)|(?<9>9)|(?<10>0)/(?{1}\\x{6F1}:(?{2}\\x{6F2}:(?{3}\\x{6F3}:(?{4}\\x{6F4}:(?{5}\\x{6F5}:(?{6}\\x{6F6}:(?{7}\\x{6F7}:(?{8}\\x{6F8}:(?{9}\\x{6F9}:\\x{6F0})))))))))/gm}"}},
{"command": "select_all", "args": null},
{"command": "insert_snippet", "args": {"contents": "${SELECTION/(?<1>\\x{661})|(?<2>\\x{662})|(?<3>\\x{663})|(?<4>\\x{664})|(?<5>\\x{665})|(?<6>\\x{666})|(?<7>\\x{667})|(?<8>\\x{668})|(?<9>\\x{669})|(?<10>\\x{660})|(?<11>\\x{64A})|(?<12>\\x{643})/(?{1}\\x{6F1}:(?{2}\\x{6F2}:(?{3}\\x{6F3}:(?{4}\\x{6F4}:(?{5}\\x{6F5}:(?{6}\\x{6F6}:(?{7}\\x{6F7}:(?{8}\\x{6F8}:(?{9}\\x{6F9}:(?{10}\\x{6F0}:(?{11}\\x{6CC}:\\x{6A9})))))))))))/gm}"}}
]
{ "keys": ["ctrl+alt+f", "a"], "command": "run_macro_file", "args": {"file": "Packages/User/Persian CharFix Arabic.sublime-macro"} },
{ "keys": ["ctrl+alt+f", "ctrl+a"], "command": "run_macro_file", "args": {"file": "Packages/User/Persian CharFix Arabic (Selection).sublime-macro"} },
{ "keys": ["ctrl+alt+f", "e"], "command": "run_macro_file", "args": {"file": "Packages/User/Persian DigitFix English.sublime-macro"} },
{ "keys": ["ctrl+alt+f", "ctrl+e"], "command": "run_macro_file", "args": {"file": "Packages/User/Persian DigitFix English (Selection).sublime-macro"} },
{ "keys": ["ctrl+alt+f", "f"], "command": "run_macro_file", "args": {"file": "Packages/User/Persian EveryFix.sublime-macro"} }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment