07 May 2012

Slick ZSH Trick for Vi-Mode

Found a neat trick today for making the commandline vi-mode much more friendly.

Back-story: As I’ve been spending more time programming, I find myself more comfortable on the commandline and in VIM. Also, there’s a setting that can be placed in .bashrc and/or .zshrc to allow vi style editing of the commandline. This is all well and good but without any visual indicator of whether I’m in INSERT MODE or NORMAL MODE… well, it’s disorenting.

So the neat trick is to add code in your .zshrc to insert a right-hand side prompt (RPROMPT) that will show a notifier depending on what mode you’re in.

Add the following code (and thank you to wherever I found this trick online)

    function zle-line-init zle-keymap-select {
        RPS1="${${KEYMAP/vicmd/--NOR--}/(main|viins)/--INS--}"
        RPS2=$RPS1
        zle reset-prompt
    }
    zle -N zle-line-init
    zle -N zle-keymap-select

Have another nice trick for your .zshrc, please share it in the comments!