tmux-resurrect icon indicating copy to clipboard operation
tmux-resurrect copied to clipboard

Avoiding ``history`` commands being shown in terminal output

Open davidjb opened this issue 9 years ago • 26 comments

Fairly sure I know the answer to this, but I was interested to know whether there's any way of avoiding resurrect showing the history -w ... commands in the terminal output. I'm using resurrect with continuum and as a result, every 60 minutes (as per my config), another command gets issued to all terminals without a program running. The result is that after a few hours, the contents of a pane are get pushed further into the scrollback, making it increasingly harder to discover what was happening in the pane.

Is there any way to for the plugin / tmux to issue the command to the shell without it being outputted?

I'm using the latest version from Git of this plugin, Tmux 1.9 and Bash as my shell.

Thanks for the plugin, it's awesome!

davidjb avatar May 12 '15 08:05 davidjb

Hey, glad you like the plugin.

As suggested in tmux-continuum issue, please try disabling bash save history feature. That should fix it.

bruno- avatar May 12 '15 14:05 bruno-

As mentioned in tmux-continuum issue, PR that fixes this (somehow) would be gladly accepted.

bruno- avatar Jul 07 '15 15:07 bruno-

If you're feeling so strongly about dirtying panes, I can try to fix this. When this feature was originally implemented, all saves were triggered manually, so it was less of an issue.

Fixing history read is simple (by moving it into pane_creation_command); hence we just need to rethink history saving. One possible solution would be to save history from each pane separately and continously, by changing either HISTFILE or PROMPT_COMMAND. Capturing has to be continous, as we don't have a clean way to inject Bash commands on save. Additional benefit from this approach is that the history will be captured even if fullscreen program is running in the pane.

I know that details need fleshing out, but does that approach sound good to you?

rburny avatar Sep 14 '15 13:09 rburny

Hey @rburny, thank you for offering help. It would be good if this feature can be improved, but no pressure: it's still marked as experimental and users know it's not perfect.

Can you provide (manual) steps for this so we can manually verify if this will work?

Again, no pressure to improve this. It's functional although not perfect and we're communicating that clearly.

bruno- avatar Sep 14 '15 23:09 bruno-

I find bash history to be the most important part of my work context and I'd like this feature to be first-class citizen. I did some research and it seems we can do much better than currently. We will need three things:

  • Continous history capturing. For this, users need to add the following line to .bashrc:

trap "history -a $HOME/hist_$$" DEBUG

This flushes history to a process-specific file after a command is issued. @davidjb suggested to do this via $PROMPT_COMMAND, but this would only save command to history after it finishes running; trap will save it immediately. OTOH, only one debug can be active, so my solution prevents user from using his own trap ... DEBUG. I guess we could allow both options.

Additionally, I would add: trap "cat $HOME/hist_$$ >> $HOME/.bash_history" EXIT This appends shell history to .bash_history on exit (just like Bash does by default). It's not strictly necessary, but without it, new shells would start without any history (which now lands in hist_$$ files).

  • Saving pane history when save is made. This is as simple as saving Bash pid for each pane, so that we can later figure out which hist_<PID> file corresponds to a given pane.
  • Restoring history. When recreating pane, we need to run: history -r $HOME/hist_$PID with the $PID we saved in step 2. We can use existing pane_creation_command function to inject this command cleanly.

rburny avatar Sep 15 '15 12:09 rburny

@rburny Any update on this? Would love this feature.

CeleritasCelery avatar Dec 14 '16 19:12 CeleritasCelery

@rburny casually I'm here with @PrgrmAtCeleritas on the same day to ask the same (:

nramirezuy avatar Dec 15 '16 01:12 nramirezuy

I just developed my own solution to this and seems to work flawlessly. Not very portable yet so when I get some time I will try and add a PR. although it looks like @bruno- doesn't maintain this anymore so it may never get added. I took @rburny ideas but used $TMUX_PANE rather then $PID for the identifier.

CeleritasCelery avatar Jan 13 '17 23:01 CeleritasCelery

@PrgrmAtCeleritas if it works on bash I can use the patch 😄

nramirezuy avatar Feb 15 '17 21:02 nramirezuy

@CeleritasCelery I'd love to try out your solution. Do you have it available on GitHub?

rburny avatar May 12 '17 18:05 rburny

Yes I just added it. My working machine has restricted internet access so it had to update it manually. CeleritasCelery/tmux-resurrect A couple of caveats

  • my bash prompt consists of two lines followed by a new line. Part of the code goes through all saved panes and removes the last three lines so I don't get an empty prompt in my pane history. You will need to be aware of that because things will look off if your prompt is not the same size as mine.
  • I had to add part of my bashrc to the repo because my solution requires changes there. Also note that I am using rcaloras/bash-preexec to manage my bash history but I have used $PROMPT_COMMAND and it works fine.
  • There are still a couple of corner cases where a pane or two do not get properly restored. I have not been able to track down why yet.

overall the solution works great. But because it involves changes to how history is managed it is not a very flexible solution for most people.

CeleritasCelery avatar May 13 '17 14:05 CeleritasCelery

I added set -g @resurrect-save-bash-history 'off' to my conf, also executed the tmux command in console, is that supposed to disable those annoying messages? Nothing changed.

Destroy666x avatar Aug 31 '17 08:08 Destroy666x

Re-source your config file

CeleritasCelery avatar Aug 31 '17 15:08 CeleritasCelery

# Plugins
set -g @plugin 'tmux-plugins/tmux-yank'
set -g @override_copy_command 'copyq -e "copy(input()); add(input())"'

set -g @plugin 'tmux-plugins/tmux-sidebar'
set -g @sidebar-tree-command 'tree -C'
set -g @sidebar-tree-position 'right'

set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @resurrect-capture-pane-contents 'on'
set -g @resurrect-save-shell-history 'on'
set -g @resurrect-strategy-vim 'session'
set -g @resurrect-save-bash-history 'off'

set -g @plugin 'tmux-plugins/tmux-continuum'
set -g @continuum-save-interval '10'
set -g @continuum-restore 'on'
set -g @continuum-boot 'on'

# Options
# set -g default-terminal "tmux-256color"
set -g default-terminal "screen-256color"
set -g status-right '#P/#{window_panes} #[default]%a, %d %b %H:%M:%S #[fg=default,bold]Autosave: #{continuum_status}'
set -g history-limit 10000
set -g display-time 3500
set -g status-interval 6
set -g focus-events on
set -g renumber-windows on
set -g base-index 1
setw -g pane-base-index 1
setw -g aggressive-resize on
setw -g automatic-rename on

# 'C-n' = new window
unbind c
bind -n C-n new-window

# 'C-/' = kill window
unbind &
bind -n ^_ kill-window

# 'C-S-left' && 'C-S-right' = previous window and next window
unbind n
unbind p
bind -n C-S-Left previous-window
bind -n C-S-Right next-window

# 'C-S-up' = list all windows
unbind w
bind -n C-S-Up list-windows

# 'C-arrow' = switch between panes
bind -n C-Left select-pane -L
bind -n C-Right select-pane -R
bind -n C-Up select-pane -U
bind -n C-Down select-pane -D

# 'C-k' = clear pane
bind -n C-k send-keys -R \; clear-history

# Prefix '/' = kill pane
bind / kill-pane

# Prefix "'" = maximize pane
bind "'" resize-pane -Z

# Prefix ';' = swap pane down
bind \; swap-pane -D

# Prefix 'l' = swap pane up
bind l swap-pane -U

# Prefix 'r' = reload config
bind r source-file ~/.tmux.conf\; display-message "Config ~/.tmux.conf reloaded"

# Mouse, prefix 'm' = switch
set -g mouse off
bind m set-window-option mouse\; display-message "Mouse mode: toggled"

# Prefix changed: 'C-b' -> 'C-a'
unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix

# Prefix '#' && '"' -> ',' && '.'
unbind '"'
unbind %
unbind .
unbind ,
bind . split-window -h
bind , split-window -v

# Automatically install plugins
if "test ! -d ~/.tmux/plugins/tpm" \
   "run 'git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm && ~/.tmux/plugins/tpm/bin/install_plugins'"

