A URL Shortener at the BASH/ZSH Prompt

URL shortening is nothing new, it was originally made popular due to the 140 character limit of Twitter and the 160 character limit of SMS messages. 

https://trimlink.site

https://trimlink.site has been a project of mine for sometime, it is a simple URL Shortener designed to be integrated into larger projects or just to be used to shorten long URL’s. I posted the project on reddit and reddit user spydas created a ZSH plugin for this.

I have modified this slightly to allow it to work on systems without xclip installed as not everyone runs X, I’m actually running OS X so xclip is out of the question.

To use TrimLink at the BASH prompt simply add the following to your .bashrc or .zshrc file in your home directory.

shorten() {
        if echo $1 | grep http > /dev/null
        then
        if  xclip &> /dev/null
        then
                curl -Gs "https://trimlink.site/shorten.php" -d "url=$1" | sed 's/<[^>]*>/\n/g' | sed 's/\s*//g' | xclip -selection clipboard
                echo 'Shortened URL copied to clipboard'
        else
                echo "  Install xclip to add clipboard support"
                curl -Gs "https://trimlink.site/shorten.php" -d "url=$1" | sed 's/<[^>]*>/\n/g' | sed 's/\s*//g' | sed 's/n//'
        fi
        else
            echo "Usage: shorten <url>"
                exit
        fi
}

Shortening a URL is now simple, just type shorten https://example.com

➜  ~ shorten https://example.com
	Install xclip to add clipboard support
http://t5.i/ccd99n

A warning will be shown if xclip is not installed, if you do not want to see this use the version below:

shorten() {
        if echo $1 | grep http > /dev/null
        then
        if  xclip &> /dev/null
        then
                curl -Gs "https://trimlink.site/shorten.php" -d "url=$1" | sed 's/<[^>]*>/\n/g' | sed 's/\s*//g' | xclip -selection clipboard
                echo 'Shortened URL copied to clipboard'
        else
                curl -Gs "https://trimlink.site/shorten.php" -d "url=$1" | sed 's/<[^>]*>/\n/g' | sed 's/\s*//g' | sed 's/n//'
        fi
        else
            echo "Usage: shorten <url>"
                exit
        fi
}

I hope this is found useful, if you have any issues or questions please leave a comment below.

Check out what else James is doing over on his blog at https://blog.jmdawson.co.uk/ and on his YouTube channel https://www.youtube.com/user/theimacmodders1.

This video, re-posted here with permission, was originally published on James’ blog here.



Be the first to comment

Leave a Reply

Your email address will not be published.


*