ChatGPT解决这个技术问题 Extra ChatGPT

Sublime Text from Command Line

I installed Sublime Text and wanted to know how to open rb files in it from the terminal. I saw What is the command to make Sublime Text my core editor? and I see that I can make Sublime my core editor, but I want to be able to type

sublime file.rb

How do I do this in Windows?

the prefix sublime isn't the important part. it could be any one word command really.

p
pjumble

Windows Command Prompt

For Windows cmd.exe you could just add the sublime text installation directory to your PATH environment variable, this would allow you to type:

sublime_text file.rb

Personally, I add a doskey (in a .bat file set to autorun with cmd) so I can type subl file.rb:

> doskey subl="C:\Program Files\Sublime Text 2\sublime_text.exe" $*

Cygwin

For the default bash shell add an alias to your ~/.bashrc file, e.g:

$ echo 'alias subl="/cygdrive/c/Program\ Files/Sublime\ Text\ 2/sublime_text.exe"' >> ~/.bashrc

Do I set up a doskey for cygwin too? or is there a different way to do that. i'm not using windows cmd
Ah, my mistake, I assumed you were using cmd. Are you using the default bash shell that comes with cygwin? If so you can just use an alias like the post you linked to: $ echo 'alias subl="/cygdrive/c/Program\ Files/Sublime\ Text\ 2/sublime_text.exe"' >> ~/.bashrc
this might help too rhyous.com/2010/10/20/…
launching SL2 from command line this way causes my SL2 copy to start with empty user preferences setting. anyone had the same problem?
note that if you do this, you can have this issue with build systems. I used simple .bat file in the sublime folder instead and put it on the PATH, works better, no build system issues.
e
ecoe

From build 3065 (Release Date: 29 August 2014) onwards Sublime text includes a command line helper, nameley subl.exe. It is at sublime's installation folder: copy it in to a folder included in the system path. For example, in my case I copied it

from C:\Program Files\Sublime Text 3

to C:\Windows\System32

You may then use in your terminal/console subl as a command to open whatever file, such as in your example:

subl file.rb

Or you may as well modify your system PATH variable to include sublime's instalation folder, but I believe that is much more involved.


I would say that adding Sublime's installation directory to PATH is more convenient way to enable Sublime command line helper. This way you'll ensure that all updates will be automatically applied. Copying subl.exe executable to windows directory will require repeating copying after every update.
Now. I have an issue. This doesn't work if sublime text is not already opened.
You should see this scotch.io/tutorials/…
"Or you may as well modify your system PATH variable to include sublime's instalation folder, but I believe that is much more involved." Simply Right click on Computer. Select properties then click on "Advanced System Settings". From there Go to Environment Variables>Path and append ;C:\Program Files\Sublime Text 3 A few more steps I'll grant you but a very easy and useful trick to know :)
Or simply create a symlink with mklink c:\Windows\System32\subl.exe "c:\Program Files\Sublime Text 3\subl.exe"
K
Kristopher Johnson

I added this to my PowerShell profile:

Set-Alias subl 'C:\Program Files\Sublime Text 2\sublime_text.exe'

Modify this as needed for Sublime Text 3 (or any future versions).


Great! Once I moved to powershell... I never looked back to cmd.
Thanks a bunch. I find PowerShell very friendly to developers coming from Linux/Mac world
great answer.. simple and straight forward +1
Yes, for powershell, it works fine. For cmd, you still have to add the path to sublime_text.exe within the windows path
I'd argue that, except in a limited set of circumstances, if you're trying to call Sublime Text from the command line you will probably also largely be using PowerShell. Happy to hear of use cases where that's not true though!
J
John Nelson

Another idea would be to include C:\Program Files\Sublime Text 2\ in your PATH, and then run an administrator command prompt:

cd "C:\Program Files\Sublime Text 2\"
mklink sublime.exe sublime_text.exe

That will make a symbolic link with the new name. And now you can use it freely:

sublime hello.txt

Update: After having a chance to use this trick and update Sublime Text 2, I'm happy to say that updating to a new build doesn't affect the symbolic link.


Btw, you elevate your Windows (7) privs by pressing Ctrl-Shift-enter after typing cmd into the Start|Run text box...
This is exactly what I was looking for. Very quick and clean
d
deadcoder0904

Add the installation folder to windows path and you will be able to open Sublime by typing the name of the executable file, which is by default "subl".

To include the Sublime installation folder in windows path,

Open command prompt and type sysdm.cpl In Advanced tab, select Environment variables Under system variables, select variable named "Path" and click Edit. Add "C:\Program Files\Sublime Text;" to the end of the existing string. Save the changes and restart command prompt.


but this dosen't open is specific folder. I mean If i am currenlty in some folder xyz , if i navigate to that folder in cmd and run subl then it will open sublime as it would normally (I wont open the specific folder i.e. xyz). How to open sublime in a specific folder.
m
mblsha

I've created subl.bat in C:\Program Files\Sublime Text 2 with contents:

start sublime_text.exe %*

Now that I have C:\Program Files\Sublime Text 2 in PATH, I can simply type 'subl folder' and it works wonderfully without having to add anything to autostart.


Thanks a lot for 'subl folder'. Didn't know that :)
I've changed subl.bat to "start sublime_text.exe %*" and now it doesn't block my console.
Instead of .bat file, you can simply create a shortcut and name it as you wish without the extensions. At least it worked for me on Windows 7.
@psycketom can you please elaborate. I have created a shortcut to the exe of sublime 2 in my project directory and called it subl. now when running subl in my console it says not recognized as an external or internal command. Only command "start subl" will open up the program
@Postscripter He means he went to Control Panel --> System --> Advanced System Settings --> Environment Variables --> and added the text C:\Program Files\Sublime Text 2; to the end of value of the variable PATH.
g
glrodasz

I think that is more easy set the Environment variable in Windows.

Then just add a new System variable called SUBLIME_HOME with value "C:\Program Files\Sublime Text 2\" (without quotes) after edit the variable Path adding in the end this value ";%SUBLIME_HOME%" (without quotes).

Restart the git BASH and enjoy, using like this:

$ sublime_text mi-new-file

(where sublime_text is the command)

Note: Also works now for cmd of Windows.


Very nice solution. Clean, quick, easy, no messing with my main %PATH%. Works really well too, works on regular cmd prompt (with sublime_text), PowerShell, GitHub's Git Shell, and Git Bash (latter three with sublime_text.exe). Fails on Cygwin though, likely because it uses a different path system. Anyone know if adding a cygwin path like, /cygdrive/c/sublime text 2 to my system environment variables will cause problems (I'd just append it to the %SUBLIME_HOME% I made above)?
J
Josh

I know this thread is a bit old, but I recently came up with this solution and thought I would share it...

If you use Cygwin, you can create a bash script that will convert the unix pathnames to windows paths and pass them to sublime. Paste the following into a new file:

#!/bin/bash

/cygdrive/c/Program\ Files/Sublime\ Text\ 2/sublime_text.exe `cygpath -w $@` &

Save it in /usr/bin/subl (or wherever you want so long as the location is in your $PATH) and make it executable ($ chmod a+x /usr/bin/subl)

With this script, you can use both UNIX and Windows style paths (/cygdrive/c/ or C:/) because the cygpath utility converts the / and ~ path aliases to their windows equivalents.

Now, you can use $ subl file1.txt file2.md ~/file3.txt to open those files in sublime!


C
Community

The accepted answer doesn't seem to work, at least for Sublime Text 3 on Windows 7. Copying the file directly into C:\Windows\System32 allows subl to resolve fine from the shell, but when I run it, nothing happens.

I've had more success with a hardlink:

mklink /h c:\windows\system32\subl.exe "c:\Program Files\Sublime Text 3\subl.exe"

With that, subl <file.txt> and subl <directory> both work fine.


upvoting this answer, but I don't have a Windows machine to test this on.
@mehulkar, I test the hardlink method, it doesn't work for me
Worked great for me - Windows 10 and ST3
T
Tiago