# Initialize plugins after $ git clone https://github.com/tmux-plugins/tpm ~/clone/path
run '~/.tmux/plugins/tpm/tpm'

Destroy666x avatar Aug 31 '17 15:08 Destroy666x

Oh, so that overwrote it, thought it was some kind of other setting, thanks.

Destroy666x avatar Aug 31 '17 16:08 Destroy666x

Trying to get proper history for each of my panes, I've written the following code for my bashrc:

# if running tmux, keep a separate bash history for each pane
if [[ $TMUX ]]; then
       [[ -d ~/.history ]] || mkdir -m 700 ~/.history
       # Set history file based on tmux pane number.
       # $TMUX_PANE looks like %3, so let's remove the first character.
       HISTFILE=~/.history/history.tmux${TMUX_PANE:1}
       PROMPT_COMMAND="history -a;$PROMPT_COMMAND"
fi

This works as intended with one exception: if I close a pane and create a new one, TMUX_PANE is increased and different from the number in the tmux status bar, while I wanted it to always match the number in the status bar. Does anyone have a suggestion how to fix this?

What do you think of this approach? So far I have less problems with it than with resurrect+continuum.

karlb avatar Oct 10 '17 08:10 karlb

This is my solution

restore_hist () {
    if [[ $TMUX_PANE ]]; then
        HISTFILE=~/.tmux/bash_history/bash_history.$($tmux_abs display-message -p '#S').$TMUX_PANE

        if [[ -f ~/.tmux/tmux_restore_running ]]; then
            local pane_index window_number session_name restore_file
	        pane_index=$(   tmux display -pt "${TMUX_PANE}" '#{pane_index}'  )
	        window_number=$(tmux display -pt "${TMUX_PANE}" '#{window_index}')
	        session_name=$( tmux display -pt "${TMUX_PANE}" '#{session_name}')
            restore_file=~/.tmux/resurrect/bash_history-"$session_name:$window_number.$pane_index"
            if [[ -f $restore_file ]]; then
	            cp "$restore_file" "$HISTFILE"
            fi
        fi
        touch "$HISTFILE"
        PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
    fi
}

restore_hist

notice that I changed resurrect to set a mutex file tmux_restore_running so that it will not run when I open new panes.

CeleritasCelery avatar Oct 11 '17 17:10 CeleritasCelery

I am also using history -a in my setup, but for clearing the history command itself from the shell this could be used: history -a '~/.tmux/resurrect/bash_history-servers:13.0' | echo -en "\e[1A\033[0K"

kingd avatar Nov 29 '17 18:11 kingd

Hi,

I also very much enjoy the plugin but I hate the commands being printed in the console. My terminals end up all filled with those messages. I see this issue is from last year. Does anyone know if any of the solutions listed here would work with zsh too ? Thanks!

marianopeck avatar May 10 '18 03:05 marianopeck

What is the current recommended workaround to this issue? To avoid printing history commands in the terminal but still keep the feature to save them. I guess that is @CeleritasCelery restore_hist(), but I'm not sure about the right setup to use it?

Thanks!

agilmor avatar Apr 22 '19 14:04 agilmor

Hey folks, any updates on this? every 15 mins my history gets polluted with history -r and history -w commands, as well as the pane. @CeleritasCelery can you elaborate on how to apply your fix for the time being?

jseaidou avatar Jan 27 '21 18:01 jseaidou

In Bash specifically you can set HISTIGNORE to an array of strings/commands you don't want saved by the history mechanism.

dragon788 avatar Jan 27 '21 21:01 dragon788

@dragon788 That's useful for the history, thank you, is there a way to stop it outputting to terminal and polluting the pane? image

jseaidou avatar Jan 28 '21 23:01 jseaidou

You are only redirecting standard out, you could try redirecting standard error and standard out to /dev/null.

some-noisy-command 2>&1 >/dev/null

There's a particular order you need to do the redirections in but I can't remember which one needs to be first offhand.

dragon788 avatar Jan 28 '21 23:01 dragon788

I don't think that works, since it's not the output of the command, but the command it seems is put on the terminal and executed

jseaidou avatar Jan 29 '21 00:01 jseaidou