jeffreytse / zsh-vi-mode

💻 A better and friendly vi(vim) mode plugin for ZSH.
MIT License
3.04k stars 105 forks source link

Remove delay after pressing Esc key #111

Closed Hubro closed 3 years ago

Hubro commented 3 years ago

General information

Basic examination

Problem description

After pressing Esc, there is a short delay before ZVM enters Normal mode.

  1. Press down Esc key
  2. Release Esc key
  3. ~200 ms
  4. Normal mode

This means that very often when I press: EsckEnter in order to run the previous command, nothing happens. If I intentionally do it slowly, it works, but this is a key combination I press very, very often, so I naturally do it really fast.

Reproduction steps

Read above

Expected behavior

Ideally ZVM would enter Normal mode immediately when the Esc key is pressed down, with no delay.

To compare, I have tried:

None of the above have any perceivable delay after pressing Esc. I can press EsckEnter as fast as I possibly can, and they never miss any of the keystrokes.

jeffreytse commented 3 years ago

Hi @Hubro

Thanks for the reporting, could your provide me your dotfiles (e.g. .zshrc) and keybindings (i.e. typing this command bindkey -M viins | grep '"\^\[' in your term)? I will try to help you solve this issue as soon as possible.

Thanks and regards

Hubro commented 3 years ago

Hi @Hubro

Thanks for the reporting, could your provide me your dotfiles (e.g. .zshrc) and keybindings (i.e. typing this command bindkey -M viins | grep '"\^\[' in your term)? I will try to help you solve this issue as soon as possible.

Thanks and regards

Of course:

.zshrc

HISTFILE=~/.histfile
HISTSIZE=10000
SAVEHIST=10000
KEYTIMEOUT=1
setopt SHARE_HISTORY nomatch notify
#bindkey -v
bindkey '^R' history-incremental-search-backward

WORDCHARS='*?_-.[]~=&;!#$%^(){}<>'

fpath=(~/Dropbox/Config/zsh-completions $fpath)

#
# Completion config
#

zstyle ':completion:*' completer _expand _complete _ignored _correct _approximate
zstyle ':completion:*' max-errors 2
zstyle :compinstall filename '/home/tomas/.zshrc'

autoload -Uz compinit
compinit

#
# Antigen plugin manager
#
# This block of config works automatically with the "antigen-git" AUR package
# on Arch and with the "antigen" homebrew package on macOS.
#

# Possible init script locations
ANTIGEN_INIT_SCRIPT=(
    "/usr/share/zsh/share/antigen.zsh"
    "/usr/local/share/antigen/antigen.zsh"
)

for ANTIGEN_INIT_SCRIPT_PATH in "${ANTIGEN_INIT_SCRIPT[@]}"; do
    if [[ -f "$ANTIGEN_INIT_SCRIPT_PATH" ]]; then
        source "$ANTIGEN_INIT_SCRIPT_PATH"

        antigen bundle jeffreytse/zsh-vi-mode

        #NVM_NO_USE=true
        #NVM_AUTO_USE=true
        ##NVM_LAZY_LOAD=true
        #antigen bundle lukechilds/zsh-nvm

        antigen apply

        # Configure zsh-vi-mode
        ZVM_INSERT_MODE_CURSOR=$ZVM_CURSOR_BLINKING_BEAM
        ZVM_NORMAL_MODE_CURSOR=$ZVM_CURSOR_BLOCK
        ZVM_LINE_INIT_MODE=$ZVM_MODE_INSERT
        ZVM_CURSOR_STYLE_ENABLED=true
        #ZVM_INSERT_MODE_CURSOR=$ZVM_CURSOR_BLINKING_BLOCK
        #ZVM_NORMAL_MODE_CURSOR=$ZVM_CURSOR_BLOCK

        break
    fi
done

unset ANTIGEN_INIT_SCRIPT
unset ANTIGEN_INIT_SCRIPT_PATH

# Executed automatically by zsh-vi-mode for setting up keybindings
function zvm_after_lazy_keybindings() {
    # Put custom keybinds in here
}

#
# Kubectl completion
#
#if which kubectl &>/dev/null; then
#    eval "$(kubectl completion zsh 2>/dev/null)"
#fi

#
# Shell init script
#
SHELL_SETUP="$HOME/Dropbox/Config/shell-setup.zsh"
[[ -f "$SHELL_SETUP" ]] && source "$SHELL_SETUP"
unset SHELL_SETUP

#
# Pyenv
#
if which pyenv &>/dev/null; then
    eval "$(pyenv init - --no-rehash)"
    eval "$(pyenv virtualenv-init - --no-rehash)"
fi

#
# NVM (Node Version Manager)
#
NVM_INIT_SCRIPT="/usr/share/nvm/init-nvm.sh"
if [[ -f "$NVM_INIT_SCRIPT" ]]; then
    source "$NVM_INIT_SCRIPT" --no-use
    #source "$NVM_INIT_SCRIPT"
fi

#
# Starship
#
if which starship &>/dev/null; then
    eval "$(starship init zsh)"
fi

#
# Direnv (https://direnv.net/)
#
if which direnv &>/dev/null; then
    eval "$(direnv hook zsh)"
fi

#
# Broot
#
if which broot &>/dev/null; then
    source /home/tomas/.config/broot/launcher/bash/br
fi

#
# Allow parent to initialize shell
#
# This is awesome for opening terminals in VSCode.
#
if [[ -n $ZSH_INIT_COMMAND ]]; then
    echo "Running: $ZSH_INIT_COMMAND"
    eval "$ZSH_INIT_COMMAND"
fi

#
# Syntax highlighting
#
SYNTAX_HIGHLIGHTING_INIT="/usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
if [[ -f "$SYNTAX_HIGHLIGHTING_INIT" ]]; then
    source "$SYNTAX_HIGHLIGHTING_INIT"
fi

.zprofile

export PATH="$HOME/Dropbox/bin:$PATH"
export PATH="$HOME/bin:$PATH"
export PATH="$HOME/.local/bin:$PATH"
export PATH="$HOME/.poetry/bin:$PATH"

# Used by direnv
export NODE_VERSIONS="$HOME/.nvm/versions/node"
export NODE_VERSION_PREFIX="v"

# Tell Neovide to use extra fancy animations
export NeovideMultiGrid=1

# Very Linux specific stuff here
if [[ "$(uname)" != "Darwin" ]]; then
    export SWAYSOCK="/run/user/$UID/sway.sock"

    export EDITOR=vim
    export LESSCHARSET=UTF-8

    export PYENV_ROOT="$HOME/.pyenv"
    export PATH="$PYENV_ROOT/bin:$PATH"
    eval "$(pyenv init --path)"
fi

#[[ -f ~/.zshrc ]] && . ~/.zshrc
➜ bindkey -M viins | grep '"\^\['
"^[" zvm_readkeys_handler
"^[," _history-complete-newer
"^[/" _history-complete-older
"^[OA" up-line-or-history
"^[OB" down-line-or-history
"^[OC" vi-forward-char
"^[OD" vi-backward-char
"^[[200~" bracketed-paste
"^[[A" up-line-or-history
"^[[B" down-line-or-history
"^[[C" vi-forward-char
"^[[D" vi-backward-char
"^[~" _bash_complete-word
jeffreytse commented 3 years ago

Hi @Hubro

This issue has been addressed and fixed. Please update to the latest version and try again.

Thanks and regards

Hubro commented 3 years ago

@jeffreytse Awesome! The delay is still there, but now my keystrokes are never lost. They are just slightly delayed. That's a massive improvement :smile:

Alex-duzhichao commented 2 years ago

@jeffreytse Hi, I am using blinking beam as insert mode cursor style and blinking block as normal mode cursor style. When I switch from insert mode to normal mode, I noticed there is a little delay between when I press ESC key and when the cursor becomes to blinking block. But when I switch from normal mode to insert mode, the cursor becomes to blinking beam instantly. I searched this problem on the internet and find this question. Is there any solution that can switch the cursor style from insert mode to normal mode instantly? Thank you very much.

jeffreytse commented 2 years ago

Hi @Alex-duzhichao

Thanks for your question, can you provide me some more details such as a video record for this behavior, your environment info and so on.

Thanks & Regards

Alex-duzhichao commented 2 years ago

Hi @Alex-duzhichao

