shell 配置备份

  • 自行使用的 shell 配置备份

  • 资料来源:

    <>

  • 更新

    1
    2022.11.27 初始

导语

其实主要是 oh my zsh

  • 理论上可以直接配置 zsh,但挡不住 oh-my-zsh 的直接上手

zsh

1
2
sudo apt install zsh
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

主题/颜色

1
2
3
4
5
6
# vim ~/.zshrc

# 替换 ys 主题
SH_THEME="ys"
# add ; 文件夹 浅蓝底白字
export LS_COLORS=${LS_COLORS}:'di=01;37;44'

插件

  • 插件统一都在 ~/.oh-my-zsh/plugins
  • 配置在 .zshrc 添加 `plugins = (xx xx)

自带插件

  • git zsh 自带
  • z 根据历史记录,快速跳转目录
  • extract 再也不要 tar 一堆命令了.一个 x 全部搞定
  • docker docker-compose
1
2
# .zshrc
plugins=(git z extract docker docker-compose)

命令补全插件

1
2
3
4
5
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

git clone https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-completions
1
2
# .zshrc
plugins=(zsh-autosuggestions zsh-syntax-highlighting)
  • zsh-autosuggestions 命令提示 -> 键补全
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    # 添加以下内容 .zshrc 粘贴文字变慢
    # This speeds up pasting w/ autosuggest
    # https://github.com/zsh-users/zsh-autosuggestions/issues/238
    pasteinit() {
    OLD_SELF_INSERT=${${(s.:.)widgets[self-insert]}[2,3]}
    zle -N self-insert url-quote-magic # I wonder if you'd need `.url-quote-magic`?
    }

    pastefinish() {
    zle -N self-insert $OLD_SELF_INSERT
    }
    zstyle :bracketed-paste-magic paste-init pasteinit
    zstyle :bracketed-paste-magic paste-finish pastefinish
  • zsh-syntax-highlighting 命令高亮 红色/绿色
  • zsh-completions 一些 zsh 尚不支持的命令补全
    1
    2
    # 要在 source "$ZSH/oh-my-zsh.sh" 之前添加
    fpath+="${ZSH_CUSTOM:-"$ZSH/custom"}/plugins/zsh-completions/src"
  • irc-zsh 非常好用,有时又非常卡的插件. tab 补全
    1
    2
    # .zshrc
    source ~/.oh-my-zsh/plugins/incr/incr*.zsh

其他

incr 脚本 备份

  • 有一点修改: 当只有一个候选是不再自动填充
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Incremental completion for zsh
# by y.fujii <y-fujii at mimosa-pudica.net>, public domain


autoload -U compinit
zle -N self-insert self-insert-incr
zle -N vi-cmd-mode-incr
zle -N vi-backward-delete-char-incr
zle -N backward-delete-char-incr
zle -N expand-or-complete-prefix-incr
compinit

bindkey -M viins '^[' vi-cmd-mode-incr
bindkey -M viins '^h' vi-backward-delete-char-incr
bindkey -M viins '^?' vi-backward-delete-char-incr
bindkey -M viins '^i' expand-or-complete-prefix-incr
bindkey -M emacs '^h' backward-delete-char-incr
bindkey -M emacs '^?' backward-delete-char-incr
bindkey -M emacs '^i' expand-or-complete-prefix-incr

unsetopt automenu
compdef -d scp
compdef -d tar
compdef -d make
compdef -d java
compdef -d svn
compdef -d cvs

# TODO:
# cp dir/

now_predict=0

function limit-completion
{
if ((compstate[nmatches] <= 1)); then
zle -M ""
elif ((compstate[list_lines] > 6)); then
compstate[list]=""
zle -M "too many matches."
fi
}

function correct-prediction
{
if ((now_predict == 1)); then
if [[ "$BUFFER" != "$buffer_prd" ]] || ((CURSOR != cursor_org)); then
now_predict=0
fi
fi
}

function remove-prediction
{
if ((now_predict == 1)); then
BUFFER="$buffer_org"
now_predict=0
fi
}

function show-prediction
{
# assert(now_predict == 0)
if
((PENDING == 0)) &&
((CURSOR > 1)) &&
[[ "$PREBUFFER" == "" ]] &&
[[ "$BUFFER[CURSOR]" != " " ]]
then
cursor_org="$CURSOR"
buffer_org="$BUFFER"
comppostfuncs=(limit-completion)
zle complete-word
cursor_prd="$CURSOR"
buffer_prd="$BUFFER"
#if [[ "$buffer_org[1,cursor_org]" == "$buffer_prd[1,cursor_org]" ]]; then
# CURSOR="$cursor_org"
# if [[ "$buffer_org" != "$buffer_prd" ]] || ((cursor_org != cursor_prd)); then
# now_predict=1
# fi
#else
BUFFER="$buffer_org"
CURSOR="$cursor_org"
#fi
echo -n "\e[32m"
else
zle -M ""
fi
}

function preexec
{
echo -n "\e[39m"
}

function vi-cmd-mode-incr
{
correct-prediction
remove-prediction
zle vi-cmd-mode
}

function self-insert-incr
{
correct-prediction
remove-prediction
if zle .self-insert; then
show-prediction
fi
}

function vi-backward-delete-char-incr
{
correct-prediction
remove-prediction
if zle vi-backward-delete-char; then
show-prediction
fi
}

function backward-delete-char-incr
{
correct-prediction
remove-prediction
if zle backward-delete-char; then
show-prediction
fi
}

function expand-or-complete-prefix-incr
{
correct-prediction
if ((now_predict == 1)); then
CURSOR="$cursor_prd"
now_predict=0
comppostfuncs=(limit-completion)
zle list-choices
else
remove-prediction
zle expand-or-complete-prefix
fi
}