Home:ALL Converter>Sublime Text from Command Line

Sublime Text from Command Line

Ask Time:2012-02-25T10:27:50         Author:mehulkar

Json Formatter

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?

Author:mehulkar,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/9440639/sublime-text-from-command-line
John Nelson :

Another idea would be to include C:\\Program Files\\Sublime Text 2\\ in your PATH, and then run an administrator command prompt:\n\ncd \"C:\\Program Files\\Sublime Text 2\\\"\nmklink sublime.exe sublime_text.exe\n\n\nThat will make a symbolic link with the new name. And now you can use it freely:\n\nsublime hello.txt\n\n\nUpdate: 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.",
2012-07-27T12:55:07
Vishnu Mishra :

try this:\n\nalias subl='\"/c/Program Files/Sublime Text 2/sublime_text.exe\"'\n\n\nmake sure that the sublime install which directory.\nIt will create the alias for sublime then type:\n\nsubl file.rb\n",
2015-06-13T08:13:07
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.\n\nFrom the command line:\nmyFile.py\n\nWill open the file in sublime. I suppose this saves you about five keystrokes.",
2017-02-02T19:41:57
ben_joseph :

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".\nTo include the Sublime installation folder in windows path,\n\nOpen command prompt and type sysdm.cpl\nIn Advanced tab, select Environment variables\nUnder system variables, select variable named "Path" and click Edit.\nAdd "C:\\Program Files\\Sublime Text;" to the end of the existing string.\nSave the changes and restart command prompt.\n",
2015-07-29T12:33:59
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! \n\nHere's what to do:\n\nNavigate to your sublime text directory in Windows Explorer and copy the full path from the path bar. \n\nFor example, I have a few development tools set up as portable applications in Dropbox, so I copied the path: \n\nC:\\Users\\username\\Dropbox\\Programs\\Sublime Text Build 3114 x64\n\nGo to Control panel-> System-> Advanced System Settings-> Environment Variables\n\nIn the resulting window, navigate to \"path\" in the bottom window, highlight it and click \"Edit\".\n\nIn the window that appears, click \"New\", paste in your Sublime Text path, and click \"OK\". \n\nNow open CMD or Powershell and type subl\n\nSublime Text window should open. Enjoy!\n\nNote: 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. ",
2016-08-10T02:47:37
Yordan Georgiev :

@echo off\n :: File: TextFiles.Starter.DESKTOP-M175NUE.cmd v1.1.0 docs at the end \n\n :: this just an iso-8601 wrapper for windows:\n :: src: http://www.cs.tut.fi/~jkorpela/iso8601.html\n call GetNiceTime.cmd\n\n :: go the run dir\n cd %~dp0z\n\n :: this is the dir containing the batch file\n set _MyDir=%CD%\n\n :: look around , set vars\n for %%A in (%0) do set _MyDriveLetter=%%~dA\n for %%A in (%0) do set _MyPath=%%~pA\n for %%A in (%0) do set _MyName=%%~nA\n for %%A in (%0) do set _MyEtxtension=%%~xA\n\n :: contains absolute file paths of the files to open like this\n set _ListFile=%_MyDir%\\%_MyName%.lst\n :: example of lines in the list file - take out the ::space\n :: C:\\Users\\ysg\\Desktop\\TextFiles.Starter.DESKTOP-M175NUE.cmd\n :: C:\\Users\\ysg\\Desktop\\TextFiles.Starter.DESKTOP-M175NUE.lst\n\n\n :: set _Program=\"C:\\Program Files\\TextPad 8\\TextPad.exe\"\n set _Program=\"C:\\Program Files\\Sublime Text 3\\sublime_text.exe\"\n set _\n :: DEBUG PAUSE\n\n :: sleep 2\n ping -n 2 www.google.com > NUL\n\n :: for each line of the cat file do open \n :: for TextPad , obs note the quoting \n :: for /f \"tokens=*\" %%i in ('type \"%_ListFile%\"') do ^\n :: cmd /c \"%_Program% \"%%i\"\"\n\n :: for sublime, obs note the quoting \n for /f \"tokens=*\" %%i in ('type \"%_ListFile%\"') do ^\n cmd /c \"%_Program% -t \"%%i\"\"\n :: DEBUG PAUSE\n\n :: Purpose: \n :: to start a list of non-binary files from a list file on Windows 10\n :: Tested on Windows 10, should work on Win7 too\n :: \n :: Requirements:\n :: TextPad 8 or Sublime\n :: \n :: \n :: Usage: \n :: copy this file onto your Desktop, list the absolute paths in to the list file \n :: change the program name in the _Program if if you want other editor\n :: \n :: VersionHistory: \n :: 1.1.0 --- 2017-10-06 09:42:54 --- ysg --- added sublime \n :: 1.0.1 --- 2013-04-15 08:19:10 --- ysg --- added - todo-%today%.txt file opening\n :: 1.0.0 --- 2012-05-23 09:08:57 --- ysg -- Initial creation \n",
2017-10-06T07:08:48
mblsha :

I've created subl.bat in C:\\Program Files\\Sublime Text 2 with contents:\n\nstart sublime_text.exe %*\n\n\nNow 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.",
2012-05-24T12:06:48
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.\n\nSo, I use a batch file in my user path to launch 'sublime'. Here is basically what I use (**the batch file contents):\n\n@start \"sublime\" \"%~dp0Sublime Text\\sublime_text.exe\" --new-window %*\n\n\n(my batch file %~-dp0 is located one folder above the Sublime Text installation path.)\n\nI know this is an old thread, but I thought I'd add what I was looking for (and found). It might help someone else!",
2013-09-10T16:08:17
JayRizzo :

Here is what worked for me in PowerShell:\n\n\n\n# CHECK IF YOUR ALIAS FOR SUBLIME TEXT 3 EXISTS\nGet-Alias subl\n\n# REMOVE YOUR ALIAS FOR SUBLIME TEXT 3 IF IT EXISTS\n# REF 1\nRemove-Item alias:subl\n\n# SET YOUR ALIAS FOR SUBLIME TEXT 3\nSet-Alias subl -Value \"C:\\Program Files\\Sublime Text 3\\sublime_text.exe\"\n\n# CREATE A TEST FILE ON YOUR Desktop\n# REF 2\necho \"Testing`nNew`nFile`n.\" > $HOME\\Desktop\\TestFile.txt\n\n# TEST YOUR NEW ALIAS\nsubl $HOME\\Desktop\\TestFile.txt\n\n\nThis is the easiest way to set and test the alias. If you want to keep this against your profile try this:\n\n# 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.\n# REF 3 & 4\nif (!(Test-Path -Path $PROFILE.CurrentUserAllHosts)) {New-Item -ItemType File -Path $PROFILE.CurrentUserAllHosts -Force}\n\n# open powershell PROFILE\nise $PROFILE.CurrentUserAllHosts\n\n# Add the Set-Alias to your file\nSet-Alias subl -Value \"C:\\Program Files\\Sublime Text 3\\sublime_text.exe\"\n\n\nREF 1 Remove-Alias: https://superuser.com/q/883914/247728\n\nREF 2 Newline: https://stackoverflow.com/a/36738723/1896134\n\nREF 3 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-5.1\n\nREF 4 https://devblogs.microsoft.com/scripting/understanding-the-six-powershell-profiles/\n\nOperating System (OS): Windows 10 Pro",
2020-04-22T20:55:10
glrodasz :

I think that is more easy set the Environment variable in Windows.\n\nThen 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). \n\nRestart the git BASH and enjoy, using like this: \n\n$ sublime_text mi-new-file\n\n(where sublime_text is the command)\n\nNote: Also works now for cmd of Windows.",
2012-10-19T06:08:18
pratik_nalage :

\nSet the path for sublime:\nMy computer >> properties >> advanced setting >> environment variables >> path variable >> Add>> C:\\Program Files\\Sublime Text 3 \nJust go to directory where sublime is installed and rename sublime_text to sublime.\n\n\nOpen command prompt and type sublime. ",
2017-02-09T19:11:50
deepika yadav :

Windows ONLY: How to open -a “Sublime Text” in windows?\nAssuming your Sublime Text 3 was located in the "C:\\Program Files\\Sublime Text 3" directory\n\nIn Git Bash copy paste and run:\necho 'alias subl="/C/Program\\ Files/Sublime\\ Text\\ 3/sublime_text.exe"' >> ~/.bashrc\n\nClose Git Bash and Open it again.\n\nIn Git bash type:\nsubl\n\n",
2021-01-17T04:01:00
Josh :

I know this thread is a bit old, but I recently came up with this solution and thought I would share it...\n\nIf 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:\n\n#!/bin/bash\n\n/cygdrive/c/Program\\ Files/Sublime\\ Text\\ 2/sublime_text.exe `cygpath -w $@` &\n\n\nSave 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)\n\nWith 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.\n\nNow, you can use $ subl file1.txt file2.md ~/file3.txt to open those files in sublime!",
2012-09-14T18:30:44
Pankaj Barnwal :

You can simply install Notepad Replacer http://www.binaryfortress.com/NotepadReplacer/\nwhat 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\n\n`//to open test.txt, just type\nnotepad test.txt\n//to open sublime, Type\nnotepad`\n",
2015-08-17T14:04:52
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\n''' subl mytext.txt ''' in cmd. ",
2019-11-10T07:46:14
pneumatics :

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. \n\nI've had more success with a hardlink:\n\nmklink /h c:\\windows\\system32\\subl.exe \"c:\\Program Files\\Sublime Text 3\\subl.exe\"\n\nWith that, subl <file.txt> and subl <directory> both work fine.",
2014-11-20T23:21:56
Giovanni Dappa Hosang :

add Sublime's installation folder to your path. \n@set PATH=C:\\Program Files\\Sublime Text 3;%PATH%\n\nor\n\nTo set an environment variable permanently in Windows (so that it is available to all the Windows' processes), \n\nstart 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\".\n\n\nin that case prepend C:\\Program Files\\Sublime Text 3 to the path.\n\nNow, can make a copy of 'sublime_text.exe' as 'sublime.exe'\n\nThen in any command prompt you may be able to run a file.txt file by \n\nC:\\Users\\MyUsername>sublime filename.txt\n",
2017-01-07T01:28:58
rahuljaiswar :

\nAdd the installation folder to windows path.\n\n\n- Click on windows button \n- Type advance system settings in search box\n- Click on the result (View Advance System Setting)\n- Click on the Environment Variable Button at the Bottom right\n- Go to System Variable Section Second\n- Search for Path \n- Click on Path and Click on Edit Button \n- Click on New on right sidebar \n- Copy the Folder Path of Sublime Text Editor Sample(C:\\Program Files\\Sublime Text 3)\n- Paste on New Line on Environment Variable\n- Click on OK on Every Dialog Box\n- All Set Now Follow below Steps\n\n\n\nClose the CMD if Open or just start new CMD.\nGo to your project folder.\nClick on the Address line in Windows File Explorer.\nSelect all and type CMD.\nIt will open the project folder in CMD.\nThen just type subl . //don't forget to add the\ndot (.) after (subl) ex (subl .)\nAll done, it will open the project in sublime text editor.\n",
2021-02-06T05:38:14
user1973194 :

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.\n\nworked fine in regular cmd and \"command prompt with ruby and rails\" cmd",
2013-01-12T22:24:56
pensebien :