Thanks for your question, can you provide me some more details such as a video record for this behavior, your environment info and so on.

Thanks & Regards

Hi @jeffreytse Thank you for you reply. After some test, I find the npm plugin in ohmyzsh is the reason causes the delay. After uninstall the npm plugin, the delay disappears. Thank you for your help again.

jeffreytse commented 2 years ago

Hi @Alex-duzhichao

It's glad to hear your good news. Welcome to star this project for further update in the future. And hope you have a good day! : )

Thanks & Regards

Hubro commented 2 years ago

Thank you for you reply. After some test, I find the npm plugin in ohmyzsh is the reason causes the delay. After uninstall the npm plugin, the delay disappears.

I had the exact same issue with the exact same plugin. I don't think it's really the plugin's fault, but NVM itself. When you source the NVM script it takes a second or two for some reason. I worked around the issue by installing NVM through my package manager and sourcing it myself with the --no-use flag, which avoids the startup delay.

# Arch Linux
NVM_INIT_SCRIPT="/usr/share/nvm/init-nvm.sh"
if [[ -f "$NVM_INIT_SCRIPT" ]]; then
    source "$NVM_INIT_SCRIPT" --no-use
fi
Alex-duzhichao commented 2 years ago

Thank you for you reply. After some test, I find the npm plugin in ohmyzsh is the reason causes the delay. After uninstall the npm plugin, the delay disappears.

I had the exact same issue with the exact same plugin. I don't think it's really the plugin's fault, but NVM itself. When you source the NVM script it takes a second or two for some reason. I worked around the issue by installing NVM through my package manager and sourcing it myself with the --no-use flag, which avoids the startup delay.

# Arch Linux
NVM_INIT_SCRIPT="/usr/share/nvm/init-nvm.sh"
if [[ -f "$NVM_INIT_SCRIPT" ]]; then
    source "$NVM_INIT_SCRIPT" --no-use
fi

@Hubro Hi, I face this problem with npm plugin in ohmyzsh and it has nothing to do with NVM.

https://github.com/ohmyzsh/ohmyzsh/blob/914b6399e88a16fdced427c2ae7738785dcb16b0/plugins/npm/npm.plugin.zsh#L108-L111 Actually I find if I annotate above code in npm plugin and then the delay disappered. So I think the reason is those three bindkey line code.

Best Regards

cd-a commented 2 years ago

I have the same issue, not using ohmyzsh but Prezto

ljurk commented 2 years ago

@jeffreytse Hi, I am using blinking beam as insert mode cursor style and blinking block as normal mode cursor style. When I switch from insert mode to normal mode, I noticed there is a little delay between when I press ESC key and when the cursor becomes to blinking block. But when I switch from normal mode to insert mode, the cursor becomes to blinking beam instantly. I searched this problem on the internet and find this question. Is there any solution that can switch the cursor style from insert mode to normal mode instantly? Thank you very much.

Thanks for this link. I experienced the same issue and for me, tmux was the problem. Adding set -s escape-time 0 to my .tmux.conf solved the issue for me.

hgl commented 1 year ago

I have the same issue, using presto, without nvm or tmux. zsh-vi-mode is installed by cloning them renaming zsh-vi-mode.plugin.zsh to init.zsh. Normal to Insert mode is fine, but Insert to Normal manifests a noticeable delay where the bar cursor is turned into block after the delay.

MahdiNazemi commented 1 year ago

Thanks for developing this great plugin!

I experience about half a second delay when changing from the insert mode to normal mode. I experience this delay on a Linux server, an M1 Mac, and both inside and outside tmux (my tmux escape-time is 1 ms). I use Oh My Zsh as my package manager.

These are the plugins I have installed:

plugins=(
    git
    colored-man-pages
    colorize
    zsh-syntax-highlighting
    zsh-autosuggestions
    autoupdate
    sudo
    fzf
    zsh-vi-mode
)

Today I learned that I can reduce my Vim's delay of going from insert mode to normal mode from about two to three seconds to near zero by set noescapekeys (or adjusting timeoutlen and ttimeoutlen). So I started looking for a similar option in zsh-vi-mode and ended up in this issue.

maptv commented 1 year ago

I still experience this delay. If anyone has a suggestion, please let me know.