10 tiny helper scripts for devs!
Tags: productivity, linux, webdev
Published: 2020-08-24 06:12:14

---
title: 10 tiny helper scripts for devs!
published: true
description: A small collection of my productivity scripts to get things done
tags: productivity, linux, webdev
cover_image: https://i.imgur.com/7DYgUGj.jpg
---

## Notes
- This is a collection of tiny scripts that you can execute through the terminal.
- These are designed for Ubuntu (with zsh), but may work on other unixy systems
- Some are stolen scripts, other's are original!
- Scripts are in various languages e.g; `#!/usr/bin/env ruby`
- These are part of a `dotfiles` collection:
  - Which is basically a folder on your system for organising personal scripts and terminal configuration.
  - Here's the source repo: https://github.com/benwinding/dotfiles

## 10 tiny helper scripts

## (1) `git-undo`
``` sh
#!/bin/sh

# Undo your last commit  (in current folder), but don't throw away your changes

git reset --soft HEAD^
```

## (2) `gclonecd https://github.com/reactjs/react-modal`
``` sh
#!/bin/zsh

# Clones git repo and cd's into it
gclonecd() {
  git_dir="$(basename "$1" .git)"
  git_dir_resolved=${2:-$git_dir}
  git clone "$@" && cd "$git_dir_resolved";
}
```

## (3) `killport 3000`
``` sh
#!/bin/zsh

# Kills a process running on a specified tcp port
killport() {
  echo "Killing process on port: $1"
  fuser -n tcp -k $1;
}
```

## (4) `mvp ./this-dir ./some/other/new/dir`
``` sh
#!/bin/zsh

# Move and make parents

function mvp () {
    source="$1"
    target="$2"
    target_dir="$(dirname "$target")"
    mkdir --parents $target_dir; mv $source $target
}
```

## (5) `dos2unixd ./dir` 
``` sh
#!/bin/zsh

# Dos2unix a whole directory (crlf to lf)
dos2unixd() {
  find $1 -type f -print0 | xargs -0 dos2unix
}
```

## (6) `docker-kill-all`
``` sh
# Kills all docker containers running
alias docker-kill-all='docker kill `docker ps -aq`'
```

## (7) `docker-rm-all`
``` sh
# Deletes all docker containers
alias docker-rm-all='docker rm `docker ps -aq`'
```

## (8) `git-delete-local-merged`
``` sh
#!/bin/bash
#
# Delete all local branches that have been merged into HEAD. Stolen from
# our favorite @tekkub:
#
#   https://plus.google.com/115587336092124934674/posts/dXsagsvLakJ

branches=`git branch --merged master | grep -v '\*\|master\|unstable\|develop'`
[ -z "$branches" ] && printf '\nNo merged branches to delete...\n' && exit;
command="git branch -d $branches"

echo ''
printf '%s\n' "$branches"
echo ''
printf 'Delete merged branches locally? Press [Enter] to continue...'
read _

echo ''
echo 'Safely deleting merged local branches...'
echo 'Running command: '$command

`$command`
```

## (9) `git-delete-remote-merged`
``` sh
#!/bin/bash

remote='origin'
echo "Will delete the following branches from [$remote]:"
branches=`git branch -r --merged master | grep -v '\*\|master\|unstable\|develop'`
[ -z "$branches" ] && printf '\nNo merged branches to delete...\n' && exit;

echo ''
printf '%s\n' "$branches"
echo ''
printf 'Delete merged remote branches from '$remote'? Press [Enter] to continue'
read _
echo ''
echo 'Safely deleting merged remote branches...'

echo $branches | sed -e "s:$remote/::g" | xargs -t git push $remote -d
```

## (10) `github-open` 
``` ruby
#!/usr/bin/env ruby

# Opens the current github repository in the default browser
remotes = `git remote`
hasNoRemotes = remotes.size < 2
if hasNoRemotes then 
  return
end
repolink = `git remote get-url origin`
if repolink.include?("git@github.com:") then 
  # Converts from ssh format to https format if necessary

  # git@github.com:benwinding/dotfiles
  # https://github.com/benwinding/dotfiles
  repolink = repolink.gsub("git@github.com:", "https://github.com/")
end

`xdg-open #{repolink}`
```
I highly recommend anyone who isn't familiar with shell scripting to checkout [holman's dotfiles](https://github.com/holman/dotfiles) which is targetted towards make users. Or my own fork [ben's dotfiles](https://github.com/benwinding/dotfiles) which is targetted towards ubuntu users.

When you begin writing and maintaining your own little library of scripts and snippets, you'll be amazed at what you can automate and accomplish.