70
24
Just discovered doskey emacs=emacs -nw $*, but without something like .bashrc, I'll have to type that every time I start a shell, right?
70
24
Just discovered doskey emacs=emacs -nw $*, but without something like .bashrc, I'll have to type that every time I start a shell, right?
73
This is a very good question. I found this. I suppose you could make a cmd script and have it run when starting cmd :-?
; Run a command when CMD.exe starts
[HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor]
"AutoRun"=-
EDIT:
I just tried it. I have AutoRun=C:\mini\bashrc.cmd
and bashrc.cmd is
@echo off
set TEST_VAR=something
when I start cmd and enter echo %TEST_VAR%
it says something
. So it works :)
5I must be an idiot but where do I put this code? – Marcel – 2014-10-10T23:46:53.167
@Marcel Just added an answer that explains this. – Humdinger – 2015-05-19T02:33:44.593
1probably easiest to set to... AUTORUN=%USERPROFILE%\autorun.cmd
so that it will use one for the current user... cool trick though, will try it when I get home... I mostly use bash lately, but this would help. – Tracker1 – 2015-09-18T22:40:33.783
10Small snippet I use: doskey∙♦=exit
(where ♦
is [char]4
). Enables you to close the shell with Ctrl+D, Enter. – Joey – 2010-05-23T12:13:49.690
Following @Joey's comment: you can enter ♦
in Notepad++ by opening Edit -> Character Panel and clicking on the character EOT
– Yibo Yang – 2016-09-15T05:31:58.647
This work great, nice job nc3b. – Robert S Ciaccio – 2010-07-28T03:57:55.913
Unfortunately, this solution edits the system-wide regkey, so user-specific profiles are difficult and may impede the use of cmd - does it cause terminating errors if a profile doesn't exist, for example? – TheIncorrigible1 – 2018-12-17T19:24:28.413
This is absolutely amazing +1 – user541686 – 2011-06-11T20:15:49.397
2HKEY_CURRENT_USER\Software\Microsoft\Command Processor with AutoRun=%HOMEPATH%\bashrc.bat So every user would have his own startup-sequence – bambam2174 – 2013-07-18T08:29:41.623
22
You can create a shortcut to cmd.exe
and add the /k
switch to run a certain .bat file on startup, something like this:
cmd.exe /k "%HOMEDRIVE%\%HOMEPATH%\cmd-startup.bat"
cmd-startup.bat
in your home dir would then work just like .bashrc
16
Modification of mc3b's answer for those who are windows noobs like myself.
Make a file in C:\bashrc.cmd
or wherever you want your "rc" file to reside.
@echo off
set TEST_VAR=something
Run regedit
by searching for it or running it in cmd.
In the folders on the left navigate to:
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor
Right click on the Command Processor
folder and click New -> String Value
. Enter the key AutoRun
. Double click on AutoRun
and enter the bashrc location C:\bashrc.cmd
.
Test: Open a new cmd window and type:
echo %TEST_VAR%
if it says something
, it works.
5
Assuming your shell is Powershell, not cmd, you can create a profile for your shell. Such profiles basically are scripts which are executed when you start a shell. Just like .bashrc
. An important part of this post might be the following:
The locations (on Windows Vista) of the profiles for the powershell.exe host are as follows:
%windir%\system32\WindowsPowerShell\v1.0\profile.ps1
This is for all users of the computer and for all shells.%windir%\system32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
This is for all users of the computer, but it is only for the Microsoft.PowerShell shell.%UserProfile%\Documents\WindowsPowerShell\profile.ps1
This is for the current user only and all shells.%UserProfile%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
This is for the current user only and only for the Microsoft.PowerShell shell.These profiles aren't created by default. They exist only if you create them.
It seems to be the same on Windows 7, 8 and 10. You can also find more information here.
Note: You may need to run the following. Otherwise, your profile may not be executed.
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
You can also run the following as administrator to change the configuration for all users.
Set-ExecutionPolicy RemoteSigned
3
Basically nc3b's answer works, but Chocolatey's DevBox-Common package will make it easier.
choco install devbox-common -y
.bashrc.bat
and .bashrc.include.aliases-common.bat
in your home path. Every file matching .bashrc.include.*.bat
will be executed at every command shell start.3
To all people coming from a unix-like world and landing on this question (i.e. everybody, since it asks about .bashrc
): Radically improve your Windows console life and stop using stock cmd.exe
and the default Windows terminal!! No one asking this question, or looking at answers to this question, should be using the current default cmd.exe
and terminal!
If willing to switch off of stock cmd.exe
(I know of no valid reason not to), one simple answer to the original question is:
Install and use Cmder and your .bashrc
equivalent becomes
%CMDER_ROOT%\config\user_profile.cmd
, while also significantly improving your shell/console experience.
Opinions, extensions, and loosely related recommendations follow...
Cmder is a metapackage that packages up ConEmu, Clink, and a pile of extra settings and stuff. ConEmu is the terminal emulator that provides the startup script capability, which (through the default ConEmu settings Cmder provides) ends up routing to the indicated %CMDER_ROOT%\config\user_profile.cmd
.
You may want to just explicitly use ConEmu and Clink independently, rather than use the bundled Cmder package.
To do that, and to add in some more "gotta work on windows, what should I do" bliss, below are some good/opinionated things to do. These are admittedly VERY loosely related to the initial question, but I've recently been through a Windows setup again, have typed up most of this already, and the target audience in this question is correct, so here you go...
extras
bucket for many non-console things (scoop bucket add extras
)cmd.exe
scoop install clink
clink autorun install
to make it run when cmd.exe
is runscoop install conemu
Settings::Integration->Default term
scoop install cmder
), but I personally think it is cleaner to install/run Clink and ConEmu separately.scoop install gow
grep
, vim
, sed
, ls
, cat
, curl
, wc
, less
, find
, etc, etc) you know and love, directly in your cmd.exe
-like terminal without non-native weirdnessfind->gfind
and awk->gawk
scoop search
and scoop info
to find the ones you want, or browse the buckets (there are several buckets, not just main and extras)3
EDIT: Found this: http://msdn.microsoft.com/en-us/library/bb613488(VS.85).aspx
To expound on Martin's solution (the shortcut), here's the PowerShell equivalent.
NOTE: You will need to have PowerShell script execution enabled. If this is disabled, you'll need to run an elevated PowerShell and execute Set-ExecutionPolicy Unrestricted
. Obviously, this is not recommended for security reasons, but there ya go.
powershell.exe -noexit -File "%HOMEDRIVE%\%HOMEPATH%\ps-startup.ps1"
Have a file named ps-startup.ps1
in your user directory and your shortcut will execute it as a PowerShell file on startup. Just run PowerShell with this shortcut from here on out.
This can be achieved in a "better" way through powershell profiles. You can find the location of your current profile by: echo $profile . You can add lines to that file, and that should do it for future sessions. For existing sessions, you can reload the profile with: & $profile . – ashic – 2015-12-16T10:54:36.680
1This question isn't identical, but the answer covers your scenario. – Marcelo Cantos – 2010-05-23T10:51:23.833