Skip to content

Instantly share code, notes, and snippets.

@mertant
Last active October 1, 2023 10:03
Show Gist options
  • Save mertant/a0d2667126cec2865a40e3cd4e2c89b6 to your computer and use it in GitHub Desktop.
Save mertant/a0d2667126cec2865a40e3cd4e2c89b6 to your computer and use it in GitHub Desktop.
#### Zsh general utils/aliases
#
# Add to your .zshrc, or other terminal config file (may or may not be compatible with non-Zsh shells)
#
#### Constants
# Your text editor program. Replace with kate or whatever application you prefer.
# (This could probably be done with a symlink, but oh well.)
MY_TEXT_ED='vim'
#### General utilities
### Configs for terminals
## Bash - default shell for MacOS
alias bashprof="$MY_TEXT_ED ~/.bash_profile" # Open bash profile for editing settings
## Zsh - better shell
#
# https://ohmyz.sh/
# https://www.howtogeek.com/362409/what-is-zsh-and-why-should-you-use-it-instead-of-bash/
#
alias zprof="$MY_TEXT_ED ~/.zshrc" # Open Zsh profile for editing settings - i.e. this file.
alias zupdate="exec zsh" # Load zsh profile in current terminal window
# From now on, you can simply type 'zprof' to open this settings file, and then, after editing,
# 'zupdate' to load the new settings to your terminal window!
# Try to list aliases defined in zsh (to e.g. find which Zsh plugin is overriding your custom aliases).
# Example usage: check_aliases | grep 'll='
alias check_aliases="zsh -ixc : 2>&1 | grep '> alias '"
### Aliases for built-in commands for impatient people
# Better list: files line by line, showing "hidden" ones, in descending time order, / appended to directories etc.
# (I would prefer this to be aliased to ll but it's being overridden by .oh-my-zsh/lib/directories.zsh:37> alias 'll=ls -lh')
alias lll="ls -altrF"
# c - clear for the impatient
alias c="clear"
# grip - grep case-insensitively
alias grip="grep -i"
## History
#
# 'history' is an alias for 'fc'
# 'fc -l -5' does the same thing as 'history -5'
# h - history for the impatient
function h() {
default_n_displayed_lines=30;
n_displayed_lines=$default_n_displayed_lines;
if [ -z "$1" ] # Default when no arg
then
echo;
else
n_displayed_lines=$1;
fi
echo "[$0()] Displaying last $n_displayed_lines commands:"
fc -l -$n_displayed_lines
}
# hig/hg - grep history (case-insensitive)
# example usage: hg "\-X POST"
# N.B. The name hg conflicts with the Mercurial VCS, so rename this to hig if you use Mercurial.
function hg() {
history_search_n_lines=100000;
if [ -z "$1" ] # Default when no arg
then
echo "[$0()] Provide an argument for history grep";
else
echo "[$0()] Searching for $1 in last $history_search_n_lines commands"
h $history_search_n_lines | grip --color $1
fi
}
## Looping/control flow utilities
# Repeat
# example usage: repeat 10 (echo foo; echo bar;)
function repeat() {
n=$1
shift
for i in $(seq $n); do
$@
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment