Pro Yearly is on sale from $80 to $50! »

tmux 入門 / Introduction to tmux

tmux 入門 / Introduction to tmux

CAMPHOR- HOUSE で開催された,「CLI について語る会」 https://camphor.connpass.com/event/57358/ での発表資料です.

D984cd543d2abef28596cc5cfe82240c?s=128

Yusuke Miyazaki

May 28, 2017
Tweet

Transcript

  1. CLI ʹ͍ͭͯޠΔձ tmux ೖ໳ Yusuke Miyazaki @ymyzk May 28th, 2017

    CAMPHOR- HOUSE 1 / 30
  2. ͓͠ͳ͕͖ 1. ͸͡Ίʹ 2. tmux ͷػೳ঺հ 3. tmux ͷ࢖͍ํ 4.

    tmux ͷ׆༻ 2 / 30
  3. tmux ͱ͸? 3 / 30

  4. tmux is a terminal multiplexer 4 / 30

  5. terminal multiplexer? It lets you switch easily between several programs

    in one terminal, detach them (they keep running in the background) and reattach them to a different terminal. And do a lot more. — From http://tmux.github.io 5 / 30
  6. tmux ͷػೳ 6 / 30

  7. ͜Μͳͱ͖͋Γ·ͤΜ͔? (λʔϛφϧͰ)͋Δ࡞ۀΛ͍͕ͯͨ͠, ผͷ࡞ۀΛ͍ͨ͠ Ͱ΋, લͷը໘͸ดͨ͘͡ͳ͍ ΤσΟλͰϓϩάϥϜΛॻ͖ͳ͕Β, ͦͷϓϩάϥϜΛί ϯύΠϧ/࣮ߦ͍ͨ͠ 7 /

    30
  8. ͜Μͳ͜ͱͯ͠·ͤΜ͔? λʔϛφϧͷ΢Οϯυ΢΍λϒΛ։͚͹ྑ͍͕, ࢖͍ʹͦ͘͏ 8 / 30

  9. tmux Λ࢖͏ͱ 1ͭͷλʔϛφϧͰηογϣϯ ɾ ΢Οϯυ΢ ɾ ϖΠϯΛ࢖ͬͯ, ෳ਺ ͷϓϩάϥϜΛ࣮ߦͰ͖Δ! 9

    / 30
  10. ͜Μͳ͜ͱ͋Γ·ͤΜ͔? 2 ࣮ߦʹ͕͔͔࣌ؒΔॲཧΛߦ͍ͬͯΔؒ, ผͷ࡞ۀΛ͍ͨ͠ SSH Ͱ࡞ۀ͍͕ͯͨ͠, ωοτϫʔΫ͕੾Εͯ, ࡞ۀ͕தஅ ͯ͠͠·ͬͨ !

    ͋Δ୺຤Ͱ࡞ۀ͍͕ͯͨ͠, ผͷ୺຤ʹҠಈͯ͠࡞ۀΛଓ͚ ͍ͨ 10 / 30
  11. tmux Λ࢖͏ͱ σλονͯ͠, ࡞ۀΛதஅग़དྷΔ ϓϩάϥϜΛόοΫάϥ΢ϯυͰ࣮ߦͨ͠·· Ξλονͯ͠, ࡞ۀΛ࠶։͢Δ͜ͱ͕ग़དྷΔ SSH ઀ଓ͕੾Εͯ΋, Ξλον͢Ε͹େৎ෉

    ෳ਺ͷ৔ॴ͔Βಉ͡ηογϣϯΛΞλονͰ͖Δ 11 / 30
  12. ·ͱΊ: tmux Ͱग़དྷΔ͜ͱ 1ͭͷλʔϛφϧͰෳ਺ͷϓϩάϥϜΛ࣮ߦ (ηογϣϯ ɾ ΢Οϯυ΢ ɾ ϖΠϯ) ը໘

    (΢Οϯυ΢) Λ੾Γସ͑Δ ΢Οϯυ΢Λ෼ׂͯ͠࢖͏ (ϖΠϯ) λʔϛφϧͰͷ࡞ۀΛதஅ ɾ ࠶։ (σλον ɾ Ξλον) ෳ਺ͷλʔϛφϧ͔Β1ͭͷηογϣϯΛಉ࣌ʹΞλον ෳ਺ͷϖΠϯʹಉ࣌ʹೖྗ etc 12 / 30
  13. ࢖ͬͯΈͨ͘ͳΓ·͔ͨ͠? " 13 / 30

  14. ͪΐͬͱدΓಓ # 14 / 30

  15. ൪֎ฤ: GUI Ͱ͸μϝ? $ ྫ͑͹iTerm (macOS ༻) Ͱ΋ը໘෼ׂ͸ग़དྷΔ ηογϣϯΛΞλον ɾ

    σλον͢Δػೳ͕ͳ͍ (ཁௐࠪ) OS ʹґଘͯ͠͠·͏ͷͰ, ଞͷ OS Ͱಉ͡΋ͷ͕࢖͑ͳ͍ SSH ܦ༝Ͱαʔόʔʹ઀ଓͨ͠ࡍͳͲʹ͸࢖͑ͳ͍ ͔ͬ͠Γ࢖͏ͳΒ tmux ͷํ͕ྑͦ͞͏ 15 / 30
  16. ൪֎ฤ: ྨࣅͷϓϩδΣΫτ GNU Screen ػೳ͸ tmux ʹࣅ͍ͯΔ ͪ͜Βͷํ͕͔ͳΓݹ͍ϓϩδΣΫτ (80೥୅ʙ) GPL

    ϥΠηϯε (फڭతཧ༝͕͋Ε͹) Byobu screen ΍ tmux ͷϥούʔ 16 / 30
  17. tmux Λ࢖ͬͯΈΑ͏ 17 / 30

  18. Πϯετʔϧ Linux (Debian ܥ): apt install tmux macOS (Homebrew): brew

    install tmux Windows: Bash on Windows ͰೖΔΒ͍͠...? (ཁݕূ) 18 / 30
  19. ىಈ ɾ جຊૢ࡞ ɾ ऴྃ ىಈ: tmux جຊૢ࡞͸ Prefix-key some-key

    Prefix-key ͷσϑΥϧτ͸ Ctrl-b Ctrl-b ͸ Ctrl Λԡ͠ͳ͕Β b Λԡ͢ͷҙ มߋͰ͖·͢ (มߋͯ͠Δਓ͕ଟ͍ؾ͕͢Δ) ྫ Prefix c : Ctrl-b Λԡͯ͠, Ұ౓ΩʔΛ཭ͯ͠ c Λԡ͢ ৽ن΢Οϯυ΢͕։͖·͢ ऴྃ: ΢Οϯυ΢͕ͳ͘ͳΔͱউखʹऴྃ 19 / 30
  20. ηογϣϯͷૢ࡞ σλον: Prefix d ηογϣϯΛσλον ϓϩάϥϜ͸όοΫάϥ΢ϯυͰ࣮ߦ͞Ε·͢ ྲྀੴʹϚγϯΛ࠶ىಈ͢Δͱఀࢭ͠·͢ Ξλον: tmux attach

    -t <session_name> σλονͨ͠ηογϣϯΛ։͘ ηογϣϯ໊ͷมߋ: Prefix $ 20 / 30
  21. ΢Οϯυ΢ૢ࡞ ΢Οϯυ΢࡞੒: Prefix c ΢Οϯυ΢੾Γସ͑: ࣍ͷ΢Οϯυ΢: Prefix n લͷ΢Οϯυ΢: Prefix

    p x ൪ͷ΢Οϯυ΢: Prefix <x> 21 / 30
  22. ϖΠϯૢ࡞ ΢Οϯυ΢Λ෼ׂͯ͠ϖΠϯΛ࡞੒ ॎʹ෼ׂ (ԣʹϖΠϯΛฒ΂Δ): Prefix % ԣʹ෼ׂ (ॎʹϖΠϯΛฒ΂Δ): Prefix "

    ϖΠϯͷ෼ׂΛ΍Ίͯ΢Οϯυ΢ʹ: Prefix ! ϖΠϯͷ੾Γସ͑: Prefix o 22 / 30
  23. ηογϣϯ ɾ ΢Οϯυ΢ ɾ ϖΠϯ 23 / 30

  24. ηογϣϯ ɾ ΢Οϯυ΢ ɾ ϖΠϯ ηογϣϯ1 (Ξλον ɾ σλον͢Δͷ͸͜ͷ୯Ґ) ΢Οϯυ΢1

    (λϒʹ͍ۙ) ϖΠϯ1 (෼ׂ͞Εͨ1ͭͷ෦෼) ϖΠϯ2 ΢Οϯυ΢2 ϖΠϯ1 ΢Οϯυ΢3 ϖΠϯ1 ηογϣϯ2 ... 24 / 30
  25. tmux Λ׆༻͠Α͏ 25 / 30

  26. tmux ͷઃఆ tmux ͷΩʔόΠϯυ౳͸ઃఆϑΝΠϧͰมߋͰ͖Δ ઃఆ͸ ~/.tmux.conf ʹஔ͘ Vi-like ͳઃఆ΍ Emacs-like

    ͳઃఆ΋બ΂Δ ࣗ෼ͷखʹͳ͡ΉઃఆΛ͍ͯ͘͠ ৽͍͠ઃఆΛಡΈࠐΉࡍͷ஫ҙ tmux source-file ~/.tmux.conf Λ࣮ߦ͢Δ͔ ͢΂ͯͷηογϣϯΛऴྃͯ͠࠶ىಈ͢Δ 26 / 30
  27. tmux.conf ͷྫ # Prefix ͷมߋ (Ctrl-t ʹ) unbind C-b set

    -g prefix C-t bind C-t send-prefix # ΢Οϯυ΢ͷ൪߸Λ 1-index ʹ (Ωʔ഑ྻͱἧͬͯ௚ײత) set -g base-index 1 # Prefix Ctrl-r ͰઃఆಡΈࠐΈ bind C-r source-file ~/.tmux.conf 27 / 30
  28. ͞Βʹৄ͘͠ ݱࡏͷઃఆ: Prefix ? ϚχϡΞϧ: man 1 tmux ઃఆΛ୳͢: GitHub

    Ͱݕࡧ filename:tmux.conf @ymyzk ͷઃఆ͸ https://github.com/ymyzk/dotfiles ʹ͋Γ·͢ ϓϥάΠϯ: Tmux Plugin Manager Tips: tmux ͸ΞοϓσʔτͰઃఆͷ࢓༷͕Α͘มΘΔͷͰ, ৘ใͷ৽͠͞΍, ࣗ෼ͷ࢖͍ͬͯΔ tmux ͷόʔδϣϯʹ஫ ҙ! 28 / 30
  29. ·ͱΊ շదͳ CLI ؀ڥʹ͸ tmux ͷΑ͏ͳλʔϛφϧϚϧνϓϨΫ α͕ඞਢ tmux ͕खʹ߹Θͳ͍ਓ͸ screen

    ΍ byobu Λࢼͯ͠Έ ͯ΋͍͍͔΋ গͣͭ͠ઃఆΛ௥Ճͯ͠, ࣗ෼ͷखʹͳ͡ΉઃఆΛ 29 / 30
  30. Happy Hacking with tmux!! 30 / 30