WSL2 with Windows Terminal and syncing with Mac OSX

It has been a long time since I’ve posted anything here and it’s time to bring something new to this blog that’s not cloud specific.

My work from home setup consists of a powerful Windows 10 machine with 2 monitors (in horizontal + vertical orientation). I also have a company provided Macbook Air 2019 model, which is not performing upto the mark, especially when multitasking with multiple IDE’s, especially resource-hungry Google Chrome and Google Meet.

This prompted me to configure my desktop terminal with the personalization and settings of iTerm2 on my OSX. So let’s get started…

Install WSL2 on Windows 10

For this, you need:

  • updated Windows 10.
  • A computer with Hyper-V Virtualization support

You can follow the Microsoft provided documentation to get started or run the steps manually mentioned below:

Enable WSL

Open the PowerShell tool as an Administrator and run the command below:

1
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

Enable “Virtual Machine Platform”

In the same Administrator Terminal, run the below command to enable Virtual Machine Platform

1
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Set WSL 2 as default

In the same Administrator Terminal, run the below command to set WSL 2 as default

1
wsl --set-default-version 2

Install a Linux distribution

For this I went ahead with Ubuntu 18.04 LTS as it’s my preferred choice.

  1. Open Microsoft Store and search for Ubuntu. Select Ubuntu 18.04 to install and Launch it.
    Screenshot_1

    Launching the distro for the 1st time will take some time to set things up in the backend.

  2. Congratulations ! You have successfully installed the WSL 2 with Ubuntu. Update your machine with the following commands:

    1
    2
    3
    sudo apt update
    sudo apt upgrade -y
    sudo reboot

    Note the space before the 3rd command.

Terminal Configuration

Time for more fun.

In this section, we’ll configure the terminal with our configuration (in this case, mine). We all know OSX ships with ZSH by default (no Bash, because licenses and shit), so we’ll configure that here as well.

By default Ubuntu comes with Bash Shell, and no pre-installed ZSH.

Install ZSH

  1. I prefer the package manager version for installing ZSH:

    1
    sudo apt install zsh -y
  2. Verify installation by running the below command for output zsh 5.4.2 or higher.

    1
    zsh --version
  3. Now make your default shell to ZSH

    1
    chsh -s $(which zsh)
  4. Log out and log back in and run echo $SHELL to see zsh configured.

Install Oh My ZSH

Oh My Zsh is an open source, community-driven framework for managing your zsh configuration. It really brings out a whole new perspective in your terminal environment and will make you more productive.

I have OMZ installed on my MacBook, so I’ll be doing that here as well. Official instructions for installation are available on their Github repo, but I’ll post here for convenience:

1
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Once the installation is completed, you can install themes, add your favourite plugins (I’ll share my heavily modified ~/.zshrc later). I’ll be not doing it as I will bring in my zshrc configuration file from my MacBook.

Theme I use

Syncing between OSX and Windows

Here I’ll be sharing a beautiful method of sharing configuration files using git. Thanks to Ferry Boender and the link to the blog here: https://www.electricmonk.nl/log/2015/06/22/keep-your-home-dir-in-git-with-a-detached-working-directory/. I’d highly recommended you to go through the entire article. Very informative. We could have also used file sharing service such as Dropbox or Google Drive, but that has some issues. Also, I am not comfortable using those for this specific use case.

I’ll summarize the steps here from the blog here.

On your Github

  1. Create a private repo with a name of your choice (preferably with a dot prefix to keep it hidden, if supported)

On your MacBook

dgit Setup

  1. Create a directory to hold your git database (the .git dir):

    1
    2
    3
    mkdir ~/.dotfiles/
    cd ~/.dotfiles/
    git init .
  2. Create a .gitignore file with everything ignored:

    1
    2
    3
    echo "*" > .gitignore
    git add -f .gitignore
    git commit -m "gitignore"
  3. Create a dgit alias (magic happens here) and add to your shell profile, and source it:

    1
    2
    echo "alias dgit='git --git-dir ~/.dotfiles/.git --work-tree=\$HOME'" >> ~/.zshrc
    source ~/.zshrc
  4. Go to your home directory, and do dgit reset --hard.

