Can Tmux Save Commands to a File, Like .Bash_History

Can tmux save commands to a file, like .bash_history?

There is history-file option which does what you are looking for.

history-file path

If not empty, a file to which tmux will write command prompt history on exit and load it from on start.

Add this to your .tmux.conf

set -g history-file ~/.tmux_history

Note it was added in 2.1 version. if you have older version of tmux read
https://unix.stackexchange.com/questions/26548/write-all-tmux-scrollback-to-a-file

How can I make all tmux panes have their own unique shell history?

If you're using the bash shell, your command history is written to a file defined by the HISTFILE variable, which defaults to ~/.bash_history. Inside a tmux pane, you have access to the variable $TMUX_PANE which looks something like this:

$ echo $TMUX_PANE
%3

You could use this to create a per-pane history by adding something like this to your ~/.bashrc file:

if [[ $TMUX_PANE ]]; then
HISTFILE=$HOME/.bash_history_tmux_${TMUX_PANE:1}
fi

This will store the history for pane 2, for example, in ~/.bash_history_tmux_2.

The downside to this idea is that you're going to end up with a bunch of .bash_history_tmux_* files in your home directory.

Preserve bash history in multiple terminal windows

Add the following to your ~/.bashrc:

# Avoid duplicates
HISTCONTROL=ignoredups:erasedups
# When the shell exits, append to the history file instead of overwriting it
shopt -s histappend

# After each command, append to the history file and reread it
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"

Where is linux terminal's session history stored?

Add these lines to your ~/.bashrc, and every single command from any session gets written to ~/.bash_history.

shopt -s histappend
export PROMPT_COMMAND='history -a'

... also saves you from sessions overwriting each others history.

Execute a command without keeping it in history

Start your command with a space and it won't be included in the history.

Be aware that this does require the environment variable $HISTCONTROL to be set.

  • Check that the following command returns ignorespace or
    ignoreboth:

     echo $HISTCONTROL
  • To add the environment variable if missing, the following line can be added to the Bash profile. E.g., to file %HOME/.bashrc.

     export HISTCONTROL=ignorespace

After sourcing the profile again, space-prefixed commands will not be written to $HISTFILE.

How can I copy the output of a command directly into my clipboard?

One way of doing it follows:

  1. Install xclip, such as:

    sudo apt-get install xclip

  2. Pipe the output into xclip to be copied into the clipboard:

    cat file | xclip

  3. Paste the text you just copied into a X application:

    xclip -o

To paste somewhere else other than an X application, such as a text area of a web page in a browser window, use:

cat file | xclip -selection clipboard

Consider creating an alias:

alias "c=xclip"
alias "v=xclip -o"

To see how useful this is, imagine I want to open my current path in a new terminal window (there may be other ways of doing it like Ctrl+T on some systems, but this is just for illustration purposes):

Terminal 1:
pwd | c

Terminal 2:
cd `v`

Notice the ` ` around v. This executes v as a command first and then substitutes it in-place for cd to use.

Only copy the content to the X clipboard

cat file | xclip

Bash history without line numbers

Try this:

$ history | cut -c 8-


Related Topics



Leave a reply



Submit