diff options
author | Hsieh Chin Fan <typebrook@topo.tw> | 2023-01-26 22:34:24 +0800 |
---|---|---|
committer | Hsieh Chin Fan <typebrook@topo.tw> | 2023-01-26 22:34:24 +0800 |
commit | e2a2009b2f5f0e84a8971184d3d1832ba856c194 (patch) | |
tree | 36634fea57d5ce07c31b31207c31bfaa22f1f81d | |
parent | ff0b7fe2d22f7ad93f838bc1fac96642f654dcec (diff) |
Update
-rw-r--r-- | zsh/bd.zsh | 64 | ||||
-rw-r--r-- | zsh/zshrc | 33 |
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 @@ | |||
1 | bd () { | ||
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 | } | ||
64 | compctl -V directories -K _bd bd | ||
@@ -4,20 +4,30 @@ | |||
4 | # See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes | 4 | # See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes |
5 | ZSH_THEME="archcraft" | 5 | ZSH_THEME="archcraft" |
6 | 6 | ||
7 | |||
7 | # helper repo | 8 | # helper repo |
8 | export SETTING_DIR=$HOME/helper | 9 | export SETTING_DIR=$HOME/helper |
9 | source $SETTING_DIR/tools/init/load-settings.sh | 10 | source $SETTING_DIR/tools/init/load-settings.sh |
10 | fpath=($SETTING_DIR/zsh $fpath) | 11 | fpath=($SETTING_DIR/zsh $fpath) |
11 | 12 | ||
13 | |||
12 | # Options | 14 | # Options |
13 | setopt extended_glob | 15 | setopt extended_glob |
14 | setopt HIST_SAVE_NO_DUPS # Do not write a duplicate event to the history file. | 16 | setopt 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 | ||
20 | autoload -Uz edit-command-line; zle -N edit-command-line | ||
21 | bindkey -- "^X^E" edit-command-line | ||
22 | |||
23 | |||
24 | # Enable completion | ||
17 | autoload -Uz compinit; compinit | 25 | autoload -Uz compinit; compinit |
26 | _comp_options+=(globdots) # With hidden files | ||
18 | zstyle ':completion:*' menu select | 27 | zstyle ':completion:*' menu select |
19 | zstyle ':completion::complete:*' gain-privileges 1 | 28 | zstyle ':completion::complete:*' gain-privileges 1 |
20 | 29 | ||
30 | |||
21 | # PS1 with git status at right | 31 | # PS1 with git status at right |
22 | autoload -Uz add-zsh-hook | 32 | autoload -Uz add-zsh-hook |
23 | add-zsh-hook precmd precmd | 33 | add-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. | 42 | setopt AUTO_PUSHD # Push the current directory visited on the stack. |
32 | #setopt PUSHD_IGNORE_DUPS # Do not store duplicates in the stack. | 43 | setopt PUSHD_IGNORE_DUPS # Do not store duplicates in the stack. |
33 | #setopt PUSHD_SILENT # Do not print the directory stack after pushd or popd. | 44 | setopt PUSHD_SILENT # Do not print the directory stack after pushd or popd. |
45 | alias d='dirs -v' | ||
46 | for 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 | |||
66 | bindkey -- "\C-f" forward-char | 77 | bindkey -- "\C-f" forward-char |
67 | bindkey -- "\Eb" backward-word | 78 | bindkey -- "\Eb" backward-word |
68 | bindkey -- "\Ef" forward-word | 79 | bindkey -- "\Ef" forward-word |
69 | bindkey -- "\C-x-\C-e" edit-command-line | ||
70 | bindkey -- "\C-v" quoted-insert | 80 | bindkey -- "\C-v" quoted-insert |
71 | bindkey -- "\E." insert-last-word | 81 | bindkey -- "\E." insert-last-word |
72 | bindkey -- "${key[PageUp]}" beginning-of-buffer-or-history | 82 | bindkey -- "${key[PageUp]}" beginning-of-buffer-or-history |
@@ -74,6 +84,15 @@ bindkey -- "${key[PageDown]}" end-of-buffer-or-history | |||
74 | bindkey -- "${key[Shift-Tab]}" reverse-menu-complete | 84 | bindkey -- "${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 | ||
89 | alias ls='ls --color' | ||
90 | |||
91 | |||
92 | # Quick jump to parent folders | ||
93 | source ~/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. |
79 | if (( ${+terminfo[smkx]} && ${+terminfo[rmkx]} )); then | 98 | if (( ${+terminfo[smkx]} && ${+terminfo[rmkx]} )); then |