DEV Community

Cover image for Sharing gh cli aliases between Windows & WSL ⚡
Cody Antonio Gagnon
Cody Antonio Gagnon

Posted on • Updated on

Sharing gh cli aliases between Windows & WSL ⚡

Aliases are a fantastic way to shave seconds off of repetitively typed commands. In this post we'll explore how to share gh cli aliases between Windows and WSL!

borrow your laptop

Source: https://xkcd.com/1806

✔️ Pre-reqs

  • gh cli installed on:
    • Windows
    • WSL

📃 Setup

Create the shared gh aliases file

Under Windows using cmd.exe:

# Using Windows %USERPROFILE%
cd %USERPROFILE%
mkdir config
notepad.exe %USERPROFILE%\config\shared-gh-aliases.yaml
Enter fullscreen mode Exit fullscreen mode

When using a yaml file to store gh aliases, use the format described in cli man pages:

name_of_alias: actual_command
Enter fullscreen mode Exit fullscreen mode

Example:

# shared-gh-aliases.yaml
prc: pr create
prv: pr view
Enter fullscreen mode Exit fullscreen mode

Configure Windows and WSL to use the shared aliases file

As the gh cli does not include an exposed store for aliases, we instead rely on running shell commands that import all the aliases into the session per-windows/WSL.

In PowerShell, edit your profile (I like to use CurrentUserAllHosts and YMMV):

notepad $profile.CurrentUserAllHosts
Enter fullscreen mode Exit fullscreen mode

Append the following:

gh alias import "C:/Users/YOUR_USER/config/shared-gh-aliases.yml" --clobber
# add '> $null' to suppress output
Enter fullscreen mode Exit fullscreen mode

We use the --clobber option to indicate we are OK with updating/potentially clobbering our aliases when this command runs as we intend to source our aliases from the file

⚠️ gh alias list to review existing aliases on both platforms before potentially clobbering

Restart your terminal and confirm that your gh aliases loaded!

Heading over to WSL Ubuntu:

wslpath -u "C:/Users/YOUR_USER/config/shared-gh-aliases.yml" # copy this for next step
notepad.exe ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

And append the following,

gh alias import "/mnt/c/Users/YOUR_USER/config/shared-gh-aliases.yml" --clobber
# add '>/dev/null' to suppress output
Enter fullscreen mode Exit fullscreen mode

💥 Your gh aliases should now work in WSL & Windows! 🎸

Top comments (1)

Collapse
 
7h33mp7ym4n profile image
7h3-3mp7y-m4n

Fantastic 😊