Terminal-like BASH on Windows

January 17, 2015 - 6 minute read -
windows

Many of you, I believe, would love the experience when using the Terminal App on Mac OS X. Now I would like to show you how to have a similar experience when you are (unavoidably) using Windows.

Features:

  • A terminal like multi-tab window.
  • Use familiar *nix commands like ls, cd and etc.
  • Selection of text like human (compared with CMD).
  • Copy and paste by Ctrl + C / Ctrl + V.
  • Install packages by choco install <package name>.
  • Open a text file with Sublime Text using edit filename.
ConEmu

A screenshot. (I am using Windows 7, really.)

We are using the following tools to imitate the Terminal-like feeling:

Steps to install

Step 1: Installing ConEmu

ConEmu is a Windows console emulator, which is available here.

You can customize a lot of stuffs in ConEmu. It would be quite simple to be done in the GUI. You may refer to their official guide, or google for the answer.

Step 2: Installing & Setting up Git BASH on ConEmu

Git Bash is a component from Git for Windows or msysgit. It is created for those who want to use Git in Windows.

Git for Windows offers a lightweight, native set of tools that bring the full feature set of the Git SCM to Windows, including a BASH emulation called Git BASH.

During installation, please take a note of the location where it is installed.

In my PC, Git for Windows is installed at the location below by default.

C:\Program Files (x86)\Git\

Open ConEmu after installation, click the small triangle next to the plus (+) symbol on the top right corner.

It will open a Settings window. Find a square button with a plus sign in the left side. Click it.

It will add an item into the list of predefined tasks.

Select the task you just created. You may give it a name like Git Bash or whatever you like. You may also select it as the default task for new console.

Please leave the task parameters empty and add the following code to the Commands.

"C:\Program Files (x86)\Git\bin\sh.exe" --login -i

Please modify the installation path accordingly.

Click “Save settings” button on the bottom right corner to close the setting window when finished.

You can verify your setting by trying to create a new console in ConEmu. It can be done using the plus (+) button on the top right.

Step 3: Installing Chocolatey

Installation of Chocolatey is optional. You can also do this anytime in the future.

From their introduction,

Chocolatey is a CLI-based package manager for Windows that is sort of like apt-get.

To install Chocolatey, you may follow the official guide in there website, in a PowerShell or CMD window. Or you can start using ConEmu. Both are the same because they would change your system’s environmental variables so that you can use the choco command everywhere.

To install Chocolatey in ConEmu, click the small triangle next to the plus (+) symbol on the top right corner. And then select PowerShell in the dropdown menu. It will start a new PowerShell console.

After that, run the following command by pasting them in the ConEmu window by Ctrl + C or right click.

iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))

Step 4: Configuring SSH IDs and Setting up Git

This step is also optional, but it will be very helpful if you are going to use Git in the future.

The Git BASH will load your BASH profile from a file name .bashrc in your home directory, like the “real” BASH.

I’ve forked and modify a piece of code from jirutka, that will do a lot of settings for you automatically.

#
# Bash settings
#
 
# Append to the history file instead of overwrite it 
shopt -s histappend
 
# Append the previous command to history each time a prompt is shown
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
 
# Increase history size
export HISTSIZE=1000
export HISTFILESIZE=1000
 
# Customize prompt
# export PS1='\[\033[01;32m\]\u@localhost:\[\033[01;34m\] \w \$\[\033[00m\] '
 
 
#
# Set EDITOR
#
 
editors=(
    "@PROGRAMFILES@\Sublime Text 3\sublime_text.exe!-w -n"
    "@PROGRAMFILES@\Sublime Text 2\sublime_text.exe!-w -n"
    "@PROGRAMFILES@\Notepad++\notepad++.exe!-nosession -multiInst"
)
 
editor=
for item in "${editors[@]}"; do
    if [[ "$item" = *@PROGRAMFILES@* ]]; then
        for pfpath in "$PROGRAMFILES" "$PROGRAMW6432"; do
            cmd="${item/@PROGRAMFILES@/$pfpath}"
            if [ -f "${cmd%!*}" ]; then
                editor="$cmd"
                break 2
            fi
        done
    elif [ -f "${item%!*}" ]; then
        editor="$item"
        break
    fi
done
 
if [ -n "$editor" ]; then
    export EDITOR="'${editor%!*}' ${editor#*!}"
else
    export EDITOR=${EDITOR:-vim}
    echo
    echo "!! Cannot find Sublime Text or Notepad++! If you don't want to use $EDITOR"
    echo "!! as your default editor, then install one of these, or you will cry..."
    echo
fi
 
 
#
# Auto-launch ssh-agent
# Source: https://help.github.com/articles/working-with-ssh-key-passphrases
#
 
# Note: ~/.ssh/environment should not be used, as it
#       already has a different purpose in SSH.
env=~/.ssh/agent.env
 
# Note: Don't bother checking SSH_AGENT_PID. It's not used
#       by SSH itself, and it might even be incorrect
#       (for example, when using agent-forwarding over SSH).
agent_is_running() {
    if [ "$SSH_AUTH_SOCK" ]; then
        # ssh-add returns:
        #   0 = agent running, has keys
        #   1 = agent running, no keys
        #   2 = agent not running
        ssh-add -l >/dev/null 2>&1 || [ $? -eq 1 ]
    else
        false
    fi
}
 
agent_has_keys() {
    ssh-add -l >/dev/null 2>&1
}
 
agent_load_env() {
    . "$env" >/dev/null
}
 
agent_start() {
    (umask 077; ssh-agent >"$env")
    . "$env" >/dev/null
}
 
if ! agent_is_running; then
    agent_load_env
fi
 
# if your keys are not stored in ~/.ssh/id_rsa.pub or ~/.ssh/id_dsa.pub, you'll need
# to paste the proper path after ssh-add
if ! agent_is_running; then
    agent_start
    ssh-add
elif ! agent_has_keys; then
    ssh-add
fi
 
unset env
 
 
#
# Aliases
#
 
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias cd..='cd ..'
alias cp='cp -irv'
alias du='du -h --max-depth=1'
alias ll='ls -FGahl --show-control-chars --color=always'
alias ls='ls -AF --show-control-chars --color=always'
alias md='mkdir -p'
alias mv='mv -iv'
alias rm='rm -ir'
alias edit='eval "$EDITOR"'
 
#
# Others
#
 
# Set Java to JAVA_HOME
export PATH="/c/Program Files/Java/jdk1.7.0_71/bin":$PATH

Step 5: Enjoy

It’s time to enjoy now. Cheers :)