Show Git branch & untracked/modified files in terminal

Show Git branch & untracked/modified files in terminal

Bash Git Resources

Many people know that you can add the Git branch name you are currently working in to the end of your command prompt but did you know that you can also have the command prompt notify you if you have any untracked of modified files?

Screen clip of the final result

Clip of the final result showing the Git branch name and one or more untracked files represented by the “U” after the branch name

I have created this post with linux in mind but I’m sure you can Google what to do if you have a different OS

First place the following in ~/.bashrc so that .bashrc can read my script.

# Include my very own git parse type function 🙂
source ~/

Then I update the PS1 variable (the command prompt text) to output the result of the script

export PS1=”\n${debian_chroot:+($debian_chroot)}${RED}\u@\H ${YELLOW}\A ${BLUE}\w \$(gitParseStatus)${NORMAL}\n$ ”

It’s the \$(gitParseStatus) bit that works the magic we want.

and then of course I have the script which I save as ~/ (don’t forget to make the script executable with

chmod +x ~/

#function get_out() {
# log $1
# return
#function log() {
# echo '* '$1
#if [ $1 = "--debug" ]; then
# DEBUG=true
# echo
# echo "==========="
# echo "Debug is on"
# echo "==========="
# echo
# Set Variables

# What do you want to show when everything is up to date
# Your options are…

# Now set the all ok message

# What do you want to display when there git needs updating in some way?
# I have used “M” for files have been modified

# What do you want to display when there are untracked files in the repo?
# I have used “U” but you can use whatever you want

# How do you want to display the repo branch
# I have used square brackets

# What colour do you want the git section of you command line to be when
# everything is ok?
# I have it as green
# Set some colours #
# Some examples are…


function gitParseStatus {

# If we fail to get a branch name then simply return empty handed 🙁
#IN=$(git symbolic-ref HEAD 2> /dev/null) || echo “You are not in a Git repo”
IN=$(git symbolic-ref HEAD 2> /dev/null) || return
#echo $IN

BRANCH_NAME=`echo $IN | sed ‘s/refs\/heads\///’`
#log ‘Branch Name=”‘$BRANCH_NAME'”‘

# get a list of files to run through
FILES=$(git status -z 2> /dev/null)
#log ‘Git Status=”‘”$FILES”‘”‘

if [[ -z “$FILES” ]]; then

#log “All the files in your Git repo are up to date”




#log “You have discrepancies in your Git Repo”

#log ‘Clearing the $UPDATES variable’

#echo ‘About to start testing on “‘${FILES}'”‘
#log ‘Testing for untracked files (ones beginning with ??)’
UNTRACKED=$(echo ${FILES} | grep $’\x000?{2}’)

if [ -n “$UNTRACKED” ] ; then

#log “Untracked files were found”


UNTRACKED=$(echo ${FILES} | grep ^?{2})

if [ -n “$UNTRACKED” ] ; then

#log “Untracked files were found”


#log “No Untracked files were found”

#log “Testing for modified files”

modified=$(echo ${files} | grep $’\x000[rmdc ]{2}’)
#echo ‘modified=”‘$modified'”‘
if [ -n “$modified” ] ; then]

#log “modified files were found”


modified=$(echo ${files} | grep ‘^[rmdc ]{2}’)
#echo ‘modified=”‘$modified'”‘
if [ -n “$modified” ] ; then

#log “modified files were found”


#log “No Modified files were found”


echo $OUTPUT

I hope it is of use to some of you and if you have any problems you know where I am.


Vimtip #1: start gvim from terminal and still use terminal

Resources Vim-tips

This tip will allow you to start gvim from the terminal and still use it after Gvim has opened.

Step 1:
Add the following to your ~/.bashrc or mac equivalent

function gvim {
/usr/bin/gvim -f $* > /dev/null 2> /dev/null & disown

Step 2:
Type gvim in your terminal and it will not hog it until you close gvim i.e

john@John-Tosh-Lin 10:31 ~
$ gvim
[1] 10381

john@John-Tosh-Lin 10:31 ~

As a bonus, if you start gvim in the directory that you are working in then NERDTree’s root will be the directory you are in. That in itself saves a lot of time 🙂
Enjoy your newly reclaimed terminal people 🙂

Find string in Files

Web Development

Well tonight I was supposed to be pulling the new project to bits that I’m working on but I ran into a problem – let me explain.

Every time I have wanted to find some text in a directory I’ve had to either search the internet or my bash history for the right command. With this in mind I thought I may as well take a little time to create a function that would just search for the string that I want. After all while I’m learning the layout etc of this project I’m going to need it. Not knowing where all the files are as yet and what they all do it’s going to be a lot easier to run the function and see what files the string turns up in.

The function to put in your ~/.bashrc or similar file

function find-string() {

# re-iterate what we're looking for
echo 'looking for "'$1'"'

# perform a search on all files
find ./ -xdev -type f -print0 | \

# process each file through the grep,
# --colour=always = always show the coloured version
# --ignore-case = search is not case-sesitive
# --line-number = show the line numbers after the filename
# --context=1 = this will print 1 line both above and below the line where the string occurs
# "${1}" = finally search for the passed variable
xargs -0 grep --colour=always --ignore-case --line-number --context=1 "${1}" | \

# replace each tab with 2 spaces to cut down on the amount of indentation in
# the search results from files with large areas of indentation
sed -re 's/\t/ /g'


And to get it up and running just run

source ~/.bashrc
to enable your system to see the function and then when you want to use it you can simply enter something like:
find-string viewer-thumbnails-wrapper
find-string being the function and in this case we are searching for file containing viewer-thumbnails-wrapper which will bring results like these:
Looking for "viewer-thumbnails-wrapper"
./css/viewer.css-418-/* Thumbnails */
./css/viewer.css-420- background: #FFF;
./js/mediagallery.js-61- min_thumbs: 5,
./js/mediagallery.js:62: thumbwrapper: 'div.viewer-thumbnails-wrapper',
./js/mediagallery.js-63- btn_thumbs_show: 'a.viewer-thumbs',

Ok, that’s it, if you can make use of the this function then go ahead and copy and paste.”

if there’s any help I can offer then let me know in the comments section below.