Skip to content

Commit

Permalink
Refactor the ZSH scripts to use Zi and whatnot
Browse files Browse the repository at this point in the history
  • Loading branch information
htr3n committed Jul 1, 2023
1 parent ad894af commit 657f414
Show file tree
Hide file tree
Showing 16 changed files with 216 additions and 200 deletions.
Empty file removed .gitmodules
Empty file.
2 changes: 1 addition & 1 deletion LICENSE.md → LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Huy Tran
Copyright (c) 2019-2023 Alex Tran

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ In case you want to have private setting excluded from public Git repos, just cr

## Plugin Management

~~Instead of manually installing some ZSH plugins, I use [antibody](https://getantibody.github.io) to manage necessary plugins (some are not working with _antibody_ such as 'fzf', so manual task it is).~~

Recently, I have switched to [zplugin](https://github.com/zdharma/zplugin) with cool Turbo Mode to accelerate the loading and checking of ZSH plugins. Thus, `zplugin` needs to be installed as well.
Instead of manually installing ZSH plugins, I use [Zi](https://wiki.zshell.dev/), which is a continuing work of [zplugin](https://github.com/zdharma/zplugin), with cool Turbo Mode to accelerate the loading and checking of ZSH plugins. Thus, `zi` needs to be installed as well.

```sh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/zdharma/zplugin/master/doc/install.sh)"
sh -c "$(curl -fsSL get.zshell.dev)" -- -i skip -b main
```

Note that `zplugin` installation script will automatically add some commands into the end of `.zshrc` to initialise / load `zplugin`. Nonetheless, `_init.sh` has already the initialisation of `zplugin`, thus, we should remove or comment the parts added by `zplugin` installation script.
Note that `zi` installation script will automatically add some commands into the end of `.zshrc` to initialise / load `zi`. Nonetheless, `_init.sh` has already the initialisation of `zi`, thus, we should remove or comment the parts added by `zi` installation script.

If `Zi` is set up properly, you can use many handy commands, for instance, updating all or some plugins with `zi update` , removing a plugin `zi delete plugin_name`.

Some useful plugins:
Some useful ZSH plugins:

* [zsh-autosuggestions](https://github.com/zsh-users/zsh-autosuggestions)
* [zsh-completions](https://github.com/zsh-users/zsh-completions)
Expand All @@ -42,4 +42,4 @@ I developed most of the configuration on my own but nevertheless learned a lot f

## License

<3 MIT License.
<3 [MIT License](LICENSE.txt).
37 changes: 16 additions & 21 deletions _init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
# vim:syntax=sh
# vim:filetype=sh

#-----------------------------------------------------
# ensure to only execute on ZSH
# https://stackoverflow.com/a/9911082/339302
[ ! -n "$ZSH_VERSION" ] && return

# For MacOS
if [[ "x$SYSTEM" = "xDarwin" ]]; then
# system executables
#export PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/libexec
Expand All @@ -10,28 +16,17 @@ if [[ "x$SYSTEM" = "xDarwin" ]]; then
fi

#-----------------------------------------------------
# ensure to only execute on ZSH
# https://stackoverflow.com/a/9911082/339302
[ ! -n "$ZSH_VERSION" ] && return

#-----------------------------------------------------
#
# Antibody: Load the plugins before scripts
#
#source ${ZSHCONFIG}/antibody-init.zsh
#
#
#source ${ZSHCONFIG}/zsh-managed-plugins.zsh
#
#-----------------------------------------------------
# Load zinit
export ZI_HOME_DIR="${HOME}/.zi"
export ZI_BIN_DIR="${ZI_HOME_DIR}/bin"

#-----------------------------------------------------
# bootstrap the zplugin script
#
source "$HOME/.zplugin/bin/zplugin.zsh"

# and load the plugins
source "$HOME/.zsh-config/zplugin-init.zsh"
# and the plugins
if [[ -f "${ZI_BIN_DIR}/zi.zsh" ]]; then
source "${ZI_BIN_DIR}/zi.zsh"
else
echo "The Zi init script does not exist at '${ZI_BIN_DIR}/zi.zsh'"
exit 1
fi
#-----------------------------------------------------

# https://github.com/sindresorhus/pure#options
Expand Down
31 changes: 0 additions & 31 deletions antibody-init.zsh

This file was deleted.

117 changes: 117 additions & 0 deletions autoloaded/colors
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Put standard ANSI color codes in shell parameters for easy use.
# Note that some terminals do not support all combinations.

emulate -L zsh

typeset -Ag color colour

color=(
# Codes listed in this array are from ECMA-48, Section 8.3.117, p. 61.
# Those that are commented out are not widely supported or aren't closely
# enough related to color manipulation, but are included for completeness.

# Attribute codes:
00 none # 20 gothic
01 bold # 21 double-underline
02 faint 22 normal
03 standout 23 no-standout
04 underline 24 no-underline
05 blink 25 no-blink
# 06 fast-blink # 26 proportional
07 reverse 27 no-reverse
08 conceal 28 no-conceal
# 09 strikethrough # 29 no-strikethrough

# Font selection:
# 10 font-default
# 11 font-first
# 12 font-second
# 13 font-third
# 14 font-fourth
# 15 font-fifth
# 16 font-sixth
# 17 font-seventh
# 18 font-eighth
# 19 font-ninth

# Text color codes:
30 black 40 bg-black
31 red 41 bg-red
32 green 42 bg-green
33 yellow 43 bg-yellow
34 blue 44 bg-blue
35 magenta 45 bg-magenta
36 cyan 46 bg-cyan
37 white 47 bg-white
# 38 iso-8316-6 # 48 bg-iso-8316-6
39 default 49 bg-default

# Other codes:
# 50 no-proportional
# 51 border-rectangle
# 52 border-circle
# 53 overline
# 54 no-border
# 55 no-overline
# 56 through 59 reserved

# Ideogram markings:
# 60 underline-or-right
# 61 double-underline-or-right
# 62 overline-or-left
# 63 double-overline-or-left
# 64 stress
# 65 no-ideogram-marking
)

# A word about black and white: The "normal" shade of white is really a
# very pale grey on many terminals; to get truly white text, you have to
# use bold white, and to get a truly white background you have to use
# bold reverse white bg-xxx where xxx is your desired foreground color
# (and which means the foreground is also bold).

# Map in both directions; could do this with e.g. ${(k)colors[(i)normal]},
# but it's clearer to include them all both ways.

local k
for k in ${(k)color}; do color[${color[$k]}]=$k; done

# Add "fg-" keys for all the text colors, for clarity.

for k in ${color[(I)3?]}; do color[fg-${color[$k]}]=$k; done

# This is inaccurate, but the prompt theme system needs it.

color[grey]=${color[black]}
color[fg-grey]=${color[grey]}
color[bg-grey]=${color[bg-black]}

# Assistance for the color-blind.

colour=(${(kv)color}) # A case where ksh namerefs would be useful ...

# The following are terminal escape sequences used by colored prompt themes.

local lc=$'\e[' rc=m # Standard ANSI terminal escape values

typeset -Hg reset_color bold_color
reset_color="$lc${color[none]}$rc"
bold_color="$lc${color[bold]}$rc"

# Foreground

typeset -AHg fg fg_bold fg_no_bold
for k in ${(k)color[(I)fg-*]}; do
fg[${k#fg-}]="$lc${color[$k]}$rc"
fg_bold[${k#fg-}]="$lc${color[bold]};${color[$k]}$rc"
fg_no_bold[${k#fg-}]="$lc${color[normal]};${color[$k]}$rc"
done

# Background

typeset -AHg bg bg_bold bg_no_bold
for k in ${(k)color[(I)bg-*]}; do
bg[${k#bg-}]="$lc${color[$k]}$rc"
bg_bold[${k#bg-}]="$lc${color[bold]};${color[$k]}$rc"
bg_no_bold[${k#bg-}]="$lc${color[normal]};${color[$k]}$rc"
done
12 changes: 6 additions & 6 deletions autoloaded/path
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# display a neatly formatted PATH content with colors
# -------------------------------------------------------------------

echo $PATH | tr ":" "\n" | \
awk "{ sub(\"/usr\", \"$fg_no_bold[green]/usr$reset_color\"); \
sub(\"/bin\", \"$fg_no_bold[blue]/bin$reset_color\"); \
sub(\"/opt\", \"$fg_no_bold[cyan]/opt$reset_color\"); \
sub(\"/sbin\", \"$fg_no_bold[magenta]/sbin$reset_color\"); \
sub(\"/local\", \"$fg_no_bold[yellow]/local$reset_color\"); \
echo "$PATH" | tr ":" "\n" | \
awk "{ sub(\"/usr\", \"\x1b[0;32m/usr\x1b[0m\"); \
sub(\"/bin\", \"\x1b[0;34m/bin\x1b[0m\"); \
sub(\"/opt\", \"\x1b[0;36m/opt\x1b[0m\"); \
sub(\"/sbin\", \"\x1b[0;35m/sbin\x1b[0m\"); \
sub(\"/local\", \"\x1b[0;33m/local\x1b[0m\"); \
print }"
39 changes: 17 additions & 22 deletions bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,20 @@

ZSHCONFIG=${HOME}/.zsh-config

function zsh_bootstrap(){

# need zplugin
echo "Cloning zplugin"
mkdir ~/.zplugin
git clone https://github.com/zdharma/zplugin.git ~/.zplugin/bin

echo "Link resource files to ${HOME}"
ln -sf ${ZSHCONFIG}/zlogin ${HOME}/.zlogin
ln -sf ${ZSHCONFIG}/zprofile ${HOME}/.zprofile
ln -sf ${ZSHCONFIG}/zshenv ${HOME}/.zshenv
ln -sf ${ZSHCONFIG}/zshrc ${HOME}/.zshrc
echo "Done!"
}

function zsh_cleanup(){
echo "Clean up links to resource files at ${HOME}"
rm -f ${HOME}/.zlogin ${HOME}/.zprofile ${HOME}/.zshenv ${HOME}/.zshrc
echo "Done!"
}

zsh_bootstrap
ZI_HOME_DIR="${HOME}/.zi"
ZI_BIN_DIR="${ZI_HOME_DIR}/bin"

echo "Cloning zi repository"

mkdir -p "${ZI_HOME_DIR}"

git clone https://github.com/z-shell/zi.git "${ZI_BIN_DIR}"

echo "Link ZSH resource files to '${HOME}'"

ln -srnf "${ZSHCONFIG}/zlogin" "${HOME}/.zlogin"
ln -srnf "${ZSHCONFIG}/zprofile" "${HOME}/.zprofile"
ln -srnf "${ZSHCONFIG}/zshenv" "${HOME}/.zshenv"
ln -srnf "${ZSHCONFIG}/zshrc" "${HOME}/.zshrc"

echo "Done!"
2 changes: 1 addition & 1 deletion lib/history.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# ZSH HISTORY
#
export HISTFILE=${HOME}/.history
export HISTSIZE=10000
export HISTSIZE=100000
export SAVEHIST=$HISTSIZE
#
setopt EXTENDED_HISTORY # write in the ":start:elapsed;command" format
Expand Down
31 changes: 1 addition & 30 deletions lib/z-custom.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ setopt extended_glob # Use extended globbing syntax.
setopt clobber # turn off warning "file exists" with > and >>

#-----------------------------------------------------
# Automatically expanding zsh global aliases
# Automatically expanding zsh global aliases
# https://goo.gl/fJbtmJ
#
globalias() {
Expand All @@ -39,32 +39,3 @@ zle -N globalias
bindkey " " globalias
bindkey "^ " magic-space # control-space to bypass completion
bindkey -M isearch " " magic-space # normal space during searches


#-----------------------------------------------------
# https://github.com/zsh-users/zsh-autosuggestions
#
# change ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE in case the color contrast is bad
# https://github.com/zsh-users/zsh-autosuggestions/blob/master/src/config.zsh
#ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8'


#-----------------------------------------------------
# https://github.com/zsh-users/zsh-completions
#
# fpath=(${ZSHCONFIG}/zsh-completions/src $fpath)
#autoload -U compinit && compinit
# Performance
# https://gist.github.com/ctechols/ca1035271ad134841284

#-----------------------------------------------------
# https://github.com/junegunn/fzf#fuzzy-completion-for-bash-and-zsh
#
# brew install fzf
#
test -d ${ZDOTDIR:-$HOME}/.fzf && source ${ZDOTDIR:-$HOME}/.fzf.zsh

#-----------------------------------------------------
# https://github.com/nvbn/thefuck
# brew install thefuck
# eval $(thefuck --alias)
44 changes: 44 additions & 0 deletions zi-init.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env zsh
# vim:syntax=sh
# vim:filetype=sh

##
# ZSH plugins managed by Zi
# https://github.com/z-shell/zi
# https://wiki.zshell.dev/
# https://wiki.zshell.dev/docs/getting_started/overview
##

zi ice wait"0" atload"_zsh_autosuggest_start" lucid
zi light zsh-users/zsh-autosuggestions

zi ice wait"0" lucid
zi light zsh-users/zsh-completions

zi ice wait"0" atinit"zpcompinit; zpcdreplay" lucid
zi light zdharma-continuum/fast-syntax-highlighting

zi from"gh-r" as"program" mv"direnv* -> direnv" \
atclone'./direnv hook zsh > zhook.zsh' atpull'%atclone' \
pick"direnv" src="zhook.zsh" for \
direnv/direnv

zi light zsh-users/zsh-history-substring-search

zi ice wait"0" lucid
zi load htr3n/history-search-multi-word

zi ice from"gh-r" as"program"
zi light junegunn/fzf

# Load the pure theme, with zsh-async library that's bundled with it
zi ice pick"async.zsh" src"pure.zsh"
zi light sindresorhus/pure

zi ice as"completion" lucid
zi snippet https://github.com/docker/cli/blob/master/contrib/completion/zsh/_docker

# https://github.com/laggardkernel/git-ignore
zi ice pick'init.zsh' blockf
zi light laggardkernel/git-ignore
alias gi="git-ignore"
Loading

0 comments on commit 657f414

Please sign in to comment.