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?
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
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 added this to my PowerShell profile:
Set-Alias subl 'c:\Program Files\Sublime Text\subl.exe'
Modify this as needed for Sublime Text 3 (or any future versions).
You also may read about profiles in built-in help:
Get-Help about_Profiles
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.
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.
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.
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.
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!
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.
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
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
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
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
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
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.
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!
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.
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`
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
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
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
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" %*
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"
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.
#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
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://learn.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
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
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.
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.
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:<the path of the sublime_text.exe on your PC"
To apply the changes, save the profile file, and then restart PowerShell.
Now on Powershell, cd the directory of your choice and type:
subl .
e.g,
$newfile subl .
Related
I use WSL (Windows Subsystem for Linux).
I have Neovim/Vim/etc installed in WSL, and wish to be able to open a file in explorer, and have it open in say Neovim installed in WSL, within windows terminal. What script does one write to launch it, I am struggling with escaping double quotes from cmd, through wt.exe, then into WSL bash.
It can be a pain keeping two versions of Neovim/Vim configured for windows and for linux. Rather just use only the WSL version in both windows and WSL.
TL;DR
Associate the file type with running this batch script (set myapp accordingly):
#echo off
set my_app=nvim
set my_wt_profile="Ubuntu-20.04"
set pp=%1
set pp=%pp:'='\''%
set pp=%pp:;=\;%
set launch="p=$(wslpath '%pp:"=%') && cd \\"^""$(dirname \\"^""$p\\"^"")\\"^"" && %my_app% \\"^""$p\\"^""
start wt.exe new-tab -p %my_wt_profile% bash -i -c %launch%
And github repo of the scripts
Intro
Unfortunately one cannot associate a PowerShell script with file type (via open with → choose another app → Look for another app on this pc). Choose to write a batch file, and put all the logic in there. It would have been easier to create a bash script, or vim plugin on file load, but then there are 2 parts of the puzzle which need to be in syn with each other.
Perform the following steps:
Create a batch file and paste in the following code (we will refer to this script as the "launch script", I named it wsl_nvim.bat):
Note: The following code is the same as the TL;DR version but with comments:
:: This batch script is meant to be associated with file types, such that when
:: the associated file type is opened, it calls this script.
:: This script then open it with Neovim within WSL in a windows terminal (wt).
:: If require a " in the bash command, escape it with \\"^""
:: Example1: To print in bash via cmd the following string: hel'lo
:: bash -i -c "echo "^""hel'lo"^"" "
:: Example2: To print in bash via cmd via wt.exe: hel'lo
:: wt.exe new-tab -p "Command Prompt" cmd /k bash -i -c "echo \\"^""hel'lo\\"^"" "
::
:: To cd to the parent dir: cd "$(dirname "$p")"
:: Escaping it becomes: cd \"^""$(dirname \"^""$p\"^"")\"^""
#echo off
:: === REQUIRED CUSTOM VALUES ==================================================
:: The name of the WSL app to run
set my_app=nvim
:: The name of your windows terminal linux profile, open the windows terminal
:: settings file and file the linx profile name, e.g.: "name": "Ubuntu-20.04",
set my_wt_profile="Ubuntu-20.04"
:: =============================================================================
:: Windows passes in the filepath in double quotes, e.g.: "C:\Users\Michael\foo.txt"
set pp=%1
:: We passing the path into bash, which has $ and \, so we pass within single quotes
:: so all chars will be taken literally, except the single quote, which we can
:: escape with '\''
set pp=%pp:'='\''%
:: When wt.exe interprets the string, need to escape the semicolon with \;
set pp=%pp:;=\;%
:: Launch basically does: pass in $p, get wslpath of $p, then cd to the dir
:: of the wslpath, then open wslpath with nvim.
:: wslpath requires the input to be within single quotes, or else it will fail.
:: full wt.exe path: %LOCALAPPDATA%\Microsoft\WindowsApps\wt.exe
:: GIANT GOTCHA! Can only strip outter double quotes from %pp% if placing within
:: double quotes, else special chars will be interpreted literally, e.g. ^ will escape.
set launch="p=$(wslpath '%pp:"=%') && cd \\"^""$(dirname \\"^""$p\\"^"")\\"^"" && %my_app% \\"^""$p\\"^""
:: Use `start` to launch cmd and cleanup/close the parent process immediately.
:: bash -i starts bash interactively.
:: bash -c "long command" start bash and allow one to pass in a command to run.
start wt.exe new-tab -p %my_wt_profile% bash -i -c %launch%
Bonus 1 - associate "launch script" with text file types
Let's make the batch script available as an option in the explorer.exe's right click Open with options. Under Open with one may have to select choose another app and scroll down, which is still much easier than hunting the filesystem for the batch script for each new file type.
Perform the following steps:
Copy the following batch script and paste it into a file in the same dir as the above script.
Change the value of myscript to point to the "launch script" name you choose.
Save and close the batch script.
Right click on the batch script and select Run as administrator
:: This batch script associates the files in %list% with the `txtfile` type, and
:: changes the `txtfile` type to open with with %myscrip%.
:: It does not make it the default app.
:: One can't programmatically change the default file association of an already
:: associated filetype in Windows 10 after the first login without the gui,
:: this is by design for security.
#echo off
echo !!! THIS SCRIPT MUST BE RUN AS ADMIN !!!
:: === CUSTOM VALUES START =====================================================
:: A space separated list of extensions to be associated with the `txtfile` type
set list=css gitignore html ini js json lua log markdown md php py render sass scss template text txt xml
:: Set myscript to the double quote filepath of the script to run
:: %~dp0 is the dir of this script file
set myscript="%~dp0wsl_nvim.bat"
:: === CUSTOM VALUES END =======================================================
:: e.g. require the same as if one typed into cmd: ftype txtfile="C:\current dir\wsl_nvim.bat" "%1"
echo:
echo Create a `ftype` called `txtfile` and assign it to run with WSL NVIM:"
ftype txtfile=%myscript% "%%1"
echo:
echo `ftype` set for `txtfile`, let's check its set:
ftype | findstr "txtfile"
echo:
echo Create a `assoc` between extensions in %list% with `txtfile`
(for %%a in (%list%) do (
assoc .%%a=txtfile
))
echo:
echo `assoc` set for each extension, lets check `assoc`:
assoc | findstr ".txt"
echo:
echo Now if you right click on one of these file extensions, and select `Open with`,
echo and select `choose another app`, it should list %myscript% there.
echo SCRIPT COMPLETE.
pause
Bonus 2 - create a taskbar shortcut
Copy the "launch script" path.
In explorer right click New → Shortcut.
Type in a suitable name, e.g. WSL NVIM
Now if one right clicks on the newly created shortcut, there is no option to pin to taskbar or start.
Right click on the shortcut, select Properties and change the target field to (make sure to customise both paths!):
cmd.exe /s /c ""C:\path\to\launch\script\wsl_nvim.bat" "\\wsl$\Ubuntu-20.04\desirable\default\location\temp_filename""
For me the above looks like :
cmd.exe /s /c ""C:\code\software_setup\utils\wsl_nvim.bat" "\\wsl$\Ubuntu-20.04\home\michael\temp""
Now if you double click the shortcut, it should open the wsl app with a blank file in the given location, with temp file name (depending on how your app handles paths). You can now right click on the shortcut and pin it to the taskbar or start menu.
If I understand your question correctly, you want to edit files in Windows using your WSL Vim. The following approach creates an entry Open in Vim in your (right-click) context menu which you can click, to open any file in WSL-Vim. Of course you can execute any other program that can run in a terminal window (i.e. bash) by adapting this procedure.
Open regedit.exe and navigate to HKEY_CLASSES_ROOT\*\shell
Right-click shell and select New->Key, which you name Vim
Double-click on (Default) and set value to Open with Vim
Right-click on Vim-Key and select New->Key, which you name command
(Left-) click on your newly created command-Key, then double-click on (Default) (right pane)
Under Value Data enter: C:\Windows\System32\bash.exe -c "vim \"$(wslpath '%1')\""
(In this step, you can replace vim with any other program that can run in bash. Keep in mind, that you might need the -i flag to get an interactive shell)
It should look like this:
Now you should see "Open with Vim" if you right-click on any document in Windows.
If you also want the Vim icon to appear in the context menu, do the following:
Download Vim icon from here: https://raw.githubusercontent.com/vim/vim/master/src/vim.ico
Store it somewhere on disk (I have it under C:\Users\<USERNAME>\Pictures\Saved Pictures\vim.ico)
Open regedit.exe and navigate to HKEY_CLASSES_ROOT\*\shell
Right-click Vim and select New->String Value, which you name Icon
Double-click on your newly created Icon-entry and under Value Data enter the path to the vim.ico.
It should look like this:
And this is the final outcome:
For quick access to the command prompt pointing to a specific directory, I would like to double click on a .bat file (saved on the desktop), and then the command prompt will open pointing to a specific folder, and would like the command prompt to remain open.
This is what I did:
rem -changing the drive to e:
e:
rem - setting the path to the specific folder
cd \users\myName\Documents\folderName\folderName\folderName\
==> this results on the command prompt opening in the specify directory, but closing right away.
I have no problem opening in the 'e' drive (start cmd e: ), but when I specify the path to the folder, it does not work
Is there a way to do this?
You need to use the option /K of the cmd.exe
Please take a look at the following link:
open command prompt window and change current working directory
Hope that helps,
The /K option is the key, as has been said. However, if you want a better CMD environment, considering creating shortcuts to CMD in a desktop folder. By changing the properties of each shortcut, you can then customize the environment, window size, fonts, buffers, etc.
To do this, create a shortcut with a Target something like this:
%windir%\system32\cmd.exe /K C:\Batch\your_batch_file.bat
In your_batch_file.bat, you can set environment variables, paths, select the drive, set the working directory, etc.
By changing the properties of the shortcut, you can totally customize the window size, fonts, colors, buffers, etc.
I use this to set up separate environments for Ruby, JRuby, CMD, etc.
The shortcut properties looks something like :
#echo off
cd /d "e:\users\myName\Documents\folderName\folderName\folderName\"
cmd /k
exit
How do I change default startup directory for command prompt in Windows 7?
I usually do the following to start command prompt from C:\
WIN-R (Run Prompt)
cmd /K cd C:\
I want to do the following to start command prompt from C:\
WIN-R (Run Prompt)
cmd
Make a shortcut pointing to cmd.exe somwhere (e.g. desktop) then right-click on the copy and select "properties". Navigate to the "Shortcut" menu and change the "Start in:" directory.
The following solution worked well for me. Navigate to the command prompt shortcut in the start menu:
C:\Users\ your username \AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Accessories\Command Prompt
Right click on the shortcut file to open the properties dialog. Inside the "Start in:" textbox you should see %HOMEDRIVE%%HOMEPATH%. If you want the prompt to start in C:\ just replace the variables with "C:\" (without quotes).
update
It appears that Microsoft has changed this behavior recently and so now an additional step is required. After performing the steps above copy the modified shortcut "Command Prompt" and rename it to "cmd". Then when typing "cmd" in the start menu it should once again work.
Open regedit and browse to this path
HKEY_CURRENT_USER\Software\Microsoft\Command Processor
Create new string vale named Autorun. Set its value to cd /d C:\.
Run cmd again. Voila!
While adding a AutoRun entry to HKEY_CURRENT_USER\Software\Microsoft\Command Processor like Shinnok's answer is the way to go it can also really mess things up, you really should try to detect a simple cmd.exe startup vs a script/program using cmd.exe as a child process:
IF /I x"%COMSPEC%"==x%CMDCMDLINE% (cd /D c:\)
Easiest way to do this
Click "Start" and type "cmd" or "command prompt".
Select Top most search application named exactly same "cmd" or "command prompt".
Right Click on it and select "Send To"=>"Desktop".
On Your Desktop New "cmd" Shortcut will appear
Right Click on that icon and choose "properties"
Popup will appear, In "Shortcut" Tab Type the new location in "Start In" option (e.g D:\xyz)
Drag that icon and add/pin it in "Task Bar"
On Windows Start Menu, right click on Command Prompt.
Click on "Properties".
"Command Prompt Properties" dialog box opens.
Edit the field "Start in " to a location where you want to start the command prompt.
Example: Chand %HOMEDRIVE%%HOMEPATH% to D:\PersonalPrograms.
Next time when you start command prompt the start up directory will be D:\PersonalPrograms
Bit late but ignore the registry mods.
Simply change the shortcut target to:
cmd /k "command"
i.e.
cmd /k "cd\myStartUpFolder"
Voila!
This doesn't work for me. I've tried this both under Win7 64bit and Vista 32.
I'm using the below commandline to add this capability.
reg add "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v AutoRun /t REG_SZ /d "IF x"%COMSPEC%"==x%CMDCMDLINE% (cd /D c:)"
I think the easiest way is to make a cmd shortcut, then change the shortcut's "Start in" directory to the one you want to start with.
On windows 7:
Do a search for "cmd" on your Windows computer
right-click cmd and left click "Pin to start menu" (Alternatively, right-click cmd - click copy and then paste to your desktop )
right-click the cmd in your start menu or on your desktop (depending on choice 2 above) - left click properties
inside the "start in" text box paste the location of your default start directory
Press Apply and OK
Every time you click on the cmd in your start menu or your desktop shortcut, the CMD will open in your default location
changing shortcut under Windows System on 8.1 worked for me - another thing I found is that 'Start In:' WORKS when Advanced -> Run as admin is UNCHECKED, however, if CHECKED, it does not work
HKEY_CURRENT_USER\Software\Microsoft\Command Processor
string: Autorun
value: cd /d %~dp0
all bat files will run from the bat file location
One easy way to do it + bonus.
Start cmd with administrator rights, the default directory for the prompt will be C:\WINDOWS\system32.
I created a bat file in that directory (notes.bat)
Opened it with notepad and wrote the following lines. Each line is followed with a comment which should not be added to the bat file.
#echo off
prompt $S$CYourNamel$F$S$G$S
/* This is a comment, do not include it in the bat file - above line will make the prompt look like (YourName) > */
cd C:\Your_favorite_directory
/* This is a comment, do not include it in the bat file - above line will navigate you to your desired directory */
Saved the file and that was it.
Now when You open cmd with admin rights, just write: notes or notes.bat
and it will execute the notes.bat file with desired changes.
go to regedit ( go to search and type regedit)
expand "HKEY_CURRENT_USER" node
under HKEY_CURRENT_USER node expand "software" node
under software node expand "microsoft" node
under microsoft node click on "Command Processor"
path looks like this : "HKEY_CURRENT_USER\Software\Microsoft\Command Processor"
it looks something like this :
if you do not see "Autorun" String Value
Right Click - New - Expandable String Value, and rename it to Autorun
double click on "Autorun"
10.enter this value path format:
"CD/d C:\yourfoldername\yoursubfoldername"
Edit: It actually seems that editing the file shortcut breaks the Win+x, c key shortcut. (Moral of the story: only change system files you know how to fix! Eventually after a Windows update it repaired itself.)
What I ended up doing is creating a new customized Command Prompt shortcut in the start folder and pinned to the taskbar that I launch instead of cmd.exe
As other answers point out, changing the registry Autorun cmd start location is a bad idea because it silently will break other programs that shell out for tasks, like Visual Studio Code.
You should just change whatever shortcut you use to open cmd to have a Start In entry.
If you use Win+x, c to launch cmd, you can edit the Start In for
"%LOCALAPPDATA%\Microsoft\Windows\WinX\Group3\02 - Command Prompt.lnk"
type
cmd.exe /k cd c:/
in a text file and save as cmd.bat Clicking this file does the trick. You can pin it to the start menu as well.
To start in a partitioned drive (e.g. D), use
cmd.exe /k d:
My default dir was system32 when starting CMD.
I then created a batch file in that directory to change dir to the one I was after.
This caused me to always call that bat when starting CMD every time.
So I made a reg file & put this inside:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"Autorun"="cd C:\\Users\\Me\\SomeFolder"
After saving it, I opened the file, clicked ok to merge with registry, and since then every time I open CMD, I get my dir
In the new Windows Terminal, you can click Settings and edit the line "startingDirectory" to achieve something similar.
Please note, however, that this changes the default startup directory only in Windows Terminal, and not for the command prompt globally.
Use Windows Terminal and configure a starting directory.
Partial settings.json:
{
// Make changes here to the cmd.exe profile.
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"name": "Command Prompt",
"commandline": "cmd.exe",
"hidden": false,
"startingDirectory": "C:\\DEV"
},
HKEY_CURRENT_USER\Software\Microsoft\Command Processor is no longer valid.
The new key is here:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
Change "Autorun" to "CD /d C:\YourFolder" (without double quotes)
Keep the opening of the command prompt clean. Avoid editing the registry key and adding an Autorun, it may come back to bite you.
Create a simple batch file and save it in the C:\Windows or C:\Windows\System32 folder. I call mine !.bat (exclamation mark). It has the following commands:
#echo off
c:
cd \
cls
whoami
It goes to the folder where I need to work, clears the screen and tells me what security context I'm in.
"start in directory" command
cmd /K cd C:\WorkSpace
but if WorkSpace happens to be on different than C drive, console will be launched in default folder and then you still need to put D: to change drive
To avoid this use cd with -d parameter
cmd /K cd -d D:\WorkSpace
create a shortcut and your fixed ;)
hi if you want cmd to automatically open when the machine starts up you can place the cmd.exe executable in the startup folder(just search for startup and place a shortcut of cmd.exe there)
regedit worked great. HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\Command Processor, all you have to do is change the AutoRun key value, which is already set to wherever you are currently getting dumped into to a new value in the format of:
cd /d <drive:path>
for c:\, that would be cd /d c:\
for junk, that would be cd d/ c:\junk
its very simple, even a novice thats never used regedit should be able to figure it out. if not, go to the c:\prompt and just type in regedit, then follow the path to the key.
I have many scripts which I interact with from the command line. Everytime I need to use them, I have to open a command line window and copy+paste and CD to the path to the directory they are in. This is tedious (they are in a rather deep file system, so typing out the full path is a pain, copy+paste is better but not much). I tried to create a .BAT file that I could double-click on that would open a new command-line window in the folder the .bat file exists in but it does not work. It opens a new window, but the working directory is not the directory that .bat file is in. Here's what I've got after much googling (My cmd skills ain't so great):
cd %CD%
cmd.exe
I know from when I used Linux that Konqueror had a "Command-line window here" feature, and that's the effect I'm trying to get on Windows.
you probably want to do this:
cd /d %~dp0
cmd.exe
this will set your current directory to the directory you have the batch file in
Create a file named open_dos_here.cmd with the following lines:
%~d1
cd "%~p1"
call cmd
Put this file at any folder.
Then, go to your Send To folder (Win+E; Alt+D;shell:sendto;Enter).
Create a shortcut to point to this open_dos_here.cmd
Then, in any folder, select any file or sub-folder. Right-click and select "Send To" and then select open_dos_here.cmd to open the DOS in that folder.
You can just enter cmd into the address bar in Explorer and it starts up in that path. Likewise for PowerShell.
There's a simpler way -
start /d "folder path"
As a more general solution you might want to check out the Microsoft Power Toy for XP that adds the "Open Command Window Here" option when you right-click: http://www.microsoft.com/windowsxp/downloads/powertoys/xppowertoys.mspx
In Vista and Windows 7, you'll get that option if you hold down shift and right-click (this is built in).
I'm thinking that if you are creating a batch script that relies on the Current Directory being set to the folder that contains the batch file, that you are setting yourself up for trouble when you try to execute the batch file using a fully qualified path as you would from a scheduler.
Better to add this line to your batch file too:
REM Change Current Directory to the location of this batch file
CD /D %~dp0
unless you are fully qualifying all of your paths.
Another solution is to use a shortcut file to cmd.exe instead of a batch file.
Edit the shortcut's start in property to %~dp0.
You achieve the same thing, except it has the Cmd icon (and you can change this).
Some people don't like clicking on batch files without knowing what's in them, and some corporate network drives have a ban on .bat files...
The simplest command to do this:
start
You can always run this in command line to open new command line window in the same location. Or you can place it in your .bat file.
Most simple way in explorer is to Shift + right mouse click on the folder or on an empty space in the folder and click on Open command prompt here.
CMD will then start in that folder
I must say, I'm not sure if it works for Windows Vista and below, but it surely works for Windows 7, 8, 8.1 and 10.
Referring to answer of #Chris,
We can also go to parent directory of batch file and run commands using following
cd /d %~dp0..
<OTHER_BATCH_COMMANDS>
cmd.exe
To understand working of command cd /d %~dp0.. please refer below link
What does it mean by command cd /d %~dp0 in Windows
You could add a context menu entry through the registry:
Navigate in your Registry to
HKEY_LOCAL_MACHINE/Software/Classes/Folder/Shell
and create a key called "Command Prompt" without the quotes.
Set the default string to whatever text you want to appear in the right-click menu.
Create a new key within your newly created command prompt named "command," and set the default string to
cmd.exe /k pushd %1
You may need to add %SystemRoot%\system32\ before the cmd.exe if the executable can't be found.
The changes should take place immediately. Right click a folder and your new menu item should appear.
Also see http://www.petri.co.il/add_command_prompt_here_shortcut_to_windows_explorer.htm
When you are in the desired folder , just type CMD in your address bar
A bit late to the game but if I'm understanding your needs correctly this will help people with the same issue.
Two solutions with the same first step:
First navigate to the location you keep your scripts in and copy the filepath to that directory.
First Solution:
Click "Start"
Right-click "Computer" (or "My Computer)
Click "Properties"
On the left, click "Advanced System Settings"
Click "Environment Variables"
In the "System Variables" Box, scroll down and select "PATH"
Click "Edit"
In the "Variable Value" field, scroll all the way to the right
If there isn't a semi-colon (;) there yet, add it.
Paste in the filepath you copied earlier.
End with a semi-colon.
Click "OK"
Click "OK" again
Click "OK" one last time
You can now use any of your scripts as if you were already that folder.
Second Solution: (can easily be paired with the first for extra usefulness)
On your desktop create a batch file with the following content.
#echo off
cmd /k cd "C:\your\file\path"
This will open a command window like what you tried to do.
For tons of info on windows commands check here: http://ss64.com/nt/
Create a new file startCmdLine.bat in your directory and put this line in it
call cmd
That is it. Now double click on the .bat file. It works for me.
You can replace call with start, it will also work.
this code works for me
name it cmd.bat
#echo off
title This is Only A Test
echo.
:Loop
set /p the="%cd%"
%the%
echo.
goto loop
you can try:
shift + right click
then, click on Open command prompt here
Inside given folder click on the top Adddress Bar and type cmd and click enter
It will open command prompt with current folder address.
You can simply create a bat file in any convenient place and drop any file from the desired directory onto it.
Haha. Code for this:
cmd
In gVim if the cursor is over a filename (like "C:\Program Files\Vim\vim71\README.txt" ) which contains spaces then using 'gf' fails.
I found the isfname option but the documentation says one should avoid using spaces.
Is there I can make gVim open files under these circumstances ?
You can select text in visual mode and then do a gf on it, that get's round the problem to some extent.
Another option is to use Windows' "8dot3" filenames. I've tested it out in Windows 7 + GVIM succesfully.
Find out the short filename by using the /X flag in the CMD prompt. You want to CD to the parent folder to that which has spaces in it. I'm not sure how to get the whole path at once in "8dot3" form.
Typing "cd " and then dragging from the Explorer's address bar to the CMD window can be easier than typing out the path:
> cd "C:\Users\Fabrice\Documents"
> dir /X
Then use it in Vim, eg:
C:\Users\Fabrice\Documents\MYGAME~1\Skyrim\SkyrimPrefs.ini
If using a folder instead of a file name, the path appears properly expanded in the Netrw listing (:Ex file list).