2017/12/24

tmux

Common Shortcuts

<prefix> means ctrl + b.

# start new session
$ tmux new -s myname

# list sessions
$ tmux ls

# attach to session
$ tmux a -t 0

# rename session
$ tmux rename-session -t 0 NEW_NAME

# kill session
$ tmux kill-session -t 0

Windows Control

  • <prefix> c — Create window
  • <prefix> w — List windows
  • <prefix> n — Next window
  • <prefix> p — Previous window
  • <prefix> f — Find window
  • <prefix> , — Name window
  • <prefix> & — Kill window
  • <prefix> [ — Check history

Pane Control

  • <prefix> % — Vertical split
  • <prefix> " — Horizontal split
  • <prefix> o — Swap panes
  • <prefix> q — Show pane numbers
  • <prefix> x — Kill pane
  • <prefix> q — Show pane numbers (press number key to switch to that pane)
  • <prefix> { — Move the current pane left
  • <prefix> } — Move the current pane right
  • <prefix> z — Toggle pane zoom

Resize Pane

  • <prefix> : resize-pane -D (Resizes the current pane down)
  • <prefix> : resize-pane -U (Resizes the current pane upward)
  • <prefix> : resize-pane -L (Resizes the current pane left)
  • <prefix> : resize-pane -R (Resizes the current pane right)
  • <prefix> : resize-pane -D 20 (Resizes the current pane down by 20 cells)
  • <prefix> : resize-pane -U 20 (Resizes the current pane upward by 20 cells)
  • <prefix> : resize-pane -L 20 (Resizes the current pane left by 20 cells)
  • <prefix> : resize-pane -R 20 (Resizes the current pane right by 20 cells)
  • <prefix> : resize-pane -t 2 20 (Resizes the pane with the id of 2 down by 20 cells)
  • <prefix> : resize-pane -t -L 20 (Resizes the pane with the id of 2 left by 20 cells)

Start With Configuration

#!/bin/bash
tmux -2 new-session \; \
send-keys ''cd /var/www/test; vim'' C-m \; \
split-window -h \; \
send-keys 'top''top' C-m \; \
resize-pane -R 30 \; \
split-window -v \; \
send-keys ''while true; do date; sleep 1; done'' C-m \; \

My Config

套件

  • tpm 套件管理
  • tmux-pain-control 提供便捷的快捷鍵來控制 tmux 面板的導航與大小調整
  • tmux-sensible 提供一組合理的預設設置,讓 tmux 更加易於使用,特別是對新手友好
  • tmux-copycat 讓你在 tmux 中進行文字搜尋,並能夠快速複製選中的文字

安裝套件管理

git clone https://github.com/tmux-plugins/tpm.git ~/.tmux/plugins/tpm

~/.tmux.conf

# ~/.tmux.conf
set -g default-terminal "xterm"
setw -g mode-keys vi
set -g pane-border-status top
set -g mouse on

# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'dracula/tmux'
set -g @dracula-plugins "git cpu-usage ram-usage time"
set -g @dracula-show-powerline true
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-pain-control'
set -g @plugin 'tmux-plugins/tmux-copycat'

bind-key -T copy-mode-vi 'v' send -X begin-selection

run '~/.tmux/plugins/tpm/tpm'

~/.bashrc

# ~/.bashrc
alias tmux='tmux -2'

開啟 tmux 後執行 <prefix> + I

Auto Start Config

#!/bin/sh

SERVICE=apache_watcher

tmux has-session -t $SERVICE

if [[ $? != 0 ]]; then
	tmux new -s $SERVICE -d

	tmux split-window -h
	tmux split-window -v -t $SERVICE:0.1
	tmux split-window -v -t $SERVICE:0.0

	tmux send-keys -t $SERVICE:0.0 'htop'  C-m
	tmux send-keys -t $SERVICE:0.2 'nload' C-m
	tmux send-keys -t $SERVICE:0.3 'sudo apachetop' C-m

	tmux clock-mode -t $SERVICE:0.1
	tmux resize-pane -R 30
fi

tmux a -t $SERVICE

Interesting Command

  • <prefix> : select-pane -T “NAME” - Set the title of the active pane
  • <prefix> : select-layout even-horizontal - Make horizontal pane widths equal
  • <prefix> : select-layout even-vertical - Make vertical pane heights equal
  • <prefix> : select-layout tiled - Arrange panes in a tiled layout
  • <prefix> space - Cycle through available pane layouts
  • <prefix> alt + arrow - Resize pane in the arrow’s direction
  • <prefix> : resize-pane -x 100 - Set the pane width to 100 cells
  • <prefix> : resize-pane -y 100 - Set the pane height to 100 cells

Reference Site

https://gist.github.com/MohamedAlaa/2961058
https://blog.htbaa.com/news/tmux-scripting

2017/12/18

Simple Regular Expression In PHP, Python, Node.js

今天需要用正規式找出符合 xxx[yyy] 後面配任意內容的結果,紀錄一下三種語言怎麼操作正規式。

PHP
<?php

$pattern = '/\w+\[\w+\].*/i';
$strings = [
    'aaa[bbb]',
    '1234[bbb][cccc]',
    '[111][22]',
    'dd[gg]ff',
    'aa[',
    'aaa][bbbb',
    'aaabbbccc',
];

foreach ($strings as $s) {
    var_dump($s.' is '.preg_match($pattern, $s));
}
Python
import re

pattern = r'\w+\[\w+\].*';
strings = [
    'aaa[bbb]',
    '1234[bbb][cccc]',
    '[111][22]',
    'dd[gg]ff',
    'aa[',
    'aaa][bbbb',
    'aaabbbccc'
];

for s in strings:
    print('%s is %s' % (s, True if re.match(pattern, s, re.IGNORECASE) else False))
Node.js
const pattern = /\w+\[\w+\].*/i;
const strings = [
    'aaa[bbb]',
    '1234[bbb][cccc]',
    '[111][22]',
    'dd[gg]ff',
    'aa[',
    'aaa][bbbb',
    'aaabbbccc'
];

for (v of strings) {
    console.log(v, 'is', pattern.test(v));
}