2

Is it possible to bypass hist_verify for specific history commands? For instance, I rarely have the need to verify !$ or !!, whereas I find verification to be a useful feature for more complicated history expansions. Can select history operators be excluded from history verification?

1

Maybe something like:

accept-line() {
  if [[ ${BUFFER//![\$!]} = *!* ]]; then
    set -o histverify
  else
    set +o histverify
  fi
  zle .$WIDGET
}
zle -N accept-line

added to your ~/.zshrc as a heuristic. That is, redefine the accept-line widget (invoked when you press Enter) so that it sets the histverify option only if there's a ! followed by a character other than ! and $ in the current line.

That's fooled for command lines like echo '!e' !! or \!!rm but it seems it should be an acceptable limitation.

Personally, I add set +o banghist to my ~/.zshrc and prefer relying on history-search-backward (bound on Alt+p by default in emacs mode) or history-beginning-search-backward (which I bind on PageUp) which saves some typing and always gives me a visual feedback before I accept the command.

  • I agree the limitations are quite minor and unlikely to arise during normal use. The main reasons I use the two history expansions that I mentioned are for encapsulating the previous line in a substitution (e.g., <(!!) or $(!!)) or re-using the last argument of the last line (which in bash can also be done by Alt+. or Esc+.). Not sure if there are better alternatives for these patterns. – user001 Sep 3 '18 at 18:45
  • 1
    @user001, Alt+. works in zsh as well (in emacs mode). For <(!!), you can always recall the command you want (Up, ^R, Alt+p...), then ^U to erase and <(^Y) – Stéphane Chazelas Sep 3 '18 at 20:22
  • Thanks, that's an interesting idea (^U / ^Y). I must have disabled Alt+. by setting vi mode. – user001 Sep 3 '18 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.