
다음을 수행하기 위해 간단한 tmux conf를 만들고 싶습니다.
- 가로로 분할된 창/창/whatever_stupid_terminology(hsplit)
- 상단 창에서 열기
tail -f foo
- 하단 창에서 열기
tail -f bar
tmux로 어떻게 할 수 있나요?
이것이 내가 가진 것입니다.
#!/bin/sh
tmux new-session -s asdf -n myWindow
tmux select-window -t myWindow
tmux split-window "tail -f /var/log/apache2/samperror.log"
tmux attach-session -t asdf
가능한 해결책이 생각나지 않습니다. 그래서 나는 그것이 모두 잘못되었다는 것을 압니다. 가장 직관적이지 않은 conf 파일 중 하나
답변1
원하는 것을 달성하기 위한 빠르고 간단한 명령줄은 다음과 같습니다.
$ tmux new-session -s asdf -n myWindow -d 'tail -f foo'\; \
split-window -d 'tail -f bar'\; attach-session
이 솔루션에는 몇 가지 단점이 있습니다.
확장성이 좋지 않습니다(명령이 몇 개 더 추가되고 결과를 이해하기 어렵습니다).
두 개의 tail 명령은 대화형 셸에서 실행되지 않으므로 두 명령을 종료하면 창이
myWindow
소멸됩니다(더 이상 세션을 생성하지 않으면 창이 세션과 함께 소멸됩니다).
다음은 시도한 방식으로 작동하는 쉘 스크립트입니다. 내 목표를 달성하는 방법에 대해 생각하는 것이 항상 가장 쉽습니다.수동그런 다음 tmux 명령으로 변환하십시오. 이는 가장 간단하거나 깨끗한 접근 방식은 아니지만 일반적으로 다음과 같이 작동합니다.
#!/bin/sh
# create a new session. Note the -d flag, we do not want to attach just yet!
tmux new-session -s asdf -n 'myWindow' -d
# send 'tail -f foo<enter>' to the first pane.
# I address the first pane using the -t flag. This is not necessary,
# I'm doing it so explicitly to show you how to do it.
# for the <enter> key, we can use either C-m (linefeed) or C-j (newline)
tmux send-keys -t asdf:myWindow.0 'tail -f foo' C-j
# split the window *vertically*
tmux split-window -v
# we now have two panes in myWindow: pane 0 is above pane 1
# again, specifying pane 1 with '-t 1' is optional
tmux send-keys -t 1 'tail -f bar' C-j
# uncomment the following command if you want to attach
# explicitly to the window we just created
#tmux select-window -t asdf:mywindow
# finally attach to the session
tmux attach -t asdf
시도해 본 후에도 여전히 명령 및 구성 구문이 마음에 들지 않으면 다음 tmux
을 살펴보는 것이 좋습니다.tmuxator, 간단하고 보다 투명한 구문을 사용하여 tmux 세션을 관리할 수 있는 Ruby gem입니다.
내 대답에X 서버 없이 여러 터미널을 동시에 사용tmux
유용한 리소스에 대한 링크를 찾을 수 있습니다
답변2
외부 스크립트가 아닌 사용자 스크립트에서 실제로 실행하고 싶다면 ~/.tmux.conf
다음을 구성 파일에 추가할 수 있습니다.
# session initialisation
new -s SessionName -n WindowName 'tail -f /var/log/apache2/samperror.log'
splitw -h -p 50 -t 0 'tail -f /var/log/apache2/other.log'
selectw -t 0
그런 다음 세션에 연결된 명령을 사용하여 tmux를 시작하면 tmux a
SessionName이 생성되고 -h
창을 수평으로 반으로 분할 ( -p 50
)하고 두 명령을 모두 실행합니다.
답변3
기반으로답변보다 일반적인 코드는 다음과 같습니다.
tmuxMany(){
#https://unix.stackexchange.com/a/149729/47116
if [ $# -lt 2 ]; then
echo usage: sessionName command1 command2 ...
return 1
fi
local sessionName=$1
local firstCommand=$2
local secondAndRestCommands=( "${@:3}" )
tmux new-session -s $sessionName -n 'myWindow' -d
tmux send-keys -t $sessionName:myWindow.0 "$firstCommand" C-j
local i
for i in "${!secondAndRestCommands[@]}"
do
echo $i
local command=${secondAndRestCommands[$i]}
echo $command
tmux split-window -v
local tabNumber=$((i+1))
tmux send-keys -t $tabNumber "$command" C-j
done
tmux select-layout even-vertical
tmux attach -t $sessionName
}
용법:
tmuxMany sessionName "tail -f file" "tail -f file2" htop
tmux는 동일한 높이의 3개 창으로 열립니다.
답변4
다음과 같이 시도해 보세요.
$ chmod +x tmux.conf
$ cat tmux.conf
#!/usr/bin/tmux source-file
new-session -s asdf -n myWindow "tail -f /var/log/maillog"
split-window "tail -f /var/log/messages"
를 실행하면 ./tmux.conf
tmux는 각 명령을 차례로 실행하여 요청한 레이아웃을 만듭니다.
놀랍게도 tmux -f tmux.conf
이 명령은 생성된 첫 번째 세션에서만 실행됩니다. tmux.conf
이미 실행 중인 세션이 있으면 -f tmux.conf
자동으로 무시됩니다. 를 사용하여 이 문제를 해결할 수 있습니다 tmux source-file tmux.conf
. Shebang 줄에서 이것을 사용하여 스크립트로 만들 수 있습니다.
나는 이 기술이 매우 유용하다고 생각한다. 각 프로젝트 디렉토리의 루트에는 실행 파일이 있습니다 tmux.conf
. 다시 프로젝트 작업을 시작하게 되면 ./tmux.conf
프로젝트에 마음에 드는 작업 환경을 만들기 위해 달려가겠습니다.