zsh HISTFILE - ~/.zsh_historyでまだ読む

zsh HISTFILE - ~/.zsh_historyでまだ読む

$HISTFILE環境変数をカスタム値に設定し、zshは実際に新しいhistfileを作成しています。

ただし、上矢印やその他の履歴検索機能を使用する場合それでも~/.zsh_historyから読みます。

つまり、新しいシェルを開き、上矢印を押すと最後の行が書き込まれます。~/.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構成の設定は、実際に記録が記録される場所に変更する必要があります。そして読める場所。たぶんそれはすべてです。ああ、HISTFILE=~/.zsh_history設定する前に設定してください。この場合、履歴はで読み込まれます~/.zsh_history

見ているああ、コードには、この問題を解決する 2 つの方法があります。

  • HISTFILEロード前の設定ああ、。つまり、次を~/.zshrc含む行の前に設定する必要があります。

    source $ZSH/oh-my-zsh.sh
    

    単に変更したい場合は、HISTFILEこれが簡単な解決策になります。

  • history.zsh独自のカスタムバージョンでモジュールをオーバーロードします。ああ、通常、同じ名前のファイルでない場合は、起動時に(通常は)一致するすべてのファイルをロード$ZSH/lib/*.zshします$ZSH。履歴設定は で見つけることができるので、 に置き換えることができます。~/.oh-my-zsh${ZSH_CUSTOM}/lib/ZSH_CUSTOM$ZSH/custom$ZSH/lib/history.zsh${ZSH_CUSTOM}/lib/history.zsh

    内部のより多くの設定を変更するには、これが$ZSH/lib/history.zsh正しい選択かもしれません。それ以外の場合は、HISTFILEロードする前に設定する必要があります。ああ、そしてそれ以降のすべて。


HISTFILE後でシェルセッションでこれを(一時的に)変更する方法は次のとおりです。

fc -p /path/to/new_history

これにより、現在の履歴がスタックに配置され、HISTFILE=/path/to/new_historyそのファイルがある場合はそのファイルの履歴を設定して読み込みます。新しいコマンドも新しく作成されますHISTFILE。で元の記録に戻ることができますfc -P

おすすめ記事