zsh HISTFILE - 仍然从 ~/.zsh_history 读取

zsh HISTFILE - 仍然从 ~/.zsh_history 读取

我将$HISTFILE环境变量设置为自定义的值,并且我的 zsh 确实正在写入新的 histfile。

但是当使用向上箭头或其他历史搜索功能时,它仍然从 ~/.zsh_history 读取

即,如果我打开一个新的 shell,然后直接按向上箭头,我将把最后一行写入~/.zsh_history:(

我使用 oh-my-zsh (带有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

答案1

HISTFILEzsh 配置中的设置确实应该更改为写入历史记录的位置从哪里读取。可能就是这样哦我的zshHISTFILE=~/.zsh_history在设置之前设置,在这种情况下,历史记录已经从 中读取~/.zsh_history

看着哦我的zsh代码中,有两种方法可以解决这个问题:

  • HISTFILE加载前设置哦我的zsh。也就是说,它必须设置在~/.zshrc包含以下内容的行之前

    source $ZSH/oh-my-zsh.sh
    

    如果您只想更改HISTFILE,这将是一个简单的解决方案。

  • history.zsh使用您自己的自定义版本重载模块。哦我的zsh在启动时加载所有匹配的文件$ZSH/lib/*.zsh$ZSH通常是),除非 in是同名文件(通常是)。历史设置可以在 中找到,因此可以用 替换。~/.oh-my-zsh${ZSH_CUSTOM}/lib/ZSH_CUSTOM$ZSH/custom$ZSH/lib/history.zsh${ZSH_CUSTOM}/lib/history.zsh

    如果您想更改其中找到的更多设置,这$ZSH/lib/history.zsh可能是正确的选择。否则你必须HISTFILE在加载之前设置哦我的zsh以及之后的一切。


HISTFILE稍后在 shell 会话中(暂时)更改的方法是

fc -p /path/to/new_history

这会将当前历史记录放在堆栈上,设置HISTFILE=/path/to/new_history并读取该文件中的历史记录(如果存在)。任何新命令也将被写入新的HISTFILE.您可以通过 回到原始历史fc -P

相关内容