-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvimtmp.sh
More file actions
executable file
·63 lines (49 loc) · 1.54 KB
/
vimtmp.sh
File metadata and controls
executable file
·63 lines (49 loc) · 1.54 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
#!/usr/bin/env bash
set -euo pipefail
usage() {
local cmd
cmd=$(basename "$0")
cat << EOF
Usage: $cmd [OPTIONS]
Create a temporary scratch file and open it in your default editor.
Creates a temporary file in the system's temporary directory and opens it in
your preferred text editor (defined by the EDITOR environment variable). The
file persists after the editor closes, allowing you to reference it later in
the same session. This is useful for quick notes, temporary calculations, or
drafting text without cluttering your workspace with permanent files.
OPTIONS:
-h, --help Show this help message and exit
PREREQUISITES:
- EDITOR environment variable must be set
EXAMPLES:
$cmd Create and edit a scratch file
$cmd --help Show this help
EXIT CODES:
0 Scratch file created and editor launched successfully
1 EDITOR not set or other error occurred
EOF
}
if [[ "${1:-}" == "-h" ]] || [[ "${1:-}" == "--help" ]]; then
usage
exit 0
fi
check_dependencies() {
if [[ -z "${EDITOR:-}" ]]; then
echo "Error: EDITOR environment variable is not set"
echo "Set it to your preferred editor, for example:"
echo " export EDITOR=vim"
echo " export EDITOR=nvim"
echo " export EDITOR=nano"
echo " export EDITOR=code"
exit 1
fi
}
main() {
check_dependencies
local scratch_file
scratch_file=$(mktemp)
echo "Opening scratch file: $scratch_file"
# Launch editor with the scratch file
$EDITOR "$scratch_file"
}
main "$@"