How do you switch from a tmux pane that you've scrolled up in to another tmux pane? - tmux

If I have two tmux panes open and I go to one and scroll back to the history using CTRL-B [, then for some reason I can't switch to the other pane without making the current pane scroll back to the bottom. Does anyone know how to work around this? Note that I use some custom tmux configuration, as described here, to make it easier to navigate between vim windows and tmux panes. Is this somehow messing up how tmux scrolling works?

Is it possible you're running into a conflicting default keybinding for tmux when it's in copy mode?
When tmux is running, you can check the currently active keybindings by doing prefix+?. When I do this on a bare tmux config, I can see that C-k is bound to send-keys -X copy-end-of-line, and it will move a buffer that's in copy mode to the end of the buffer, like you're describing. Alternatively, if you're using tmux's copy-mode-vi, then it looks like C-j is bound by default to send-keys -X copy-selection-and-cancel.
Try adding the following to your .tmux.conf:
unbind-key -Tcopy-mode C-k
unbind-key -Tcopy-mode-vi C-j
After this configuration update, I'm able to use C-j and C-k to navigate panes like described in the article you linked.

Related

Adding a keybinding to enter vi copy mode in tmux

Pressing C-b and [ puts tmux in copy mode, this lets you select and copy text in a manner similar to vi.
Could you please show me how to add a new key binding like C-j to do this?
I guess it would look like - bind -n C-j function-to-enter-copy-mode
But, I don't know what the function to enter vi copy mode is called. I think I just need the function name.
bind -n C-j copy-mode
Add this to the tmux configuration file and reload tmux config

Configure tmux copy mode to also dump a copy into the system clipboard

When working with multiple panes, it's convenient to use the tmux copy mode (ctrl+b [) to be able to use pane-aware copying. However, copy mode copies into a tmux copy buffer, rather than into the system clipboard. Ideally, I would like a copy to automatically copy into the system clipboard too.
A basic starting point was to add this alias to my bashrc:
alias tmux2clip='tmux save-buffer - | xsel -i -b' (on linux, requires xsel to be installed)
alias tmux2clip='tmux save-buffer - | pbcopy' (on macos)
After I copy using copy mode, I run the alias and the text gets copied into my clipboard! Works like a charm, but always requires that one extra manual step.
It would be ideal, if that alias can trigger as a kind of "post tmux copy hook" that I configure in tmux.
The best thing I could come up with is this line in my ~/.tmux.conf:
bind-key -T copy-mode MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xsel -i -b"
# similarly, I believe only needed for keyboard based copies:
bind-key -T copy-mode C-w send-keys -X copy-pipe-and-cancel "xsel -i -b"
bind-key -T copy-mode M-w send-keys -X copy-pipe-and-cancel "xsel -i -b"
This causes any mouse based selection (note: I prefer mouse selection over keyboard based selections) to trigger a copy of that selection into my system clipboard. The annoying thing is though, that it will always happen, i.e. not only in copy mode, just anytime I select something it happens. This turns out to be quite sensitive in practice, just clicking the window with my mouse to give it focus often already accidentally triggers a small selection (often accidentally overwriting existing content in my clipboard, causing me to sometimes paste completely different stuff than intended).
I searched many similar questions but haven't been able to find any satisfying answers. I would really hope to find a way to get a kind of "post tmux copy hook" where I can insert my copy-to-clipboard command.
I use Ubuntu 18.04, with tmux 2.6. Please also be aware that I use mouse mode in my ~/.tmux.conf:
set -g mouse on
Any help would be highly appreciated. I've been wanting this for many months now already, and all of my revived efforts have only led to lost time :(
If you don't want tmux to enter copy mode when you drag on the pane, then unbind MouseDrag1Pane in the root table (unbind -n MouseDrag1Pane).

how to search within shortcuts of tmux

Pressing ? within a tmux session lists out the available shortcuts.
But there are so many... how can one search for some text within those displayed shortcuts?
I figured it out. My tmux's default mode was in emacs. I just had to switch to Vi (which I am comfortable with). So all I had to do was:
set-window-option -g mode-keys vi

Tmux command to run shell command on active pane?

I understand that run-shell command runs the code in a "global" session which isn't necessarily linked to any active window, pane or session. However I would like to bind a key to run a command in the shell for the active pane.
Use case: map a key to opening vim in the current active pane (perhaps with certain parameters). Just like you would run run-shell "command command-parameters*".
Is there a way to do this in tmux?
Sending commands to the current pane
If you run tmux send-keys vim Enter, that will send the literal keypresses to your current pane to spawn vim. You can then bind this to whatever key combination you like.
Caveat: Because this is sending literal key presses, the pane needs a shell running already to interpret what is sent (e.g. bash / zsh / fish).
Targeting a specific pane
You can target specific destinations with the -t flag. For example, to open vim in the current session, window 5, pane 2:
tmux send-keys -t ":5.1" vim Enter
The syntax of a target is quite flexible (see the COMMANDS section of the manpage, for target-session and target-window), and this is just one example:
session-name:window-id.pane-id
When session-name is empty (as in the above example), the current session is used. The 5 identifies which window you're referring to. The .1 specifies the second pane in window #5 (counting up from 0).
$TMUX_PANE
Each pane in tmux has its own unique ID (and it is unique across all sessions). It's exposed via the $TMUX_PANE environment variable. This can also be used as a target for various tmux commands, e.g. tmux rename-window -t $TMUX_PANE $new_name.

tmux - split-window with custom environment variables

I want to split the current tmux window and have it inherit my current environment.
I need to do this programmatically so I can't do it through some predefined config file.
Simply put, I want to do something like,
tmux split-window my-binary
my-binary is in my current PATH but not the usual one (it seems tmux just goes through my bash_profile).
Is it possible? Or do I have to start bash and setup the env before starting my-binary?
To split a window that will run an instance of your default shell, use the env command that runs whatever is configured as default-command in your ~/.tmux.conf:
tmux split-window "env FOO=bar $(tmux show-options -vg default-command)"

Resources