\nIn the Environmental variable I added %SUBLIME_HOME% variable to the path variable as well\nThen made a symbolic link from my Command terminal \n\n\nmklink sb sublime_text.exe",
2017-02-18T11:45:52
pjumble :

Windows Command Prompt\n\nFor Windows cmd.exe you could just add the sublime text installation directory to your PATH environment variable, this would allow you to type:\n\nsublime_text file.rb\n\n\nPersonally, I add a doskey (in a .bat file set to autorun with cmd) so I can type subl file.rb:\n\n> doskey subl=\"C:\\Program Files\\Sublime Text 2\\sublime_text.exe\" $*\n\n\n\n\nCygwin\n\nFor the default bash shell add an alias to your ~/.bashrc file, e.g:\n\n$ echo 'alias subl=\"/cygdrive/c/Program\\ Files/Sublime\\ Text\\ 2/sublime_text.exe\"' >> ~/.bashrc\n",
2012-02-25T02:55:20
digarok :

I just ran the Command Prompt program as an Administrator, and executed the following command.\nSimple, but works for me.\n\necho \"c:\\Program Files\\Sublime Text 3\\subl.exe\" %1 > %systemroot%\\system32\\subl.bat\n\n\nOf course, you can call the bat file whatever you want. From then on, as any user you can simply use:\n\nsubl myfile.txt\n",
2014-10-29T18:24:25
feech :

create in registry:\n\nHKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\sublime.exe\n\nupdate value of default parameter (REG_SZ) to:\n\nC:\\Program Files\\Sublime Text 2\\sublime_text.exe",
2012-11-07T07:09:15
sandcastles :

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. \nFor example, in my case I copied it\n\nfrom C:\\Program Files\\Sublime Text 3 \n\nto C:\\Windows\\System32\n\nYou may then use in your terminal/console subl as a command to open whatever file, such as in your example:\n\nsubl file.rb\n\nOr you may as well modify your system PATH variable to include sublime's instalation folder, but I believe that is much more involved.",
2014-08-30T00:21:30
Clooner :

I created a simple batch file (s.bat) in the windows directory to start sublime text\n\nThis is the code I put in the batch file to start Sublime Text 2\n\n@start \"sublime\" \"%ProgramW6432%\\Sublime Text 2\\sublime_text.exe\" %*\n\n\nI call sublime by typing\n\ns\n\n\nI directly open a file using\n\ns filename.ext\n",
2013-12-13T14:15:03
George Mauer :

This powershell allows me to pipe to the edit function (or to use it in the normal way)\n\nfunction edit\n{\n param( [Parameter(ValueFromPipeline=$true,Position=0)] $file )\n begin { set-alias EDITOR 'W:\\tools\\sublime_text.bat' }\n process { EDITOR $file }\n}\n\n\nhere is the sublime_text.bat which for some reason seems necessary (anyone know why?)\n\nSTART \"Sublime Text 2\" \"C:\\Program Files\\Sublime Text 2\\sublime_text.exe\" %*\n",
2012-09-17T19:01:52
Kristopher Johnson :

I added this to my PowerShell profile:\nSet-Alias subl 'c:\\Program Files\\Sublime Text\\subl.exe'\n\nModify this as needed for Sublime Text 3 (or any future versions).\nYou also may read about profiles in built-in help:\nGet-Help about_Profiles\n",
2012-06-01T15:54:45
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 \n\nYou can then add ( this is for sublime text 3 )\n\nsubl=\"C:\\Program Files\\Sublime Text 3\\sublime_text.exe\" $1\n\n\nFrom within Cmder, you can then open any directory in sublime via \n\nsubl=\"C:\\Program Files\\Sublime Text 3\\sublime_text.exe\" $1\n",
2015-05-12T06:13:18
staytime :

Hollow every one.\n\nI build a very easy way to do this.\n\nproject github page\n\njust download \"install.bat\"\nand right click on install.bat -> click \"run as administrator\"",
2015-03-01T10:35:04
yy