Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zsh clear RPS1 before adding line to linebuffer

Tags:

shell

zsh

I'm a vim user, and just recently started playing with zsh to be able to use vi mode in the shell (which isn't practical in bash due to lack of insert/command mode indicator). After reading some examples, I set up the following function to update the line accordingly (I picked the colorscheme to match vim-powerline, which I also use):

vim_ins_mode="%{$fg[white]%}%{$fg_bold[blue]$bg[white]%} INSERT %{$reset_color%}"
vim_cmd_mode="%{$fg[green]%}%{$fg_bold[black]$bg[green]%} COMMAND %{$reset_color%}"
vim_mode=$vim_ins_mode
function zle-line-init zle-keymap-select {
    RPS1="${${KEYMAP/vicmd/${vim_cmd_mode}}/(main|viins)/${vim_ins_mode}}"
    RPS2=$RPS1
    zle reset-prompt
}
zle -N zle-line-init
zle -N zle-keymap-select

It works great, except that as I run more commands, RPS1 from previous commands gets output to the screen. As you can imagine, having multiple bright boxes all over my terminal can get quite distracting: here is an example

I'm hoping to clear this field somehow before it gets dumped into the linebuffer as previous command, but so far neither the examples nor zsh themes I looked at bother doing this. In my old .bashrc file I had a trap setup to reset the coloring of my command so that the output color is not affected, I'm wondering if something similar would be possible here? Or perhaps there is a cleaner way with zsh?

like image 432
Alexander Tsepkov Avatar asked Jan 14 '13 10:01

Alexander Tsepkov


1 Answers

Try the following code:

function _-accept-line()
{
    emulate -L zsh
    local SAVEDRPS1="$RPS1"
    RPS1=""
    zle reset-prompt
    RPS1="$SAVEDRPS1"
    zle .accept-line
}
zle -N accept-line _-accept-line

Forget about this code. Zsh has an option for the behavior you want:

setopt transientrprompt

Remove any right prompt from display when accepting a command line. This may be useful with terminals with other cut/paste methods.

like image 65
ZyX Avatar answered Nov 08 '22 20:11

ZyX