From 72cf5c2bd23819fa8b18a56ae1be2f5ef6e2a3f1 Mon Sep 17 00:00:00 2001
From: Roland Hopferwieser <develop@int0x80.at>
Date: Tue, 29 Aug 2017 19:33:46 +0200
Subject: [PATCH] Add function for alias deletion

---
 doc/vcsh.1.ronn |  8 +++++---
 vcsh            | 22 ++++++++++++++++++++--
 2 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/doc/vcsh.1.ronn b/doc/vcsh.1.ronn
index cc7edc2..719736b 100644
--- a/doc/vcsh.1.ronn
+++ b/doc/vcsh.1.ronn
@@ -5,7 +5,7 @@ vcsh(1) - Version Control System for $HOME - multiple Git repositories in $HOME
 
 `vcsh` [<options>] <command>
 
-`vcsh` alias [<alias>[=<command>]]
+`vcsh` alias [-d] [<alias>[=<command>]]
 
 `vcsh` clone [-b <branch>] <url> [<repo>]
 
@@ -85,11 +85,13 @@ an interactive user.
 ## COMMANDS
 
 * alias:
-  List all defined aliases.
+  List all aliases in <$XDG_CONFIG_HOME/vcsh/aliases>.
 
   `<alias>`: Print the right side of alias definition.
 
-  `<alias>=<command>`: Add an alias definition to <XDG_CONFIG_HOME/vcsh/aliases>.
+  `<alias>=<command>`: Add alias. Make backup in <$XDG_CONFIG_HOME/vcsh/aliases.bak>.
+
+  `-d <alias>`: Delete alias. Make backup in <$XDG_CONFIG_HOME/vcsh/aliases.bak>.
 
 * clone:
   Clone an existing repository.
diff --git a/vcsh b/vcsh
index 99d201f..1361f4a 100755
--- a/vcsh
+++ b/vcsh
@@ -193,14 +193,32 @@ alias_write() {
 	fi
 }
 
+alias_remove() {
+	[ -w "$XDG_CONFIG_HOME/vcsh/aliases" ] ||
+	[ ! -e "$XDG_CONFIG_HOME/vcsh/aliases" -a -w "$XDG_CONFIG_HOME/vcsh/" ] ||
+		fatal "File '$XDG_CONFIG_HOME/vcsh/aliases' not writeable"
+	[ -n "$1" ] || fatal 'Empty alias'
+	local regex="/^$1\s*=/d"
+	sed -i.bak -re "$regex" "$XDG_CONFIG_HOME/vcsh/aliases"
+}
+
 aliases() {
 	if [ -n "$1" ]; then
-		local alias=$(<<<"$@" sed -re 's/(\w+)\s*=\s*(\w.*)/\1 = \2/')
+		local opts subcmd
+		while getopts d: opts; do
+			if [ $opts == d ]; then
+				alias_remove "$OPTARG"
+			fi
+		done
+		shift $(($OPTIND - 1))
+		local alias=$(<<<"$@" sed -nre 's/(\w+)\s*=\s*(\w.*)/\1 = \2/p')
 		if [ -n "$alias" ]; then
 			alias_write $alias
+		else
+			alias_get "$1"
 		fi
-		alias_get "$1"
 	else
+		# print all aliases
 		local a cmd
 		while read a cmd; do
 			if [ -n "$a" ]; then
-- 
GitLab