2

I've found a few posts on command line aliases in Windows but most of them relate to aliasing long cd commands/paths that are frequently used and I can't make them work for my needs.

In my case, I flip back and forth between Windows and MacOS on a semi-frequently basis when I use command prompts. Since I'm back and forth between the two, habitually I'll enter ls or dir in one or the other and get an obvious error.

Specific to this question, what I'd like to do is map some type of alias of "ls" to "dir", so that if I accidentally enter "ls", I get the same functionality as the "dir" command. I don't need any complex parameter mapping, just a simple alias for ls to dir.

If there is a solution that works for cmd, I'd appreciate the guidance.

RLH
  • 4,185
  • 16
  • 42
  • 55
  • I don't understand this question? `ls` is already an alias for `dir` in PowerShell. PowerShell is the default on all supported versions of Windows 10 while Windows Terminal (PowerShell) is (becoming/became) the default on Windows 11. Command Prompt is on it's way out. – Ramhound Jun 09 '22 at 13:42
  • @Ramhound I apologize. I don't use PS except when I have too, over cmd. Anyway, I updated my question to remove the bit about Power Shell. I added it on a whim because I do use it occasionally and if wanted to be sure ls was working every where. – RLH Jun 09 '22 at 13:47
  • https://stackoverflow.com/questions/20530996/aliases-in-windows-command-prompt – Keltari Jun 09 '22 at 13:50
  • 1
    @RLH [`Set`](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/set_1) is used in Cmd to set variables and all cmd programs can be run within PowerShell by prefacing with `Cmd /c ` _(while `Dir` is aliased to `Get-ChildItem`, its parameters aren't and must be accessed via `Cmd /c Dir`)_. Many cmd-based programs are simply more efficient to use than PowerShell's long verb-noun structure, and when needing to use them, preface them with `Cmd /c `. For example, PowerShell's equivalent of the 16char `cmd /c dir /a:hs` is 39char `Get-ChildItem -Attributes Hidden,System`. – JW0914 Jun 09 '22 at 14:00
  • @RLH I guess my curiosity question here would be, "Why?" If you are using a shell that much, what use-case do you have that keeps you in CMD 99% of the time? PowerShell is the modern replacement for CMD, and it just provides so much more, including aliases (with, as mentioned, `ls` built-in to some degree). Not to mention tab-completion, modern script syntax, etc., etc. I'd also recommend Windows Subsystem for Linux, since you could run the "real" `ls` command (including the handy `-h` flag). That would also give you, perhaps 97% command-line parity with MacOS. But what keeps you on CMD? – NotTheDr01ds Jun 09 '22 at 14:09
  • If you really do need to spend 99% of your time in CMD, I might also recommend [Take Command](https://jpsoft.com). It's a paid product, but I used it personally for many years before I switched to primarily Cygwin then WSL. – NotTheDr01ds Jun 09 '22 at 14:10

2 Answers2

2

In PowerShell both dir and ls have the same effect, one is an alias of the other.

harrymc
  • 455,459
  • 31
  • 526
  • 924
  • There is a caveat with this, as while `Dir` is aliased to `Get-ChildItem`, its parameters are not and must be accessed via `Cmd /c Dir` – JW0914 Jun 09 '22 at 13:36
  • Ah, 99% of the time I'm in cmd. Ok, that's fine. So, I just need to map "dir" to "ls" in cmd. Well, I'd still like to know. Give me a few minutes, and I'll update my question. – RLH Jun 09 '22 at 13:43
1

In PowerShell, such an alias is already present by default. (More precisely, both ls and dir are aliases to the Get-ChildItem cmdlet. It also has default aliases for cat, rm, etc.) So just use DOSKEY for Cmd alone.

Similar to Bash, PowerShell has aliases and shell functions:

Set-Alias -Name ls -Value Get-ChildItem
Set-Alias -Name vim -Value notepad.exe
Remove-Item -Path Alias:curl -ErrorAction Ignore
Function kssh {ssh -o PreferredAuthentications=gssapi-with-mic $args}

DOSKEY defines aliases at console window level; they work in Cmd because it reads a whole line at a time and lets the console handle the actual input (file tab-completion is also provided by the console) but don't work in PowerShell because it reads keys in "raw" mode, one key at a time (it has its own 'PSReadline' much like Bash's readline).

u1686_grawity
  • 426,297
  • 64
  • 894
  • 966
  • Sorry, I updated my question. I am only requesting this now for cmd because Power Shell already uses "ls" to map to "dir". – RLH Jun 09 '22 at 13:47