[Linux] 在Linux環境下使用多視窗 screen工作,設定.screenrc以及擅用bindkey
更新日期:2019年11月10日
GNU Screen 算是使用 Linux 工作時最實用的軟體之一
其多重視窗管理的能力以及切換是創的方便性
讓你只要使用一個SSH連線就可以同步處理很多事情
而 screen 所管理的是視窗放在遠端的工作站上
不管目前本地的電腦是否開機
其遠端的視窗都會持續執行
就如同遠端桌面一樣地管理遠端的主機
舉例來說
如果有一天你的 windows 電腦需要更新
你連線到 Linux 的 SSH 視窗會被關閉
也代表許多工作(task)都要重開
這樣是非常不方便的事情
或是今天回到家想要 SSH 遠端回去看模擬的狀態
但是模擬是在公司的電腦中的 putty 執行的
這樣子根本沒辦法得知現在的狀況
如果今天使用 screen 的話
每當重開電腦或是使用其他台主機來連線
只要將之前所開啟的screen reload 回來即可
所有開啟的視窗都還會存在
以及所有執行中的程式都仍然繼續(背景)執行
基礎的 screen 指令
● screen // 開啟一個screen
● ctrl a+d // detached screen 回到 terminal (screen仍在背景執行)
● screen -rd //重新載入之前開啟的screen
● exit //關閉 screen
在screen中所有的指令都必須先按下 ctrl+a
比如說建立新視窗,可以先按下ctrl+a,再按下c
這可以說是麻煩到極點
所以使用 screen 勢必要自行設定 bindkey 在 .screenrc 裡面
以下為我的 .screenrc 內容
算是小而美對我來說已經堪用的設定
#start message
startup_message off
# to avoid visual bell
vbell off
# Allow bold colors - necessary for some reason
attrcolor b ".I"
# Tell screen how to set colors. AB = background, AF=foreground
termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'
# Erase background with current bg color
defbce "on"
# Refresh the display when exiting programs
altscreen on
# Dynamic title
shelltitle '$ | bash'
#dont use thit to avoid screen muse
# term xterm-256color
# Cache 30000 lines for scroll back
#defscrollback 30000
# Set hardstatus always on
hardstatus alwayslastline "%{= Kd} %{= Kd}%-w%{= Kr}[%{= KW}%n %t%{= Kr}]%{= Kd}%+w %-= %{KG} %D %M %d %Y%{= Kc} | %0c:%s "
# # Set default encoding using utf8
defutf8 on
#
# # Keboard binding
bindkey "^[z" prev
bindkey "^[x" next
bindkey "^[[D" prev
bindkey "^[[C" next
bindkey "^[n" screen
bindkey "^[s" split
bindkey "^[e" split -v
bindkey "^[w" focus
bindkey "^[q" remove
bindkey "^[k" kill
這裡就簡單敘述一下功能
首先我在設定下方欄位顯示時間、目前在第幾個視窗
例如我將ctrl +a +c這個新增視窗的功能改成 Alt + n
以下為就是我 bindkey 後自訂的指令為
● Alt + n #新增視窗
● Alt + z #切換到前一個視窗 ● Alt + x #切換到下一個視窗 ● Alt + s #將兩個視窗以split畫面顯示 ● Alt + q #關閉所有screen視窗 另外附註一下
如果需要垂直切割的vspilt
需要在特定的Linux系統下才能更正常運作喔