From e2a2009b2f5f0e84a8971184d3d1832ba856c194 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Thu, 26 Jan 2023 22:34:24 +0800 Subject: Update --- zsh/bd.zsh | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ zsh/zshrc | 33 +++++++++++++++++++++++++------- 2 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 zsh/bd.zsh (limited to 'zsh') diff --git a/zsh/bd.zsh b/zsh/bd.zsh new file mode 100644 index 0000000..33664d6 --- /dev/null +++ b/zsh/bd.zsh @@ -0,0 +1,64 @@ +bd () { + (($#<1)) && { + print -- "usage: $0 " + print -- " $0 " + return 1 + } >&2 + # example: + # $PWD == /home/arash/abc ==> $num_folders_we_are_in == 3 + local num_folders_we_are_in=${#${(ps:/:)${PWD}}} + local dest="./" + + # First try to find a folder with matching name (could potentially be a number) + # Get parents (in reverse order) + local parents + local i + for i in {$num_folders_we_are_in..2} + do + parents=($parents "$(echo $PWD | cut -d'/' -f$i)") + done + parents=($parents "/") + # Build dest and 'cd' to it + local parent + foreach parent (${parents}) + do + dest+="../" + if [[ $1 == $parent ]] + then + cd $dest + return 0 + fi + done + + # If the user provided an integer, go up as many times as asked + dest="./" + if [[ "$1" = <-> ]] + then + if [[ $1 -gt $num_folders_we_are_in ]] + then + print -- "bd: Error: Can not go up $1 times (not enough parent directories)" + return 1 + fi + for i in {1..$1} + do + dest+="../" + done + cd $dest + return 0 + fi + + # If the above methods fail + print -- "bd: Error: No parent directory named '$1'" + return 1 +} +_bd () { + # Get parents (in reverse order) + local num_folders_we_are_in=${#${(ps:/:)${PWD}}} + local i + for i in {$num_folders_we_are_in..2} + do + reply=($reply "`echo $PWD | cut -d'/' -f$i`") + done + reply=($reply "/") +} +compctl -V directories -K _bd bd diff --git a/zsh/zshrc b/zsh/zshrc index 01e97cf..1958b56 100644 --- a/zsh/zshrc +++ b/zsh/zshrc @@ -4,20 +4,30 @@ # See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes ZSH_THEME="archcraft" + # helper repo export SETTING_DIR=$HOME/helper source $SETTING_DIR/tools/init/load-settings.sh fpath=($SETTING_DIR/zsh $fpath) + # Options setopt extended_glob setopt HIST_SAVE_NO_DUPS # Do not write a duplicate event to the history file. -_comp_options+=(globdots) # With hidden files + +# Edit Command Line +autoload -Uz edit-command-line; zle -N edit-command-line +bindkey -- "^X^E" edit-command-line + + +# Enable completion autoload -Uz compinit; compinit +_comp_options+=(globdots) # With hidden files zstyle ':completion:*' menu select zstyle ':completion::complete:*' gain-privileges 1 + # PS1 with git status at right autoload -Uz add-zsh-hook add-zsh-hook precmd precmd @@ -27,13 +37,14 @@ function precmd() { RPROMPT="%B%K{red}%F{black}${RIGHT}%f%k%b" } + # Directory Stack -#setopt AUTO_PUSHD # Push the current directory visited on the stack. -#setopt PUSHD_IGNORE_DUPS # Do not store duplicates in the stack. -#setopt PUSHD_SILENT # Do not print the directory stack after pushd or popd. +setopt AUTO_PUSHD # Push the current directory visited on the stack. +setopt PUSHD_IGNORE_DUPS # Do not store duplicates in the stack. +setopt PUSHD_SILENT # Do not print the directory stack after pushd or popd. +alias d='dirs -v' +for index ({1..9}) alias "$index"="cd +${index}"; unset index -#alias d='dirs -v' -#for index ({1..9}) alias "$index"="cd +${index}"; unset index # create a zkbd compatible hash; # to add other keys to this hash, see: man 5 terminfo @@ -66,7 +77,6 @@ bindkey -- "\C-b" backward-char bindkey -- "\C-f" forward-char bindkey -- "\Eb" backward-word bindkey -- "\Ef" forward-word -bindkey -- "\C-x-\C-e" edit-command-line bindkey -- "\C-v" quoted-insert bindkey -- "\E." insert-last-word bindkey -- "${key[PageUp]}" beginning-of-buffer-or-history @@ -74,6 +84,15 @@ bindkey -- "${key[PageDown]}" end-of-buffer-or-history bindkey -- "${key[Shift-Tab]}" reverse-menu-complete #bindkey -- "${key[Insert]}" overwrite-mode + +# Common shell options +alias ls='ls --color' + + +# Quick jump to parent folders +source ~/helper/zsh/bd.zsh + + # Finally, make sure the terminal is in application mode, when zle is # active. Only then are the values from $terminfo valid. if (( ${+terminfo[smkx]} && ${+terminfo[rmkx]} )); then -- cgit v1.2.3-70-g09d2