PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 6, 2022

[FIXED] How to remove unknown commands from shell terminal history (zsh)

 October 06, 2022     shell, terminal, zsh, zshrc     No comments   

Issue

I'm trying to setup my history in zsh. I have activated option like HIST_IGNORE_ALL_DUPS which removes duplicated commands in the history.

But I am also looking for some option that can remove commands that don't exist which return 127 "command not found".


Solution

There's no such option in Zsh, but this can be easily achieved with the zsh-hist plugin:

autoload -Uz add-zsh-hook

command-not-found () {
  # -f: force
  # -s: silent
  # -1: most recent history item
  (( ? == 127 )) && 
      hist -fs delete -1
}

add-zsh-hook precmd command-not-found

This will automatically delete the last command line from history, if it returned 127.

Alternatively, in addition to deleting it, you can also load the deleted command into the editing buffer, so you can immediately fix whatever typo you made, by using hist fix instead of hist delete:

autoload -Uz add-zsh-hook

command-not-found () {
  (( ? == 127 )) && 
      hist -fs fix -1
}

add-zsh-hook precmd command-not-found


Answered By - Marlon Richert
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing