-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path.bashrc
More file actions
127 lines (114 loc) 路 3.34 KB
/
Copy path.bashrc
File metadata and controls
127 lines (114 loc) 路 3.34 KB
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
# ~/.bashrc: executed by bash(1) for non-login shells.
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# History settings
export HISTCONTROL=ignoreboth:erasedups
export HISTIGNORE="ls:cd:exit:history*"
export HISTSIZE=1000
export HISTFILESIZE=2000
shopt -s histappend
# Check window size after each command
shopt -s checkwinsize
# Git branch function
git_branch() {
local branch
branch=$(git branch --show-current 2>/dev/null)
if [[ -n "$branch" ]]; then
# Purple color for git branch
echo -e "\[\e[35m\]$branch\[\e[0m\] "
fi
}
# Color prompt setup
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
set_prompt() {
local branch=$(git_branch)
local title="\[\e]0;\u@\h: \w\a\]"
PS1="${title}[\[\e[32m\]\A\[\e[0m\]][\[\e[34m\]\h\[\e[0m\]][${branch}\[\e[33m\]\w\[\e[0m\]]\$ "
}
prompt_command() {
history -a # Append current session history to file
history -n # Read new history entries from file
set_prompt # Set the prompt dynamically
}
PROMPT_COMMAND=prompt_command
else
PS1='[\A][\h][\w]\$ '
fi
# Enable programmable completion
if ! shopt -oq posix; then
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
fi
# Load aliases
[[ -f ~/.config/aliasrc ]] && . ~/.config/aliasrc
# Optimized FZF key-bindings discovery loop
for fzf_script in \
"/usr/share/doc/fzf/examples/key-bindings.bash" \
"$HOME/.fzf.bash" \
"/usr/share/fzf/key-bindings.bash" \
"/usr/share/fzf/shell/key-bindings.bash"; do
if [[ -f "$fzf_script" ]]; then
source "$fzf_script"
break
fi
done
# Keep bash history clean (Optimized: Now running strictly on exit)
cleanup-history() {
local histfile="$HOME/.bash_history"
[[ -r "$histfile" ]] || return
awk '
{
cleaned = $0
gsub(/[[:space:]]*$/, "", cleaned)
if (cleaned != "") {
last[cleaned] = NR
line[NR] = cleaned
}
}
END {
for (i = 1; i <= NR; i++) {
if (i in line && last[line[i]] == i) {
print line[i]
}
}
}
' "$histfile" > "${histfile}.tmp"
if [[ -s "${histfile}.tmp" ]]; then
command mv -f "${histfile}.tmp" "$histfile" >/dev/null 2>&1
else
rm -f "${histfile}.tmp" >/dev/null 2>&1
fi
history -c
history -r
}
# Trap terminal exits to clean the history file once per session
trap cleanup-history EXIT
# Enhanced history search (CTRL-R)
__fzf_history__() {
history -a
history -c
history -r
local line
line=$(history | fzf --height 100% --tac --tiebreak=index --no-sort --exact \
--bind 'ctrl-d:page-down,ctrl-u:page-up' | sed 's/^ *[0-9]* *//')
if [[ -n "$line" ]]; then
READLINE_LINE=$line
READLINE_POINT=${#line}
fi
}
# Bind CTRL-R to the history search function in interactive shells
if [[ $- == *i* ]]; then
bind -x '"\C-r": __fzf_history__'
fi
# FZF preview with bat if available, otherwise cat
if command -v bat >/dev/null 2>&1; then
export FZF_CTRL_T_OPTS="--preview 'bat --color=always --line-range :500 {}'"
else
export FZF_CTRL_T_OPTS="--preview 'cat {}'"
fi