Bash

Update: You can now find all of my .dotfiles on GitHub.


 

Find below my “.bash_profile" file in OS X Lion and the referenced “.bash_ps1" (slightly modified from lifehacker).

Detailed walkthrough

  • add some additional directories to PATH
  • reference .bash_ps1 (see the file below)
  • set grep and ls to display some fancy colors
  • two aliases to often used commands
    • order top by CPU usage
    • display more human readable directory listing output
  • Set Textmate’s “mate” command as the default editor
  • Ignore duplicates in history. E. g. if I issue “ls” multiple times, it only occurs once when I press the cursor up key, to cycle through my last commands.
  • Export DISPLAY environment variable if it’s not already set. This helps with X-forwarding from remote systems.
  • SSH host completion lets me type the first few characters of any host I already connected to successfully and completes it just like normal shell commands or files after pressing TAB.
  • The “myip” function lets me look up my external IP.
  • “git_stats” spits out detailed infos about any git repository you issue this command in.

.bash_profile

export PATH=$PATH:~/bin:/usr/local/bin:/usr/local/sbin:/usr/local/git/bin

# set fancy prompt
if [ -f "$HOME/.bash_ps1" ]; then
    . "$HOME/.bash_ps1"
fi

# some settings to be more colorful
export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32'
export CLICOLOR=true
export LSCOLORS=ExGxFxdxCxDxDxBxBxExEx

# handy aliases
alias top='top -o cpu'
alias ll='ls -lh'

# use Textmate as default editor
export EDITOR="mate"

# no duplicates in bash history
export HISTCONTROL=ignoredups

# export DISPLAY if it's not set yet
[[ -z $DISPLAY ]] && export DISPLAY=":0.0"

# ssh host tab completion
complete -W "$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | sed -e s/,.*//g | uniq | grep -v "\["`;)" ssh

################################################
# bash functions
################################################
function myip {
  res=$(curl -s checkip.dyndns.org | grep -Eo '[0-9\.]+')
  echo "$res"
}

function git_stats {
# awesome work from https://github.com/esc/git-stats
# including some modifications
if [ -n "$(git symbolic-ref HEAD 2> /dev/null)" ]; then
    echo "Number of commits per author:"
    git --no-pager shortlog -sn --all
    AUTHORS=$( git shortlog -sn --all | cut -f2 | cut -f1 -d' ')
    LOGOPTS=""
    if [ "$1" == '-w' ]; then
        LOGOPTS="$LOGOPTS -w"
        shift
    fi
    if [ "$1" == '-M' ]; then
        LOGOPTS="$LOGOPTS -M"
        shift
    fi
    if [ "$1" == '-C' ]; then
        LOGOPTS="$LOGOPTS -C --find-copies-harder"
        shift
    fi
    for a in $AUTHORS
    do
        echo '-------------------'
        echo "Statistics for: $a"
        echo -n "Number of files changed: "
        git log $LOGOPTS --all --numstat --format="%n" --author=$a | cut -f3 | sort -iu | wc -l
        echo -n "Number of lines added: "
        git log $LOGOPTS --all --numstat --format="%n" --author=$a | cut -f1 | awk '{s+=$1} END {print s}'
        echo -n "Number of lines deleted: "
        git log $LOGOPTS --all --numstat --format="%n" --author=$a | cut -f2 | awk '{s+=$1} END {print s}'
        echo -n "Number of merges: "
        git log $LOGOPTS --all --merges --author=$a | grep -c '^commit'
    done
else
    echo "you're currently not in a git repository"
fi
}

.bash_ps

############################################
# Modified from emilis bash prompt script
# from https://github.com/emilis/emilis-config/blob/master/.bash_ps1
###########################################
fill="--- "
reset_style='\[\033[00m\]'
status_style=$reset_style'\[\033[0;90m\]' # gray color; use 0;37m for lighter color
prompt_style=$reset_style
command_style=$reset_style'\[\033[1;29m\]' # bold black
PS1="$status_style"'$fill \t\n'"$prompt_style"'${debian_chroot:+($debian_chroot)}\[\033[0;32m\]::>'"$command_style "
# this is invoked every time before a command is executed
trap 'echo -ne "\033[00m"' DEBUG

function prompt_command {
    let fillsize=${COLUMNS}-9
    fill=""
    while [ "$fillsize" -gt "0" ]
        do
            fill="-${fill}" # fill with underscores to work on
            let fillsize=${fillsize}-1
        done
}
export PROMPT_COMMAND=prompt_command

Click here to see the ifttt tasks

Click here to see the Mail Rule

This is kind of a stripped down version of Read It Later or Instapaper, but functions perfectly for my workflow.

Requirements

  • Apple Mail
  • Apple Safari (with Reading List Support)
  • ifttt Account

First I’ve set up actions in ifttt that forward Links I favorite in Twitter or Google Reader, to my personal Email and prepend the subject with “#ReadLater”. Next I constructed a Mail Rule that acts on all messages who’s subjects begin with said keyword. The Mail Rule will invoke the following Applescript, mark the message as read and move it to the trash.

using terms from application "Mail"
	on perform mail action with messages theSelectedMessages
		tell application "Mail"
			repeat with eachMessage in theSelectedMessages
				set mailbody to content of eachMessage
				set theUrl to paragraph 1 of mailbody
				tell application "Safari"
					add reading list item theUrl
				end tell
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Use cases

  • Read newsfeeds on iPhone. Save longer or interesting articles for later by clicking the favorite star.
  • Interesting item pops up on twitter, but you don’t have time now to read the containing link right now.
  •  While walking in the street you see a URL you want to remember, just mail it to yourself with the appropriate keyword in the subject.

With the introduction of Apple’s newest operating system OS X 10.7 Lion, the company has taken one step further to disconnect their users from other competing user interaction schemes.

Let me elaborate on this rather strong statement by going back in history a bit.
I can still remember, when I got my first Mac, it was a 12″ iBook G4 with OS X Panther. Being a rather curious person, I started tweaking the user interface and tested out all available options, until I was satisfied with the speed and metaphors of my personal set up. On thing that I started using very frequently almost immediately was Exposé (now extinct due to Lion’s Mission Control). I had several hot corners set up, so windows would fly out of the way and reveal the Desktop whenever I moved my mouse in the lower left corner of the screen or reveal all opened windows when I moved to the lower right corner. This method of interacting with my programs quickly became very natural to me. Going so far, that I started moving my mouse into corners on Windows systems several times in vain, until I realized that I had to use the taskbar for those similar tasks. This wasn’t more than a little annoyance to me. I just found out that OS X permitted me to work faster than what I’d be able to achieve in the rare cases, where I’d need to use another system.

But the problem also ran in the other direction. People not used to Mac systems and their concept of hot corners seemed to have a much more clumsy way of moving their mouse around on the screen. Thus they inadvertently triggered the “Show Desktop” or “Show all windows” functions all of the time and were completely irritated by windows flying around all of the sudden. I merely laughed about that fact back then, but from my point of view today, that’s where the gap began to widen. I was used to a UI paradigm, that wasn’t logical at all for users not familiar with OS X.

Skip a few years forward and now we’ve got OS X 10.7 Lion with many more ways of interaction, that are nowhere near to be seen on any other platform. Take gestures for instance: they save a lot of time and clicks when used properly, but setting ones muscle memory further apart from the rest of computer users, who don’t have or use gestures. Now this isn’t a very big problem, because gestures need special hardware to be applicable and add new ways of interaction that don’t overrule the way of using a computer you got used to before. A much bigger problem could be the way Apple changed the scrolling direction in Lion. They call it “natural scrolling” and if you think about it, it makes a lot of sense and there isn’t much of a learning curve, once you’ve wrapped your head around it. But think of the ramifications that arise when others happen to sit in front of your computer or if you want to use another persons system.

If I think about a typical day at work, there are several times when a colleague  might sit down at my Mac to look something up, load some spreadsheet or notes about what we are just discussing, or similar examples. In the past this was no big deal, they might only be annoyed by triggering hot-corners or when they searched for the “close window” button on the wrong side of a window’s title bar. But now there is also the reversal of scrolling, which not only is new and “weird” to them, but is completely contradictory to how the same metaphor worked for them over the last decades.

Don’t be mistaken, I’m not against the new “natural scrolling”. I’m getting used to it rather quickly and enjoy how I can now interact with content more logically. But on the other hand it will make it more difficult for me, whenever I have to use a Windows or Linux Desktop and now even when I use an older OS X system. At the same time other users will find it more irritating and difficult to use my machine. It will be interesting where this will take us in the future. Will the gap grow even more? Or will operating system manufacturers decide on a common ground for user interactions?
I don’t mind adapting to the new principles Apple comes up with, as long as they make sense for me and help me interact with my computer in a more efficient way. But this also means I’m separating myself further from the herd (aka millions of Windows users), which might not be entirely bad in itself 😉

Growl iconIt’s time again to circumvent Apple’s Mail Plugin policy and update the GrowlMail Bundle with the correct identifier string. Otherwise, after updating, Mail will tell you it has disabled the GrowlMail plugin and will move the bundle to ~/Library/Mail/Bundles (disabled).

To amend this, follow these steps:

  1. Quit Mail
  2. Move the GrowlMail.mailbundle Folder back into ~/Library/Mail/Bundles
  3. Enter the following two commands into your Terminal:
    defaults write ~/Library/Mail/Bundles/GrowlMail.mailbundle/Contents/Info SupportedPluginCompatibilityUUIDs -array-add "1C58722D-AFBD-464E-81BB-0E05C108BE06"
    defaults write ~/Library/Mail/Bundles/GrowlMail.mailbundle/Contents/Info SupportedPluginCompatibilityUUIDs -array-add "9049EF7D-5873-4F54-A447-51D722009310"
  4. Start Mail and enjoy Growl notifications for new eMail again!

For quite some time Mac users where searching for a way to paste the currently playing song from iTunes in chat messages etc. I never thought about it much but recently a friend of mine, who was a former AMIP user on Windows, needed a similar functionality under OS X. Together we constructed a working solution.

So here it is the AMIP alternative for Mac:

    • Open Automator and start a new “Service”
    • Choose “no input” for “Service receives”
    • Drag the “Run AppleScript” Action into the workflow
    • Paste in the following and edit the output in (green) to your hearts desire
on run
 try
 tell application "iTunes"
 set songTitle to the name of the current track
 set songArtist to the artist of the current track
 set songAlbum to the album of the current track
 set songYear to the year of the current track
 set the clipboard to "np:" & the songArtist & " - " & songTitle & " \"" & songAlbum & ", " & songYear & "\""
 end tell
 end try
 try
 set the clipboard to Unicode text of (the clipboard as record)
 on error errMsg
 display dialog errMsg
 end try
 tell application "System Events"
 key code 9 using {command down}
 end tell
end run
  • Save the service. I named mine “music” (creative isn’t it? 🙂 )
  • Now your able to run the service from every application’s menu under “Services”. Be sure to have your cursor in a text field because the service will immediately paste in the constructed string.
  • If you’re in a super nerdy mood today, you can also give your service a keyboard shortcut.
    Go to “System Preferences”, open “Keyboard”, go to “Keyboard Shortcuts” and set your desired Hotkey. (mine’s CMD + SHIFT + M)

I’d be happy to see further improvements, thoughts & ideas about this solutions in the comments!

 

And to all my Austrian friends: Frohen Faschingsdienstag (not that I’d care!)