Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to use terminal on Windows and don’t go crazy…

How to use terminal on Windows without going crazy…

Windows is really horrible system for developers and especially for devops. It doesn’t even have a usable terminal and shell, so working with command line is really pain in the ass. If you really don’t want to switch to any usable system (OS X, Linux, BSD…), then this guide should help you to setup somewhat reasonable environment – usable terminal, proper shell, ssh client, git and Sublime Text as a default editor for shell.

Install stuff

  1. Download and install Git for Windows* with:
    • [✘] Use Git from the Windows Command Prompt
    • [✘] Checkout as-is, commit Unix-style line endings
  2. Download ConEmu Installer (stable or preview) and install x86 (32bit**) version.
  3. Download and install Sublime Text 3 – text editor for real coders. ;)
    • If you’re using 64bit Windows, then download the 64bit version. If you don’t know what the heck this means, just pick the 32bit version.

* Wait a moment… why Git? Well, every developer needs a Version Control System and git is the best one! However, the main reason is that “Git for Windows” is not just a git, it’s a bundle of some basic “unix utilities” – the easiest way to install it on Windows.

** Why 32bit version when you’re maybe running 64bit Windows? Well, just because I didn’t want to test this guide twice, or risk that something will be different on 64bit version and people start to complain…

Configure ConEmu

  1. Start ConEmu (press Win key, write ConEmu and hit enter), the fast configuration screen should appear on the first launch, just click on OK.
  2. Open Settings (Win+Alt+P) and set:
    • Startup:
      • [✘] Specified named task: {Bash::Git bash}
    • Startup/Environment:
      • Copy these lines to the text box:

        set LANG=en_GB.UTF-8
        set LC_ALL=en_GB.UTF-8
        
    • Main:
      • [✘] Clear Type
    • Main/Confirm:
      • [_] Confirm creating new console/tab (Win+W, toolbar [+])
      • [_] Confirm tab closing
    • Main/Update:
      • [✘] Check on startup
      • [✘] Stable (or Preview)
  3. Restart ConEmu.

Configure shell and ssh

  1. Generate SSH key, if you don’t have one yet:
    • ssh-keygen (copy&paste to terminal and hit enter)
    • Use default key file location.
    • Enter some password to protect your SSH key! You’ll be prompted to enter this password after opening terminal, but just once per Windows session (i.e. only after Windows reboot).
  2. Enable SSH Agent Forwarding in .ssh/config:
    • cd; echo 'ForwardAgent yes' >> .ssh/config (copy&paste to terminal and hit enter)
  3. Download preconfigured .bashrc:
    • cd; curl https://gist.githubusercontent.com/jirutka/99d57c82fa8981f56fb5/raw/.bashrc > .bashrc (copy&paste to terminal and hit enter)
  4. Restart ConEmu.

Learn new tricks

Just some basic shortcuts and commands for a decent productivity.

ConEmu

  • Open new tab:
    • Win+W
  • Close the tab:
    • Win+Del
  • Switch to next tab:
    • Win+Alt+Arrow right
  • Switch to previous tab:
    • Win+Alt+Arrow left
  • Cycle tabs:
    • Ctr+Tab
  • Copy text from console to the system clipboard:
    • Press and hold Shift, use arrows to make a selection and then hit Ctrl+C.
    • Press and hold left mouse button, make a selection and release the button.
  • Paste text from the system clipboard to console:
    • Press Ctrl+V to paste the first line from the clipboard, or Shift+Insert to paste all the clipboard content (use with caution!)
    • Press right mouse button to paste all the clipboard content.

Bash

Alternative keys in parenthesis are environment-specific (works in ConEmu).

  • Autocomplete file/directory name:
    • Type first few characters of the file/directory name and then hit Tab.
  • Scroll your command history:
    • Arrow up and Arrow down
  • Search your commands history backwards:
    • Press Ctrl+R, start typing what you’re looking for; hit Ctrl+R again and again to scroll through history.
  • Move cursor to the beginning of the line:
    • Ctrl+A (or Home)
  • Move cursor to the end of the line:
    • Ctrl+E (or End)
  • Move cursor back (left) one word:
    • Alt+B
  • Move cursor forward (right) one word:
    • Alt+F
  • Remove one word before the cursor:
    • Ctrl+W (or Alt+Backspace)

Basic bash commands

  • List content of the current directory:
    • ls (or ll)
  • Go up one directory:
    • cd .. (or ..)
  • Go to a subdirectory:
    • cd DIRECTORY
  • Go to the home directory:
    • cd
  • Connect to a remote machine as specified user using SSH:
    • ssh REMOTE-USER@SERVER-DOMAIN
  • Open file in the default text editor (Sublime Text 3 if you’ve installed it) *:
    • edit FILE
  • Copy content of a file to system clipboard (then you can paste it using Ctrl+V) *:
    • cat FILE > clip

* This will work only on your local computer, not on a remote server via SSH!

#
# 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"'
@gaastonsr

This comment has been minimized.

gaastonsr commented Nov 16, 2015

This is awesome. Thanks for sharing.

@JakeHL

This comment has been minimized.

JakeHL commented Jun 2, 2016

Great guide!! I created a directory on my C:\ drive with a shortcut to conemu, renamed it to bash and put the directory in my PATH variable, so now I can run 'bash' in the run dialog to get here. Makes things a little more fluid.

Thanks!

@iholler

This comment has been minimized.

iholler commented Mar 13, 2017

Thanks!

@jamiewanderi

This comment has been minimized.

jamiewanderi commented May 27, 2017

THAT IS AWESOME

@XBOOS

This comment has been minimized.

XBOOS commented Jun 28, 2017

Awesome. Thanks a lot.

@xskillzx

This comment has been minimized.

xskillzx commented Sep 11, 2017

Sorry for the basic question but how do I change sublime to be my default text editor?

@cnoffsin

This comment has been minimized.

cnoffsin commented Oct 16, 2017

Dude. Thanks so much for this bashrc

@vettukal

This comment has been minimized.

vettukal commented Nov 24, 2017

Awesome write-up. It actually saved me from going crazy. I owe you my normal blood pressure and sanity.

@TheLouisHong

This comment has been minimized.

TheLouisHong commented Jan 30, 2018

Great guide. An interesting way to use gist

@rojagit

This comment has been minimized.

rojagit commented Mar 15, 2018

Awesome man....using ConEMU, was on the brink of moving to PowerShell (gaak..who needs to learn new crap) when I bumped into this - wow never knew I could use GitBash now in ConEMU !! Its guys like you who share that make this world a better place in with Windows ;) Thanks!! Also - another tip for folks who my not like the nag in Sublime free - checkout Notepad++; the bash_rc is pure genius !
Another tip - many use clink for history, and it was having issues in one of my comps; but with your bashrc, THAT's history now :)
And another: if you have multi-limited users using the above setup, you can install Git in one common place and copy the Task and Add to Path for the other user(s) to save space (Git is like 1/2G) - Dunno what this will burn up down the line, will update of it does.

@KeichiSama

This comment has been minimized.

KeichiSama commented May 31, 2018

Thanks!

@PhilipRoman

This comment has been minimized.

PhilipRoman commented Sep 4, 2018

Awesome! For Windows 10 users, I can recommend the ubuntu subsystem - it is trivial to install as a Windows store app but it is actually a full ubuntu. Its fast, and everything works without hacks - you can run any linux OR windows executable from it (apt, git, tmux, ssh, vim, cron, gcc ...). I now use it as my main shell! C: is mounted as /mnt/c. Unfortunately, you cant access ubuntu files from windows (hasn't been a problem).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment