Sure there's a super simple smart way to do this, but since history in zshell seems to be aliased to fc, there's no way to use the tricks mentioned in How to remove a single line from history?.

Any pointers on how to do this? My use case would be to remove the last issued command from history so it stops autocompleting (mostly when you've mistyped something and it keeps showing up).

I know I can get the last issued command by doing

history | tail -n 1, but history -d doesn't work and I can't find proper documentation for zsh.


Zsh doesn't offer a real history edition facility. The command history is basically read-only. All you can do is replace it wholesale.

To edit the command line history:

  1. Save it to a file: fc -W
  2. Edit the file
  3. Load the history from the file: fc -R

You can choose the file name by setting HISTFILE.

Untested code:

remove_last_history_entry () {
  setopt local_options extended_history no_hist_save_no_dups err_return

  local HISTFILE=~/.zsh_history_tmp_$$ SAVEHIST=$HISTSIZE
  fc -W
  ed -s $HISTFILE <<EOF >/dev/null
  fc -R

Usually you can just remove the line from ~/.zsh_history, but if you cannot find it look in ~/.zshrc for the location of your history file.

Then use any text editor of your choice to remove the offending line(s). Alternatively, just delete the whole file and close all zsh instances and start a new one. History purged!


So yeah, this is an old topic, but none of these questions answered it for me so I just spent ages trying to figure this out too!

Here's My solution with thanks from @Gilles for the hint of using 'fc -R' and 'fc -W' :).

Paste the script below this into your .zshrc file.

reload with source .zshrc

then type 'forget' to forget the last command :D. type 'forget 3' to forget the last 3 commands. Some sexy shizz.

Pressing the up arrow will take you straight to the last command and won't remember the word 'forget' either :).

Updated: Added home path, so it works in all directories now lol.

Update 2: Added the ability to pass the number of last commands you want to forget :D. Try 'forget 2' to forget the last 2 commands :D.

# Put a space at the start of a command to make sure it doesn't get added to the history.
setopt histignorespace

alias forget=' my_remove_last_history_entry' # Added a space in 'my_remove_last_history_entry' so that zsh forgets the 'forget' command :).

# ZSH's history is different from bash,
# so here's my fucntion to remove
# the last item from history.
my_remove_last_history_entry() {
    # This sub-function checks if the argument passed is a number.
    # Thanks to @yabt on stackoverflow for this :).
    is_int() ( return $(test "$@" -eq "$@" > /dev/null 2>&1); )

    # Set history file's location
    line_cout=$(wc -l $history_file)

    # Check if the user passed a number,
    # so we can delete x lines from history.
    if [ $# -eq 0 ]; then
        # No arguments supplied, so set to one.
        # An argument passed. Check if it's a number.
        if $(is_int "${1}"); then
            echo "Unknown argument passed. Exiting..."

    # Make the number negative, since head -n needs to be negative.

    fc -W # write current shell's history to the history file.

    # Get the files contents minus the last entry(head -n -1 does that)
    #cat $history_file | head -n -1 &> $history_temp_file
    cat $history_file | head -n "${lines_to_remove}" &> $history_temp_file
    mv "$history_temp_file" "$history_file"

    fc -R # read history file.

So there's a few things going on here. This command will let us type a space before any command, and it'll not be added to history.

setopt histignorespace

So we can press the space bar, and type 'echo hi', press enter and when we press the up arrow, 'echo hi' is not in our history :).

Notice how the alias 'forget' has a space before my_remove_last_history_entry. This is so that zsh doesn't save our 'forget' to history.

The function explained

ZSH uses fc for the history or something, so we do 'fc -W' to write our current commands to the history file, them we use 'head -n -1' to trim the last command off the file. We save that output to a temporary file and then replace the original history file with the temp one. And finally reload the history with fc -R.

There is however a problem with the function which is fixed with the alias.

If we run the function by it's name, it'll remove the last command, which is the call to the function. This is why we use the alias with a call to it using a space, so that zsh doesn't add this function name to the history file, making the last entry the one we want :D.

  • what if the last command was multi-line? – avp Dec 2 '19 at 9:09
  • This should be voted as the official answer. Thanks. – gsl Jan 16 at 18:01

In zsh, set the histignorespace option (set -o histignorespace). And in bash, add ignorespace or ignoreboth (some distributions already do that in the default ~/.bashrc) to $HISTCONTROL or set HISTIGNORE=' *', and then you can start the current command with a <space> to avoid recording it.

 : none
fc -nl -1


In zsh, you can still recall that previous command once, but it's not stored in the history.

  • This doesn’t address the question. – Chris Page Nov 12 '19 at 20:22

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.