Vincent Touache - Character TD
/tutos/25_windows_aliases/content.php
data=================:ArraySetting aliases for powershell

Table of Contents

  1. Disclaimer
  2. Windows Terminal
  3. Aliases
    1. Source your file on Windows Terminal opening
    2. Populate the file with aliases



Disclaimer

I am miles away from being an Microsoft wizard, so I suggest you take what I say here with a grain of salt. This page is primarily a note for myself, after I struggled several times with this issue, so next time I can directly refer to it. If it can help other people on the way, great, but I won't pretend everything I say here is absolutely accurate

Windows Terminal

If, like me, you're coming from a unix background (either Linux or MacOS), you must be astonished every day by how shitty, slow, buggy and limited Microsft can be. To make the transition a tad less painful, you probably want to use WindowsTerminal

Windows Terminal is a terminal emulator, developped by Microsoft. It doesn't come by default (go figure), but I strongly suggest you install it, as it'll make your experience with the terminal much more similar to what you'd have on Linux. It can run on a command prompt environment, but also on a powerShell environment, which is what I'm using. You can download it from the app store here, not much to say about it

Aliases

Once you have your Windows Terminal setup with the PowerShell core, you can start creating aliases. As a reminder, an alias on Unix is a word that you can use in the terminal to run more complex commands. The typical use case is:

alias go_somewhere = "cd /path/to/a/very/deep/folder"

Every time you'll type go_somewhere in your terminal, it'll swap it with cd /path/to/.../folder

On Windows, the logic is a bit different. You could use the DOSKEY command, but this works (afaik) only for command prompt. For PowerShell, you need to use directly some scripts, which is a 2 steps operation.

Source your file on Windows Terminal opening

If you don't want to have to manually source your file every time you open a new terminal, you need to put it in a specific location. Typically, that would be something like

C:\Users\{your_user_name}\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

(although it seems that you can name your *.ps1 file like you want), but to be sure, you can run echo $profile and see where it points.

Populate the file with aliases

Once you have your file ready, open it and start writing functions. Those functions an be extremely simple, like

    function nvimPrefs{
        cd C:\Users\{your_username}\AppData\Local\nvim-data
        neovide.exe .
    }
or more complex, with arguments, like
    function mkcd {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory=$true)]
            [string]$name
            )
        mkdir $name
        cd $name
    }
Once your file has been saved (and after you relaunch a terminal), your functions are part of this scope, and you can call then directly. Which means that if you type nvimPrefs, it will automatically cd to your vim prefs folder and launch neovide in this folder