Shell

zsh HISTFILE - 仍然從 ~/.zsh_history 讀取

  • March 2, 2016

我將我的$HISTFILEenv var 設置為自定義的東西,並且我的 zsh 確實正在寫入新的 histfile。

但是當使用向上箭頭或其他歷史搜尋功能時,它仍然從 ~/.zsh_history 讀取

即,如果我打開一個新外殼,然後直接按向上箭頭,我會將最後一行寫入~/.zsh_history:(

我使用 oh-my-zsh (with osx brew celery gem git-flow npm pip screen vi-mode last-working-dir docker),這裡是我使用的 setopts:

# zsh options
#Initial
setopt appendhistory autocd beep extendedglob nomatch notify
#history
HISTSIZE=100000000
SAVEHIST=100000000
setopt HIST_IGNORE_SPACE
setopt extended_history
setopt hist_expire_dups_first
setopt hist_ignore_dups # ignore duplication command history list
setopt hist_ignore_space
setopt hist_verify
setopt inc_append_history
setopt share_history # share command history data
#dirs
setopt autopushd pushdminus pushdsilent pushdtohome pushdignoredups
setopt auto_name_dirs
#appearance
setopt multios
setopt cdablevarS
setopt prompt_subst
#misc
setopt long_list_jobs
#correction
setopt correct_all
#completion
setopt auto_menu         # show completion menu on succesive tab press
setopt complete_in_word
setopt completealiases
setopt always_to_end
#syml
setopt chaselinks
#stop pissing me off when using ! in line
unsetopt banghist    

# The following lines were added by compinstall
zstyle :compinstall filename '/Users/alex/.zshrc'

# Already in ohmyzsh
#autoload -Uz compinit
#compinit
# End of lines added by compinstall

########
# Key bindings, vi, etc.
autoload -U edit-command-line
zle -N edit-command-line
bindkey -M vicmd 'v' edit-command-line

# create a zkbd compatible hash;
# to add other keys to this hash, see: man 5 terminfo
typeset -A key

key[Home]=${terminfo[khome]}
key[BackSpace]=${terminfo[kbs]}
key[End]=${terminfo[kend]}
key[Insert]=${terminfo[kich1]}
key[Delete]=${terminfo[kdch1]}
key[Up]=${terminfo[kcuu1]}
key[Down]=${terminfo[kcud1]}
key[Left]=${terminfo[kcub1]}
key[Right]=${terminfo[kcuf1]}
key[PageUp]=${terminfo[kpp]}
key[PageDown]=${terminfo[knp]}

# setup key accordingly
[[ -n "${key[Home]}"    ]]  && bindkey  "${key[Home]}"    beginning-of-line
[[ -n "${key[BackSpace]}" ]]  &&  bindkey "${key[BackSpace]}" backward-delete-char
[[ -n "${key[BackSpace]}" ]] &&  bindkey -M vicmd "${key[BackSpace]}" backward-delete-char
bindkey '^H' backward-delete-char
bindkey -M vicmd '^H' backward-delete-char
bindkey "^?" backward-delete-char
bindkey -M vicmd "^?" backward-delete-char
[[ -n "${key[End]}"     ]]  && bindkey  "${key[End]}"     end-of-line
[[ -n "${key[Insert]}"  ]]  && bindkey  "${key[Insert]}"  overwrite-mode
[[ -n "${key[Delete]}"  ]]  && bindkey  "${key[Delete]}"  delete-char
[[ -n "${key[Delete]}"  ]]  && bindkey -M vicmd "${key[Delete]}"  delete-char

[[ -n "${key[Left]}"    ]]  && bindkey  "${key[Left]}"    backward-char
[[ -n "${key[Right]}"   ]]  && bindkey  "${key[Right]}"   forward-char

[[ -n "${key[Up]}"   ]]  && bindkey  "${key[Up]}"    history-beginning-search-backward && bindkey -M vicmd "${key[Up]}"    history-beginning-search-backward
[[ -n "${key[Down]}" ]]  && bindkey  "${key[Down]}"  history-beginning-search-forward && bindkey -M vicmd "${key[Down]}"  history-beginning-search-forward

bindkey -M vicmd 'h'  backward-char
bindkey -M vicmd 'l'  forward-char
bindkey -M vicmd '^R' redo
bindkey -M vicmd 'u'  undo
bindkey -M vicmd 'ga' what-cursor-position
bindkey -M vicmd 'v'  edit-command-line


# Finally, make sure the terminal is in application mode, when zle is
# active. Only then are the values from $terminfo valid.
if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
   function zle-line-init () {
       printf '%s' "${terminfo[smkx]}"
   }
   function zle-line-finish () {
       printf '%s' "${terminfo[rmkx]}"
   }
   zle -N zle-line-init
   zle -N zle-line-finish
fi

zsh 配置中的設置HISTFILE確實應該更改為歷史記錄的寫入位置讀取位置。oh-my-zsh很可能在您設置HISTFILE=~/.zsh_history之前設置,在這種情況下,歷史記錄已經從~/.zsh_history.

查看oh-my-zsh程式碼,有兩種方法可以解決這個問題:

  • HISTFILE在載入oh-my-zsh之前設置。也就是說,它必須在~/.zshrc包含的行之前設置在您的
source $ZSH/oh-my-zsh.sh

如果您只想更改HISTFILE.

  • 使用您自己的自定義版本重載history.zsh模組。Oh-my-zsh在啟動時載入所有匹配的文件$ZSH/lib/*.zsh$ZSH通常是~/.oh-my-zsh),除非 in${ZSH_CUSTOM}/lib/是同名的文件(ZSH_CUSTOM通常是$ZSH/custom)。歷史設置可以在 中找到,$ZSH/lib/history.zsh因此可以替換為${ZSH_CUSTOM}/lib/history.zsh

如果您想更改更多設置,$ZSH/lib/history.zsh這可能是要走的路。否則,您必須HISTFILE在載入oh-my-zsh和之後的所有其他內容之前進行設置。


HISTFILE稍後在 shell 會話中更改(臨時)的方法是

fc -p /path/to/new_history

這會將目前歷史記錄放在堆棧上,HISTFILE=/path/to/new_history從該文件(如果存在)設置和讀取歷史記錄。然後,任何新命令也將寫入新的HISTFILE. 您可以使用fc -P.

引用自:https://unix.stackexchange.com/questions/265957