Settings to move to end of line using tmux - tmux

Annoying question to have to ask! In bash (or anywhere.. like when editing this question) I can do CTRL + left|right to move left left or right to different bits of whitespace. With tmux running.. this does work.. it doesn't do anything. How do you do the same thing but with tmux?
Thank you.

You can do it by binding C-Left and C-Right to "send-keys M-b" and "send-keys M-f" in your ~/.tmux.conf file like this:
bind-key -n C-Left send-keys M-b
bind-key -n C-Right send-keys M-f

You don't. bash, respectively readline, has an idea of what you typed and can therefore jump as many characters until the next "word boundary". tmux does not have this information. Also, in tmux and the ANSI code space, I would interpret "end of line" in fact as \r\e[xB (with x being the size of the window), though that did not match up with your expectation.

Related

Make Alt + h switch to the left pane in Tmux

I'm trying to make Alt-h switch to the left pane in Tmux. This does not work:
bind -n M-h select-pane -L
Pressing Alt-h in cat generates the following output in my terminal:
$ cat
^[h
That is, it looks like Alt generates ^[ in my terminal. So, in my Tmux configuration, I have also tried this binding:
bind -n ^[h select-pane -L
I have made "^[" both a literal two character string (i.e., "^" + "[") as well as the special "^[" character you can access in Vim. Neither work.
How to solve this problem?
EDIT: I'm primarily using xfce4-terminal as my terminal emulator, but I've tried using gnome-terminal as well as xterm, and the problem persists.
Actually the secret is how you enter ^[ ;-)
I assume you want to edit your .vimrc
vim ~/.vimrc
navigate to the correct position
press i
press ctrl-v
press ctrl-[ (on a german keyboard I have to press ctrl-altgr-8 because altgr-8 is [)
After that you should have the escape sequence generated required to write the binding.

tmux split-window without changing focus

Is there any way to split a window in tmux without changing the current focus?
I'm running a script inside one of my tmux panes that occasionally runs "tmux split-window ..." with some command that takes a minute to complete and MAY request input.
I can end up trying to type input into one of the tmux panes but in the middle of my typing, the original pane executes "tmux split-window ..." and (mid word) my cursor shifts to the new pane, and I end up typing part of the input into the wrong pane.
Note: this answer is correct, but obsolete. The right way is to use -d flag for split-window command. I'm leaving this answer as a demonstration how to do some yak shaving with tmux.
A split-window command flag provided by tmux would be the right solution for this. Unfortunately tmux does not provide such command flag. Update: there is a -d split-window flag that does this.
The simple solution is to immediately switch to previous pane after split-window:
tmux split-window
tmux last-pane
This can be also written as a one liner:
tmux split-window\; last-pane
The downside of this solution is that *theoretically* you might end up writing a character in the wrong window if you type it in time interval between split-window and last-pane command execution.
Here another approach with the downside that it's more complex.
Create a new window in the background and get the pane_id of this window (note how this command is wrapped in $(...) because we want it executed in a subprocess:
pane_id=$(tmux new-window -d -P -F "#{pane_id}")
Now join the window we just created with the window where your cursor is located (will not change cursor focus):
tmux join-pane -b -t "$pane_id"
Add -h to the join-pane above if you want a horizontal split.
I recommend taking the first approach for it's simplicity. It's highly unlikely you'll have any practical issues with it.

tmux only shows current pane title if queried from a key binding

Suppose have 3 panes in a tmux window: vim, vom, and vam
I want to access the pane title of the first pane as part of a keyboard mapping (forward command to vim if vim's in pane 1). It wasn't working as expected and I've narrowed it down to the following...
If I run:
tmux display-message "`tmux list-panes -F '#{pane_title}'`"
the result is:
vim
vom
vam
This is what I'd expect. If I try to run the same from a key mapping:
bind -n C-h run "tmux display-message \"`tmux list-panes -F '#{pane_title}'`\""
I get the title of the current pane repeated three times:
vom
vom
vom
Does run-shell execute in a different context or something?
Thanks for your help
Run tmux list-keys in the terminal and see what C-h gets mapped to. I get something like this:
bind-key C-t run-shell "tmux display-message "name1
name2
name3""
From the above, you can see the backtick interpolation happens at the moment when the key is bound, not later when the binding is executed.
I don't know how to get around this and you must be having a lot of pain because of so many nested commands.
Personally, when hacking tmux I always apply the rule of "get to the shell as soon as possible".
For your example that would mean:
keep the key binding simple: bind -n C-h run "/path/to/script.sh"
create a script, make it executable and put the rest of what you want to do in it. This would be its content:
tmux display-message "`tmux list-panes -F '#{pane_title}'\`"
I just did it and it worked for me locally. Hope that helps!

Split pane switching in tmux: switch once per command

I've been a happy tmux user for a while now but there's one behaviour that's bugging me. When I switch panes using ^b-arrow, and then immediately press arrow-up (to get a command from history, for example), the window pane switches again. I understand this can be useful if you want to move through multiple panes quickly, but for me it's a pain in the backside since I keep ending up in panes I never meant to be in.
So, is there a way to set tmux so that the ^b-arrow command only switches pane once and ignores any following arrow key presses?
That happens because the default bindings for the arrow keys are setup with bind-key -r, specifying that they may be repeated. There are two ways that you can disable this.
First, you can use set-option repeat-time 0 to disable repeating entirely. This will affect all bindings. I find that to be very annoying when resizing panes.
Secondly, you can change the bindings for the arrow keys to use bind-key without the -r option:
bind-key Up select-pane -U
bind-key Down select-pane -D
bind-key Left select-pane -L
bind-key Right select-pane -R
If you spend a lot of times navigating panes, why not set up global mappings so you don't have to use prefixes at all, e.g. bind -n C-h select-pane -L to map ctrl-h to switching left, same as Vim.
See http://robots.thoughtbot.com/seamlessly-navigate-vim-and-tmux-splits for an even better solution that also navigates across Vim windows.
Another option is to make a binding to jump to the previous pane, if you are flicking back and forth between the same two panes.
bind-key C-a last-pane

How can you tell which pane in Tmux is focused?

I'm starting to use tmux (I'm thinking of switching from screen), but I'm having a hard time telling which pane is focused when I split a window into multiple panes. Is there a config customization or something that can highlight the focused pane a little more explicitly?
Here are the relevant settings:
pane-active-border-style fg=colour,bg=colour
Set the pane border colour for the currently active pane.
So, try adding something like this to your ~/.tmux.conf:
set-option -g pane-active-border-style fg=blue
That will set a blue border around the active pane. The pane-active-border-style bg=colour option can be used for a more visible solution, as well.
As answered in another post it is now possible in tmux 2.1 to set the colours of individual panes. Ones can use:
set -g window-style 'fg=colour247,bg=colour236'
set -g window-active-style 'fg=colour250,bg=black'
in the ~/.tmux.conf file to show a difference between the active/inactive panes.
With Vim If you find it does not work with Vim panes, it might be down to the colourscheme you are using. First, try another colourscheme such as pablo. For further details, see the other post.
Customize status-left and use the #P character pair, which is the pane number. You will probably want to include more than just the pane number in the status bar, but here is an example of the line you would add to your ~/.tmux.conf for just the pane number:
set-option -g status-left '#P'
See the tmux man page for more character pairs: http://manpages.ubuntu.com/manpages/precise/en/man1/tmux.1.html
One Solution that works for me is to add a display-pane at the end of the hotkey for a pane switch. This displays all the pane numbers, with the current pane in a different color. You can also use <escape_key> + q to display pane numbers.
I use alt+h/j/k/l to switch between panes, and I use the following binding.
bind -n M-j select-pane -D \; display-pane
bind -n M-k select-pane -U \; display-pane
bind -n M-h select-pane -L \; display-pane
bind -n M-l select-pane -R \; display-pane
I wanted the active pane's borders to be brighter than other panes,
so I went with this (works in tmux 1.8 w/CentOS 7):
~/.tmux.conf fragment
# rgb hex codes from https://www.rapidtables.com/web/color/RGB_Color.html
set-option -g pane-active-border-fg '#33FF33' # brighter green
set-option -g pane-border-fg '#006600' # darker green
The tmux man page says hex-RGB colors will be approximated, and I find the hex codes easier to understand than remembering "colour47" (out of colour0-255) is a kind of light green (as described in How does the tmux color palette work?).
tmux man-page excerpt:
message-bg colour
Set status line message background colour, ...etc...
or a hexadecimal RGB string such as ‘#ffffff’, which chooses the closest
match from the default 256-colour set.
For tmux 3 I was able to set the following in my .tmux.conf for a subtle border indicator:
set-option -g pane-active-border-style bg=yellow

Resources