Skip to content

Instantly share code, notes, and snippets.

@QinMing
Last active June 21, 2024 02:06
Show Gist options
  • Save QinMing/364774610afc0e06cc223b467abe83c0 to your computer and use it in GitHub Desktop.
Save QinMing/364774610afc0e06cc223b467abe83c0 to your computer and use it in GitHub Desktop.
.zshrc (lazy loading shell functions)
# Copyright (c) 2016-2018 Ming Qin (覃明) <https://github.com/QinMing>
# Open source under MIT LICENSE.
ZSH_THEME="agnoster"
plugins=(zsh-autosuggestions git pip django docker docker-compose)
# ......
unsetopt AUTO_CD
alias l='ls -alhtr'
alias upthis='ss u'
alias downthis='ss d'
alias g='git'
alias gs='git status'
alias gl='git l'
alias gd='git diff'
alias gb='git branch'
alias gbd='git checkout -q master && git remote prune origin && git for-each-ref refs/heads/ "--format=%(refname:short)" | while read branch; do mergeBase=$(git merge-base master $branch) && [[ $(git cherry master $(git commit-tree $(git rev-parse $branch^{tree}) -p $mergeBase -m _)) == "-"* ]] && git branch -D $branch; done; git prune'
# Delete local branches that squash-merged to `master`. Forked from https://github.com/not-an-aardvark/git-delete-squashed
alias gck='git checkout'
alias gp='git pull'
alias dcp='docker-compose'
alias docker-clean='docker system prune --volumes -f'
# https://stackoverflow.com/questions/1904860/how-to-remove-unreferenced-blobs-from-my-git-repo
alias git-clean='git -c gc.reflogExpire=0 -c gc.reflogExpireUnreachable=0 -c gc.rerereresolved=0 -c gc.rerereunresolved=0 -c gc.pruneExpire=now gc "$@"'
lazy_load() {
# Act as a stub to another shell function/command. When first run, it will load the actual function/command then execute it.
# E.g. This made my zsh load 0.8 seconds faster by loading `nvm` when "nvm", "npm" or "node" is used for the first time
# $1: space separated list of alias to release after the first load
# $2: file to source
# $3: name of the command to run after it's loaded
# $4+: argv to be passed to $3
echo "Lazy loading $1 ..."
# $1.split(' ') using the s flag. In bash, this can be simply ($1) #http://unix.stackexchange.com/questions/28854/list-elements-with-spaces-in-zsh
# Single line won't work: local names=("${(@s: :)${1}}"). Due to http://stackoverflow.com/questions/14917501/local-arrays-in-zsh (zsh 5.0.8 (x86_64-apple-darwin15.0))
local -a names
if [[ -n "$ZSH_VERSION" ]]; then
names=("${(@s: :)${1}}")
else
names=($1)
fi
unalias "${names[@]}"
. $2
shift 2
$*
}
group_lazy_load() {
local script
script=$1
shift 1
for cmd in "$@"; do
alias $cmd="lazy_load \"$*\" $script $cmd"
done
}
export NVM_DIR=~/.nvm
group_lazy_load $HOME/.nvm/nvm.sh nvm node npm truffle
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
group_lazy_load $HOME/.rvm/scripts/rvm rvm irb rake rails
unset -f group_lazy_load
alias ve="lazy_load 've' $HOME/git/simple-virtualenv-wrapper/ve.sh ve"
# If using GnuPG to sign git commits, etc, need the following.
export GPG_TTY=$(tty)
# Other applications
# eval $(thefuck --alias)
# eval "$(pyenv init -)"
@averissimo
Copy link

awesome!!

@brandonbloom
Copy link

Thank you for this!

@iShawnWang
Copy link

Why i got Unrecognized command line argument: nvm node npm when input nvm each time I open a new window?
but after source ~/.bash_profile everything works fine ~

@rsurjano
Copy link

great!! thank you!

@axsaucedo
Copy link

This is great!

@aronerben
Copy link

Ah, back to my old zsh startup speed, thanks!

@kinoute
Copy link

kinoute commented Nov 3, 2019

Why i got Unrecognized command line argument: nvm node npm when input nvm each time I open a new window?
but after source ~/.bash_profile everything works fine ~

I have the same problem. Works fine only after I source ~/.zshrc again. Any ideas?

Capture d’écran 2019-11-03 à 20 22 31

@taeksoo-shin
Copy link

awesome!
thanks!

@EduardoMejiaL
Copy link

Wow, thank you!!

@moghaazi
Copy link

Works fine but every time I open a new terminal I got this error

/home/usrname/.nvm/nvm.sh:2348: defining function based on alias `nvm'
/home/username/.nvm/nvm.sh:2348: parse error near `()'

and it repeat more than 50 times

@DevER-M
Copy link

DevER-M commented Apr 17, 2022

my zsh thing used to take around 1.5 seconds not its like 0.4 seconds AWESOME

@sethderrick
Copy link

sethderrick commented May 13, 2024

Works fine but every time I open a new terminal I got this error

/home/usrname/.nvm/nvm.sh:2348: defining function based on alias `nvm'
/home/username/.nvm/nvm.sh:2348: parse error near `()'

and it repeat more than 50 times

Did you ever get this resolved? Here we are in 2024 and I'm getting this! (NOTE: Mine is "nvm.sh:2833")

@QinMing
Copy link
Author

QinMing commented May 14, 2024

Hi @moghaazi and @sethderrick

I couldn't reproduce. I just pulled the latest nvm, and was still able to use this tool as normal.
I'm wondering if the issue you are facing is specific to nvm itself. Are you able to run . $HOME/.nvm/nvm.sh directly?

disclaimer: I haven't used nvm or node.js for years

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment