Skip to content

Commit 485809e

Browse files
A Dynamic Collection of Shell Scripts with Educational Purpose
1 parent f14515b commit 485809e

4 files changed

Lines changed: 552 additions & 158 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Complete var without echo or print
2+
_complete_env_var_bind() {
3+
local line="$READLINE_LINE"
4+
local point="$READLINE_POINT"
5+
local before="${line:0:$point}"
6+
local _ENV_COLOR_HIGHLIGHT='\e[1;35m'
7+
local _ENV_COLOR_RESET='\e[0m'
8+
9+
# Extract the word before the cursor
10+
local current_word="${before##*[^a-zA-Z0-9_]}"
11+
local word_start=$(( point - ${#current_word} ))
12+
local after="${line:$point}"
13+
14+
# If it's a new word, rebuild
15+
if [[ -z "$_ENV_ORIGINAL_WORD" ]] || [[ "$current_word" != "$_ENV_ORIGINAL_WORD"* && "$_ENV_ORIGINAL_WORD" != "$current_word"* ]]; then
16+
_ENV_ORIGINAL_WORD="$current_word"
17+
_ENV_MATCHES=()
18+
_ENV_INDEX=-1
19+
20+
# Search for matches
21+
while IFS= read -r var; do
22+
_ENV_MATCHES+=("$var")
23+
done < <(compgen -v -- "$current_word")
24+
25+
# Case-insensitive fallback
26+
if [[ ${#_ENV_MATCHES[@]} -eq 0 && -n "$current_word" ]]; then
27+
while IFS= read -r var; do
28+
if [[ "${var,,}" == "${current_word,,}"* ]]; then
29+
_ENV_MATCHES+=("$var")
30+
fi
31+
done < <(compgen -v)
32+
fi
33+
34+
if [[ ${#_ENV_MATCHES[@]} -eq 0 ]]; then
35+
_ENV_ORIGINAL_WORD=""
36+
return 1
37+
fi
38+
39+
# Show options with colored highlighting
40+
echo
41+
local match
42+
for match in "${_ENV_MATCHES[@]}"; do
43+
# Separate the matching part from the rest
44+
local prefix="${match:0:${#_ENV_ORIGINAL_WORD}}"
45+
local suffix="${match:${#_ENV_ORIGINAL_WORD}}"
46+
47+
# If case-insensitive, highlight the matching part
48+
if [[ "${match,,}" == "${_ENV_ORIGINAL_WORD,,}"* ]]; then
49+
# Get the actual prefix (preserving original case)
50+
printf "${_ENV_COLOR_HIGHLIGHT}%s${_ENV_COLOR_RESET}%s " "$prefix" "$suffix"
51+
else
52+
printf '%s ' "$match"
53+
fi
54+
done
55+
echo
56+
fi
57+
58+
# Advance to the next match (cycles through)
59+
_ENV_INDEX=$(( (_ENV_INDEX + 1) % ${#_ENV_MATCHES[@]} ))
60+
61+
# Apply the match
62+
local selected="${_ENV_MATCHES[$_ENV_INDEX]}"
63+
READLINE_LINE="${line:0:$word_start}${selected}${after}"
64+
READLINE_POINT=$(( word_start + ${#selected} ))
65+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
_remove_timestamp_in_single_line_format_from_bash_history() {
2+
[[ ! -f "$HISTFILE" ]] && touch "$HISTFILE"
3+
[[ -f "$HISTFILE" ]] && sed -i 's/^:[[:space:]][0-9]\{10\}:0;//g' "$HISTFILE" >/dev/null 2>&1
4+
return 0
5+
}
6+
7+
# https://www.baeldung.com/linux/history-remove-avoid-duplicates
8+
remove_duplicate_commands_from_bash_history() {
9+
[[ ! -f "$HISTFILE" ]] && touch "$HISTFILE"
10+
local history_file="${HISTFILE}"
11+
local temp_file="${HISTFILE}.tmp"
12+
[[ -f "${temp_file}" ]] && rm -f "${temp_file}"
13+
14+
# Filter duplicate commads
15+
awk '!a[$0]++' "$history_file" > "$temp_file"
16+
17+
# Replace history
18+
mv -f "$temp_file" "$history_file" >/dev/null 2>&1
19+
return 0
20+
}
21+
22+
_remove_unused_timestamps_from_bash_history() {
23+
[[ ! -f "$HISTFILE" ]] && touch "$HISTFILE"
24+
local history_file="${HISTFILE}"
25+
local temp_file="${HISTFILE}.tmp"
26+
[[ -f "${temp_file}" ]] && rm -f "${temp_file}"
27+
28+
# Filter history lines with timestamps and associated commands
29+
awk '/^#[0-9]+$/ { if (getline c && c !~ /^#[0-9]+$/) { print $0 ORS c } } !/^#[0-9]+$/ { print }' "$history_file" > "$temp_file"
30+
31+
# Replace history
32+
mv -f "$temp_file" "$history_file" >/dev/null 2>&1
33+
return 0
34+
}
35+
36+
_include_timestamp_in_bash_history() {
37+
[[ ! -f "$HISTFILE" ]] && touch "$HISTFILE"
38+
local history_file="${HISTFILE}"
39+
local temp_file="${HISTFILE}.tmp"
40+
[[ -f "${temp_file}" ]] && rm -f "${temp_file}"
41+
42+
# Filter history lines with timestamps and associated commands
43+
awk '/^#[0-9]+$/ { if (getline c && c !~ /^#[0-9]+$/) { print $0 ORS c } } !/^#[0-9]+$/ { print strftime("#%s") ORS $0 }' "$history_file" > "$temp_file"
44+
45+
# Replace history
46+
mv -f "$temp_file" "$history_file" >/dev/null 2>&1
47+
return 0
48+
}

0 commit comments

Comments
 (0)