DEV Community

Cover image for 10 tiny helper scripts for devs!
Ben Winding
Ben Winding

Posted on

10 tiny helper scripts for devs!

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:

10 tiny helper scripts

(1) git-undo

#!/bin/sh

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

git reset --soft HEAD^
Enter fullscreen mode Exit fullscreen mode

(2) gclonecd https://github.com/reactjs/react-modal

#!/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";
}
Enter fullscreen mode Exit fullscreen mode

(3) killport 3000

#!/bin/zsh

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

(4) mvp ./this-dir ./some/other/new/dir

#!/bin/zsh

# Move and make parents

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

(5) dos2unixd ./dir

#!/bin/zsh

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

(6) docker-kill-all

# Kills all docker containers running
alias docker-kill-all='docker kill `docker ps -aq`'
Enter fullscreen mode Exit fullscreen mode

(7) docker-rm-all

# Deletes all docker containers
alias docker-rm-all='docker rm `docker ps -aq`'
Enter fullscreen mode Exit fullscreen mode

(8) git-delete-local-merged

#!/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`
Enter fullscreen mode Exit fullscreen mode

(9) git-delete-remote-merged

#!/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
Enter fullscreen mode Exit fullscreen mode

(10) github-open

#!/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}`
Enter fullscreen mode Exit fullscreen mode

I highly recommend anyone who isn't familiar with shell scripting to checkout holman's dotfiles which is targetted towards make users. Or my own fork ben's 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.

Top comments (13)

Collapse
 
moopet profile image
Ben Sinclair • Edited

A few thoughts -

1 might be better named "git-uncommit" because "undo" implies reverting whatever the last action was, not specifically the last commit.

2, 3 and 4 don't need a shebang. They need to be sourced rather than executed else they'll do nothing.

4 doesn't need the function keyword, and has two commands on the same line separated with a semicolon which is not great for readability.

6 and 7 would be better off using $() than backticks.

8 and 9 mix a lot of styles and seems like they could be made more readable by sticking to printf instead of echo and consistently using double-quotes.

8 and 9 rely on the main branch being called "master" which is less and less likely to be the case nowadays. If you're using git-flow, you can use this hack to get the name of the "master" branch:

main_branch=$(git config --local --get gitflow.branch.master)
Enter fullscreen mode Exit fullscreen mode

They also expect certain conventions for other branches such as "unstable" and the principle remote to be called "origin", which is not always the case.

Collapse
 
benwinding profile image
Ben Winding

Thanks Fellow Ben,
Cheers for the feedback, you seem to have some knowledge of shell code, do you have your own library of snippets too? If not you should consider starting a dotfiles git repo πŸ‘
Cheers

Collapse
 
moopet profile image
Ben Sinclair

I have one! And it has a few git tools in it.

I had the same alias as you for "undo" but as a git alias and just updated it to be "uncommit" rather than my rather dumb, "oops" command. I never used "oops" for exactly the reason I put in my comment. I remembered I had it but not what it did and couldn't be bothered to check. Now maybe I will...

github.com/moopet/dotfiles

Thread Thread
 
benwinding profile image
Ben Winding

Thanks for sharing, yeah personally I like to name functions as an extension of the original command e.g; git-uncommit is longer but it's easily discoverable when you're typing git and tab-completion makes it fairly easy to type.

I like your alias alias gti='git' I always mistype that!

Collapse
 
goodidea profile image
Joseph Thomas

love this! I'm on OS X and added the github-open command - i just had to change the last line to open #{repolink}

Also, it took me a minute to figure out that I needed to add these files to my $PATH, a note in the post on how to do this would be helpful for others!

Collapse
 
helderberto profile image
Helder Burato Berto • Edited

Nice tips!

I've some useful things on my dotfiles: github.com/helderburato/dotfiles

Collapse
 
benwinding profile image
Ben Winding

Nice, thanks for sharing. I like the functions you have here:

github.com/helderburato/dotfiles/b...

Especially the find function, looks much simpler πŸ‘Œ

Collapse
 
helderberto profile image
Helder Burato Berto

You're welcome :D

Collapse
 
dephraiim profile image
Ephraim Atta-Duncan

Someone should make them into an npm package

Collapse
 
benwinding profile image
Ben Winding

See the beauty of making your own scripts (dotfiles) is that you maintain them yourself! Don't have to worry about breaking changes, fixing other peoples issues, it's great!

Collapse
 
dephraiim profile image
Ephraim Atta-Duncan

Yh, It is

Collapse
 
harrisgeo88 profile image
Harris Geo πŸ‘¨πŸ»β€πŸ’»

that list is πŸ”₯

Collapse
 
adriangrigore profile image
Adrian Emil Grigore • Edited

If you're into this stuff checkout my projects mkws.sh and adi.tilde.institute. Some are WIP. Feedback is welcomed!