Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบ >> Linux

วิธีติดตั้งและตั้งค่า Tmux สำหรับ Linux

หากคุณต้องสลับระหว่าง terminals ตลอดเวลาและหาหน้าต่างที่ถูกต้องไม่เจอเมื่อต้องการ พิจารณาใช้ multiplexer โปรแกรมเหล่านี้ช่วยให้ผู้ใช้รัน multiple terminal sessions ภายในหน้าต่างเดียว

Tmux เป็น multiplexer ยอดนิยมที่เสนอ amazing features เยอะ มันทำให้คุณรัน different applications ใน separate sessions และสลับระหว่างพวกเขาได้ง่าย

วิธีติดตั้ง Tmux ใน Linux

คู่มือนี้แสดงวิธีติดตั้งและตั้งค่า tmux สำหรับ Linux machines เปิด terminal และใช้วิธีติดตั้งที่เหมาะสมกับระบบของคุณ

บน Debian-based distributions:

sudo install tmux

บน CentOS/REHL:

yum install tmux

เพื่อติดตั้ง tmux บน Arch Linux:

pacman -S tmux

เมื่อติดตั้งเสร็จ ให้เปิด terminal และพิมพ์ tmux เพื่อเริ่ม fresh tmux session คำสั่งนี้จะ initiate new session ภายในหน้าต่างและเริ่ม shell Status bar ที่ด้านล่างแสดงข้อมูลเกี่ยวกับ current session

tmux

วิธีใช้ Tmux สำหรับ First Timer

Tmux มี notions เช่น sessions, windows, panes และ hotkeys ดังนั้น beginners มัก intimidating (รู้สึกหวาดกลัว) ตอนแรก แตเมื่อคุณจับได้แล้ว คุณจะรู้สึก productive มากขึ้น

Tmux session 定义 (defines) งานที่มีอยู่ Windows ให้ผู้ใช้ทำ different activities ภายใน session Panes ให้คุณทำงานกับ separate views ภายใน window

คุณจัดการทั้งหมดนี้ผ่าน tmux hotkeys ซึ่งเป็น combination ของ tmux prefix ตามด้วย specific key Default prefix คือ Ctrl + B

วิธีจัดการ Sessions ใน Tmux

Tmux จัดการ multiple sessions พร้อมกันได้ นี้ช่วยให้ developers สลับระหว่าง various projects ได้ง่าย คุณสร้าง sessions ใหม่ได้ทุกเมื่อ

tmux new -s test-session

คำสั่งข้างต้นสร้าง session ใหม่ชื่อ test-session คุณสร้าง sessions จาก running tmux instance ที่มีอยู่ได้ด้วย โดย invoke tmux interpreter แล้วพิมพ์ prefix ตามด้วย colon character หรือ Ctrl + B : สำหรับ default prefix

คำสั่งจะเปิด tmux interpreter ใหม่ที่เราสามารถ input control commands ได้ พิมพ์ต่อไปนี้เพื่อสร้าง session ใหม่:

:new -s test-session

กด Ctrl + B s เพื่อดู active sessions ทั้งหมด คุณสลับไป session อื่นได้โดยเลือกแล้วกด Enter

คุณ detach จาก session ใดก็ได้ได้ และ tmux จะยังคง run process ไว้ พิมพ์ :detach ใน tmux interpreter หรือกด Ctrl +B d สำหรับ detach current session

ใช้คำสั่งต่อไปนี้เพื่อ attach ไปยัง last session:

tmux attach

คุณ attach ไป specific tmux session ได้โดยระบุ session name ใช้ option -t เพื่อทำเช่นนี้

tmux attach -t test-session

เพื่อ kill tmux session ใช้คำสั่ง kill-session

tmux kill-session -t test-session

วิธีจัดการ Windows ใน Tmux

Tmux windows ครอบคลุมหน้าจอทั้งหมด และสามารถ split เป็น several panes แต่ละ pane จะทำหน้าที่เป็น pseudo-terminal แยก คุณสร้าง tmux window ใหม่ได้ด้วย Ctrl + B c

ใช้ Ctrl + B , สำหรับ rename tmux windows จะ invoke tmux interpreter พิมพ์ new window name ที่นี่

Tmux ทำให้สลับระหว่าง windows ต่างๆ ไปได้ง่าย ให้พิมพ์ tmux prefix ตามด้วย window number ตัวอย่าง คุณสลับไป window ที่สองได้เร็วด้วย Ctrl + B 2

คุณยัง swap tmux windows ได้ โดย invoke interpreter ด้วย Ctrl + B : และพิมพ์:

:swap-window -s 1 -t 3

คำสั่งนี้สลับ window แรกและสาม คุณลบ window ได้เมื่อเสร็จแล้วด้วย Ctrl + B &

วิธีจัดการ Panes ใน Tmux

จนถึงตอนนี้ เราใช้ sessions และ windows ใน tmux แต่มันน่าตื่นเต้นจริงๆ เมื่อเริ่มใช้ panes Panes คือ pseudo-terminals ที่รันภายใน window เราใช้พวกเขาสำหรับรัน several terminal instances จาก tmux window เดียว

คุณสร้าง horizontal และ vertical panes ได้ใน tmux พิมพ์ Ctrl + B " สำหรับ split view แนวนอน

สิ่งนี้จะ split current window แนวนอนและเปิด terminal ใหม่ใน window ใหม่ ใช้ Ctrl + B % สำหรับเริ่ม vertical pane

คุณสร้าง horizontal และ vertical panes พร้อมกันได้ ใช้ hotkey Ctrl + B o สำหรับสลับระหว่าง panes

Toggling pane zoom ให้ผู้ใช้ซ่อน panes อื่นๆ ฟีเจอร์นี้ handy เมื่อต้อง focus งานเฉพาะ ใช้ Ctrl + B z สำหรับ toggle panes คุณปิด current pane ได้ตลอดเวลาด้วย hotkey Ctrl + B x

วิธีตั้งค่า Tmux ใน Linux

คุณตั้งค่าเกือบทุกด้านของ tmux installation ได้ อย่างไรก็ตาม เราแนะนำเริ่มจาก basics และไป advanced options ภายหลัง

Tmux configurations ทำได้โดย tweak ไฟล์ ~/.tmux.conf ใช้ Linux text editor ที่ชอบแก้ไขไฟล์นี้

vim ~/.tmux.conf

มาแสดง basic configurations บางอย่าง ตัวอย่าง คุณเปลี่ยน tmux prefix จาก Ctrl + B เป็น Ctrl + A โดยเพิ่มบรรทัดด้านล่างในไฟล์ tmux.conf

set -g prefix C-a
unbind C-b

Unbinding default key ให้เรา reassign ให้คำสั่งต่างได้ บรรทัดที่สองเป็น good practice ไม่ได้ mandatory

เรายังเปลี่ยน base index ของ windows และ panes จากศูนย์เป็นหนึ่งได้ มัน intuitive สำหรับผู้ใช้ที่ไม่สบายกับ zero-based index

set -g base-index 1 # starts window numbering from 1
set -g pane-base-index 1 # starts pane numbering from 1

เนื่องจาก tmux เป็น terminal-driven tool มันไม่ support mouse functions out of the box อย่างไรก็ตาม คุณ enable mouse support สำหรับ tmux ได้ง่ายโดยเพิ่มบรรทัดด้านล่างใน configuration

set -g mouse on

Introduction to Tmux Plugins

คุณเลือกจาก tmux plugins หลายตัวที่พัฒนาโดยชุมชน พวกเขาเสนอ additional features สำหรับปรับปรุง productivity ซึ่ง enhance overall workflow Standalone plugin managers บางตัวก็พร้อมใช้งาน ช่วย users implement plugins เหล่านี้เข้า tmux

The Tmux Plugin Manager (TPM) เป็นหนึ่งใน tools ที่ให้เราติดตั้งและตั้งค่า third-party plugins คุณติดตั้งได้โดยทำตามคำแนะนำบน official site ของ TPM

เราแนะนำให้ users check out tmux-resurrect package คุณใช้มันสำหรับ save tmux sessions ระหว่าง reboots

จัดการ Terminal Workflow ด้วย Tmux

Tmux เสนอ full-fledged solution สำหรับ terminal enthusiasts ที่มี hard time สลับระหว่าง windows มันเป็น great tool สำหรับคนที่ serious เกี่ยวกับ productivity แม้มี learning curve แต่จะกลายเป็น habit เมื่อคุณใช้ Tmux ต่อเนื่อง

พลังของ terminals ไม่ได้จำกัดแค่ Linux OS หรือคอมพิวเตอร์ทั่วไป คุณแม้แต่ใช้ terminal ใน smartphone และทำ basic Linux computations บน Android ได้