aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsrdusr <trevorgray@srdusr.com>2023-06-04 00:59:35 +0200
committersrdusr <trevorgray@srdusr.com>2023-06-04 00:59:35 +0200
commit1d0c9378144fe508b5d4ee5d90a3902c0626404b (patch)
tree65f8604afbc52821883b8ce397f4e19f98a6d9fe
parent1a529b17b6e52ee7aec10b66a24b1cb9d22b7633 (diff)
downloaddotfiles-1d0c9378144fe508b5d4ee5d90a3902c0626404b.tar.gz
dotfiles-1d0c9378144fe508b5d4ee5d90a3902c0626404b.zip
Changed script to be more simpler because of non interactive shell environment
-rw-r--r--vi-mode.sh111
1 files changed, 16 insertions, 95 deletions
diff --git a/vi-mode.sh b/vi-mode.sh
index 46f46ec..4b3b6c2 100644
--- a/vi-mode.sh
+++ b/vi-mode.sh
@@ -1,102 +1,23 @@
#!/bin/sh
-# Set vi-mode and key bindings for zsh
-if [ -n "$ZSH_VERSION" ]; then
- bindkey -v
- export KEYTIMEOUT=1
-
- # Show which mode
- terminfo_down_sc=$(tput cud1)$(tput cuu1)$(tput sc)$(tput cud1)
-
- insert-mode () { echo "-- INSERT --"; }
- normal-mode () { echo "-- NORMAL --"; }
-
- precmd () {
- # yes, I actually like to have a new line, then some stuff and then
- # the input line
- print -rP "
- [%D{%a, %d %b %Y, %H:%M:%S}] %n %{${fg[blue]}%}%m%{$reset_color%}"
-
- # this is required for initial prompt and a problem I had with Ctrl+C or
- # Enter when in normal mode (a new line would come up in insert mode,
- # but normal mode would be indicated)
- PS1="%{${terminfo_down_sc}%$(insert-mode)%${terminfo[rc]}%}%~ $ "
- }
-
- set-prompt () {
- case $KEYMAP in
- vicmd) VI_MODE="$(normal-mode)" ;;
- main|viins) VI_MODE="$(insert-mode)" ;;
- *) VI_MODE="$(insert-mode)" ;;
- esac
- PS1="%{${terminfo_down_sc}${VI_MODE}${terminfo[rc]}%}%~ $ "
- }
-
- zle-line-init () {
- set-prompt
- zle reset-prompt
- zle-keymap-select
- }
- preexec () { print -rn -- $terminfo[el]; }
-
- zle -N zle-line-init
- zle -N zle-keymap-select
-
- # Fix backspace bug when switching modes
- bindkey '^?' backward-delete-char
-
- # Edit line in vim with alt-e
- autoload -U edit-command-line; zle -N edit-command-line
- bindkey '^e' edit-command-line
-
- # Navigate in complete menu
- bindkey -M menuselect 'h' vi-backward-char
- bindkey -M menuselect 'j' vi-down-line-or-history
- bindkey -M menuselect 'k' vi-up-line-or-history
- bindkey -M menuselect 'l' vi-forward-char
-
- # Map 'jk' to Escape key in INSERT mode
- bindkey -M viins 'jk' vi-cmd-mode
+# Show which mode
+insert_mode="-- INSERT --"
+normal_mode="-- NORMAL --"
+if [ -n "$ZSH_VERSION" ]; then
+ if [[ $KEYMAP == 'vicmd' ]]; then
+ VI_MODE=$normal_mode
+ else
+ VI_MODE=$insert_mode
+ fi
+ printf "%s\n" "$VI_MODE"
elif [ -n "$BASH_VERSION" ]; then
- # Set vi-mode and key bindings for bash
- set -o vi
-
- show-mode() {
- if [ "$BASH_MODE" = "vi" ]; then
- echo -ne "\[\033[1m\]-- NORMAL --\[\033[0m\]\n"
- else
- echo -ne "\[\033[1m\]-- INSERT --\[\033[0m\]\n"
- fi
- }
-
- PS1='$(show-mode)\u@\h:\w\$ '
-
- # Edit line in vim with alt-e
- edit-command-line() {
- local temp=$(mktemp /tmp/bash-edit-line.XXXXXXXXXX)
- history -a
- history -n
- fc -ln -1 > "${temp}"
- vim "${temp}"
- READLINE_LINE=$(cat "${temp}")
- READLINE_POINT=0
- rm -f "${temp}"
- }
- bind -x '"\ee": edit-command-line'
-
- # Navigate in complete menu
- bind -m vi-command '"h": backward-char' # map h to backward-char
- bind -m vi-command '"j": down-line-or-history' # map j to down-line-or-history
- bind -m vi-command '"k": up-line-or-history' # map k to up-line-or-history
- bind -m vi-command '"l": forward-char' # map l to forward-char
-
- # Map 'jk' to Escape key in INSERT mode
- bind -m vi-insert '"jk":vi-movement-mode'
-
- # Fix backspace bug when switching modes
- stty erase '^?'
-
+ if [[ $BASH_MODE == 'vi' ]]; then
+ VI_MODE=$normal_mode
+ else
+ VI_MODE=$insert_mode
+ fi
+ printf "%s\n" "$VI_MODE"
else
echo "Unsupported shell"
fi