想维护一套自己的 dotfiles 顺便练练 Shell 编程,有没有值得参考的仓库推荐一下

218 天前
 enchilada2020

Linux 五年使用经验的骨灰级入门选手,之前都是现用现查,查完就忘,主打一个能干活就行。最近经常需要连到服务器上查各种日志和配置之类,但往往服务器默认的 Shell 环境太裸了,非常难用,所以痛下决心想把这块搞定。

主要是想要能在主流 Linux 环境下快速配置,做到任何环境都有同样的命令行体验,不追求过度花里胡哨,保证最小可用性与生产力即可。

因为要在保证体验统一的前提下尽量兼顾各种可能的情况,比如一些限制比较严格的生产环境之类的,遵循奥卡姆剃刀原则,如无必要勿增实体,所以优先选择那种主流服务器发行版都一定会带的工具,相比 Zsh 会选择 Bash ,相比 Emacs 会选择 Vim 等等。

我知道有很多现成的优秀项目,比如 Oh My Zsh/starship ,但感觉这些大多是面向个人本地开发环境的,不适用于远程到生产环境的服务器做调查的场景。毕竟你总不可能为了查个日志在生产环境上搞了一大坨有的没的。

简单看过几个 GithHub 上比较有名的个人 dotfiles 仓库,例如 holmanMathias的,一个是面向 Mac ,一个用了 Zsh ,都不太理想。目前感觉 这个最合心意,但不太确定里面的写法是否符合最佳实践,比如其中的几个 util:

answer_is_yes() {
    [[ "$REPLY" =~ ^[Yy]$ ]] \
        && return 0 \
        || return 1
}

ask() {
    print_question "$1"
    read -r
}

ask_for_confirmation() {
    print_question "$1 (y/n) "
    read -r -n 1
    printf "\n"
}

...感觉 askask_for_confirmation 完全简化合并成一个函数的。

2414 次点击
所在节点    Linux
21 条回复
rayae
217 天前
分享一下我的:
# If not running interactively, don't do anything
[ -z "$PS1" ] && return

MAIN_DEVICE=harvey

if test -f $HOME/.config/.dotfiles_main_device; then
MAIN_DEVICE=$HOSTNAME
fi

prompt_command() {
local last_ret=$?
history -a

# terminal tab title
local cwd="$PWD"
local title="${cwd/#$HOME/\~}"

if [ "$MAIN_DEVICE"x = "$HOSTNAME"x ] ; then
echo -n -e "\\e]0;$title\\007"
local HOST_FLAG=""
else
echo -n -e "\\e]0;$HOSTNAME > $title\\007"
local HOST_FLAG="${COLOR_YELLOW}\h${COLOR_NOCOLOR} "
fi


export COLOR_NOCOLOR='\[\e[0m\]'
export COLOR_RED='\[\e[0;31m\]'
export COLOR_GREEN='\[\e[0;32m\]'
export COLOR_ORANGE='\[\e[0;33m\]'
export COLOR_BLUE='\[\e[0;34m\]'
export COLOR_PURPLE='\[\e[0;35m\]'
export COLOR_CYAN='\[\e[0;36m\]'
export COLOR_WHITE='\[\e[0;37m\]'
export COLOR_YELLOW='\[\e[0;33m\]'
export COLOR_GRAY='\[\e[0;30m\]'
export COLOR_LIGHT_WHITE='\[\e[1;37m\]'
export COLOR_LIGHT_GRAY='\[\e[0;37m\]'
export COLOR_LIGHT_RED='\[\e[1;31m\]'
export COLOR_LIGHT_GREEN='\[\e[1;32m\]'
export COLOR_LIGHT_BLUE='\[\e[1;34m\]'
export COLOR_LIGHT_PURPLE='\[\e[1;35m\]'
export COLOR_LIGHT_CYAN='\[\e[1;36m\]'
export COLOR_LIGHT_YELLOW='\[\e[1;33m\]'
export COLOR_LIGHT_GRAY='\[\e[1;30m\]'

# PS1
case "$TERM" in
xterm*|gnome*|konsole*|screen*) color_prompt=yes;;
*) color_prompt=no;;
esac

if [ "$color_prompt" == no ]; then
for name in $(env|grep ^COLOR_|cut -d= -f1);do
unset ${name}
done
fi

if [ $UID = 0 ] ; then
local USER_FLAG="#"
else
local USER_FLAG="$"
fi

local SSH_IP=`echo $SSH_CLIENT | awk '{ print $1 }'`
local SSH2_IP=`echo $SSH2_CLIENT | awk '{ print $1 }'`
if [ $SSH2_IP ] || [ $SSH_IP ] ; then
local HOST_FLAG="${COLOR_RESET}${COLOR_LIGHT_YELLOW}\h${COLOR_NOCOLOR} "
fi

if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
local DEBIAN_CHROOT=$(cat /etc/debian_chroot)
fi

local GIT_BRANCH=''
if command -v git >/dev/null ; then
local GIT_BRANCH='$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e "s#* \(.*\)# ($(git config user.name) @ \1)#")'
fi

# last exit code
PS1=''
# chroot
PS1="${PS1}${COLOR_RESET}${DEBIAN_CHROOT}"
# show hostname if current not main device
PS1="${PS1}${HOST_FLAG}"
# current directory
PS1="${PS1}${COLOR_LIGHT_GRAY}[${COLOR_LIGHT_GREEN}\w${COLOR_NOCOLOR}${COLOR_LIGHT_GRAY}]"
# git repository info
PS1="${PS1}${COLOR_PURPLE}${GIT_BRANCH}${COLOR_NOCOLOR}"
# color by exit code
if [ $last_ret == 0 ] ; then
PS1="${PS1}${COLOR_LIGHT_WHITE} ${USER_FLAG}"
else
PS1="${PS1}${COLOR_LIGHT_RED} ($last_ret)${USER_FLAG}"
fi
# root/user flag
PS1="${PS1}${COLOR_NOCOLOR} "

export PS1
export PS2='> '
export PS3='#? '
export PS4='+'

for name in $(env|grep ^COLOR_|cut -d= -f1);do
unset ${name}
done
}

export HISTFILESIZE=10000
export HISTSIZE=10000
export HISTCONTROL=erasedups:ignoredups:ignorespace # Don't put duplicate lines in the history and do not add lines that start with a space
export HISTIGNORE="pwd:l:la:[bf]g:exit" # ignore such commands
export HISTTIMEFORMAT='%Y%m%d-%H:%M:%S '
export PROMPT_COMMAND='prompt_command'
export PROMPT_DIRTRIM=3


shopt -s cmdhist # combine multiline commands into single history
shopt -s histappend # append history
shopt -s checkwinsize # re-update LINEs and COLUMNs
shopt -s autocd
shopt -s extglob
shopt -s dotglob
shopt -s cdspell
shopt -s dirspell
shopt -s progcomp
shopt -s no_empty_cmd_completion

set -o noclobber

stty -ixon # replace Ctrl-S for forward-search-history


export EDITOR='vim'


# Use bash-completion, if available
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi


bind "set completion-ignore-case on" 2>/dev/null
bind "set bell-style none" 2>/dev/null
bind "set show-all-if-ambiguous on" 2>/dev/null
bind "set show-all-if-unmodified on" 2>/dev/null
# search history with arrow
bind '"\e[A": history-search-backward' 2>/dev/null
bind '"\e[B": history-search-forward' 2>/dev/null



# alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias cd..="cd .."


if command -v dircolors > /dev/null 2>&1; then
source <(dircolors)
fi

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://tanronggui.xyz/t/1055064

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX