Skip to content

Instantly share code, notes, and snippets.

@sadasant
Last active January 20, 2020 15:29
Show Gist options
  • Save sadasant/87f28597131dac10cb3663fdabb61428 to your computer and use it in GitHub Desktop.
Save sadasant/87f28597131dac10cb3663fdabb61428 to your computer and use it in GitHub Desktop.
Computer tips to Jen Gentleman

Computer tips to Jen Gentleman.

For this Twitter thread: https://twitter.com/JenMsft/status/1218526323271823360

I've taken all of these from information I've found publicly available on the interner over the years. I haven't kept all of the sources.

I thought on adding more than bash-related stuff, but I ended up unconvinced. I hope some of this can be useful!

Index

Content

Bash specific

Specifically bash tips. You can put all of these in your ~/.bashrc (unless you have something with that specific name. It should work in most platforms with bash.

General tips

Generate passwords and passphrases with bash

To generate passwords:

pwgen() { < /dev/urandom tr -dc A-Za-z0-9_+-?\?!  | head -c$1; }

To generate passphrases, install the wamerican package, then use this:

phrase() { shuf -n$1 /usr/share/dict/words | tr '\n'  ' '; }
CD quickly into the first matching directory (3 levels deep)
# Quick CD
# goto changes directory to the first matching a given string.
# It goes only three levels deep. It also ignores files within node_modules and .git
function goto() {
    result=$(find -L . -maxdepth 3 -type d -not -path './node_modules*' -a -not -path '*.git*' | grep ${1})
    if [[ -z $result ]]; then return; fi
    cd $result
}

VIM specific

Edit all modified files with VIM
vim $(git status --porcelain | awk '{print $2}')

Git specific (from bash)

Print a git report for a specific repository
# gitReport week for one week ago
# gitReport month for one month ago
# gitReport for one day ago
# or like gitReport three months ago
gitReport() {
  if [ "$1" = "week" ]; then
    since="one week ago"
  elif [ "$1" = "month" ]; then
    since="one month ago"
  elif [ "$1" = "" ]; then
    since="one day ago"
  else
    since=$@
  fi
  pad=$(printf '%0.1s' " "{1..60})
  padlength=20
  IFS='
'
  echo -e "\n\e[37;1mChanges from $since:\e[0m"
  for author in `git log --format='%aN' | sort -u`; do
    commits=`git shortlog -s -n --since="$since" --author="$author" | awk '{print $1;}'`
    if [ "$commits" != '' ]; then
      printf '%s' "$author"
      printf '%*.*s' 0 $((padlength - ${#author})) "$pad"
      output=`git whatchanged --since="$since" --author="$author" --oneline --shortstat | grep -E "fil(e|es) changed" | awk '{files+=$1; inserted+=$4; deleted+=$6} END {print "files changed:", files, "\tlines inserted:", inserted, "\tlines deleted:", deleted }'`
      echo -e "\tcommits: $commits\t$output"
    fi
  done
}

Example output, valid for the repository Azure SDK for JS, made on January 18th of 2020.

gitReport month

Changes from one month ago:
Azure SDK Bot           commits: 2      files changed: 3        lines inserted: 3       lines deleted: 3
Brian Terlson           commits: 2      files changed: 27       lines inserted: 106     lines deleted: 102
Burke Holland           commits: 1      files changed: 1        lines inserted: 4       lines deleted: 4
Chidozie Ononiwu        commits: 2      files changed: 3        lines inserted: 48      lines deleted: 39
chradek                 commits: 12     files changed: 92       lines inserted: 1260    lines deleted: 717
Daniel Jurek            commits: 4      files changed: 15       lines inserted: 1045    lines deleted: 1315
Daniel Rodríguez        commits: 12     files changed: 222      lines inserted: 62260   lines deleted: 50808
Harsha Nalluru          commits: 15     files changed: 139      lines inserted: 2072    lines deleted: 2148
Heath Stewart           commits: 1      files changed: 3        lines inserted: 115     lines deleted: 37
Jeff Fisher             commits: 6      files changed: 76       lines inserted: 455     lines deleted: 186
Jeremy Meng             commits: 17     files changed: 136      lines inserted: 1356    lines deleted: 1258
Jonathan Turner         commits: 3      files changed: 12       lines inserted: 39      lines deleted: 16
Jose Manuel Heredia Hidalgo                                                             commits: 1      files changed: 21       lines inserted: 1826    lines deleted: 193
KarishmaGhiya           commits: 12     files changed: 177      lines inserted: 975     lines deleted: 932
Lin Jian                commits: 2      files changed: 17       lines inserted: 57      lines deleted: 40
Matt Ellis              commits: 1      files changed: 1        lines inserted: 3       lines deleted: 3
Mike Harder             commits: 3      files changed: 4        lines inserted: 302     lines deleted: 393
Mitch Denny             commits: 1      files changed: 18       lines inserted: 18      lines deleted: 0
pritesh93               commits: 1      files changed: 1        lines inserted: 1       lines deleted: 1
Qiaoqiao Zhang          commits: 14     files changed: 193      lines inserted: 13331   lines deleted: 10227
qiaozha                 commits: 14     files changed: 193      lines inserted: 13331   lines deleted: 10227
ramya0820               commits: 7      files changed: 41       lines inserted: 3448    lines deleted: 3676
Ramya Rao               commits: 8      files changed: 64       lines inserted: 1043    lines deleted: 1541
Richard Park            commits: 10     files changed: 51       lines inserted: 1598    lines deleted: 1044
Steve Faulkner          commits: 3      files changed: 30       lines inserted: 148     lines deleted: 259
Wes Haggard             commits: 1      files changed: 1        lines inserted: 1       lines deleted: 1
Will Temple             commits: 9      files changed: 300      lines inserted: 3405    lines deleted: 1144
Change to the common directory of the changes in the current branch
# csd: "changes' dierctory", as in the directory of the changes.
# Changes the directory to the common parent to all the changes in current branch.
# Specially useful to switch to the folder relevant to the changes that a specific branch or Pull Request makes in a large project.
# By default acts based on the current changes to the current branch, but another target can be spcified by passing a single argument.
# Example: csd master # changes to the common parent relative to the master branch.
# Source of the regexp: https://stackoverflow.com/a/17475354
function csd() {
  common_prefix=$(git diff $1 --name-only | sed -e 'N;s/^\(.*\).*\n\1.*$/\1\n\1/;D') 
  common_parent=${common_prefix%/*}
  cd $(git rev-parse --show-toplevel)/$common_parent
}

TMUX specific

Reorder TMUX windows
# Reorder tmux windows
# From http://stackoverflow.com/a/8835493
function sortmux() {
  # re-number tmux sessions
  for session in $(tmux ls | awk -F: '{print $1}') ;do
    inum=0
    active_index=$(tmux lsw -t ${session} | grep -n "active" | head -c 1)
    old_active=$(tmux lsw -t ${session} | awk "NR==${active_index}" | head -c 1)
    for window in $(tmux lsw -t ${session} | awk -F: '/^[0-9*]/ {print $1}') ;do
      if [ ${window} -gt ${inum} ] ;then
        echo "${session}:${window} -> ${session}:${inum}"
        tmux movew -d -s ${session}:${window} -t ${session}:${inum}
      fi
      inum=$((${inum}+1))
    done
    active=$(tmux lsw -t ${session} | awk "NR==${active_index}" | head -c 1)
    if [ $old_active -ne $active ]; then
      tmux select-window -t ${session}:${active}
      echo "Active ${old_active} is now ${active}"
    fi
  done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment