[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [lmi] Failed vim-mode .zshrc experiment
From: |
Vadim Zeitlin |
Subject: |
Re: [lmi] Failed vim-mode .zshrc experiment |
Date: |
Sat, 26 Oct 2019 00:33:34 +0200 |
On Fri, 25 Oct 2019 18:21:28 +0000 Greg Chicares <address@hidden> wrote:
GC> but that can't work because $KEYMAP is internal to ZLE. But that's
GC> not what you recommended--you propose defining $PS1 globally, and
GC> then using the functions to modify it. Here's how I've experimentally
GC> implemented that:
GC>
GC> prompt='%d[%?]%(!.#.$)'
GC>
GC> function zle-line-init zle-keymap-select {
GC> if [[ ${KEYMAP} == vicmd ]]; then
GC> prompt="%F{yellow}${prompt}%f"
GC> else
GC> prompt="${${prompt##%F{yellow\}}%%%f}"
GC> fi
GC> zle reset-prompt
GC> }
GC>
GC> zle -N zle-line-init
GC> zle -N zle-keymap-select
GC>
GC> IMO that's drastically worse.
It's less clear as written, but when you change your PS1 in your .zshrc in
a couple of years (or however long it will take you to forget everything
about changing it in ZLE functions -- it could be a couple of days for me)
you won't have to spend an hour trying to understand why does the new value
get lost.
GC> I arrived at that through experimentation, but one minute later I can't
GC> understand it.
That's fine, nobody understands shell code. It just works.
GC> And even if this were comprehensible and maintainable, it still seems
GC> fragile. What does the prompt become if we paste the following line
GC> into the terminal?
GC> prompt="abc%F{red}%F{yellow}%f%fxyz$"
GC>
GC> Did you have a cleaner implementation in mind?
I thought to just save the original value of the prompt in a global
variable, i.e.
if [[ ${KEYMAP} == vicmd ]]; then
VZ_SAVED_PROMPT=$prompt
prompt="%F{yellow}${prompt}%f"
else
if [[ -n $VZ_SAVED_PROMPT ]]; then
prompt=$VZ_SAVED_PROMPT
fi
fi
I haven't actually tested it, but thinking about it more, this approach
does have at least one problem: if you change the prompt interactively, it
will be overwritten by the saved prompt. But this could be solved by
replacing the test for the saved prompt being non-empty with comparison of
the current prompt with "%F{yellow}${VZ_SAVED_PROMPT}%f", at the price of
some minor extra complexity.
Alternatively, I think your implementation could be made a bit clearer by
using sed (shell substitutions are so bad that even sed is clearer than
them!) as
sed -e 's/^%F{yellow}\(.*\)%f$/\1/'
doesn't seem too bad. But I wouldn't want to launch an extra program for
each prompt display just to avoid the (perfectly unreadable, but, again,
who is going to read them anyhow) shell substitutions above.
GC> But doesn't vim's "normal mode" correspond to zsh's vicmd mode,
Sorry, I inadvertently used very confusing terminology, I should have said
"main keymap" instead. You did understand what I was trying to say
eventually, sorry for making this task needlessly difficult.
Regards,
VZ
pgpyC6I5p9340.pgp
Description: PGP signature