加入 .bashrc 並調整chezmoi架構把共通內容搬出來

This commit is contained in:
Yuan Chiu 2025-05-02 10:11:11 +08:00
parent ee6816e276
commit 42f0328079
4 changed files with 172 additions and 25 deletions

View File

@ -1,5 +1,7 @@
{{/* 以下是套用到 ~/.zshrc ~/.bashrc 用的共通內容 */}}
{{- /* ---- 設定環境變數 -------------------------------------------------- */ -}}
{{/* ---- 設定環境變數 ---------------------------------------------------- */}}
{{- /* 設定預設編輯器 */ -}} {{- /* 設定預設編輯器 */ -}}
{{ if eq .chezmoi.os "linux" }} {{ if eq .chezmoi.os "linux" }}
@ -12,10 +14,24 @@ export EDITOR=nvim
export VISUAL=nvim export VISUAL=nvim
{{- /* ---- 設定ailas ---------------------------------------------------- */ -}} {{/* ---- 設定ailas ---------------------------------------------------- */}}
# alias
## Alias section
alias cp="cp -i" # Confirm before overwriting something
alias df='df -h' # Human-readable sizes
alias free='free -m' # Show sizes in MB
alias gitu='git add . && git commit && git push' alias gitu='git add . && git commit && git push'
{{/* ---- 擴充功能 ---------------------------------------------------- */}}
# 圖片顯示
# 既然即使加入判斷後還是「進入函數定義段落且噴錯」那就代表zsh 在解析 .zshrc 時,在到達 if 邏輯前,就已經知道 imgcat 是 alias並因此直接報錯這是因為
# ⚠️ 在 zsh 中,如果你寫 function_name() 這種形式,然後這個名稱之前被定義為 alias會在 parse 階段直接錯誤,不會等到 if 判斷。
# 解決方式:延遲定義(用 eval 避開 parser
# 你需要避免 zsh 在 parse 階段就看到 imgcat() 的寫法。可以這樣寫,透過 eval 延遲定義函數內容:
if ! alias imgcat &>/dev/null && ! functions imgcat &>/dev/null && command -v magick &>/dev/null; then
eval '
imgcat() {
if [[ -z "$1" ]]; then
echo "❌ 請指定圖片檔案例如imgcat ~/Pictures/foo.png"
return 1
fi
magick "$1" -geometry 800x480 sixel:-
}'
fi

View File

@ -195,6 +195,7 @@ chezmoi apply ~/.zshrc
將單一檔案轉換成tmpl範本 將單一檔案轉換成tmpl範本
``` ```
chezmoi add --template ~/.bashrc
chezmoi chattr +template ~/.config/iterm2/com.googlecode.iterm2.plist chezmoi chattr +template ~/.config/iterm2/com.googlecode.iterm2.plist
``` ```

142
dot_bashrc.tmpl Normal file
View File

@ -0,0 +1,142 @@
{{- /* ----以下為Manjaro預設內容 ------------------------------------------ */ -}}
#
# ~/.bashrc
#
[[ $- != *i* ]] && return
colors() {
local fgc bgc vals seq0
printf "Color escapes are %s\n" '\e[${value};...;${value}m'
printf "Values 30..37 are \e[33mforeground colors\e[m\n"
printf "Values 40..47 are \e[43mbackground colors\e[m\n"
printf "Value 1 gives a \e[1mbold-faced look\e[m\n\n"
# foreground colors
for fgc in {30..37}; do
# background colors
for bgc in {40..47}; do
fgc=${fgc#37} # white
bgc=${bgc#40} # black
vals="${fgc:+$fgc;}${bgc}"
vals=${vals%%;}
seq0="${vals:+\e[${vals}m}"
printf " %-9s" "${seq0:-(default)}"
printf " ${seq0}TEXT\e[m"
printf " \e[${vals:+${vals+$vals;}}1mBOLD\e[m"
done
echo; echo
done
}
[ -r /usr/share/bash-completion/bash_completion ] && . /usr/share/bash-completion/bash_completion
# Change the window title of X terminals
case ${TERM} in
xterm*|rxvt*|Eterm*|aterm|kterm|gnome*|interix|konsole*)
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/\~}\007"'
;;
screen*)
PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/\~}\033\\"'
;;
esac
use_color=true
# Set colorful PS1 only on colorful terminals.
# dircolors --print-database uses its own built-in database
# instead of using /etc/DIR_COLORS. Try to use the external file
# first to take advantage of user additions. Use internal bash
# globbing instead of external grep binary.
safe_term=${TERM//[^[:alnum:]]/?} # sanitize TERM
match_lhs=""
[[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
[[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
[[ -z ${match_lhs} ]] \
&& type -P dircolors >/dev/null \
&& match_lhs=$(dircolors --print-database)
[[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color=true
if ${use_color} ; then
# Enable colors for ls, etc. Prefer ~/.dir_colors #64489
if type -P dircolors >/dev/null ; then
if [[ -f ~/.dir_colors ]] ; then
eval $(dircolors -b ~/.dir_colors)
elif [[ -f /etc/DIR_COLORS ]] ; then
eval $(dircolors -b /etc/DIR_COLORS)
fi
fi
if [[ ${EUID} == 0 ]] ; then
PS1='\[\033[01;31m\][\h\[\033[01;36m\] \W\[\033[01;31m\]]\$\[\033[00m\] '
else
PS1='\[\033[01;32m\][\u@\h\[\033[01;37m\] \W\[\033[01;32m\]]\$\[\033[00m\] '
fi
alias ls='ls --color=auto'
alias grep='grep --colour=auto'
alias egrep='egrep --colour=auto'
alias fgrep='fgrep --colour=auto'
else
if [[ ${EUID} == 0 ]] ; then
# show root@ when we don't have colors
PS1='\u@\h \W \$ '
else
PS1='\u@\h \w \$ '
fi
fi
unset use_color safe_term match_lhs sh
alias cp="cp -i" # confirm before overwriting something
alias df='df -h' # human-readable sizes
alias free='free -m' # show sizes in MB
alias np='nano -w PKGBUILD'
alias more=less
xhost +local:root > /dev/null 2>&1
complete -cf sudo
# Bash won't get SIGWINCH if another process is in the foreground.
# Enable checkwinsize so that bash will check the terminal size when
# it regains control. #65623
# http://cnswww.cns.cwru.edu/~chet/bash/FAQ (E11)
shopt -s checkwinsize
shopt -s expand_aliases
# export QT_SELECT=4
# Enable history appending instead of overwriting. #139609
shopt -s histappend
#
# # ex - archive extractor
# # usage: ex <file>
ex ()
{
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via ex()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
{{ template "sh_common.tmpl" . }}

View File

@ -123,7 +123,10 @@ zstyle ':completion:*:directory-stack' list-colors '=(#b) #([0-9]#)*( *)==95=38;
# highlight case in select # highlight case in select
zstyle ':completion:*' menu select zstyle ':completion:*' menu select
{{ template "sh_common.tmpl" . }} ## Alias section
alias cp="cp -i" # Confirm before overwriting something
alias df='df -h' # Human-readable sizes
alias free='free -m' # Show sizes in MB
# overwrite ls # overwrite ls
if command -v colorls &> /dev/null; then if command -v colorls &> /dev/null; then
@ -169,23 +172,6 @@ export PATH="$PATH:$HOME/go/bin"
# iterm2 # iterm2
test -e "${HOME}/.iterm2_shell_integration.zsh" && source "${HOME}/.iterm2_shell_integration.zsh" test -e "${HOME}/.iterm2_shell_integration.zsh" && source "${HOME}/.iterm2_shell_integration.zsh"
# 圖片顯示
# 既然即使加入判斷後還是「進入函數定義段落且噴錯」那就代表zsh 在解析 .zshrc 時,在到達 if 邏輯前,就已經知道 imgcat 是 alias並因此直接報錯這是因為
# ⚠️ 在 zsh 中,如果你寫 function_name() 這種形式,然後這個名稱之前被定義為 alias會在 parse 階段直接錯誤,不會等到 if 判斷。
# 解決方式:延遲定義(用 eval 避開 parser
# 你需要避免 zsh 在 parse 階段就看到 imgcat() 的寫法。可以這樣寫,透過 eval 延遲定義函數內容:
if ! alias imgcat &>/dev/null && ! functions imgcat &>/dev/null && command -v magick &>/dev/null; then
eval '
imgcat() {
if [[ -z "$1" ]]; then
echo "❌ 請指定圖片檔案例如imgcat ~/Pictures/foo.png"
return 1
fi
magick "$1" -geometry 800x480 sixel:-
}'
fi
zinit light mass8326/zsh-chezmoi zinit light mass8326/zsh-chezmoi
# 自動補上 noglob 並禁用 Zsh 的 globbing 解析 # 自動補上 noglob 並禁用 Zsh 的 globbing 解析
@ -201,3 +187,5 @@ function git_pushmulti() {
# 執行 git pushmulti # 執行 git pushmulti
noglob git pushmulti "$remotes" "$branches" noglob git pushmulti "$remotes" "$branches"
} }
{{ template "sh_common.tmpl" . }}