Ben Lobaugh Online

I think, therefore I am. I am, therefore I sail

Category: OS X (Page 1 of 5)

Quickly list all hosts in your ssh config

I try to be a good citizen and create unique ssh keys for each service I ssh into. I setup all sorts of fun config items and one of my favorites is host aliases. I have accumulated dozens of hosts in my config file and remembering the names of each can be problematic. I tend to open the config file a couple times a day to find a host. This morning I whipped up a quick command that will dump a list of all my hosts into the terminal with one command. No more opening the config file with an editor and scrolling through it. Hopefully this will be something you can enjoy also!

The command is:

grep -w -i "Host" ~/.ssh/config | sed 's/Host//'

It uses grep to search through the ssh config file and sed to clean up the output.

But wait! That is a lot to type and this was supposed to make life easier. Add this as a shell alias and simply type sshhosts to get the list!

alias sshhosts="grep -w -i "Host" ~/.ssh/config | sed 's/Host//'"

Mute and unmute your mic quickly with this Alfred Workflow

I am on a lot of calls for work every day and juggle several different call applications. Finding the apps mute/unmute button quickly gets tedious. On some apps the UI is so poorly designed as to make toggling the mic nearly unusable.

Enter Alfred! Alfred is an amazing productivity app. Alfred let you run custom commands and hotkeys in custom Workflows.

I created a Workflow that, once installed, allows you to mute and unmute your mic with the command-M hotkey.

To install simply download the Workflow and double click it to load it into Alfred.

Click here to download the Mic PTT Alfred Workflow

The Workflow is on Github! If you have ideas for improvements create a fork and send them to me: https://github.com/blobaugh/Mic-PTT-Alfred-Workflow

Bash script to automatically convert git submodules to regular files

Git submodules drive me batty! They are a great idea in theory however in practical application they are a pain in the butt to work with.

I have a project that has accumulated over a dozen submodules over the past couple years. Switching branches and merging anything has become excruciating. This morning was the last straw. I WANT THEM GONE! Removing dozens of submodules by hand is time consuming so I tossed together this quick Bash script. I hope it is helpful to anyone else out there struggling with git submodules.

#!/bin/bash

# Get a list of all the submodules
submodules=($(git config --file .gitmodules --get-regexp path | awk '{ print $2 }'))

# Loop over submodules and convert to regular files
for submodule in "${submodules[@]}"
do  
   echo "Removing $submodule"
   git rm --cached $submodule # Delete references to submodule HEAD
   rm -rf $submodule/.git* # Remove submodule .git references to prevent confusion from main repo
   git add $submodule # Add the left over files from the submodule to the main repo
   git commit -m "Converting submodule $submodule to regular files" # Commit the new regular files!
done

# Finally remove the submodule mapping
git rm .gitmodules

Find largest files in a directory using the terminal

If you need to find out what the largest files are in a directory and subdirectories here is a command you can run that will find the top 5 largest files.

find . -type f | sed 's/.*/"&"/' | xargs ls -Slh | head -n 5

Breakdown:

  1. find . -type f: Find all files in this directory and below
  2. sed ‘s/.*/”&”/’: Put double quotes around the files. xargs can handle this itself, however not all implementations do, this method is safer.
  3. xargs ls -Slh: Using the filenames from 1 list them all sorted by size.
  4. head -n 5: Show the output of the first 5 rows. Which becomes the 5 largest files because ls sorted the output.

Autocomplete git branches and commands in bash

The tab completion for commands in bash is awesome, but it does not work for bash out of the box. Lucky for us it is relatively easy to get bash completion working.

You will need two things, a copy of the completion script and an update to your bash profile.

The completion script is available at: https://github.com/git/git/blob/master/contrib/completion/git-completion.bash

Then in your .bashrc (or .bash_profile, or .profile, whatever you are setup for) add the following:

if [ -f ~/.bin/git-completion.bash ]; then
  . ~/.bin/git-completion.bash
fi

Be sure the path to the git-completion.bash file is correct for your system. On mine I keep extra commands in ~/.bin.

Start using your new completion with:

source ~/.bashrc

Now simply type ‘git’ and hit tab to see all the commands available.

When switching branches type ‘git checkout’ and hit tab to see a list of the branches.

Page 1 of 5

Powered by WordPress & Drip City Coffee