Skip to content

Commit c8662a3

Browse files
committedJul 10, 2022
alias: smarter determination of editor
1 parent d80b836 commit c8662a3

File tree

9 files changed

+61
-41
lines changed

9 files changed

+61
-41
lines changed
 

‎bashrc.sh

+2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ unset hook
9999

100100
# modify path to include useful scripts
101101
# shellcheck source=/dev/null
102+
source "${DOTFILES__ROOT}/.dotfiles/dotenv/lib/utils.sh"
103+
# shellcheck source=/dev/null
102104
source "${DOTFILES__ROOT}/.dotfiles/dotenv/lib/path.sh"
103105
__dot_path_setup
104106

‎dotenv/aliases.sh

+12-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@ alias mv="mv -i"
2222
# run this after plugins are loaded in case gnu grep and gnu ls is added to path in plugins
2323
function __grep_ls_colors() {
2424
local LS_COLOR_FLAG LS_BIN GREP_BIN
25-
LS_BIN="$(unalias ls &>/dev/null; command -v ls)"
26-
GREP_BIN="$(unalias grep &>/dev/null; command -v grep)"
25+
LS_BIN="$(
26+
unalias ls &>/dev/null
27+
command -v ls
28+
)"
29+
GREP_BIN="$(
30+
unalias grep &>/dev/null
31+
command -v grep
32+
)"
2733

2834
# Colorize grep matches
2935
# test if color is supported, if it is, always add color
@@ -88,3 +94,7 @@ alias extip="curl -s https://api.ipify.org/?format=text"
8894
# Reload the current shell
8995
# shellcheck disable=SC2139
9096
alias reload="exec ${SHELL} -l"
97+
98+
# Alias vi to a found editor
99+
# shellcheck disable=SC2139
100+
alias vi="$(__find_editor)"

‎dotenv/darwin/aliases.sh

-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
# shellcheck shell=bash
22
# Shortcuts
33

4-
# Alias vi to vim
5-
if command -v vim &>/dev/null; then
6-
alias vi="vim"
7-
fi
8-
94
# Networking shortcuts
105
function ips() {
116
ifconfig | grep 'inet\>' | perl -nle'/(\d+\.\d+\.\d+\.\d+)/ && print $1'

‎dotenv/exports.sh

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ export COMP_TAR_INTERNAL_PATHS
1818
# Make = a wordbreak character
1919
COMP_WORDBREAKS=${COMP_WORDBREAKS/=/}
2020

21-
# set $EDITOR to vi(m) if not already set
22-
export EDITOR="${EDITOR:-$(command -v vim &>/dev/null && echo vim || echo vi)}"
21+
# set $EDITOR to a if not already set
22+
EDITOR="${EDITOR:-$(__find_editor)}"
23+
export EDITOR

‎dotenv/functions.sh

-18
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,5 @@
11
# shellcheck shell=bash
22

3-
# push a command to the prompt command
4-
function __push_prompt_command() {
5-
local command="${1/%;/}"
6-
PROMPT_COMMAND="$(echo "$(echo "${PROMPT_COMMAND/%;/}" | tr ';' '\n' | grep -v -F "${command}" | grep -v '^ *$' | tr '\n' ';')${command};" | sed 's/;;/;/' | sed 's/^;//')"
7-
}
8-
9-
# internal prompt command stack to simplify the PROMPT_COMMAND variable
10-
declare -a __prompt_actions
11-
function __push_internal_prompt_command() {
12-
local command="${1/%;/}"
13-
__prompt_actions+=("${command}")
14-
}
15-
function __run_prompt_command() {
16-
for l in "${__prompt_actions[@]}"; do
17-
eval "$l"
18-
done
19-
}
20-
213
# update the dotfiles repo
224
function dotfiles-update() (
235
# shellcheck disable=SC2164

‎dotenv/lib/utils.sh

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# shellcheck shell=bash
2+
3+
# push a command to the prompt command
4+
function __push_prompt_command() {
5+
local command="${1/%;/}"
6+
PROMPT_COMMAND="$(echo "$(echo "${PROMPT_COMMAND/%;/}" | tr ';' '\n' | grep -v -F "${command}" | grep -v '^ *$' | tr '\n' ';')${command};" | sed 's/;;/;/' | sed 's/^;//')"
7+
}
8+
9+
# internal prompt command stack to simplify the PROMPT_COMMAND variable
10+
declare -a __prompt_actions
11+
function __push_internal_prompt_command() {
12+
local command="${1/%;/}"
13+
__prompt_actions+=("${command}")
14+
}
15+
function __run_prompt_command() {
16+
for l in "${__prompt_actions[@]}"; do
17+
eval "$l"
18+
done
19+
}
20+
21+
# helper function get the closest base editor
22+
function __find_editor() {
23+
local editor
24+
# shellcheck disable=SC2209
25+
editor="$(which vi nano | head -1)"
26+
27+
if command -v code-insiders &>/dev/null && echo "${PATH}" | grep -q "/.vscode-server-insiders/bin/" && ! (echo "${PATH}" | grep -q "/.vscode-server/bin/"); then
28+
editor="code-insiders --wait"
29+
elif command -v code &>/dev/null && echo "${PATH}" | grep -q "/.vscode-server/bin/"; then
30+
editor="code --wait"
31+
elif [[ -n "${DOT___IS_WSL}" ]] && command -v npp &>/dev/null; then
32+
editor=npp
33+
elif command -v nvim &>/dev/null; then
34+
editor=nvim
35+
elif command -v vim &>/dev/null; then
36+
editor=vim
37+
fi
38+
39+
echo "${editor}"
40+
}

‎dotenv/linux/aliases.sh

-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
# shellcheck shell=bash
22

3-
# Alias vi to vim
4-
if command -v vim &>/dev/null; then
5-
alias vi="vim"
6-
fi
7-
83
# Networking shortcuts
94
function ips() {
105
ip addr | grep 'inet\>' | perl -nle'/(\d+\.\d+\.\d+\.\d+)/ && print $1'

‎dotenv/wsl/aliases.sh

-9
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,3 @@
44
# test by running `net.exe sessions` vs winsudo net.exe sessions
55
alias wudo="winsudo"
66
alias wsl-sudo="winsudo"
7-
8-
# Alias vi to vscode or notepad++
9-
if command -v code-insiders &>/dev/null && echo "${PATH}" | grep -q "/.vscode-server-insiders/bin/" && ! (echo "${PATH}" | grep -q "/.vscode-server/bin/"); then
10-
alias vi="code-insiders --wait"
11-
elif command -v code &>/dev/null && echo "${PATH}" | grep -q "/.vscode-server/bin/"; then
12-
alias vi="code --wait"
13-
elif command -v npp &>/dev/null; then
14-
alias vi="npp"
15-
fi

‎dotenv/wsl/bin/npp

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ if [[ -d "$(wslpath -ua "c:/")Program Files (x86)/Notepad++/" ]]; then
77
fi
88

99
PROGRAM="$(wslpath -ua "c:/${PGM_PATH}/Notepad++/notepad++.exe")"
10+
if [[ ! -e "${PROGRAM}" ]]; then
11+
# fallback to a vi program
12+
exec "$(which nvim vim vi | head -1)" "$@"
13+
fi
1014

1115
# if there are any baked in arguments they are defined here
1216
args=()

0 commit comments

Comments
 (0)
Please sign in to comment.