382

A URL Shortener at the BASH/ZSH Prompt.

 5 years ago
source link: https://www.tuicool.com/articles/hit/NNvUfm6
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK