Skip to content

Instantly share code, notes, and snippets.

@cnoffsin
Forked from jirutka/-README.md
Created May 10, 2018 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cnoffsin/a4070ab5193ea8cdba560103d594e4a1 to your computer and use it in GitHub Desktop.
Save cnoffsin/a4070ab5193ea8cdba560103d594e4a1 to your computer and use it in GitHub Desktop.
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"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment