From dc19e6210735217d9659d2c0a6034ce0ce82bc86 Mon Sep 17 00:00:00 2001 From: srdusr Date: Tue, 4 Apr 2023 04:56:30 +0200 Subject: Better prompt (new line) for zsh --- vi-mode.sh | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/vi-mode.sh b/vi-mode.sh index fa1f6c6..d7f4e4f 100644 --- a/vi-mode.sh +++ b/vi-mode.sh @@ -6,16 +6,38 @@ if [[ -n "$ZSH_VERSION" ]]; then export KEYTIMEOUT=1 # Show which mode - function zle-keymap-select { - if [[ $KEYMAP == vicmd ]] || - [[ $1 = 'block' ]]; then - echo -n -- NORMAL -- - else - echo -n -- INSERT -- - fi - echo -ne '\n' - zle reset-prompt + terminfo_down_sc=$terminfo[cud1]$terminfo[cuu1]$terminfo[sc]$terminfo[cud1] + + function insert-mode () { echo "-- INSERT --" } + function 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]%}%~ $ " } + function 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]%}%~ $ " + } + + function zle-line-init zle-keymap-select { + set-prompt + zle reset-prompt + } + preexec () { print -rn -- $terminfo[el]; } + + zle -N zle-line-init zle -N zle-keymap-select # Fix backspace bug when switching modes -- cgit v1.2.3