如何在非tmux的zsh会话中访问tmux的命令历史

huangapple go评论58阅读模式
英文:

How to access command history of `tmux` in non-tmux `zsh` session

问题

I had below in my .zshrc

export HISTFILE="$HOME/.zsh_history"
export SAVEHIST=10000
export HISTSIZE=$SAVEHIST
setopt HIST_IGNORE_DUPS
setopt HIST_SAVE_NO_DUPS
setopt HIST_NO_STORE
setopt HIST_IGNORE_SPACE

Later, I realized commands I used inside tmux session are not available session outside tmux. I assume they're not in $HOME/.zsh_history file. So searched internet made these changes:
in my .tmux.conf

set -g history-limit 10000
set -g history-file ~/.zsh_history

& in my .zshrc file set additional history option

setopt append_history
setopt hist_expire_dups_first
setopt hist_find_no_dups
setopt hist_ignore_all_dups
setopt inc_append_history

After restart, all my command history in ~/.zsh_history are replaced by the ones that I entered today, all the old commands are gone. I had around 3000+ commands.

I've couple of question

  • what might've went wrong with steps that made my history gone
  • if tmux saved command history, without me explicitly setting history-file option in .tmux.conf file, can I access that file now? Hoping I could some how restore.
  • What should I do to save all commands from various tmux session get saved in common file ~/.zsh_history on exit of the session.
  • is there any best practice to manage command history, in scenarios like above, so that history can be restored across.

Thanks for your time. Your help is much appreciated.

--
Sudhir.

英文:

I had below in my .zshrc

export HISTFILE="$HOME/.zsh_history"
export SAVEHIST=10000
export HISTSIZE=$SAVEHIST
setopt HIST_IGNORE_DUPS
setopt HIST_SAVE_NO_DUPS
setopt HIST_NO_STORE
setopt HIST_IGNORE_SPACE

Later, I realized commands I used inside tmux session are not available session outside tmux. I assume they're not in $HOME/.zsh_history file.
So searched internet made these changes:
in my .tmux.conf

set -g history-limit 10000
set -g history-file ~/.zsh_history

& in my .zshrc file set additional history option

setopt append_history
setopt hist_expire_dups_first
setopt hist_find_no_dups
setopt hist_ignore_all_dups
setopt inc_append_history

After restart, all my command history in ~/.zsh_history are replaced by the ones that I entered today, all the old commands are gone. I had around 3000+ commands.

I've couple of question

  • what might've went wrong with steps that made my history gone
  • if tmux saved command history, without me explicitly setting history-file option in .tmux.conf file, can I access that file now? Hoping I could some how restore.
  • What should I do to save all commands from various tmux session get saved in common file ~/.zsh_history on exit of the session.
  • is there any best practice to manage command history, in scenarios like above, so that history can be restored across.

Thanks for your time. Your help is much appreciated.

--
Sudhir.

答案1

得分: 1

The tmux history-file setting is for tmux commands (what you enter after Ctrl+B:), not shell commands. tmux read from ~/.zsh_history, probably didn't know what to do with the unexpected format, and either deleted it or modified it enough that zsh couldn't understand it. If you're lucky, ~/.zsh_history could still have all the commands, just modified somehow.

The settings you're now using will write commands to the history file immediately, but only load the history when you start a new session. If you want the history to be available immediately in all sessions, you can remove setopt inc_append_history and add setopt share_history (they're incompatible).

For the purposes of history, shell sessions in tmux shouldn't act any different from shell sessions in separate terminal windows, as long as the shell tmux is starting is actually zsh.

英文:

The tmux history-file setting is for tmux commands (what you enter after <kbd>Ctrl+B</kbd><kbd>:</kbd>), not shell commands. tmux read from ~/.zsh_history, probably didn't know what to do with the unexpected format, and either deleted it or modified it enough that zsh couldn't understand it. If you're lucky, ~/.zsh_history could still have all the commands, just modified somehow.

The settings you're now using will write commands to the history file immediately, but only load the history when you start a new session. If you want the history to be available immediately in all sessions, you can remove setopt inc_append_history and add setopt share_history (they're incompatible).

For the purposes of history, shell sessions in tmux shouldn't act any different from shell sessions in separate terminal windows, as long as the shell tmux is starting is actually zsh.

huangapple
  • 本文由 发表于 2023年5月18日 11:00:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定