4

In zsh, I have alt+. bound to insert-last-word.

When I press alt+., I can list through the last word of previous commands.

How can I exclude some words from being shown when I cycle through ?

ie, if this is my history:

echo
foo
ls

and I want to ignore foo, then alt+. should skip foo.

3
  • 2
    On what basis do you want to ignore that foo? Because it's foo (do you want to be able to specify a blacklist of exact words or of patterns as an array for instance), or because it's the second last one or because it's not a valid command? ... ? Dec 30, 2018 at 21:36
  • 1
    @Stéphane Chazelas - I want to be able to specify a blacklist of exact words or patterns Dec 31, 2018 at 5:08
  • I keep researching into your issue and your post is the only thing that comes back as relevant. None of the zsh docs or add-ons mention anything about this behavior. Is there a specific reason for this? It is pulling from your history so you could look at clearing your history and making sure not to call foo again? If that is not a solution why specifically does foo need to be ignored?
    – kemotep
    Jan 8, 2019 at 19:54

1 Answer 1

1

I’ve tested this and it works:

setopt EXTENDED_GLOB
bindkey '^[.' insert-last-word
autoload smart-insert-last-word
zle -N insert-last-word smart-insert-last-word
zstyle :insert-last-word match '*~(*last*|match)'

What this does:

  1. Replaces insert-last-word implementation with the function smart-insert-last-word, which is distributed with Zsh, but not loaded by default.
  2. Says that we want our new insert-last-word widget to match any word (*), but not any word that contains the word last or the literal word match.
  3. EXTENDED_GLOB is required to be able to use the ~ ("but not") operator.

Of course, it's up to you to replace that pattern in the last line with something that excludes all the words or patterns that you never want to be inserted. 🙂

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .