aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorHsieh Chin Fan <typebrook@topo.tw>2023-01-26 22:34:24 +0800
committerHsieh Chin Fan <typebrook@topo.tw>2023-01-26 22:34:24 +0800
commite2a2009b2f5f0e84a8971184d3d1832ba856c194 (patch)
tree36634fea57d5ce07c31b31207c31bfaa22f1f81d
parentff0b7fe2d22f7ad93f838bc1fac96642f654dcec (diff)
Update
-rw-r--r--zsh/bd.zsh64
-rw-r--r--zsh/zshrc33
2 files changed, 90 insertions, 7 deletions
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 @@
1bd () {
2 (($#<1)) && {
3 print -- "usage: $0 <name-of-any-parent-directory>"
4 print -- " $0 <number-of-folders>"
5 return 1
6 } >&2
7 # example:
8 # $PWD == /home/arash/abc ==> $num_folders_we_are_in == 3
9 local num_folders_we_are_in=${#${(ps:/:)${PWD}}}
10 local dest="./"
11
12 # First try to find a folder with matching name (could potentially be a number)
13 # Get parents (in reverse order)
14 local parents
15 local i
16 for i in {$num_folders_we_are_in..2}
17 do
18 parents=($parents "$(echo $PWD | cut -d'/' -f$i)")
19 done
20 parents=($parents "/")
21 # Build dest and 'cd' to it
22 local parent
23 foreach parent (${parents})
24 do
25 dest+="../"
26 if [[ $1 == $parent ]]
27 then
28 cd $dest
29 return 0
30 fi
31 done
32
33 # If the user provided an integer, go up as many times as asked
34 dest="./"
35 if [[ "$1" = <-> ]]
36 then
37 if [[ $1 -gt $num_folders_we_are_in ]]
38 then
39 print -- "bd: Error: Can not go up $1 times (not enough parent directories)"
40 return 1
41 fi
42 for i in {1..$1}
43 do
44 dest+="../"
45 done
46 cd $dest
47 return 0
48 fi
49
50 # If the above methods fail
51 print -- "bd: Error: No parent directory named '$1'"
52 return 1
53}
54_bd () {
55 # Get parents (in reverse order)
56 local num_folders_we_are_in=${#${(ps:/:)${PWD}}}
57 local i
58 for i in {$num_folders_we_are_in..2}
59 do
60 reply=($reply "`echo $PWD | cut -d'/' -f$i`")
61 done
62 reply=($reply "/")
63}
64compctl -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 @@
4# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes 4# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
5ZSH_THEME="archcraft" 5ZSH_THEME="archcraft"
6 6
7
7# helper repo 8# helper repo
8export SETTING_DIR=$HOME/helper 9export SETTING_DIR=$HOME/helper
9source $SETTING_DIR/tools/init/load-settings.sh 10source $SETTING_DIR/tools/init/load-settings.sh
10fpath=($SETTING_DIR/zsh $fpath) 11fpath=($SETTING_DIR/zsh $fpath)
11 12
13
12# Options 14# Options
13setopt extended_glob 15setopt extended_glob
14setopt HIST_SAVE_NO_DUPS # Do not write a duplicate event to the history file. 16setopt HIST_SAVE_NO_DUPS # Do not write a duplicate event to the history file.
15 17
16_comp_options+=(globdots) # With hidden files 18
19# Edit Command Line
20autoload -Uz edit-command-line; zle -N edit-command-line
21bindkey -- "^X^E" edit-command-line
22
23
24# Enable completion
17autoload -Uz compinit; compinit 25autoload -Uz compinit; compinit
26_comp_options+=(globdots) # With hidden files
18zstyle ':completion:*' menu select 27zstyle ':completion:*' menu select
19zstyle ':completion::complete:*' gain-privileges 1 28zstyle ':completion::complete:*' gain-privileges 1
20 29
30
21# PS1 with git status at right 31# PS1 with git status at right
22autoload -Uz add-zsh-hook 32autoload -Uz add-zsh-hook
23add-zsh-hook precmd precmd 33add-zsh-hook precmd precmd
@@ -27,13 +37,14 @@ function precmd() {
27 RPROMPT="%B%K{red}%F{black}${RIGHT}%f%k%b" 37 RPROMPT="%B%K{red}%F{black}${RIGHT}%f%k%b"
28} 38}
29 39
40
30# Directory Stack 41# Directory Stack
31#setopt AUTO_PUSHD # Push the current directory visited on the stack. 42setopt AUTO_PUSHD # Push the current directory visited on the stack.
32#setopt PUSHD_IGNORE_DUPS # Do not store duplicates in the stack. 43setopt PUSHD_IGNORE_DUPS # Do not store duplicates in the stack.
33#setopt PUSHD_SILENT # Do not print the directory stack after pushd or popd. 44setopt PUSHD_SILENT # Do not print the directory stack after pushd or popd.
45alias d='dirs -v'
46for index ({1..9}) alias "$index"="cd +${index}"; unset index
34 47
35#alias d='dirs -v'
36#for index ({1..9}) alias "$index"="cd +${index}"; unset index
37 48
38# create a zkbd compatible hash; 49# create a zkbd compatible hash;
39# to add other keys to this hash, see: man 5 terminfo 50# to add other keys to this hash, see: man 5 terminfo
@@ -66,7 +77,6 @@ bindkey -- "\C-b" backward-char
66bindkey -- "\C-f" forward-char 77bindkey -- "\C-f" forward-char
67bindkey -- "\Eb" backward-word 78bindkey -- "\Eb" backward-word
68bindkey -- "\Ef" forward-word 79bindkey -- "\Ef" forward-word
69bindkey -- "\C-x-\C-e" edit-command-line
70bindkey -- "\C-v" quoted-insert 80bindkey -- "\C-v" quoted-insert
71bindkey -- "\E." insert-last-word 81bindkey -- "\E." insert-last-word
72bindkey -- "${key[PageUp]}" beginning-of-buffer-or-history 82bindkey -- "${key[PageUp]}" beginning-of-buffer-or-history
@@ -74,6 +84,15 @@ bindkey -- "${key[PageDown]}" end-of-buffer-or-history
74bindkey -- "${key[Shift-Tab]}" reverse-menu-complete 84bindkey -- "${key[Shift-Tab]}" reverse-menu-complete
75#bindkey -- "${key[Insert]}" overwrite-mode 85#bindkey -- "${key[Insert]}" overwrite-mode
76 86
87
88# Common shell options
89alias ls='ls --color'
90
91
92# Quick jump to parent folders
93source ~/helper/zsh/bd.zsh
94
95
77# Finally, make sure the terminal is in application mode, when zle is 96# Finally, make sure the terminal is in application mode, when zle is
78# active. Only then are the values from $terminfo valid. 97# active. Only then are the values from $terminfo valid.
79if (( ${+terminfo[smkx]} && ${+terminfo[rmkx]} )); then 98if (( ${+terminfo[smkx]} && ${+terminfo[rmkx]} )); then