tmux with mosh

  • Terminal emulator supporting OSC52
    • iTerm2, Preference -> General -> Applications in terminal may access clipboard
    • mlterm, –osc52=true
  • latest mosh from git (tagged releases are too old, both client and server)
  • latest tmux
  • Override the OSC 52 sequence generated by tmux by adding the following line to your tmux.conf
    • set-option -ag terminal-overrides ",xterm-256color:Ms=\\E]52;c;%p2%s\\7"

Reference

tmux + mosh OSC 52 clipboard paste hack
tmux in practice: integration with the system clipboard

Git Rebase

1
2
3
4
5
6
7
8
# git pull is always equivalent to git pull --rebase (unless branch.<branchname>.rebase is explicitly set to false)
git config --global pull.rebase true

# Force existing branches to use rebase.
git config branch.<branchname>.rebase true

# Force new branches to use rebase
git config --global branch.autosetuprebase always

Count Lines of Code

1
2
3
4
5
# count lines of code in current directory
grep -c -e "$" -r -h . | python -c "import sys; a = sys.stdin.read().strip().split(); a = [int(x) for x in a]; print(sum(a));"

NAME_PATTERN="*.cc"
find . -type f -name "${NAME_PATTERN}" -print0 |xargs -0 -n1 grep -c -e "$" | python -c "import sys; a = sys.stdin.read().strip().split(); a = [int(x) for x in a]; print(sum(a));"

jQuerify

1
2
3
4
5
6
7
8
9
if (typeof $.fn != 'undefined' && typeof $.fn.jquery != 'undefined') {
console.log("jquery " + $.fn.jquery + " already exist");
} else {
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', 'https://code.jquery.com/jquery-3.3.1.min.js');
document.body.appendChild(script);
console.log("jquerfied");
}

Matlab/Octave Notes

Operators

1
2
eq, ==
ne, ~=

Matrix index

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
a = [1;2;3];
% index starts from 1, including 2, i.e. [1;2]
a(1:2);

b = [1,3,7;
2,5,8;
3,6,9];

% true
b(1) == 1;
b(2) == 2;
b(9) == 9;

% 1 3 7
b(1,:)

% 1
% 2
% 3
% 4
% 5
% 6
% 7
% 8
% 9
b(:)

% unroll
% c = [ 1
% 2
% 3
% 1
% 2
% 3
% 4
% 5
% 6
% 7
% 8
% 9 ]
c = [a(:); b(:)]

b = reshape(c(4:12), 3, 3);

References

Matlab Language Fundamentals
GNU Octave Doc

xargs Notes

Useful Options

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#-n max-args, --max-args=max-args
# Use at most max-args arguments per command line.
find . | xargs -n1 echo

#-I replace-str
find . | xargs -n1 -I% echo % suffix

#-P max-procs, --max-procs=max-procs
# Run up to max-procs processes at a time; the default is 1.
find . | xargs -P10 -n1 -I% sleep 1

#-0, --null
# Input items are terminated by a null character instead of by whitespace.
find . -type f -name "*.java" -print0 |xargs -0 grep -li "keyword"

#-p, --interactive
# Prompt the user about whether to run each command line and read a line from the terminal. Implies -t.
find . | xargs -p -n1 echo

#-t, --verbose
# Print the command line on the standard error output before executing it.

#--delimiter=delim, -d delim
# Input items are terminated by the specified character.

#-a file, --arg-file=file
# Read items from file instead of standard input.

Tricks

Execute Shell Scripts

1
2
3
4
5
6
# The word bash at the end of the line is interpreted by bash
# -c as special parameter $0.
# The single quote ' is critical, otherwise $filename will be
# replaced before the script is executed.
find /path -type f -name '*~' -print0 | xargs -0 bash -c \
'for filename; do cp -a "$filename" ~/backups; done' bash

Prevent xargs from Quitting on Error

1
2
# instead echo the commands and pipe to bash, echo will not return error
find . | xargs -n1 echo command_that_might_exit | bash

Reference

xargs - Wikipedia
prevent xargs from quitting on error

Increase Open File Limit

1
2
3
4
5
6
7
8
9
10
11
12
# Add the following line to /etc/security/limits.conf
# {user}/@{group} soft/hard/-(both) {item} {vaule}
* - nofile 999999

# make sure the file (/etc/pam.d/login for console, /etc/pam.d/lightdm for lightdm and so on) contains the following line
session required pam_limits.so

# login again

# Check
ulimit -Sn # soft limit; can be raised up to the hard limit
ulimit -Hn # hard limit

Reference

Increase the open files limit on Linux