In tmux, how can I move the current window in the next/previous session without specifying its name or index? - tmux

I'm currently using tmux 3.1c; I'd like to bind two keys in order to move the current window in the next/previous session. I want that the window is moved regardless the current session's name/index. The basic idea is that:
if I press e.g. C-b + some <key1>, the current window is moved in the session following the current one, without giving any further input; the window should be moved in the last position of this next session;
if I press e.g. C-b + some <key2> the current window is moved in the session before the current one, without giving any further input; the window should be moved in the last position of this previous session;
On the man pages I found some tokens which act as aliases in order to refer to windows/panes, but no tokens for sessions. I found some interesting examples also here, but no one pointed me to the result I wanted to achieve.
My guess is that in my .tmux.conf I need to set something like
bind-key <key1> move-window -t <target_for_next_session>
bind-key <key2> move-window -t <target_for_previous_session>
(and maybe target the current window somehow too for -s option). I tried to play around with those targets but with no luck. My main guess is that I need to combine the session_id variable in the FORMATS section with the -t option from bind-key, with some +1/-1 increment.
Unfortunately answers for this question always refer to session names/indexes, which are something I don't want to specify.
Thanks in advance!

Related

Closing multiple iTerm2 tabs makes zsh_history lose most of the history

I use zsh 5.8 (oh-my-zsh + my custom configs) with iTerm2.
I want every zsh instance to store the command to the history every time I type it and share the history with the others.
It basically works as I want, but the problem is, if I (manually) quit iTerm2 with multiple tabs open (e.g. so that I can recover those tabs after reboot), the zsh_history file loses most of its content and contains only the first N commands.
My guess is the zsh instances kind of compete with each other in the last moment.
HISTSIZE and SAVEHIST are set largely enough (10000000).
setopt prints:
alwaystoend
autocd
autopushd
noclobber
completeinword
extendedhistory
noflowcontrol
histexpiredupsfirst
histfcntllock
histignoredups
histignorespace
histverify
incappendhistory
interactive
interactivecomments
login
longlistjobs
monitor
promptsubst
pushdignoredups
pushdminus
sharehistory
shinstdin
zle
Is there any way I can prevent this from happening?
EDIT: After seeing the comment, I disabled inc_append_history, but it's happening again.
EDIT2: Disabled appendhistory as well, but still the same.
EDIT3: After manually unsetting histsavebycopy (i.e., nohistsavebycopy), looking good so far.
EDIT4: Since the last change (EDIT3), I've experienced zsh: corrupt history file once.
EDIT5: Lost some history even without the corrupt history file error.

zsh completion : suggestion of multiples directory and how to choose one without knowing the content of this directory

I have a simple issue with zsh. Sometimes, I am in a directory with multiples sub-directories.
So, when I do a $ ls[TAB] or $cd[TAB], I list all these sub-directories.
But how to accept one of the suggestions for sub-directories? Is there a short cut or a key to choose a directory to go deeper in this directory.
I must precise that I don't know systematically the content of these subdirectories, so I can't often choose a subdirectory in which the first letter of filename could allow me to choose automatically the sub-directory to explore.
I was looking for a solution on the web but documentation about zsh completion is pretty big.
Edit: simplest solution to accomplish the desired effect:
press [/] key to 'accept' the current suggested directory ; then press again [tab] key to show suggestions of its subdirectories
Old suggested solution:
Install https://ohmyz.sh/
Then pressing the [tab] key displays a list and the first item is highlighted.
Hit the [tab] key again to choose the desired item and hit the [enter] key to write it in the command line interface, without actually executing the command, only as if you have just typed it in.
Then you can continue hitting the [tab] key to select another subdirectory, and so on.
It also works on any autocompletable, not only dirs.
The only way I know is: double click your target + cmd c + cmd v and then press Enter.

sendkey to active tmux window

I have a tmux session called test with several windows, one for each test file. I have another tmux session with vim and the tmuxify plugin. When I tap <f8>, my .vimrc file is programmed to send the <f7> key to the the left pane in window #0 like so:
nmap <buffer> <F8> :execute "silent !tmux send-keys -t test:0.left 'F7'" <bar>:redraw!<CR>
<f7> triggers the test to be run. Works well.
However, notice the test:0.left bit. I have window #0 hardcoded in there. If I want to run the tests in window #7, for example, I first have to swap it with window #0 and then run the test.
What I'd rather do is just send the <f7> key to whatever window in the test session is currently open.
Is there a way to do this?
I consulted ye olde manual. Solution:
test:.left
Leaving the window blank defaults to the current window.

tmux determine if a pane is marked

I want to create a tmux keybind which sends a set of keystrokes to the marked pane. This is convenient for implementing a hotkey that runs something so I don't have to manually focus another pane and then come back.
But I'd like for this key to not do anything if there's no marked pane present. I've looked in the manpage and found nothing obvious that I can use to check this state.
To target the marked pane you can use -t '~' or -t '{marked}'.
In this case it is simply a matter of send -t '~' <keys here>.
A more generic way to get the marked pane in your shell would be tmux display -p -t '~' '#D'.

In zsh, how to save interrupted (^C) commands to history automatically?

When a command is executed, the command is saved to history. Is there an option in zsh which automatically save the commands interrupted by Ctrl-C to history?
If you mainly want to put a command back and run another first, I would suggest using the push-line widget, which is made just for that. It puts the current command on hold and clears the command line. After running another command, the original command is automatically restored. If you want to run multiple commands, you can repeat the process. As it works on a stack, you can even put multiple commands on hold. They will be restored in reverse order.
From the zshzle(1) manpage:
push-line (^Q ESC-Q ESC-q) (unbound) (unbound)
Push the current buffer onto the buffer stack and clear the buffer. Next time the editor starts up, the buffer will be popped off the top of the buffer stack and loaded into the editing buffer.
As you can see, by default it is bound in emacs mode (bindkey -e) to ^[q, ^[Q and ^Q. That is Alt+q (or Esc, q), Alt+Shift+q (or Esc, Shift+q) and Ctrl+q. The last of which only works if the option FLOW_CONTROL is disabled (setopt noflowcontrol).
There is also the push-line-or-edit widget. It behaves the same on a primary (PS1) prompt. But on a secondary (PS2) prompt it makes the whole multi-line construct (not just the current line) editable. Just like when you pull a multi-line construct from history.

Resources