-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzapmarks.plugin.zsh
60 lines (49 loc) · 1.79 KB
/
zapmarks.plugin.zsh
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
# ZapMarks - Quick command bookmarks for Zsh
# File: zapmarks.plugin.zsh
# Author: [Iliuta Adrian](https://github.com/iliutaadrian)
ZAPMARKS_FILE="$HOME/.config/zapmarks.json"
zapmarks() {
local selected_command
# Check if jq is installed
if ! command -v jq &> /dev/null; then
echo "jq is not installed. Please install it to use ZapMarks."
return 1
fi
# Create the bookmarks file if it doesn't exist
[ ! -f "$ZAPMARKS_FILE" ] && echo '[]' > "$ZAPMARKS_FILE"
# Use fzf to select a command from the bookmarks file
selected_command=$(jq -r '.[] | "\(.command)\t\(.description)"' "$ZAPMARKS_FILE" | \
fzf --height 40% \
--reverse \
--preview 'echo -e "Command:\n{1}\n\nDescription:\n{2}"' \
--preview-window right:50%:wrap \
--bind 'ctrl-/:change-preview-window(down|hidden|)' \
--delimiter '\t' \
--with-nth 1)
if [ -n "$selected_command" ]; then
BUFFER=$(echo "$selected_command" | cut -f1)
CURSOR=$#BUFFER
zle redisplay
fi
}
# Create a widget from the function
zle -N zapmarks
# Bind the widget to Ctrl+B
bindkey '^B' zapmarks
# Function to add a new bookmark
zapmarks-add-f() {
local command description
echo "Enter the command to bookmark:"
read -r command
echo "Enter a short description:"
read -r description
jq --arg cmd "$command" --arg desc "$description" '. += [{"command": $cmd, "description": $desc}]' "$ZAPMARKS_FILE" > "$ZAPMARKS_FILE.tmp" && mv "$ZAPMARKS_FILE.tmp" "$ZAPMARKS_FILE"
echo "Bookmark added successfully!"
}
# Function to edit bookmarks file
zapmarks-edit-f() {
${EDITOR:-lvim} "$ZAPMARKS_FILE"
}
# Make functions available as commands
alias zapmarks-add='zapmarks-add-f'
alias zapmarks-edit='zapmarks-edit-f'