@edit on Windows

I often use @which but would also like to use @edit.

On Windows @edit doesn’t seem to work for me by default, so I did some investigating and managed to get something that works.
Sharing here in case it helps any other long-suffering Windows users.
Also some questions below:

I managed to get @edit working with Atom, Sublime Text and Notepad++.

If you don’t want to read the long story below, just put one of these into your .juliarc.jl file.

ENV["JULIA_EDITOR"] = "\"C:\\Users\\plowman\\AppData\\Local\\atom\\bin\\atom.cmd\""
ENV["JULIA_EDITOR"] = "\"C:\\Program Files\\Sublime Text 3\\sublime_text.exe\""
ENV["JULIA_EDITOR"] = "\"C:\\Program Files (x86)\\Notepad++\\notepad++.exe\""

Note the extra escaped quotes \"
Notepad++ requires some extra steps, read on.

Firstly it seems the default editor for Windows is open which results in edit() running the command:

`cmd /c start /b $path`

This doesn’t work for me.

So I tried setting JULIA_EDITOR, but without “double”-quoting the path gets split.

julia> ENV["JULIA_EDITOR"] = "C:\\Program Files\\Sublime Text 3\\sublime_text.exe"
"C:\\Program Files\\Sublime Text 3\\sublime_text.exe"

julia> Base.editor()
4-element Array{AbstractString,1}:
 "C:Program"
 "FilesSublime"
 "Text"
 "3sublime_text.exe"

editor() calls shell_split().
1. Is this the expected behaviour of shell_split()?

One way around this is to “double”-quote the path:

julia> ENV["JULIA_EDITOR"] = "\"C:\\Program Files\\Sublime Text 3\\sublime_text.exe\""
"\"C:\\Program Files\\Sublime Text 3\\sublime_text.exe\""

julia> Base.editor()
1-element Array{AbstractString,1}:
 "C:\\Program Files\\Sublime Text 3\\sublime_text.exe"

This it relatively easy (although unintuitive) when setting JULIA_EDITOR from within Julia.
However, I couldn’t find a way to make this work by setting the Windows environment variable JULIA_EDITOR.
2. Does anyone know how to set JULIA_EDITOR in Windows so path is preserved as single string?

OK so this works for Atom, Sublime Text and Notepad++.

ENV["JULIA_EDITOR"] = "\"C:\\Users\\plowman\\AppData\\Local\\atom\\bin\\atom.cmd\""
ENV["JULIA_EDITOR"] = "\"C:\\Program Files\\Sublime Text 3\\sublime_text.exe\""
ENV["JULIA_EDITOR"] = "\"C:\\Program Files (x86)\\Notepad++\\notepad++.exe\""

For Atom and SublimeText, existing code in edit() (in interactiveutil.jl) handles the command line options to open the file at the appropriate line number.

For Notepad++ the following snippet needs to be inserted into edit():

elseif startswith(name, "notepad++")
        cmd = line != 0 ? `$command $path -n$line` : `$command $path`

3. Does anyone else use Notepad++? Is this worth adding to base?

Hope this helps.
Would also like to hear other Windows users experience with @edit

3 Likes

Maybe you need to set a default application for .jl files?

Yes that’s a good point.
I checked this in Control Panel -> Default Programs, and I can also open .jl files by clicking in Windows Explorer, so I think this is OK.

After some further investigation, I think I have narrowed down the issue for me:

As an example @edit cmp(1,2) attempts to run the command:

cmd /c start /b 'C:\Program Files\Julia-0.5.0\bin\../share\julia\base\operators.jl'

I think there are two issues here:

  1. The space in file path: start tries run C:\Program. I tried many variations of extra quoting with single and double quotes but couldn’t get it to work.
  2. The forward slash in path (../share)

So my workaround is:

cmd = `cmd /c start /b /D $(realpath(dirname(path))) $(basename(path))`

which runs the command:

cmd /c start /b /D 'C:\Program Files\Julia-0.5.0\share\julia\base' operators.jl

For some reason space is OK in directory /D, realpath removes the forward slash.

However the default open still has the limitation of not supporting line number.

This path indeed comes from base/build_h.jl and is computed at compile time by the contrib/relative_path.sh script. I’m not sure what’s the best solution to fix this: either improve the script to detect Windows or fix the path later from Julia. Could you file an issue on GitHub to discuss the best approach?

GitHub issues opened:

  • @edit on Windows using default editor: #20364
  • Setting JULIA_EDITOR in Windows: #20365
  • Adding Notepad++ support for @edit: #20366

Not sure if this is still useful, but to open a file with the default program on Windows (without spawning cmd), the command is explorer filename.ext.

Yes this does work. Thanks.
It seems to allow spaces in filename, but not forward slashes. So maybe requires realpath:

cmd = `explorer $(realpath(path))`

Another suggestion was made over on GitHub `@edit` on Windows using default editor · Issue #20364 · JuliaLang/julia · GitHub
Indeed the following appears to overcome both the spaces and forward slash issues:

cmd = `cmd /c start /b "" $path`