PS1 with nice PWD

This is my favorite customized bash prompt :

[12:09:44]/Library/.../Documents/dashboard >

Which shows:

  • current time ( which is useful when you want to calculate the execution time)
  • Current working directory, but if it’s more than 30 characters, it would shorten it

This is how my ~/.bash_profile looks for this customization:


export MYPS='$(echo -n "${PWD/#$HOME/~}" | awk -F "/" '"'"'{if (length($0) > 30){ if (NF>4) print $1 "/" $2 "/.../" $(NF-1) "/" $NF; else if (NF>3) print $1 "/" $2 "/.../" $NF; else print $1 "/.../" $NF; } else print $0;}'"'"')'

export PS1="[\$(date +%k:%M:%S)]$(eval 'echo ${MYPS}') > "

And this is actually how it looks:

[12:09:42]/Library/WebServer/Documents >
[12:09:43]/Library/WebServer/Documents > cd dashboard/
[12:09:44]/Library/.../Documents/dashboard >

So if PWD is less than 30 characters it would show it completely, otherwise it would shorten the path which is nice to see where you currently are.

Update1:

If you are in to colourful prompt and don’t mind multi-line, this is nice too:

export PS1="\[\033[0m\]\[\033[32m\][\$(date +%k:%M:%S)] \
\[\033[33m\]\w \[\033[0m\] \n$"

Which outputs something like this (but colourful):

[16:16:40] /Library/WebServer/Documents
$

References: This was a very useful thread on SO.

Update2:

If you want to have your git branch shown in your prompt with a different color, you could change your PS1 to the following:

export PS1="\[\033[0m\]\[\033[32m\][\$(date +%k:%M:%S)] \
\[\033[33m\]\w \[\033[0m\] \
\[\033[1;35m\]\$(__git_ps1 " (%s)")\[\033[0m\] \n$"

and the output is like the following:

[10:47:51] /Library/WebServer/Documents   (master)
$

Update3:

To make it clean and clear I found a very nice blog post to define the colors in variables and just use the color names instead of codes.

So now my `.bash_profile` looks like this:


# Reset
Color_Off="\[\033[0m\]"       # Text Reset

# Regular Colors
Black="\[\033[0;30m\]"        # Black
Red="\[\033[0;31m\]"          # Red
Green="\[\033[0;32m\]"        # Green
Yellow="\[\033[0;33m\]"       # Yellow
Blue="\[\033[0;34m\]"         # Blue
Purple="\[\033[0;35m\]"       # Purple
Cyan="\[\033[0;36m\]"         # Cyan
White="\[\033[0;37m\]"        # White

# Bold
BBlack="\[\033[1;30m\]"       # Black
BRed="\[\033[1;31m\]"         # Red
BGreen="\[\033[1;32m\]"       # Green
BYellow="\[\033[1;33m\]"      # Yellow
BBlue="\[\033[1;34m\]"        # Blue
BPurple="\[\033[1;35m\]"      # Purple
BCyan="\[\033[1;36m\]"        # Cyan
BWhite="\[\033[1;37m\]"       # White

export PS1="$BGreen[\$(date +%k:%M:%S)] $Yellow\w $Purple\$(__git_ps1) $Color_Off\n$"

git Push & Automatic deployment

Sometimes it’s more convenient to just deploy the code to your staging server automatically when it’s being pushed to the remote repository. Although this is not recommended for Production at all, but it works well for the Staging server.

To set this up you just need to setup a hook on the remote repository for “post-receive”.


$ ssh to_remote_server
$ cd /path/to/git/repository
$ git config core.worktree /path/to/git/repository
$ git config receive.denycurrentbranch ignore
$ cat > .git/hooks/post-receive
#!/bin/sh
git checkout -f
^D

And finally make sure that the “post-receive” hook, has proper permissions to be executed.

chmod +x hooks/post-receive

Note: Make sure that the user is already part of the group who owns the directory which repository is located under.

Otherwise add it manually to the group.
In my case which the remote computer is a Mac, I have to execute the following command to add my username to the “wheel” group which owns the directory:

sudo dseditgroup -o edit -a my_username -t user wheel

References: This blog post helped me a lot.

In process of moving my blog

I’m in the process of moving my blog, I used to host wordpress on my server. I’m studying some other solutions and so far Jekyll has caught my eyes, and hosting on github pages, but it involves a bit of manual work to design the template and migrate my previous posts and comments. Till I completely migrate my new blog, I will be hosting the imported version of it here on wordpress.com.

A lot of code snippets are broken and also images may not be loading, so please bear with me till we get up and running again.

Thanks,