From a4937816c2bb7b1ff77eca011ba92112a0818f1a Mon Sep 17 00:00:00 2001 From: Vladislav Doster Date: Sat, 14 Sep 2024 12:28:07 -0500 Subject: [PATCH] feat(zsh): `v` func redirects to available editor Fixes issue where `v` used invalid/moved/deleted editor since `v` alias was set at start of zsh session. Signed-off-by: Vladislav Doster --- zsh/.config/zsh/completions/_v | 7 ++++++ zsh/.config/zsh/functions/v | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 zsh/.config/zsh/completions/_v create mode 100644 zsh/.config/zsh/functions/v diff --git a/zsh/.config/zsh/completions/_v b/zsh/.config/zsh/completions/_v new file mode 100644 index 00000000..dff24efe --- /dev/null +++ b/zsh/.config/zsh/completions/_v @@ -0,0 +1,7 @@ +#compdef v +# vim: ft=zsh sw=2 ts=2 et + +_arguments \ + '(-d --debug)'{-d,--debug}'[Enable debug mode]' \ + '(-h --help)'{-h,--help}'[Show list of command-line options]' \ + "*:files:_files" diff --git a/zsh/.config/zsh/functions/v b/zsh/.config/zsh/functions/v new file mode 100644 index 00000000..ac3c03fe --- /dev/null +++ b/zsh/.config/zsh/functions/v @@ -0,0 +1,40 @@ +#autoload +# vim: ft=zsh sw=2 ts=2 et + +typeset -gx EDITOR='v' + +v() { + emulate -L zsh + + zmodload zsh/zutil || return + + setopt typeset_silent + local debug editor help nvim_found + + zparseopts -D -F -K -F -- \ + {d,-debug}=debug \ + {h,-help}=help \ + || return 1 + + if (( ${#debug} )); then + setopt xtrace + fi + + if (( ${#help} )); then + print "Usage: $0 [options] [files...]" + print 'Options:' + print ' -d, --debug turn on execution tracing' + print ' -h, --help show list of command-line options' + fi + + command -v nvim >/dev/null + nvim_found=$? + editor="${${(M)nvim_found:#0}:+n}vim" + if (( ${#debug} )); then + print -- "${editor}" "${@}" + else + ${editor} ${@} + fi +} + +v "$@"