Skip to content

Commit

Permalink
Installation now supports .deb method, script is no longer sourced bu…
Browse files Browse the repository at this point in the history
…t rather invoked using child shell. Some minor bug fixes and code cleanup. README file updated, now with a demo gif.
  • Loading branch information
bhayatus committed Mar 23, 2021
1 parent 832f9fc commit 7dabfad
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 83 deletions.
81 changes: 34 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,69 @@
A Shell CLI tool to conveniently manage aliases for bash and zsh.

## Installation
Clone the repository, then copy the script to a folder of your choice:

If you are using a Debian-based Linux distribution such as Ubuntu, download the latest `.deb` file from the `Releases` section, and run the following:

$ sudo dpkg -i aliaser_*.deb

Alternatively, you can do the following:

$ git clone https://github.com/bhayatus/aliaser.git
$ cd aliaser
$ cp aliaser /usr/local/scripts
$ sudo cp aliaser/aliaser /usr/local/bin

Create a file that will contain your aliases (if you aren't using one already):
Next, create a file that will contain your aliases (if you aren't using one already):

$ touch ~/.aliases

Add the following to a startup script such as .bashrc or .zshrc (modifying where necessary):
Add the following to a startup script such as `.bashrc` (or `.zshrc` if you prefer):

alias aliaser="source /usr/local/scripts/aliaser"
export ALIASES_FILE=~/.aliases
source $ALIASES_FILE

# Necessary if you don't want to restart your current session for changes to take effect.
aliaser () {
command aliaser "$@"
if [ $? -eq 0 ]; then
if [ $1 = "add" ]; then
source $ALIASES_FILE
elif [ $1 = "rm" ]; then
unalias $2 &> /dev/null
fi
fi
}

Ensure that you source your newly modified startup script like so:

$ source ~/.bashrc

## Usage
Running `aliaser help` displays the following:
CLI tool to manage aliases for bash and zsh

Tool for managing bash/zsh aliases

usage: aliaser <operation>

operations:
ls
Lists all aliases saved in the aliases file, in alphabetical order
Lists all aliases saved in the aliases file, in alphabetical order

add <alias> <command>
Creates/replaces an alias for the specified command in the aliases file
The alias name must not contain any spaces
If command contains spaces, it must be wrapped in quotes
Creates/replaces an alias for the specified command in the aliases file
The alias name must not contain any spaces, and can only consist of alphanumeric characters

rm <alias>
Removes the alias permanently from the aliases file
The alias name must not contain any spaces
Removes the alias permanently from the aliases file
The alias name must not contain any spaces

help
Prints the help text
Prints the help display

version
Prints the current version

Visit https://github.com/bhayatus/aliaser for details on setup

Note that this tool only manages aliases stored within the aliases file, any declared outside will not be affected.

## Examples

$ helloworld
helloworld: command not found
$ aliaser ls
No aliases found in /home/bhayatus/.aliases
$ aliaser add helloworld "echo \"hello world\""
Added alias 'helloworld'
$ helloworld
hello world
$ aliaser ls
alias helloworld='echo "hello world"'
$ aliaser rm helloworld
Removed alias 'helloworld'
$ helloworld
helloworld: command not found

$ ll
ll: command not found
$ aliaser add ll "ls"
Added alias 'll'
$ ll
projects test.txt
$ aliaser add ll "ls -la"
Alias 'll' already exists, replace? (y to continue) y
Added alias 'll'
$ ll
total 16
drwxr-xr-x 3 bhayatus bhayatus 4096 Dec 14 18:18 .
drwxr-xr-x 9 bhayatus bhayatus 4096 Dec 14 23:50 ..
drwxr-xr-x 3 bhayatus bhayatus 4096 Dec 14 11:43 projects
-rw-r--r-- 1 bhayatus bhayatus 92 Dec 14 18:20 test.txt
## Demo
![Demo](demo/demo.gif)
82 changes: 46 additions & 36 deletions aliaser
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
#!/bin/sh
#!/bin/bash

function print_add_usage() {
version="1.3.0"

function print_version {
echo $version
}

function print_add_usage {
echo "usage: aliaser add <alias> <command>"
echo " The alias name must not contain any spaces"
echo " If command contains spaces, it must be wrapped in quotes"
}

function print_remove_usage() {
function print_remove_usage {
echo "usage: aliaser rm <alias>"
echo " The alias name must not contain any spaces"
}

function print_help() {
echo "CLI tool to manage aliases for bash and zsh"
function print_help {
echo "Tool for managing bash/zsh aliases"
echo
echo "usage: aliaser <operation>"
echo
Expand All @@ -23,7 +28,6 @@ function print_help() {
echo " add <alias> <command>"
echo " Creates/replaces an alias for the specified command in the aliases file"
echo " The alias name must not contain any spaces, and can only consist of alphanumeric characters"
echo " If command contains spaces, it must be wrapped in quotes"
echo
echo " rm <alias>"
echo " Removes the alias permanently from the aliases file"
Expand All @@ -32,30 +36,32 @@ function print_help() {
echo " help"
echo " Prints the help display"
echo
echo " version"
echo " Prints the current version"
echo
echo "Visit https://github.com/bhayatus/aliaser for details on setup"
}

function list_aliases() {
count=`cat $alias_file | wc -l`
if [[ count -eq 0 ]]; then
echo "No aliases found in $alias_file"
else
function list_aliases {
count=`grep -o "^alias" $alias_file | wc -l`
if [[ $count -gt 0 ]]; then
sort $alias_file
else
echo "No aliases found in $alias_file"
fi
}

function add_alias() {

if [[ "$alias" =~ [^0-9A-Za-z]+ ]]; then
function add_alias {
if [[ $alias =~ [^0-9A-Za-z]+ ]]; then
echo "Alias name can only contain alphanumeric characters"
return
exit 1
fi
count=`grep -o "^alias $alias=" $alias_file | wc -l`
if [[ count -eq 1 ]]; then
if [[ $count -eq 1 ]]; then
echo "Alias '$alias' already exists, replace? (y to continue)"
read response
if [[ "$response" -ne "y" ]]; then
return
if [[ $response != "y" ]]; then
exit
else
sed -i.bak "s/^alias $alias=.*$/alias $alias='$command'/g" $alias_file
rm "$alias_file.bak"
Expand All @@ -64,33 +70,31 @@ function add_alias() {
echo "alias $alias='$command'" >> $alias_file
fi

source $alias_file
echo "Added alias '$alias' for command '$command'"
}

function remove_alias() {
function remove_alias {
count=`grep -o "^alias $alias=" $alias_file | wc -l`
if [[ count -eq 0 ]]; then
if [[ $count -eq 0 ]]; then
echo "No alias named '$alias' found in $alias_file"
return
exit 1
fi

sed -i.bak "/^alias $alias=/d" $alias_file
rm "$alias_file.bak"

unalias $alias
echo "Removed alias '$alias'"
}

function main() {
if [ "$#" -eq 0 ]; then
function main {
if [ $# -eq 0 ]; then
print_help
return
exit 1
fi

if [[ -z "${ALIASES_FILE}" ]]; then
if [[ -z ${ALIASES_FILE} ]]; then
echo "ALIASES_FILE environment variable must be set"
return
exit 1
else
alias_file=$(realpath ${ALIASES_FILE})
fi
Expand All @@ -100,7 +104,7 @@ function main() {
command=""

arg_pos=1
for i in "$@"; do
for i in $@; do
if [[ $arg_pos -eq 1 ]]; then
operation=$i
fi
Expand All @@ -110,27 +114,33 @@ function main() {
if [[ $arg_pos -eq 3 ]]; then
command=$i
fi
if [[ $arg_pos -gt 3 ]]; then
command="${command} ${i}"
fi
((arg_pos=arg_pos+1))
done

case "$operation" in
case $operation in
"ls")
list_aliases
;;
"add")
if [[ "$#" -ne 3 || $alias =~ " " ]]; then
if [[ $# -lt 3 || $alias =~ " " ]]; then
print_add_usage
return
exit 1
fi
add_alias $alias $command
;;
"rm")
if [[ "$#" -ne 2 || $alias =~ " " ]]; then
if [[ $# -ne 2 || $alias =~ " " ]]; then
print_remove_usage
return
exit 1
fi
remove_alias $alias
;;
"version")
print_version
;;
"help")
print_help
;;
Expand All @@ -140,4 +150,4 @@ function main() {
esac
}

main "$@"
main $@
Binary file added demo/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7dabfad

Please sign in to comment.