The 2024 Wheel Reinvention Jam is in 5 days. September 23-29, 2024. More info

Using .bat to start Sublime

I followed the steps in "Day 001" to create a batch file to open up my text editor (text below).

If I run the .bat file from the directory that it is in, it will open up sublime okay, but it will not open if I run the .bat file from a parent directory (i.e.: w:\handmade or even w:\) like it does in the video.

Also, when it opens (from w:\handmade\misc) the cursor in the cmd window moves down one line, the line is blank, and I cannot enter any commands. When I close Sublime, the cursor in the cmd window moves down another line to an actual prompt. Is this just a feature of Sublime? Is there a way to run sublime from the cmd window and not freeze up the cmd window?

I tried following 'Using Sublime Text 3 for Handmade Hero (by Matthew VanDevander)' but I had the same issues.

<w:\handmade\misc\sublime.bat>
---------------
@echo off
"C:\Program Files\Sublime Text 3\sublime_text.exe"
In your sublimetext directory should be a subl.exe
try pointing to that exe in your sublime.bat should resolve that freezing,
at least it did for me
I'm not familiar with Sublime, but I'm pretty sure that is not a feature of Sublime. That is simply how processes work on Windows commandline (and Linux, OSX,...). When you invoke some other program (in your case submlime_text.exe), parent process (cmd.exe window) will wait till it finishes, and only then resume waiting more input.

If you want to launch other program (sublime_text.exe) and not wait till it ends, you need to invoke it differently:
1
start "C:\Program Files\Sublime Text 3\sublime_text.exe"
thrahistan
In your sublimetext directory should be a subl.exe
try pointing to that exe in your sublime.bat should resolve that freezing,
at least it did for me


:cheer: that worked!
thanks.

Edited by Gregory on
mmozeiko
If you want to launch other program (sublime_text.exe) and not wait till it ends, you need to invoke it differently:
1
start "C:\Program Files\Sublime Text 3\sublime_text.exe"


In my edit.bat I can run one of four editors
atom
emacs
notepad++
sublimetext3

only notepad++ keeps the console window claimed.
I think it's up to the invoked .exe what it does with the console because
subl.exe works for sublimetext3 but not the sublimetext3.exe itself.

Or a whole other reason behind this we don't understand. :)
[quote=thrahistan]only notepad++ keeps the console window claimed.
I think it's up to the invoked .exe what it does with the console because
subl.exe works for sublimetext3 but not the sublimetext3.exe itself.

Add quotation marks as parameter and it works without blocking.
1
start "" "C:\Program Files\Sublime Text 3\sublime_text.exe"


I've created a cmd shortcut with the following target in the properties:
1
%windir%\system32\cmd.exe /k w:\misc\shell.bat & start "" "C:\Program Files (x86)\Notepad++\notepad++.exe" & devenv

This will startup the whole environment for me. You could also get rid of the shell.bat, by runnig vcvarsall.bat directly.

For more info about cmd, bash etc. have a look at http://ss64.com

Edited by sinsizer on
sinsizer

Add quotation marks as parameter and it works without blocking.
1
start "" "C:\Program Files\Sublime Text 3\sublime_text.exe"


Thats amazing!


sinsizer

I've created a cmd shortcut with the following target in the properties:
1
%windir%\system32\cmd.exe /k w:\misc\shell.bat & start "" "C:\Program Files (x86)\Notepad++\notepad++.exe" & devenv

For more info about cmd, bash etc. have a look at http://ss64.com


Thanks I will have a look :)
If you check the Coding Resources link, Matthew VanDevander wrote a guide to use Sublime Text 3, you might want to check it out
I have an st.bat in my misc directory that looks like this:

1
start "" "D:\Programs\Sublime Text 2\sublime_text.exe" %*


IIRC, the %* will forward any command line options you pass to the batch file onto the command, so you can type things like

1
>st win32_handmade.cpp


...to open or create particular files. But it's been a while since I made it, so I'm not sure if it's actually necessary to get that kind of functionality.

I haven't tried it with subilme text, but I did it slightly different with notepad++, and used a doskey= command in the shell.bat script (the one that gets auto executed when opening the cmd shortcut). It looks like this:

1
doskey np="D:\Programs\Notepad++\notepad++.exe" $*


...for similar functionality as above but with np rather than st. These two methods are probably interchangeable, but I haven't actually tried both with the same program. AFAICT, doskey is sort of like the window version of unix's alias?

Note the difference: %* and $*. Someone more knowledgeable at batch files can probably explain this, but if I had to guess it's because %* is a variable within the batch file (note the common % usage) whereas the $* is maybe just a command line option for the doskey command. Pretty much guessing here, though.

Edited by Nines Baobaberson on Reason: accidentally a word
midnight_mero
Note the difference: %* and $*. Someone more knowledgeable at batch files can probably explain this, but if I had to guess it's because %* is a variable within the batch file (note the common % usage) whereas the $* is maybe just a command line option for the doskey command. Pretty much guessing here, though.


I think you're right @midnight_mero. The doskey documentation suggests that [color=#44bb00]"everything you type on the command line after the macro name is substituted for the $* in the macro"[/color].
my edit.bat file content looks like this now:

1
2
3
4
5
6
7
@echo off

IF /i %1==atom (atom %2)
IF /i %1==emacs ("D:\Users\Andi\Desktop\emacs 24.4\bin\runemacs.exe" %2 & devenv)
IF /i %1==ntp (start "" "C:\Program Files (x86)\Notepad++\notepad++.exe" %2)
IF /i %1==sublime ("C:\Program Files\Sublime Text 3\subl.exe" %2)
cls


commands like
edit atom
or
edit atom somefilename.somefileextension
would work both

and for now I don't need any further parameter for the editors i use so %2 fits my needs.

I cannot say something about the "$*" parameter, never seen this in a terminal before and I'm just new to working with cmd/powershell

Edited by Andre Bauland on