24

I can't seem to figure out how to open Sublime Text 2 from Bash on Ubuntu on Windows. Ultimately I would like to be able to use subl . to open the contents of the directory I am in.

On my machine, Sublime Text 2 is installed at C:\Program Files\Sublime Text 2. sublime_text.exe is located in this directory. In Bash on Ubuntu on Windows, I can access any files located on C through /mnt/c/.

I tried setting an alias: alias subl="/mnt/c/Program\ Files/Sublime\ Text\ 2/sublime_text.exe". However, upon trying subl . I get the following error:

cannot execute binary file: Exec format error

How can I set this up properly?

Sara Fuerst
  • 5,688
  • 8
  • 43
  • 86

7 Answers7

45

It's now possible to launch Windows executables (like Sublime Text) from the Bash on Ubuntu command line.

You should also be able to set an alias to use it from the command line (as you were trying to do). Mine is as follows (and works fine):

alias subl='"/mnt/c/Program Files/Sublime Text 3/subl.exe"'

Note the second set of quotes: this is necessary because of the spaces in the file path. Alternatively you can escape the spaces (as you did in your example).

Nick F
  • 9,781
  • 7
  • 75
  • 90
  • 2
    Impressive - if anyone is interested in how this works, read/watch [this](https://blogs.msdn.microsoft.com/wsl/2016/10/19/windows-and-ubuntu-interoperability/) – dahrens Feb 02 '17 at 22:55
  • Me too. In my case it's Atom, but I guess it stems from the same issue. – Naoto Ida Mar 09 '17 at 07:32
  • still, this keeps the terminal window busy, and when u exit with ctrl+c, the sublime windows is closed. can this be prevented? – Ulf Gjerdingen Jul 21 '19 at 12:20
  • This gets a little closer, but my main use is to invoke the `subl` command to edit a file from the command line (i.e `subl -n some_file.json`), so pointing an alias to the windows wont help. I created a separate installation of sublime from within WSL, however, the command doesn't successfully open a new window. Has anyone found a workaround for this? – Jonathan Apr 16 '20 at 19:42
  • @Jonathan Hmm, that's odd: I use the above alias exactly as you describe eg. `subl -n some_file.json` from within WSL and it works fine for me. – Nick F Apr 16 '20 at 21:37
  • @NickF weird. did you have to manually configure a Linux windows server? BTW, I was able to find a sublime plugin that works great, check out [RemoteSubl](https://github.com/randy3k/RemoteSubl). I prefer this method, because you can manage a single windows sublime installation and not have to reconfigure for each limux VM – Jonathan Apr 20 '20 at 00:30
  • 1
    This method does not seem to work properly on `wsl2`. After I try to execute `subl ~ / .zshrc`, I can indeed evoke the sublime program, but what is displayed is not the content of the file` .zshrc`. Twenty re-created a `.zshrc` file with the file path showing ` \\\ root\ .zshrc` – zhuziyi Jul 16 '22 at 05:33
11

Nick F's answer is correct, but there is an important caveat.

If your intent is to edit Linux files using Sublime Text, then you should reconsider. Doing so can cause data corruption, as I've discovered first hand.

See here for more details on why: https://blogs.msdn.microsoft.com/commandline/2016/11/17/do-not-change-linux-files-using-windows-apps-and-tools/

I realize this is an old post, but I'm hoping this will serve as a warning to other users attempting to set up Sublime Text to edit Linux files.

Sam D
  • 111
  • 1
  • 3
  • 1
    This is true - you definitely don't want to edit Ubuntu system files (eg. those in your `/home` directory) in Sublime (or any other Windows app). But as far as I'm aware there shouldn't be any problems with editing files in project directories (eg. `/mnt/c/www`) that live outside the Linux home directory. I've been doing this most days since January with no problems at all. Your point is definitely worth knowing though, because I know from experience that it's possible to mess up WSL by editing Linux files in the home directory from Windows. – Nick F Aug 08 '17 at 21:35
  • 1
    I would like to add that in addition to not being recommendable, trying to open Ubuntu system files with a Windows installation of Sublime Text will result in the opened file being blank/empty. At least this is what I have experienced in my installation of WSL (Ubuntu 16.04). – Bremsstrahlung Dec 27 '18 at 20:01
  • @Bremsstrahlung any fix to this? – amzon-ex Sep 23 '21 at 14:23
  • 1
    Note that while this was true in 2017, Windows Subsystem for Linux fixed this issue back in 2019, as mentioned at the top of the linked article. – coredumperror Jun 27 '22 at 20:27
6

Creating alias will not let you use

subl path_to_file

instead, you can just link the subl application to your /bin folder

 sudo ln -s /mnt/c/Program\ Files/Sublime\ Text\ 3/subl.exe /bin/subl

Done!!

If there is not /bin folder do whereis ls and replace /bin with the output directory

ps- /bin is a standard directory in Unix-like OS that contains the executable(short form of binaries)

Aakash Dantre
  • 61
  • 1
  • 5
1

It can be done.

You can edit remote files with your local Sublimeusing RemoteSubl, a SublimeText plugin:

https://github.com/randy3k/RemoteSubl

Works out of the box for this scenario, and also takes care about diferent file subsystems don't messing around each other.

If anyone wants to improve.. a condition to detects if the file is in the windows or linux realm, and lanch the appropiate editor... with a different theme... Geez! that would be great.

elMestre
  • 394
  • 4
  • 7
0

https://github.com/grigger/subl-wsl

Here's a more complete script I just wrote starting from some stackoverflow answers, that will open in sublime both files with Windows or Linux paths.

i.e. subl ~/something will open sublime in C:\Users\<usr>\AppData\Local\Packages\...\LocalState\rootfs subl /mnt/d/something will open D:\something

I know it's quite a while after the answer, but maybe some other folks will be helped by this in the future.

grigger
  • 81
  • 1
  • 4
  • I tried your method with a test `~/my.cnf` file in my home directory. It opened `Sublime Text 3` with the correct file...however, as soon as I made a change and hit `CTRL+S` to save the file, `Sublime Text 3` completely froze! What do you suggest? – Paul D. Apr 16 '20 at 12:43
0

Just want to highlight to other users that there was indeed an update in WSL in May 2019, based on my reading it is now OK to edit Linux files from windows, as long as you use the 9P fileserver (e.g \\wsl$\). This is a HUGE improvement!

Jonathan
  • 781
  • 8
  • 20
0

This one is working for me by adding it in .zshrc Debian in WSL with Windows 10 Pro

alias subl="/mnt/c/Program\ Files/Sublime\ Text\ 3/sublime_text.exe"
joanis
  • 10,635
  • 14
  • 30
  • 40
Parthiban
  • 1
  • 2