I'm trying out ruby on rails in windows and include the PATH C:\Program Files\Sublime Text 2\,then change the name sublime.exe to subl.exe.

worked fine in regular cmd and "command prompt with ruby and rails" cmd


d
digarok

I just ran the Command Prompt program as an Administrator, and executed the following command. Simple, but works for me.

echo "c:\Program Files\Sublime Text 3\subl.exe" %1 > %systemroot%\system32\subl.bat

Of course, you can call the bat file whatever you want. From then on, as any user you can simply use:

subl myfile.txt

C
Clooner

I created a simple batch file (s.bat) in the windows directory to start sublime text

This is the code I put in the batch file to start Sublime Text 2

@start "sublime" "%ProgramW6432%\Sublime Text 2\sublime_text.exe" %*

I call sublime by typing

s

I directly open a file using

s filename.ext

S
Samwise

If your using Cmder, you can add the alias to your aliases file, which is located wherever you installed cmder to, then config\aliases

You can then add ( this is for sublime text 3 )

subl="C:\Program Files\Sublime Text 3\sublime_text.exe" $1

From within Cmder, you can then open any directory in sublime via

subl="C:\Program Files\Sublime Text 3\sublime_text.exe" $1

V
Vishnu Mishra

try this:

alias subl='"/c/Program Files/Sublime Text 2/sublime_text.exe"'

make sure that the sublime install which directory. It will create the alias for sublime then type:

subl file.rb

A
AveryFreeman

Going off the most accepted answer, but whose author unfortunately said modifying the PATH is "hard" - it's not at all. It's very easy. I just did it, and you can too!

Here's what to do:

Navigate to your sublime text directory in Windows Explorer and copy the full path from the path bar.

For example, I have a few development tools set up as portable applications in Dropbox, so I copied the path:

C:\Users\username\Dropbox\Programs\Sublime Text Build 3114 x64

Go to Control panel-> System-> Advanced System Settings-> Environment Variables

In the resulting window, navigate to "path" in the bottom window, highlight it and click "Edit".

In the window that appears, click "New", paste in your Sublime Text path, and click "OK".

Now open CMD or Powershell and type subl

Sublime Text window should open. Enjoy!

Note: I noticed someone else answered this in a similar fashion, but used a fixed location for Sublime Text. That may work for some people, but for others the directions will be erroneous. The fact is, the Sublime Text location can be anywhere on the drive, and knowing where it is and copying that particular location is one of the most important parts of assigning the PATH variable.


k
kodybrown

I wanted to start a new instance of Sublime Text from the command-line.. The following page helped me, even though it only says OSX: http://www.sublimetext.com/docs/3/osx_command_line.html.

So, I use a batch file in my user path to launch 'sublime'. Here is basically what I use (**the batch file contents):

@start "sublime" "%~dp0Sublime Text\sublime_text.exe" --new-window %*

(my batch file %~-dp0 is located one folder above the Sublime Text installation path.)

I know this is an old thread, but I thought I'd add what I was looking for (and found). It might help someone else!


p
pratik_nalage

Set the path for sublime: My computer >> properties >> advanced setting >> environment variables >> path variable >> Add>> C:\Program Files\Sublime Text 3

Just go to directory where sublime is installed and rename sublime_text to sublime.

Open command prompt and type sublime.


P
Pankaj Barnwal

You can simply install Notepad Replacer http://www.binaryfortress.com/NotepadReplacer/ what it will do is make sublime your default notepad in Windows and then you can simply use the commands that you generally use to open inbuilt notepad in windows. for example

`//to open test.txt, just type
notepad test.txt
//to open sublime, Type
notepad`

R
RBT

add Sublime's installation folder to your path. @set PATH=C:\Program Files\Sublime Text 3;%PATH%

or

