Skip to content

Instantly share code, notes, and snippets.

@Pcw-Life
Last active November 24, 2024 06:59
Show Gist options
  • Select an option

  • Save Pcw-Life/3dcb8a7a33fd077e3c29aa92861f3a84 to your computer and use it in GitHub Desktop.

Select an option

Save Pcw-Life/3dcb8a7a33fd077e3c29aa92861f3a84 to your computer and use it in GitHub Desktop.
Custom Commands
# Custom Functions in Shell
# **What**
# Custom functions are reusable scripts written in your shell configuration file (like `.zshrc` or `.bashrc`)
# **Why**
# They allow you to perform complex tasks with a single command. They help automate repetitive tasks, streamline workflows, and enhance productivity.
# **How to Use Them**
# Functions are defined using the `function` keyword or with the `()` syntax, followed by a block of commands.
# 1. Define: Add the function definition in your shell configuration file (`.zshrc` or `.bashrc`).
# 2. Source: Reload your shell configuration file to make the function available using:
# source ~/.zshrc # or ~/.bashrc
# 3. Call: Use the function just like a regular command in the terminal:
# mkcd my_folder # Example function to create and navigate to a directory
# Custom functions save time and reduce errors by encapsulating logic into simple, easy-to-remember commands.
# Custom Functions in Shell
# **What**
# Custom functions are reusable scripts written in your shell configuration file (like `.zshrc` or `.bashrc`)
# **Why**
# They allow you to perform complex tasks with a single command. They help automate repetitive tasks, streamline workflows, and enhance productivity.
# **How to Use Them**
# Functions are defined using the `function` keyword or with the `()` syntax, followed by a block of commands.
# 1. Define: Add the function definition in your shell configuration file (`.zshrc` or `.bashrc`).
# 2. Source: Reload your shell configuration file to make the function available using:
# source ~/.zshrc # or ~/.bashrc
# 3. Call: Use the function just like a regular command in the terminal:
# mkcd my_folder # Example function to create and navigate to a directory
# Custom functions save time and reduce errors by encapsulating logic into simple, easy-to-remember commands.
# Custom Functions
# Create a directory (if it doesn't exist) and change into it
function mkcd() {
mkdir -p "$1" # Create the directory
cd "$1" # Change into the directory
}
# Navigate to a specific project folder within ~/projects
function proj() {
cd ~/projects/"$1" # Change into the specified project folder
}
# Open the current directory in Visual Studio Code
function copen() {
code . # Launch VS Code in the current directory
}
# Search for a file or directory by name in the current directory
function fsearch() {
find . -name "$1" # Execute a search for the specified name
}
# View a file with syntax highlighting using bat (alternative to cat)
function vcat() {
bat "$1" # Display file contents with syntax highlighting
}
# Update, upgrade, and clean up Homebrew packages
function brewup() {
brew update && brew upgrade && brew cleanup # Run all three commands in sequence
}
# Stage, commit, and push all changes in the Git repository
function gacp() {
git add . # Stage all changes
git commit -m "$1" # Commit with a message
git push # Push the changes to the remote repository
}
# Open a file with Vim for editing
function vopen() {
vim "$1" # Open the specified file in Vim
}
# Display the current status of the Git repository
function gstatus() {
git status # Show the status of the repository
}
# Pull the latest changes from the remote Git repository
function gpull() {
git pull # Fetch and merge changes from the remote repository
}
# List all branches in the Git repository, including remote branches
function gbranches() {
git branch -a # Display all local and remote branches
}
# Return to the home directory
function gohome() {
cd ~ # Navigate to the home directory
}
# Search for a specific pattern in files within the current directory
function search() {
grep -rnw '.' -e "$1" # Perform a recursive search for the pattern
}
# Display the disk usage of all files and directories in the current directory
function dusage() {
du -sh * # Show a summary of disk usage for each item
}
# Open the .zshrc file in Visual Studio Code for editing
function editzshrc() {
code ~/.zshrc # Open the .zshrc file in VS Code
}
# List all defined aliases and functions
function list_aliases_functions() {
echo "Aliases:"
alias # List all aliases
echo
echo "Functions:"
declare -f | grep '^[a-zA-Z_][a-zA-Z0-9_]* ()' # List all function names and definitions
}
# Reload the .zshrc file to apply recent changes
function reloadzshrc() {
source ~/.zshrc # Reload the .zshrc configuration
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment