DEV Community

Cover image for Let's add Cygwin into Windows Terminal and customize it for development looks
Vuong
Vuong

Posted on

Let's add Cygwin into Windows Terminal and customize it for development looks

  • Cygwin is good for people who want to have a similar experience as much as possible like macOS machine/Linux terminal. May be, you like Git Bash, but Cygwin could provide a lot of additional packages which has been ported to Windows by Windows its own API. (https://www.cygwin.com/)

  • Windows Terminal is really nice, the big change that it uses GPU to render instead of CPU as basic cmd of Windows does. (https://github.com/microsoft/terminal)

I just curious to how to add Cygwin into Windows Terminal. After installed it successfully, I would like to customize its own look to more consistent with other Terminal in Windows

Installation

I can get the executeable file for installing or just install it via scoop (http://scoop.sh/). Very simple, just scoop install cygwin

After install, I could use Linux common commands in cygwin. But, wait, I would like to have it in my Windows Terminal also.

Add it into Windows Terminal

Here is my Windows Terminal config



{
    "$schema": "https://aka.ms/terminal-profiles-schema",
    // make Cygwin become default one
    "defaultProfile": "{00000000-0000-0000-0000-000000000001}",

    "profiles": {
        // ...
        "list": [
            // just add this below item for Cygwin if you don't have any other
            {
                "guid": "{00000000-0000-0000-0000-000000000001}",
                "commandline": "%UserProfile%/scoop/apps/cygwin/current/root/Cygwin.bat",
                "icon": "%UserProfile%/scoop/apps/cygwin/current/root/Cygwin-Terminal.ico",
                "hidden": false,
                "name": "Cygwin"
            },
            //...
        ]
    },
    //...
}



Enter fullscreen mode Exit fullscreen mode

The original look

Alt Text
It is lack of:

  • Shorten path
  • Git branch name
  • Emoji (lol~!)

Customize it!

To fulfill all of lacks, I did a search on Google and found this topic https://gist.github.com/justintv/168835. Looks amazing 🥰. After try some kind of suggestions, I also added a bit of cygpath to customize the path look.

Pls have a look first before have a decision! 😂

Alt Text



// Just shorten the cygwin path
function __short_wd_cygwin() {
    num_dirs=3
    newPWD="${PWD/#$HOME/~}"
    if [ $(echo -n $newPWD | awk -F '/' '{print NF}') -gt $num_dirs ]; then
        newPWD=$(echo -n $newPWD | awk -F '/' '{print $1 "/.../" $(NF-1) "/" $(NF)}')
    fi

    echo -n $newPWD
}

// Convert shorten path and shorten the Windows path
function __short_wd_cygpath() {
    num_dirs=3
    newPWD=$(cygpath -C ANSI -w ${PWD/#$HOME/~})
    if [ $(echo -n $newPWD | awk -F '\\' '{print NF}') -gt $num_dirs ]; then
        newPWD=$(echo -n $newPWD | awk -F '\\' '{print $1 "\\...\\" $(NF-1) "\\" $(NF)}')
    fi

    echo -n $newPWD
}

FMT_BOLD="\e[1m"
FMT_RESET="\e[0m"
FMT_UNBOLD="\e[21m"
FG_BLACK="\e[36m"
FG_BLUE="\e[34m"
FG_CYAN="\e[36m"
FG_GREEN="\e[32m"
FG_MAGENTA="\e[35m"
FG_RED="\e[31m"
FG_WHITE="\e[97m"
BG_BLUE="\e[44m"
BG_GREEN="\e[42m"
BG_MAGENTA="\e[45m"

export PS1=\
"\n ${FG_BLUE}${FG_GREEN}${FMT_BOLD}\u${FMT_UNBOLD} @ ${FG_GREEN}\h "\
"${FG_BLACK}\$(__short_wd_cygpath) ${FMT_RESET}${FG_BLUE}"\
"\$(git branch 2> /dev/null | grep '^*' | colrm 1 2 | xargs -I BRANCH echo -n \"${FG_GREEN}BRANCH ${FMT_RESET}${FG_MAGENTA}\")"\
"\n ${FG_BLUE}🙈 ▶ ${FG_CYAN}\$ ${FMT_RESET}"


Enter fullscreen mode Exit fullscreen mode

Oh, yes, you want to know where to put all above commands into, lah?

Just do it step by step

Step 1. Backup the current setting



cp ~/.bashrc ~/.bashrc-bak


Enter fullscreen mode Exit fullscreen mode

Step 2. Change the content of .bashrc



# Because I like VScode
code -n ~/.bashrc


Enter fullscreen mode Exit fullscreen mode

Step 3. Put all above commands into bottom of your .bashrc file
Step 4. Store it then load it by this below command



source ~/.bashrc


Enter fullscreen mode Exit fullscreen mode

Hope you can complete these steps smoothly! 😎

Notes:

  • The font of my terminal is 'IBM Plex Mono'
  • The theme of Windows Terminal is 'Solarized Dark Higher Contrast'

Nice to hear any new tips in Cygwin from you all!!

Top comments (1)

Collapse
 
vuong profile image
Vuong

Updated: Look at this post to have the improved version of the above idea gist.github.com/justintv/168835#gi...