dgit Usage

  1. Check Usage:

    1
    dgit status
  2. Add files by overriding the ignore with -f parameter:

    1
    2
    dgit add -f .zshrc
    dgit commit -m "Added .zshrc"
  3. Push to a remote repo (created earlier; I use AWS CodeCommit):

    1
    2
    dgit remote add origin ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/.dotfiles
    dgit push origin master

On your WSL 2 Machine

dgit Usage

  1. Go to your home directory and run the below commands. This will overwrite files in your home directory with same names.

    1
    2
    3
    4
    cd $HOME
    git clone ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/.dotfiles ./.dotfiles
    alias dgit='git --git-dir ~/.dotfiles/.git --work-tree=$HOME'
    dgit reset --hard
  2. Done.

Note: You can add more files/folders by the command dgit add -f <file/folder> on either machine, and then dgit pull origin master on the other machine.


My Personalized Configs

.zshrc

Since some commands behave differently on OSX (BSD based) and Linux (GNU based), I have added an if-else-fi clause in my .zshrc file to keep the commands working properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
ZSH_THEME="powerlevel10k/powerlevel10k"
#ZSH_THEME="robbyrussell"

# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in ~/.oh-my-zsh/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"

# Uncomment the following line to automatically update without prompting.
# DISABLE_UPDATE_PROMPT="true"

# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS=true

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder


# User configuration

# export MANPATH="/usr/local/man:$MANPATH"

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
# export EDITOR='vim'
# else
# export EDITOR='mvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch x86_64"
# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
#
# Which plugins would you like to load?
# Standard plugins can be found in ~/.oh-my-zsh/plugins/*
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(
git
colored-man-pages
colorize
pip
python
osx
gcloud
ansible
aws
zsh-syntax-highlighting
)
source $ZSH/oh-my-zsh.sh

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

###############################################################################################
# PATH GOES HERE
if [[ "$OSTYPE" == "darwin"* ]]; then
# golang Environment variables
export GOPATH=$HOME/.go # don't forget to change your path correctly!
export GOROOT=/usr/local/opt/go/libexec
export PATH="$GOPATH/bin:$GOROOT/bin:$PATH"
# php stuff
export PATH="/usr/local/opt/php@7.3/bin:/usr/local/opt/php@7.3/sbin:$HOME/.composer/vendor/bin:$PATH"

# ruby stuff
# Install Ruby Gems to ~/gems
export GEM_HOME=$HOME/gems
export PATH="$HOME/.gem/ruby/2.7.0/bin:$PATH"
export PATH=$HOME/gems/bin:$PATH
export PATH="/usr/local/opt/ruby/bin:$PATH"

# export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:$HOME/Library/Python/2.7/bin:/usr/local/opt/gnu-sed/libexec/gnubin:$PATH
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin:/usr/local/opt/gnu-sed/libexec/gnubin:$PATH
export PATH="/usr/local/opt/grep/libexec/gnubin:$PATH"
export PATH="/usr/local/opt/mysql-client/bin:$PATH"
export PATH="$HOME/Library/Python/3.8/bin:$PATH"

alias date=gdate
alias tar=gtar
alias brew='HOMEBREW_NO_AUTO_UPDATE=1 /usr/local/bin/brew'
alias brew-upgrade='/usr/local/bin/brew upgrade'
alias sed=gsed
alias awk=gawk
alias grep=ggrep
# alias python=/usr/local/bin/python3
alias pip=/usr/local/bin/pip3
alias python=/usr/local/bin/python3
# aws autocomplete
autoload bashcompinit && bashcompinit
complete -C '/usr/local/bin/aws_completer' aws

# The next line updates PATH for the Google Cloud SDK.
if [ -f '$HOME/google-cloud-sdk/path.zsh.inc' ]; then . '$HOME/google-cloud-sdk/path.zsh.inc'; fi

# The next line enables shell command completion for gcloud.
if [ -f '$HOME/google-cloud-sdk/completion.zsh.inc' ]; then . '$HOME/google-cloud-sdk/completion.zsh.inc'; fi

# Source chtf
if [[ -f /usr/local/share/chtf/chtf.sh ]]; then source "/usr/local/share/chtf/chtf.sh"; fi

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm

elif [[ "$OSTYPE" == "linux-gnu" ]]; then
cd $HOME
export PATH=/home/vrnchndk/.local/bin:$PATH
export PATH="$(tr ':' '\n' <<<"$PATH" | grep -Ev '^(/mnt)/c/' | paste -sd ':')"
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
fi

###############################################################################################
# aliases go here
alias dgit='git --git-dir ~/.dotfiles/.git --work-tree=$HOME'
alias ip='curl icanhazip.com'
alias ll='ls -GFlashrt'
alias axel='axel -a -n 8'
alias ssh='ssh -o StrictHostKeyChecking=false'
alias scp='scp -o StrictHostKeyChecking=false'
alias sub='subliminal download -l en'
alias harpic="clear && printf '\e[3J'"
alias youtube-dl='youtube-dl --external-downloader axel --external-downloader-args "-a -n 8"'

###############################################################################################
######## Bash History stuff goes down here ######
export SHELL_SESSION_HISTORY=0
export HISTFILESIZE=100000
export HISTSIZE=100000
export HISTTIMEFORMAT="%F %T "
export HISTCONTROL=ignoreboth
export HISTIGNORE='rm'
#setopt histappend
setopt BANG_HIST
setopt EXTENDED_HISTORY
setopt INC_APPEND_HISTORY
setopt SHARE_HISTORY
setopt HIST_EXPIRE_DUPS_FIRST
setopt HIST_IGNORE_DUPS
setopt HIST_FIND_NO_DUPS
setopt HIST_IGNORE_SPACE
setopt HIST_SAVE_NO_DUPS
setopt HIST_REDUCE_BLANKS
# After each command, append to the history file and reread it
#export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"
#export PROMPT_COMMAND='history -a'
##################################################
# Few Tricks
## File History Completion
#autoload -Uz up-line-or-beginning-search down-line-or-beginning-search
#zle -N up-line-or-beginning-search
#zle -N down-line-or-beginning-search
#bindkey '^[[A' up-line-or-beginning-search # Arrow up
#bindkey '^[OA' up-line-or-beginning-search
#bindkey '^[[B' down-line-or-beginning-search # Arrow down
#bindkey '^[OB' down-line-or-beginning-search
##

##################################################

# Create a folder and move into it in one command
function mkcd() { mkdir -p "$@" && cd "$_"; }

# Specify default editor. Possible values: vim, nano, ed etc.
export EDITOR=vim
export VISUAL=vim

# ANSIBLE_HOST_KEY_CHECKING=False
export ANSIBLE_HOST_KEY_CHECKING=False

##################################################
#PS1='${SSH_CONNECTION+"%{$fg_bold[green]%}%n@%m:"}%{$fg_bold[green]%}: %~%{$reset_color%} # '

# Terraform SDK Load to use with roles
export AWS_SDK_LOAD_CONFIG=1

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

If you check under linux-gnu block, I have added a small line for PATH

1
export PATH="$(tr ':' '\n' <<<"$PATH" | grep -Ev '^(/mnt)/c/' | paste -sd ':')"

What this does is, it removes the /mnt/c/ PATH definitions from environment variables and just retains the paths specific to the Linux box. Keeps the path simple and easy to read.

Theme Settings

The theme I use is Powerlevel10k. Link to installation: https://github.com/romkatv/powerlevel10k#oh-my-zsh.

After installing, run p10k configure to set the terminal to your taste.

Preview

Screenshot_2


In case you want to add something to this guide, or I may have missed anything, do let me know. Do comment and share if this guide have helped you.

terminal git osx windows wsl wsl2 mac macbook