Bind key sequence to Escape, zsh

keyboard shortcutsvi-modezsh

How would I map jj to Esc in zsh?

I'm trying to recreate a key-mapping that I have setup in vim, where I have jj mapped to ESC so that whenever I double-press j, it sends the an <Esc> to vim–allowing me to enter normal mode with greater convenience.

I've already tried bindkey 'jj' ^[, and I'm about to try bindkey 'jj' ^[[, but I doubt that it'll work. I also checked the list of commands provided by zsh in normal mode, but I didn't see anything that contained normal or escape.

NOTE
The ^[ characters in my ~/.zshrc file are actual hard coded escapes; not just the characters ^ and [.

Best Answer

You need -s to bind actual strings instead of widgets:

bindkey -s jj '\e'

Though you probably want to map jj to the vi-cmd-mode widget here:

bindkey jj vi-cmd-mode

(note that's for binding in insert mode, not normal/command mode)

Related Question