2011-02-02 19:53:22 +0000 2011-02-02 19:53:22 +0000
436
436

tmux窗口如何重新排序?

screen中,我只需输入C-a :number 0就可以将一个窗口移动到窗口列表的顶部,并将其他所有窗口向下推一个。tmux的等价命令序列是什么?我看了一下man页面,但我发现在这一点上很混乱。

答案 (19)

491
491
491
2011-03-01 09:39:45 +0000

swap-window命令最接近你想要的。

“前缀:”(默认为Ctrl+B,:)将你带到tmux命令提示符。在那里你可以输入:

swap-window -s 3 -t 1

让3号窗口和1号窗口交换位置。

要将当前窗口与顶部窗口互换,请执行以下操作。

swap-window -t 0

在不太可能的情况下,如果在索引0处没有窗口,请执行:

move-window -t 0

(如果基数是0,默认情况下是0)。

您可以在~/.tmux.conf中添加以下内容,将该命令绑定到一个键上(例如T代表 “top")。

bind-key T swap-window -t 0
279
279
279
2013-02-15 11:18:09 +0000

补充Gareth的答案,你可以使用以下键绑定

bind-key -n C-S-Left swap-window -t -1
bind-key -n C-S-Right swap-window -t +1

按Ctrl+Shift+Left(会将当前窗口向左移动。同理向右。不需要使用修饰符(C-b)。

对于tmux 3.0版本,你应该使用以下键绑定

bind-key -n C-S-Left swap-window -t -1\; select-window -t -1
bind-key -n C-S-Right swap-window -t +1\; select-window -t +1
``` 。
67
67
67
2011-04-05 22:12:22 +0000

tmux相当于:number 42,就是:move-window -t 42

17
17
17
2013-07-09 22:16:07 +0000

我对窗口重新编号是这样的。

Ctrl+b,.222

将使当前tmux窗口(所有窗格)编号为222。


相关。当我在洗牌的时候,我往往想做

Ctrl+b :new-session -d -s "reading"

,从那里我也可以用Ctrl+b,.reading把当前窗口(所有窗格同时)移到阅读会话中。你可以用Ctrl+b, s在会话中浏览,就像在会话中使用Ctrl+b, w浏览一样。

HTH

12
12
12
2012-04-16 15:42:48 +0000

你可以用一个外部shell脚本实现一个与screennumber命令相当的命令,在swap-windowmove-window之间进行选择。你可以这样把它绑定到一个键上:

bind < command-prompt -p index "run-shell '~/.tmux.number.sh %%'"
  • *

~/.tmux.number.sh

#!/bin/bash
if [$# -ne 1 -o -z "$1"]; then
    exit 1
fi
if tmux list-windows | grep -q "^$1:"; then
    tmux swap-window -t $1
else
    tmux move-window -t $1
fi
``` * * 

0x6&: 

0x1&
8
8
8
2011-12-12 03:13:19 +0000

使用swap-window移动到任何一个id: [最接近屏幕的:数字]

# window movement / renumbering like in screen's :number
bind-key m command-prompt -p "move window to:" "swap-window -t '%%'"

[m代表移动 –> 点击前缀-m然后输入说3 … …将窗口重新编号为3]

5
5
5
2016-03-18 17:08:47 +0000

由于Ashish Ariga的答案在1.9a及以下版本上不起作用。我使用<>分别将窗口切换到左边和右边,在.tmux.conf中加入下面一行。

# swap window to left or right
bind-key -r < swap-window -t -1
bind-key -r > swap-window -t +1
4
4
4
2015-02-09 12:34:24 +0000

man开始,最简单的解决方案就是使用默认的绑定。

{ Swap the current pane with the previous pane.
} Swap the current pane with the next pane.
3
3
3
2016-01-26 00:48:42 +0000

对于那些使用 byobu 作为 tmux 包装器的人来说,你可以用以下方法将当前窗口与上一个或下一个窗口交换。

Ctrl-Shift-F3
Ctrl-Shift-F4

byobu为这些键定义的键绑定可能会让人感兴趣。

bind-key -n C-S-F3 swap-window -t :-1
bind-key -n C-S-F4 swap-window -t :+1
2
2
2
2016-12-13 06:50:32 +0000

你可以使用base-index来改变窗口的起始编号。如果你把base-index设置为1,那么move-window -r会把所有从1开始的窗口重新编号,从而腾出0来移动一个窗口到它的槽位。

set base-index 1
move-window -r
move-window -t 0

完成后,如果你想以后使用base-index而不移动你的窗口,你可以将0重设为move-window -r

2
2
2
2016-12-29 18:30:54 +0000

tmux-pain-control提供ctrl-b >ctrl-b <来左右移动焦点窗口,环绕。

2
2
2
2013-06-24 20:29:13 +0000

我使用的方法结合了一点Ashish的答案和piec的答案;我让alt-left和right箭头绑定到一个快速的小shell callout,将窗口向左或向右移动一个,除非它是第一个或最后一个窗口,尊重它。我这样做是因为,当你在最后一个窗口发出交换+1(或在第一个窗口发出交换-1)时,它仍然会交换,而不是像你所期望的那样再次循环:

0:one 1:two 2:three 3:zero*

变成

0:zero* 1:two 2:three 3:one

而不是

0:zero* 1:one 2:two 3:three

所以,当窗口到达列表边缘时,我使用的命令就会停止工作。

bind-key -n M-Left run-shell 'tmux list-windows | head -n 1 | grep -q active || tmux swap-window -t -1'
bind-key -n M-Right run-shell 'tmux list-windows | tail -n 1 | grep -q active || tmux swap-window -t +1'

这可以很容易地与base-indexrenumber-windows结合起来,得到一个从任意数字开始的窗口列表,而且永远不会有任何空隙。

如果你像我一样使用base-index 1,而且你认为你永远不会超过999个窗口,你可以使用一个小技巧来使它正常滚动,虽然命令有点膨胀。

set -g base-index 1
set -g renumber-windows on
bind-key -n M-Left run-shell 'if tmux list-windows | head -n 1 | grep -q active ; then tmux move-window -t 999 \; move-window -r \; refresh-client -S ; else tmux swap-window -t -1 ; fi'
bind-key -n M-Right run-shell 'if tmux list-windows | tail -n 1 | grep -q active ; then tmux move-window -t 0 \; move-window -r \; refresh-client -S ; else tmux swap-window -t +1 ; fi'

这个方法的原理是把最后一个窗口暂时移到未使用的索引0上 然后调用move-window -r把它们从1开始重新编号。它的工作原理与将第一个窗口移动到最后的工作原理类似;通过选择一个你永远不会使用的巨大数字,它确保当move-window -r再次启动时,所有的东西都会像你期望的那样被编号。如果你对refresh-client -S感到疑惑,那是必要的,因为有时,虽然move-window的重新排序会正常工作,但状态栏不会更新,直到做出进一步的改变。通过强制刷新状态栏(-S),你可以避免这种情况。

我发现这种方法的唯一问题是swap-window会隐性地将最后使用的窗口改为你交换的窗口。因此,如果你在1号窗口,切换到4号窗口,然后把它移回1号窗口,你会发现你最后使用的窗口是新的4号窗口(以前是3号窗口)而不是1号窗口。似乎没有办法解决这个问题。

1
1
1
2017-10-07 08:44:19 +0000

swap-window -s 5swap-window -t 5都将当前窗口与窗口5互换,但语义不同。

swap-window -s 5

  • 窗口5程序与当前win号生效。
  • 最近的窗口号保持不变。
  • 重复同一命令将返回到以前的状态。

swap-window -t 5

  • 当前程序以5号窗口生效。 -最近对当前窗口号的覆盖。

numberswap-window -s number中的-t number可以是绝对,例如5,也可以是相对,例如-1,+2。

P.S. 下面是TMUX状态线的节选,说明swap-window -s 5

最近的影响: [0] 0:vim- 1:mpl 2:py2* 3:numpy 4:plot 5:mc

现在,last-window之后。
[0] 0:vim* 1:mpl 2:py2- 3:numpy 4:plot 5:mc

swapw -s 5之后, [0] 0:mc* 1:mpl 2:py2- 3:numpy 4:plot 5:vim
swapw -s 5

又是[0] 0:vim* 1:mpl 2:py2- 3:numpy 4:plot 5:mc
last-window

又一个[0] 0:vim- 1:mpl 2:py2* 3:numpy 4:plot 5:mc
0x6&

1
1
1
2018-07-04 22:36:52 +0000

在 tmux 窗口中,尝试以下方法将窗口 0 与 1 互换:

$ tmux swap-window -d -s 0 -t 1

0
0
0
2016-05-15 14:31:11 +0000

首先,打开tmux命令,按下并释放。

Ctrl + b

如果要把实际窗口改成右边的窗口(按循环顺序),只需:

}

要把实际窗口改成左边的:

{
  • 当按SHIFT}时,别忘了用{
0
0
0
2017-09-14 10:58:56 +0000

对于它的价值。

我黑进了这个脚本,使其能够在 “TUI "中对所有窗口进行排序. 它把所有的窗口都列在一个临时文件中,用你的默认编辑器打开它(假设你已经设置了$EDITOR)。在这之后,你可以重新排序,在你保存并关闭文件后,窗口也会相应地重新排序。这类似于做git rebase -i时的提交排序)

#!/bin/bash

# Usage: tmux-mv    
# Move your tmux windows around in an editor

tmpfile=$(mktemp)
tmux list-windows > $tmpfile
$EDITOR $tmpfile

# Move all windows to 50..x in the order you just specified
# Assumes you don't have 50 windows already(!)
cat $tmpfile | awk -F ":" '{ print " -s " $1 " -t 5" NR-1 }' |\
  xargs -I {} sh -c 'tmux move-window -d {}'

# Move them back down, retaining the order
tmux move-window -d -r
rm $tmpfile

可能还可以改进很多,特别是。

注意 。运行命令后,你可能会被移动到另一个窗口。 一个正在运行中的gif图片(github)

0
0
0
2018-05-22 20:05:43 +0000

这里没有一个答案让我满意,所以我写了一个脚本,(算是)模仿screen窗口移动行为的tmux

它检测目标窗口号是否比最小的窗口号或比最大的窗口号,如果是,则将所有其他窗口向右或向左推移(分别),并将它们重新编号为从1-N递增。这是用move-window命令完成的。

如果窗口编号介于**最小/最大的数字之间(即可能与现有的窗口编号相匹配),则只需用swap-window命令交换它们。

这是脚本。

#!/usr/bin/env bash
# Filename: ~/tmux-windowswap
# Function that provides improved window-swapping functionality for tmux
maxwin="$(tmux list-windows | cut -d: -f1 | sort -nr | head -n1)"
minwin="$(tmux list-windows | cut -d: -f1 | sort -n | head -n1)"
# Error checking
if [[-z $2]]; then
  echo "Error: No window specified."
elif [[! $2 =~ ^-?[0-9]+$ ]]; then
  echo "Error: Bad window number specified."
# Bigger than everything; slide it to the far right, then renumber
elif [[$2 -gt $maxwin]]; then
  i=0 # intialize
  tmux move-window -t:$(($maxwin+1))
  winlist="$(tmux list-windows | cut -d: -f1 | xargs)"
  for n in $winlist; do
    i=$(($i+1)) # increment
    tmux move-window -s:$n -t:$i
  done
# Smaller than everything; slide it to the far left, then renumber
elif [[$2 -lt $minwin]]; then
  tmux move-window -t:0
  winlist=($(tmux list-windows | cut -d: -f1 | xargs | rev))
  i=${#winlist[@]} # initialize; start at highest indexed window
  for n in ${winlist[@]}; do
    tmux move-window -s:$n -t:$i
    i=$(($i-1)) # decrement
  done
# In-between; just a simple swap
else
  tmux swap-window -t:$2
fi

然后,只需将这个简单的键绑定添加到你的.tmux.conf中。

bind m command -p "Send window to:" "run -b '~/tmux-windowswap #I %1'"

。为了完美地模仿screen的窗口交换行为,我相信当你移动一个窗口到一个存在的窗口号时,它应该占据该窗口的位置,而所有其他窗口都会被推到右边。要修改这个脚本来做到这一点是相当简单的。

0
0
0
2014-10-31 15:53:04 +0000

这是我使用的方法。你仍然不能把一个窗口移动到一个被占用的索引上,但是你可以把一个窗口移动到一个更高的(未使用的索引),然后在之前索引的空隙中重新排列。

假设你有3个窗口,想添加第四个窗口,但在3的位置上。

在添加新窗口之前,Tmux的前缀会在.Tmux中显示。Tmux prefix then … 会打开移动命令。输入4,3的索引现在将变成4,然后简单地添加另一个窗口,它将在索引3上,而你的旧窗口仍然在索引4上。

0
0
0
2019-09-16 23:39:34 +0000

不需要命令行(就是Ctrl + b然后:)

只需用

Ctrl + b + :

然后输入新的(空闲)窗口号,并按回车键

如果现在有空闲的窗口号,用Ctrl + b + 。

(提示:用Ctrl + b + , 命名你的窗口,如果你不知道哪个是哪个)