summaryrefslogtreecommitdiff
path: root/linux/.local/bin/lastfiles
diff options
context:
space:
mode:
Diffstat (limited to 'linux/.local/bin/lastfiles')
-rwxr-xr-xlinux/.local/bin/lastfiles83
1 files changed, 83 insertions, 0 deletions
diff --git a/linux/.local/bin/lastfiles b/linux/.local/bin/lastfiles
new file mode 100755
index 0000000..ba8c11a
--- /dev/null
+++ b/linux/.local/bin/lastfiles
@@ -0,0 +1,83 @@
+#!/bin/sh
+
+# Display help message
+usage() {
+ echo "Open the most recent file or the list of old files in fzf edited by vim."
+ echo ""
+ echo "Usage: ${0##*/} [OPTION]"
+ echo ""
+ echo "Options:"
+ echo " : Open the most recent old file in Vim."
+ echo " -h, --help : Show this help message."
+ echo " -l, --list : Show all recent files in Vim using fzf."
+ echo ""
+ echo "Examples:"
+ echo " ${0##*/} # Open the most recent file."
+ echo " ${0##*/} -l # Show all recent files in fzf and select to open."
+ exit 0
+}
+
+# List and handle oldfiles
+list_oldfiles() {
+ # Fetch the oldfiles list from Vim's viminfo (file-mark entries start with '> ')
+ viminfo="${VIMINFO:-${HOME}/.viminfo}"
+ if [ ! -f "$viminfo" ]; then
+ echo "No viminfo found at $viminfo" >&2
+ exit 1
+ fi
+
+ oldfiles=$(awk '/^> / {sub(/^> /,""); print}' "$viminfo" | sed "s|^~|$HOME|")
+
+ # Exit if no oldfiles are found
+ [ -z "$oldfiles" ] && {
+ echo "No recent files found in Vim." >&2
+ exit 1
+ }
+
+ case "$1" in
+ -h | --help)
+ usage
+ ;;
+ -l | --list)
+ # Filter valid files
+ valid_files=$(echo "$oldfiles" | while IFS= read -r file; do
+ [ -f "$file" ] && printf "%s\n" "$file"
+ done)
+
+ # Exit if no valid files exist
+ [ -z "$valid_files" ] && {
+ echo "No valid files found." >&2
+ exit 1
+ }
+
+ # Use fzf to select files
+ selected_files=$(echo "$valid_files" |
+ fzf \
+ --multi \
+ --preview 'bat -n --color=always --line-range=:500 {} 2>/dev/null || echo "Error previewing file"' \
+ --height=70% \
+ --reverse)
+
+ # Exit if no files were selected
+ [ -z "$selected_files" ] && exit 1
+
+ # Open selected files in Vim
+ openfiles "$selected_files"
+ ;;
+ *)
+ # Open the most recent file
+ for file in $oldfiles; do
+ if [ -f "$file" ]; then
+ openfiles "$file"
+ exit 0
+ fi
+ done
+
+ echo "No valid recent files found." >&2
+ exit 1
+ ;;
+ esac
+}
+
+# Parse command-line arguments
+list_oldfiles "$@"