To set an environment variable permanently in Windows (so that it is available to all the Windows' processes),

start the "Control Panel" ⇒ "System" ⇒ (Vista/7/8) "Advanced system settings" ⇒ Switch to "Advanced" tab ⇒ "Environment variables" ⇒ Choose "System Variables" (for all users) or "User Variables" (for this login user only) ⇒ Choose "Edit" (for modifying an existing variable) or "New" (to create a new variable) ⇒ Enter the variable "Name" and "Value".

in that case prepend C:\Program Files\Sublime Text 3 to the path.

Now, can make a copy of 'sublime_text.exe' as 'sublime.exe'

Then in any command prompt you may be able to run a file.txt file by

C:\Users\MyUsername>sublime filename.txt

p
pacholik

In the Environmental variable I added %SUBLIME_HOME% variable to the path variable as well Then made a symbolic link from my Command terminal

mklink sb sublime_text.exe

f
feech

create in registry:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\sublime.exe

update value of default parameter (REG_SZ) to:

C:\Program Files\Sublime Text 2\sublime_text.exe


G
George Mauer

This powershell allows me to pipe to the edit function (or to use it in the normal way)

function edit
{
    param( [Parameter(ValueFromPipeline=$true,Position=0)] $file )
    begin { set-alias EDITOR 'W:\tools\sublime_text.bat' }
    process { EDITOR $file }
}

here is the sublime_text.bat which for some reason seems necessary (anyone know why?)

START "Sublime Text 2" "C:\Program Files\Sublime Text 2\sublime_text.exe" %*

s
staytime

Hollow every one.

I build a very easy way to do this.

project github page

just download "install.bat" and right click on install.bat -> click "run as administrator"


j
jdeyrup

If you don't want to change your path you can associate files with sublime. So right click on the file, click properties, then click opens with sublime text.

From the command line: myFile.py

Will open the file in sublime. I suppose this saves you about five keystrokes.


Y
Yordan Georgiev
    @echo off
    :: File: TextFiles.Starter.DESKTOP-M175NUE.cmd v1.1.0 docs at the end 

    :: this just an iso-8601 wrapper for windows:
    :: src: http://www.cs.tut.fi/~jkorpela/iso8601.html
    call GetNiceTime.cmd

    :: go the run dir
    cd %~dp0z

    :: this is the dir containing the batch file
    set _MyDir=%CD%

    :: look around , set vars
    for %%A in (%0) do set _MyDriveLetter=%%~dA
    for %%A in (%0) do set _MyPath=%%~pA
    for %%A in (%0) do set _MyName=%%~nA
    for %%A in (%0) do set _MyEtxtension=%%~xA

    :: contains absolute file paths of the files to open like this
    set _ListFile=%_MyDir%\%_MyName%.lst
    :: example of lines in the list file - take out the ::space
    :: C:\Users\ysg\Desktop\TextFiles.Starter.DESKTOP-M175NUE.cmd
    :: C:\Users\ysg\Desktop\TextFiles.Starter.DESKTOP-M175NUE.lst


    :: set _Program="C:\Program Files\TextPad 8\TextPad.exe"
    set _Program="C:\Program Files\Sublime Text 3\sublime_text.exe"
    set _
    :: DEBUG PAUSE

    :: sleep 2
    ping -n 2 www.google.com > NUL

    :: for each line of the cat file do open 
    :: for TextPad , obs note the quoting 
    :: for /f "tokens=*" %%i in ('type "%_ListFile%"') do ^
    :: cmd /c "%_Program% "%%i""

    :: for sublime, obs note the quoting 
    for /f "tokens=*" %%i in ('type "%_ListFile%"') do ^
    cmd /c "%_Program% -t "%%i""
    :: DEBUG PAUSE

    :: Purpose: 
    :: to start a list of non-binary files from a list file on Windows 10
    :: Tested on Windows 10, should work on Win7 too
    :: 
    :: Requirements:
    :: TextPad 8 or Sublime
    :: 
    :: 
    :: Usage: 
    :: copy this file onto your Desktop, list the absolute paths in to the list file 
    :: change the program name in the _Program if if you want other editor
    :: 
    :: VersionHistory: 
    :: 1.1.0 --- 2017-10-06 09:42:54 --- ysg --- added sublime 
    :: 1.0.1 --- 2013-04-15 08:19:10 --- ysg --- added - todo-%today%.txt file opening
    :: 1.0.0 --- 2012-05-23 09:08:57 --- ysg -- Initial creation 

Hello thank you for trying to answer this question, someone flagged your post for deletion, can you briefly explain how the purpose described in your code solves OP's problem?
J
JayRizzo

Here is what worked for me in PowerShell:

# CHECK IF YOUR ALIAS FOR SUBLIME TEXT 3 EXISTS
Get-Alias subl

# REMOVE YOUR ALIAS FOR SUBLIME TEXT 3 IF IT EXISTS
# REF 1
Remove-Item alias:subl

# SET YOUR ALIAS FOR SUBLIME TEXT 3
Set-Alias subl -Value "C:\Program Files\Sublime Text 3\sublime_text.exe"

# CREATE A TEST FILE ON YOUR Desktop
# REF 2
echo "Testing`nNew`nFile`n." > $HOME\Desktop\TestFile.txt

# TEST YOUR NEW ALIAS
subl $HOME\Desktop\TestFile.txt

This is the easiest way to set and test the alias. If you want to keep this against your profile try this:

# TO PERMANENTLY KEEP EVERYTIME YOU OPEN POWERSHELL PLEASE ADD TO YOUR PROFILE. BUT FIRST VERIFY YOUR PROFILE `FILE` EXISTS, IF NOT THEN THIS WILL CREATE IT.
# REF 3 & 4
if (!(Test-Path -Path $PROFILE.CurrentUserAllHosts)) {New-Item -ItemType File -Path $PROFILE.CurrentUserAllHosts -Force}

# open powershell PROFILE
ise $PROFILE.CurrentUserAllHosts

# Add the Set-Alias to your file
Set-Alias subl -Value "C:\Program Files\Sublime Text 3\sublime_text.exe"

REF 1 Remove-Alias: https://superuser.com/q/883914/247728

REF 2 Newline: https://stackoverflow.com/a/36738723/1896134

REF 3 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-5.1

REF 4 https://devblogs.microsoft.com/scripting/understanding-the-six-powershell-profiles/

Operating System (OS): Windows 10 Pro


d
deepika yadav

Windows ONLY: How to open -a “Sublime Text” in windows?

Assuming your Sublime Text 3 was located in the "C:\Program Files\Sublime Text 3" directory

In Git Bash copy paste and run: echo 'alias subl="/C/Program\ Files/Sublime\ Text\ 3/sublime_text.exe"' >> ~/.bashrc Close Git Bash and Open it again. In Git bash type: subl


T
Toby Maguire

go to enviroment and add sublime text folder to system path directory , dont add anything to system 32 directory it's just making a mess ,after that you can type ''' subl mytext.txt ''' in cmd.


This is the most accurate solution for Windows, thanks!
r
rahuljaiswar

Add the installation folder to windows path.

- Click on windows button 
- Type advance system settings in search box
- Click on the result (View Advance System Setting)
- Click on the Environment Variable Button at the Bottom right
- Go to System Variable Section Second
- Search for Path 
- Click on Path and Click on Edit Button 
- Click on New on right sidebar 
- Copy the Folder Path of Sublime Text Editor Sample(C:\Program Files\Sublime Text 3)
- Paste on New Line on Environment Variable
- Click on OK on Every Dialog Box
- All Set Now Follow below Steps

Close the CMD if Open or just start new CMD. Go to your project folder. Click on the Address line in Windows File Explorer. Select all and type CMD. It will open the project folder in CMD. Then just type subl . //don't forget to add the dot (.) after (subl) ex (subl .) All done, it will open the project in sublime text editor.


L
LaxMaroon

After trying for a long time, the following steps worked for me.

Create a PowerShell profile Use the following command: if (!(Test-Path -Path )) { New-Item -ItemType File -Path $PROFILE.CurentUserAllHosts -Force } Edit the profile

You can open any PowerShell profile in a text editor, such as Notepad.

To open the profile of the current user in the current PowerShell host application in Notepad, type:

>notepad $PROFILE.CurentUserAllHosts

On notepad, paste the following command in the profile file Set-Alias subl -Value "C: