#!/bin/bash
# EE : Easy Editor.
# A bash script to create, open, edit, and save plain text files.
# Run chmod +x ee.sh to make it executable.
# Run sudo mv ee.sh /usr/local/bin/ee so it can be run from anywhere.
# Run ee to launch EE : Easy Editor.
# Configuration
TEXT_FILES_DIR="."
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Display usage information
show_help() {
echo "EE : Easy Editor"
echo "Usage: $0 [COMMAND] [FILE_NAME]"
echo ""
echo "Commands:"
echo " create, c <n> Create a new text file"
echo " edit, e <n> Edit an existing text file"
echo " delete, d <n> Delete a text file"
echo " search, s <term> Search for text within files"
echo " help, h Show this help message"
echo " exit, x Exit the editor"
echo ""
echo "If no command is provided, you'll be prompted to choose an action."
echo "Text files are stored in the current directory: $(pwd)"
}
# Built-in editor function
edit_text() {
local file_path="$1"
local temp_file=$(mktemp)
# Copy existing content to temp file if file exists
if [[ -f "$file_path" ]]; then
cp "$file_path" "$temp_file"
fi
echo -e "${BLUE}Built-in Text Editor${NC}"
echo "======================================"
echo "Commands:"
echo " :w - Save and continue editing"
echo " :wq - Save and quit"
echo " :q - Quit without saving"
echo "======================================"
# Show current content
if [[ -s "$temp_file" ]]; then
echo -e "${YELLOW}Current content:${NC}"
cat -n "$temp_file"
echo "======================================"
fi
echo "Enter your text (type :commands on new line to control editor):"
echo ""
# Clear the temp file for new content
> "$temp_file"
while true; do
read -r line
case "$line" in
":w")
cp "$temp_file" "$file_path"
echo -e "${GREEN}Saved!${NC}"
;;
":wq")
cp "$temp_file" "$file_path"
echo -e "${GREEN}Saved and closing editor.${NC}"
break
;;
":q")
echo "Quit without saving? (y/n):"
read -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Exited without saving."
break
fi
;;
*)
echo "$line" >> "$temp_file"
;;
esac
done
rm -f "$temp_file"
}
# Create a new text file
create_text_file() {
local file_name="$1"
local file_path="${file_name}.txt"
if [[ -z "$file_name" ]]; then
echo -e "${RED}Error: Please provide a text file name${NC}"
return 1
fi
if [[ -f "$file_path" ]]; then
echo -e "${YELLOW}Text file '$file_name' already exists.${NC}"
read -p "Do you want to edit it instead? (y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
edit_text_file "$file_name"
fi
return 0
fi
echo -e "${GREEN}Creating new text file: $file_name${NC}"
# Create initial content
echo "# $file_name" > "$file_path"
echo "Created: $(date)" >> "$file_path"
echo "" >> "$file_path"
# Use built-in editor
edit_text "$file_path"
echo -e "${GREEN}Text file '$file_name' created.${NC}"
}
# Edit an existing text file
edit_text_file() {
local file_name="$1"
local file_path="${file_name}.txt"
if [[ -z "$file_name" ]]; then
echo -e "${RED}Error: Please provide a text file name${NC}"
return 1
fi
if [[ ! -f "$file_path" ]]; then
echo -e "${YELLOW}Text file '$file_name' doesn't exist.${NC}"
read -p "Do you want to create it? (y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
create_text_file "$file_name"
fi
return 0
fi
echo -e "${GREEN}Editing text file: $file_name${NC}"
edit_text "$file_path"
echo -e "${GREEN}Finished editing '$file_name'.${NC}"
}
# Delete a text file
delete_text_file() {
local file_name="$1"
local file_path="${file_name}.txt"
if [[ -z "$file_name" ]]; then
echo -e "${RED}Error: Please provide a text file name${NC}"
return 1
fi
if [[ ! -f "$file_path" ]]; then
echo -e "${RED}Error: Text file '$file_name' not found${NC}"
return 1
fi
echo -e "${YELLOW}Are you sure you want to delete '$file_name'?${NC}"
read -p "Type 'y' to confirm: " -r
if [[ $REPLY == "y" ]]; then
rm "$file_path"
echo -e "${GREEN}Text file '$file_name' deleted.${NC}"
else
echo "Deletion cancelled."
fi
}
# Search within text files
search_text_files() {
local search_term="$1"
if [[ -z "$search_term" ]]; then
echo -e "${RED}Error: Please provide a search term${NC}"
return 1
fi
if ! ls *.txt 2>/dev/null | grep -q .; then
echo "No text files found to search."
return 1
fi
echo -e "${BLUE}Searching for '$search_term' in text files:${NC}"
grep -l "$search_term" *.txt 2>/dev/null | while read -r file; do
file_name=$(basename "$file" .txt)
echo -e "${GREEN}Found in: $file_name${NC}"
grep -n --color=always "$search_term" "$file"
echo ""
done
}
# Interactive mode
interactive_mode() {
echo "================="
echo "EE : Easy Editor"
echo "================="
echo ""
echo "What would you like to do?"
echo "c - Create a new text file."
echo "e - Edit a text file."
echo "d - Delete a text file."
echo "s - Search text files."
echo "x - Exit."
read -p "Choose an option: " -n 1 -r
echo
case $REPLY in
c)
read -p "Enter text file name: " file_name
create_text_file "$file_name"
;;
e)
read -p "Enter text file name: " file_name
edit_text_file "$file_name"
;;
d)
read -p "Enter text file name: " file_name
delete_text_file "$file_name"
;;
s)
read -p "Enter search term: " search_term
search_text_files "$search_term"
;;
x)
echo "Goodbye!"
exit 0
;;
*)
echo "Invalid option. Please try again."
;;
esac
}
# Main script logic
main() {
case "$1" in
"create"|"c")
create_text_file "$2"
;;
"edit"|"e")
edit_text_file "$2"
;;
"delete"|"d")
delete_text_file "$2"
;;
"search"|"s")
search_text_files "$2"
;;
"help"|"h"|"-h"|"--help")
show_help
;;
"exit"|"x")
echo "Goodbye!"
exit 0
;;
"")
interactive_mode
;;
*)
echo -e "${RED}Unknown command: $1${NC}"
show_help
exit 1
;;
esac
}
# Run EE : Easy Editor
main "$@"