Skip to content

Instantly share code, notes, and snippets.

@fnsoxt
Forked from nobk/.zshrc
Created February 7, 2022 14:05
Show Gist options
  • Save fnsoxt/34565421887dee4ef59f48fdfbaf8a11 to your computer and use it in GitHub Desktop.
Save fnsoxt/34565421887dee4ef59f48fdfbaf8a11 to your computer and use it in GitHub Desktop.
My light weight zshrc for WSL2 and Gentoo and MSYS2 and OpenWRT zsh shell
# z's config for the zsh
TERM=xterm-256color
# Load user rc script
[ -f "$HOME/.zsh_user.zsh" ] && source "$HOME/.zsh_user.zsh"
# History in cache directory:
HISTSIZE=10000
SAVEHIST=10000
if [ -z "$HISTFILE" ]; then
HISTFILE=$HOME/.zsh_history
fi
# Show history
# "yyyy-mm-dd"
alias history='fc -il 1'
setopt append_history
setopt extended_history
setopt hist_expire_dups_first
setopt hist_ignore_dups # ignore duplication command history list
setopt hist_ignore_space
setopt hist_save_no_dups
setopt hist_verify
setopt inc_append_history
setopt share_history # share command history data
#setopt auto_cd
unsetopt correct_all
setopt multios
setopt prompt_subst
setopt extended_glob
# fixme - the load process here seems a bit bizarre
zmodload -i zsh/complist
WORDCHARS=''
unsetopt menu_complete # do not autoselect the first completion entry
unsetopt flowcontrol
setopt auto_menu # show completion menu on successive tab press
setopt complete_in_word
setopt always_to_end
# Basic auto/tab complete:
autoload -U compinit
zstyle ':completion::complete:*' use-cache 1
zmodload zsh/complist
compinit
# case insensitive (all), partial-word and substring completion
if [[ "$CASE_SENSITIVE" = true ]]; then
zstyle ':completion:*' matcher-list 'r:|=*' 'l:|=* r:|=*'
else
if [[ "$HYPHEN_INSENSITIVE" = true ]]; then
zstyle ':completion:*' matcher-list 'm:{a-zA-Z-_}={A-Za-z_-}' 'r:|=*' 'l:|=* r:|=*'
else
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|=*' 'l:|=* r:|=*'
fi
fi
unset CASE_SENSITIVE HYPHEN_INSENSITIVE
zstyle ':completion:*' list-colors ''
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#) ([0-9a-z-]#)*=01;34=0=01'
if [[ "$OSTYPE" = solaris* ]]; then
zstyle ':completion:*:*:*:*:processes' command "ps -u $USER -o pid,user,comm"
else
zstyle ':completion:*:*:*:*:processes' command "ps -u $USER -o pid,user,comm -w -w"
fi
# disable named-directories autocompletion
zstyle ':completion:*:cd:*' tag-order local-directories directory-stack path-directories
# Use caching so that commands like apt and dpkg complete are useable
zstyle ':completion::complete:*' use-cache 1
zstyle ':completion::complete:*' cache-path $ZSH_CACHE_DIR
# Don't complete uninteresting users
zstyle ':completion:*:*:*:users' ignored-patterns \
adm amanda apache at avahi avahi-autoipd beaglidx bin cacti canna \
clamav daemon dbus distcache dnsmasq dovecot fax ftp games gdm \
gkrellmd gopher hacluster haldaemon halt hsqldb ident junkbust kdm \
ldap lp mail mailman mailnull man messagebus mldonkey mysql nagios \
named netdump news nfsnobody nobody nscd ntp nut nx obsrun openvpn \
operator pcap polkitd postfix postgres privoxy pulse pvm quagga radvd \
rpc rpcuser rpm rtkit scard shutdown squid sshd statd svn sync tftp \
usbmux uucp vcsa wwwrun xfs '_*'
# ... unless we really want to.
zstyle '*' single-ignored show
if [[ $COMPLETION_WAITING_DOTS = true ]]; then
expand-or-complete-with-dots() {
# toggle line-wrapping off and back on again
[[ -n "$terminfo[rmam]" && -n "$terminfo[smam]" ]] && echoti rmam
print -Pn "%{%F{red}......%f%}"
[[ -n "$terminfo[rmam]" && -n "$terminfo[smam]" ]] && echoti smam
zle expand-or-complete
zle redisplay
}
zle -N expand-or-complete-with-dots
bindkey "^I" expand-or-complete-with-dots
fi
# Changing/making/removing directory
setopt auto_pushd
setopt pushd_ignore_dups
setopt pushdminus
alias -g ...='../..'
alias -g ....='../../..'
alias -g .....='../../../..'
alias -g ......='../../../../..'
alias -- -='cd -'
alias 1='cd -'
alias 2='cd -2'
alias 3='cd -3'
alias 4='cd -4'
alias 5='cd -5'
alias 6='cd -6'
alias 7='cd -7'
alias 8='cd -8'
alias 9='cd -9'
alias md='mkdir -p'
alias rd=rmdir
alias d='dirs -v | head -10'
# List directory contents
[ -f /usr/bin/exa ] && {
alias ls='exa -l --time-style long-iso --icons --color=auto'
alias lsa='exa -lah --time-style long-iso --icons --color=auto'
alias l='exa -lah --time-style long-iso --icons --color=auto'
alias ll='exa -lh --time-style long-iso --icons --color=auto'
alias la='exa -lah --time-style long-iso --icons --color=auto'
alias lt='exa -Tlah --time-style long-iso --icons --color=auto'
} || {
alias ls='ls --color=auto'
alias lsa='ls -lah'
alias l='ls -lah'
alias ll='ls -lh'
alias la='ls -lAh'
}
# Push and pop directories on directory stack
alias pu='pushd'
alias po='popd'
# is x grep argument available?
grep-flag-available() {
echo | grep $1 "" >/dev/null 2>&1
}
GREP_OPTIONS=""
# color grep results
if grep-flag-available --color=auto; then
GREP_OPTIONS+=" --color=auto"
fi
# ignore VCS folders (if the necessary grep flags are available)
VCS_FOLDERS="{.bzr,CVS,.git,.hg,.svn}"
if grep-flag-available --exclude-dir=.cvs; then
GREP_OPTIONS+=" --exclude-dir=$VCS_FOLDERS"
elif grep-flag-available --exclude=.cvs; then
GREP_OPTIONS+=" --exclude=$VCS_FOLDERS"
fi
# export grep settings
alias grep="grep $GREP_OPTIONS"
# clean up
unset GREP_OPTIONS
unset VCS_FOLDERS
unfunction grep-flag-available
# cursor keys
case "$TERM" in
linux) #Linux console
bindkey '\e[1~' beginning-of-line # Home
bindkey '\e[4~' end-of-line # End
bindkey '\e[3~' delete-char # Del
bindkey '\e[2~' overwrite-mode # Insert
;;
screen*)
bindkey '\e[1~' beginning-of-line # Home
bindkey '\e[4~' end-of-line # End
# bindkey '\e[3~' delete-char # Del
bindkey '\e[2~' overwrite-mode # Insert
bindkey '\e[7~' beginning-of-line # Home
bindkey '\e[8~' end-of-line # End
bindkey '\eOc' forward-word # ctrl cursor right
bindkey '\eOd' backward-word # ctrl cursor left
# bindkey '\e[3~' backward-delete-char # This should not be necessary!
bindkey '\e[H' beginning-of-line # Home
bindkey '\e[F' end-of-line # End
bindkey '\e[3~' delete-char # Del
bindkey '\e[2~' overwrite-mode # Insert
bindkey "^[[5C" forward-word # ctrl cursor right
bindkey "^[[5D" backward-word # ctrl cursor left
bindkey "^[[1;5C" forward-word # ctrl cursor right
bindkey "^[[1;5D" backward-word # ctrl cursor left
bindkey '^t' expand-or-complete-prefix
;;
rxvt)
bindkey '\e[7~' beginning-of-line # Home
bindkey '\e[8~' end-of-line # End
bindkey '\eOc' forward-word # ctrl cursor right
bindkey '\eOd' backward-word # ctrl cursor left
bindkey '\e[3~' backward-delete-char # This should not be necessary!
bindkey '\e[2~' overwrite-mode # Insert
;;
xterm*)
bindkey '\e[H' beginning-of-line # Home
bindkey '\e[F' end-of-line # End
bindkey '^[OH' beginning-of-line # Home
bindkey '^[OF' end-of-line # End
bindkey '\e[1~' beginning-of-line # Home
bindkey '\e[4~' end-of-line # End
bindkey '\e[3~' delete-char # Del
bindkey '\e[2~' overwrite-mode # Insert
bindkey "^[[5C" forward-word # ctrl cursor right
bindkey "^[[5D" backward-word # ctrl cursor left
bindkey "^[[1;5C" forward-word # ctrl cursor right
bindkey "^[[1;5D" backward-word # ctrl cursor left
bindkey '^t' expand-or-complete-prefix
bindkey -s "^[OM" "^M" # pad Enter
;;
esac
# Load aliases and shortcuts if existent.
[ -f "$HOME/.bash_aliases" ] && source "$HOME/.bash_aliases"
autoload -U colors && colors
# Echoes a username/host string when connected over SSH (empty otherwise)
ssh_info() {
# [[ "$SSH_CONNECTION" != '' ]] && echo "%{$fg[blue]%}ssh($(echo $SSH_CLIENT | sed 's/\s.*$//'))%{$reset_color%}:" || echo ''
[[ "$SSH_CONNECTION" != '' ]] && echo "%{$fg[blue]%}ssh%{$reset_color%}:" || echo ''
}
# Load version control information
autoload -Uz vcs_info
precmd() { vcs_info }
# Format the vcs_info_msg_0_ variable
zstyle ':vcs_info:git:*' formats '%b'
setopt PROMPT_SUBST
function os_type
{
which yum >/dev/null 2>&1 && { echo "CentOS"; return; }
which zypper >/dev/null 2>&1 && { echo "OpenSUSE"; return; }
which emerge >/dev/null 2>&1 && { echo "Gentoo"; return; }
}
function os_type2
{
if [ -f /etc/os-release ]; then
# freedesktop.org and systemd
. /etc/os-release
echo "$NAME" | sed 's/ GNU\/Linux//'; return
elif type lsb_release >/dev/null 2>&1; then
# linuxbase.org
lsb_release -si; return
elif [ -f /etc/lsb-release ]; then
# For some versions of Debian/Ubuntu without lsb_release command
. /etc/lsb-release
echo "$DISTRIB_ID"; return
elif [ -f /etc/debian_version ]; then
# Older Debian/Ubuntu/etc.
echo "Debian"; return
else
uname -s; return
fi
}
# Enable colors and change prompt:
if [[ -z "${MSYSTEM}" ]]; then
read tos < /proc/version
if [[ "$tos" =~ "/*microsoft\-standard*" ]]; then
wsl="WSL2"
elif [[ "$tos" =~ "/*Microsoft*" ]]; then
wsl="WSL"
fi
os=$(os_type)
if [[ -n "$os" ]]; then
HOSTNAME="%m-$os"
else
os=$(os_type2)
if [[ -n "$os" ]]; then
HOSTNAME="%m-$os"
else
if [[ "$tos" =~ "/*OpenWrt*" ]]; then
HOSTNAME="%m-OpenWrt"
elif [[ "$tos" =~ "/*Ubuntu*" ]]; then
HOSTNAME="%m-Ubuntu"
elif [[ "$tos" =~ "/*Gentoo*" ]]; then
HOSTNAME="%m-Gentoo"
else
HOSTNAME="%m"
fi
fi
fi
if [[ -n "$wsl" ]]; then
HOSTNAME="${HOSTNAME}-${wsl}"
fi
else
HOSTNAME="%m-${MSYSTEM}"
# complete hard drives in msys2
drives=$(mount | sed -rn 's#^[A-Z]: on /([a-z]).*#\1#p' | tr '\n' ' ')
zstyle ':completion:*' fake-files /: "/:$drives"
unset drives
fi
ZSH_SH_SYS="/usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
ZSH_SH_GIT="$HOME/git/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
[ -f $ZSH_SH_GIT ] && ZSH_SYNTAX_HI=$ZSH_SH_GIT || { \
[ -f $ZSH_SH_SYS ] && ZSH_SYNTAX_HI=$ZSH_SH_SYS }
unset ZSH_SH_SYS ; unset ZSH_SH_GIT
if [ ! -z $ZSH_SYNTAX_HI ]; then
if [ "$(id -u)" -eq 0 ]; then
CMDCOLOR='fg=228'
PROMPT='$(ssh_info)%F{9}zsh%#%n@${HOSTNAME} %f%F{69}%9~%f ${vcs_info_msg_0_}
% '
else
CMDCOLOR='fg=208'
PROMPT='$(ssh_info)%F{154}zsh%#%n@${HOSTNAME} %f%F{69}%9~%f ${vcs_info_msg_0_}
% '
fi
# Load zsh-syntax-highlighting; should be last.
source $ZSH_SYNTAX_HI 2>/dev/null
unset ZSH_SYNTAX_HI
ALIASCOLOR='fg=39'
typeset -A ZSH_HIGHLIGHT_STYLES
ZSH_HIGHLIGHT_STYLES[command]="$CMDCOLOR"
ZSH_HIGHLIGHT_STYLES[builtin]="$CMDCOLOR"
ZSH_HIGHLIGHT_STYLES[alias]="$ALIASCOLOR"
else
if [ "$(id -u)" -eq 0 ]; then
PROMPT='%F{9}zsh%#%n@${HOSTNAME} %f%F{69}%9~%f ${vcs_info_msg_0_}%F{228}
% '
else
PROMPT='%F{154}zsh%#%n@${HOSTNAME} %f%F{69}%9~%f ${vcs_info_msg_0_}%F{208}
% '
fi
preexec(){
echo -ne "\e[0m"
}
fi
RPROMPT='%1{ %}''%{'$'\e[1A''%}''%F{255}%*%f''%{'$'\e[1B''%}'
# To enable zsh syntax highlighting and async git prompt
# $ mkdir -p ~/git
# $ cd ~/git
# install zsh syntax highlighting on all OS
# $ git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
# or if Ubuntu
# $ sudo apt install zsh-syntax-highlighting
# or if Gentoo
# $ sudo emerge app-shells/zsh-syntax-highlighting
# install async git
# $ git clone https://github.com/nobk/alien.git
# $ cd alien
# $ git submodule update --init --recursive
# optional: update .zshrc to new version
# $ cp ./zshrc-template.zsh ~/.zshrc
# default theme is simple style but all functional.
# you can also change to 'bnw' theme for cool powerline fonts
# or create ~/.zsh_user.zsh by command
# cat <<EOF >> ~/.zsh_user.zsh
#export ALIEN_THEME="bnw"
##STOP_ALIEN=1 #uncomment this is if do not need load alien
#EOF
if [ -z $STOP_ALIEN ]; then
[ -f "$HOME/git/alien/alien.zsh" ] && {
# export ALIEN_THEME="bnw"
unset precmd
source ${HOME}/git/alien/alien.zsh }
else
unset STOP_ALIEN
fi
#incr install
# sudo wget https://mimosa-pudica.net/src/incr-0.2.zsh -P /usr/share/incr
# or
# wget https://mimosa-pudica.net/src/incr-0.2.zsh -P $HOME/.incr
# sed -i '/compdef -d scp.*/a compdef -d rsync' $HOME/.incr/incr-0.2.zsh
if [ -z $STOP_INCR ]; then
unsetopt nomatch
INCRZSH=(/usr/share/incr/incr-*.zsh)
if [ -f $INCRZSH ]; then
source $INCRZSH
else
INCRZSH=($HOME/.incr/incr-*.zsh)
[ -f $INCRZSH ] && source $INCRZSH
fi
setopt nomatch
unset STOP_INCR
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment