LukeSmithxyz / voidrice

My dotfiles (deployed by LARBS)
GNU General Public License v3.0
4.28k stars 1.21k forks source link

zsh history split into multiple histories #1354

Closed Jocomol closed 3 months ago

Jocomol commented 1 year ago

It seems that the zsh command history isn't one unified one but split into different parts depending on how many terminals I have open. This only affects the history command as well as the search-in-history bind from zsh , the actual history file shows all executed commands. Is there a way to fix or revert (in case its a wanted feature) this?

emrakyz commented 11 months ago

Here are the settings I use for the history:

setopt append_history # Allows new commands to be appended to the history file, instead of overwriting it.

setopt inc_append_history # each command is added to the history file immediately after it's executed, not just when the session ends.

setopt share_history # synchronizes the command history between multiple zsh sessions. Commands entered in one session will be available in others.

setopt hist_ignore_dups # Prevents consecutive duplicates from being saved in the command history.

setopt hist_ignore_all_dups # Ignores all duplicate entries in the history, not just consecutive ones.

setopt hist_expire_dups_first #  When the history file reaches its size limit, remove older duplicate entries before removing unique commands.

setopt hist_find_no_dups  # When searching through history, prevent showing duplicate entries.

setopt hist_save_no_dups # When saving the history, omit duplicate entries, so only unique commands are saved.

setopt hist_reduce_blanks # Removes extra blanks from each command line being added to the history.

setopt hist_verify # After a history expansion (like using ! commands), this option prompts for verification before executing the command.

Additionally; here below is the little function I wrote. You can search your history using fzf by hitting CTRL + R. For example if you write "pacman" and hit CTRL + R, you will see all pacman commands you entered before in fzf menu.

fzf-history-widget-with-date() {
  initial_query="$LBUFFER"
  setopt noglobsubst noposixbuiltins pipefail 2>/dev/null
  entry=$(fc -lir 1 |
          sed -E 's/^[[:space:]]+[0-9]+\*?[[:space:]]+//' |
          awk '{cmd=$0; $1=$2=$3=""; sub(/^[ \t]+/, ""); if (!seen[$0]++) print cmd}' |
          fzf --query="$initial_query" +s)
  [ -n "$entry" ] && LBUFFER=${entry#* * * }
  zle redisplay
}

zle -N fzf-history-widget-with-date
bindkey '^R' fzf-history-widget-with-date

There are also nice plugins you can use called fzf-tab (tab completion using fzf instead of default), fzf-autosuggestions (fish-like suggestions based on history) and zsh-completions (complete commands in a very detailed way for example pressing tab after "grep -" will give you all possible options with explanations).