15 3

# External Editor

<10kB. Updated 2020-07-27.

## Description

Replaces built-in html editor with the one of your choice. Next time you press Ctrl + x while editing a field it'll try to execute your configured editor. I created this add-on because I had spent lot of time copying from Anki to vim, editing, and copying back. It allows you to specify your editor of preference. For Windows users, remember to escape (duplicate) the backslashes in the path (json uses backslash as a special character):  { "editor": "c:\\Program Files\\Notepad++\\notepad++.exe" }  Updates:
• 2020/07/26: Merged ijgnd contributions, and greatly improved OSX and Windows executable detection.
• 2020/03/04: Small bug fix for gvim swap files.
• 2020/03/03: Now it tries harder to find the specified editor, or a viable editor, or fallback to built-in editor silently.
• 2020/02/29: I added a workaround that should fix the PermissionError in Windows. Thanks for the report.

Supported Anki versions:
• 2.1.0 (Updated 2020-07-27)

To download this add-on, please copy and paste the following code into Anki 2.1:

16727059

If you were linked to this page from the internet, please open Anki on your computer, go to the Tools menu and then Add-ons>Browse & Install to paste in the code.

## Reviews

on 1647174943
Excellent !!!
But, where I can edit shorcut ?
on 1643142341
The addon works very well. I had to change the shortcut to Ctrl+Meta+X. My system must be configured by default to do something else with the default shortcut.
I'd like to set the Vim filetype on the command line. e.g.: "editor": "vim -gf -c 'set filetype=markdown'"
... but it doesn't work and I haven't found a solution. I added an issue to GitHub, but I realize this is more like a feature request than a bug.
on 1631137275
Ctrl X doesn't work.

If I do Ctrl Shift X to edit the HTML then Vim pops up however how do I exit insert mode to edit?
Why do I need this when I can't edit files in Vim.
on 1629031100
Linux+emacs+i3 user

Ctrl+X didn't work for me(or any similar combination that I could came up with), so I use "click 3 stripes icon" -> Click "Edit HTML"

In settings you can just type "emacs"(slower because of start time)
or "emacsclient"(instant launch but it needs running emacs server).

Thanks!
on 1627736875
How can I specify that I want to use vs code on mac os? If I simply type 'code' nothing happens. If I use the file path '/Applications/Visual Studio Code.app' I get an error message. No matter what I try, I can't get it to work. Any help would be appreciated.
on 1627597233
Essential add-on for fans of Vim, Emacs, etc.
on 1623301865
It used to work well, but in Anki 2.1.44 doesn't open the external editor anymore. Any help? I normally use Sublime Text, but I've tried it with Notepad++ too with same results.
on 1620822496
Works, after putting code --wait instead of just code. However, I don't see this reflected in the description nor in the text to the right of the configuration editor.
on 1614908418
Great app, exactly what I was looking for! I can finally edit the HTML a lot easier and faster.

Note for other users- to get it to launch atom on my Mac OS, I had to put in the "/usr/local/bin/atom" instead of just "atom" in the config. (Digging deeper, it seemed like this was due to a bug where it would try to run vim instead when I just put "atom", but putting the entire path fixed it.
Comment from author
Thanks for your feedback.
I will try to add some logic fallback for "atom" for the users still struggling to get the configuration working for them.
on 1600680927
It works!
on 1598796737
Works fine on W10.
Great Job!
Thank you very much
Comment from author
My pleasure it's useful for other people. Thanks for voting.
on 1598038703
Does exactly what it says. Excellent extension.
Comment from author
Thanks for your feedback :-)
on 1596463968
Same with Debian Buster & Sublime Text 3, the shortcut is Ctrl+Shift+X. Path for SB3 in my system is /usr/bin/subl
on 1595418810
Thanks for sharing this add-on. It's really useful. In fact the code is so useful that I forked it.

@the person who downvoted the add-on on 2020-07-22: VSCode works for me if you set "code -w"/"code --wait" as the "editor".
Comment from author
I'm glad it's useful. Thanks for your review. And thanks your help.
on 1595417176
Great idea for an extension.

I can get my field to open in vscode on my mac when I press Cmd-Shift-X, but I cannot get any edits I make in vscode to be reflected back in Anki.

What am I doing wrong?

Another question: Rather than edit the HTML, is it technically possible to send just the text from the field? I want to edit mathjax in vscode, with all its syntax highlighting for latex, and autocomplete functionality.

Thanks
Comment from author
- @𝘵𝘩𝘦_𝘶𝘴𝘦𝘳_𝘵𝘩𝘢𝘵_𝘧𝘰𝘳𝘬𝘦𝘥_𝘵𝘩𝘦_𝘳𝘦𝘱𝘰 is right. it'll work if you setup it to "code --wait". What happens if that 𝘃𝘀𝗰𝗼𝗱𝗲 closes the file without modifying it unless you ask it to wait for your changes. I'll explain this caveat in the description.

- About editing in plain text: it is not impossible, but if done poorly it could damage your custom html (some users rely on fine tuned html). A safer alternative is to handle it in your editor. I chose this path because there are multitude of wonderful editors that are excellent at this task, and each one is it's own universe. It would be almost impossible to me to write anything close to what some 𝘃𝘀𝗰𝗼𝗱𝗲 addon can easily offer you.
on 1583281867
This addon is exactly what I have been looking for.
I had this functionality in version 2.0 and really disliked not having it in 2.1
But I have three questions:
1) What exactly is the hotkey to execute the add-on?
2) Who is the author?
3) If you are using Windows 10 and trying to execute MSpaint would the script be:
"editor": "C:\\Windows\\System32\\mspaint.exe"
Thanks,
Stacey
Comment from author
Hi Stacey,

I'm glad it was useful for you as well.

1. It replaces the built-in editor you start with 𝗖𝘁𝗿𝗹 + 𝗫
2. I'm Carlos, but I wouldn't call me author. I just put together a bunch of code snips and hammered it until it worked. You can check the source code (and other repos) here: https://github.com/pointtonull/anki-external_editor
3. Yeah, you are right. If that's the absolute path of MSPaint in your machine. (I added the detail of escaping backslashes to the description, thanks for the heads up). But, you can also specify short paths for installed programs. I.E.: if you can execute the command from the Run Dialog (𝗪𝗶𝗻𝗱𝗼𝘄𝘀𝗞𝗲𝘆 + 𝗥), you can use it in the configuration. I understand "mspaint.exe", should work as well.
on 1583257952
Update & Fix:
I got this error below which is solved. Thanks to the author for responding.

Just write the path to your editors executable. Remember to replace any "\" with "\\" and it should be alright.
Screen shot: https://i.imgur.com/k2bYnhR.png

Debug info:
Anki 2.1.20 (47a1bf8b) Python 3.8.0 Qt 5.14.1 PyQt 5.14.1
Platform: Windows 10
Flags: frz=True ao=True sv=1
Add-ons, last update check: 2020-03-03 11:02:46

Caught exception:
Traceback (most recent call last):
File "aqt\webview.py", line 368, in handler
File "aqt\editor.py", line 443, in <lambda>
File "aqt\editor.py", line 485, in <lambda>
File "C:\Users\XIII\AppData\Roaming\Anki2\addons21\16727059\__init__.py", line 28, in edit_with_external_editor
text = edit(text, cmd)
File "C:\Users\XIII\AppData\Roaming\Anki2\addons21\16727059\__init__.py", line 17, in edit
proc = subprocess.Popen(cmd_list, close_fds=True)
File "subprocess.py", line 854, in __init__
File "subprocess.py", line 1247, in _execute_child
File "subprocess.py", line 549, in list2cmdline
File "os.py", line 816, in fsdecode
TypeError: expected str, bytes or os.PathLike object, not NoneType
Comment from author before post was edited
OK, I think I have it.

The editor you specified could not be found. In Windows you can try starting the editor from the run command box:

I edited the add-on to try harder to guess the editor to use, and to silently fallback to built-in editor if everything else fails.

Thanks for reporting.
on 1582972434
Thanks for this add-on, looks great
Unfortunately, I get an error when I try to use the HTML editor (i.e., when I press CTRL+SHIFT+X in the browser):

Debug info:
Anki 2.1.20 (47a1bf8b) Python 3.8.0 Qt 5.14.1 PyQt 5.14.1
Platform: Windows 10
Flags: frz=True ao=True sv=1
Add-ons, last update check: 2020-02-28 20:45:08

Caught exception:
Traceback (most recent call last):
File "aqt\webview.py", line 368, in handler
File "aqt\editor.py", line 443, in <lambda>
File "aqt\editor.py", line 485, in <lambda>
File "C:\Users\user\AppData\Roaming\Anki2\addons21\16727059\__init__.py", line 30, in edit_with_external_editor
text = edit(text, cmd)
File "C:\Users\user\AppData\Roaming\Anki2\addons21\16727059\__init__.py", line 14, in edit
with io.open(filename, mode='wt') as file:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\user\\AppData\\Local\\Temp\\tmpqnx0gc8a.html'

Any idea what's causing it?
Comment from author
Hi!,
Sorry for the delay. I just realized I can answer comments.
I think I may had fixed the issue, can you try again?

Thanks for reporting. This is very useful since I don't use Windows and some of it's internals are alien for me.