linux

奇技淫巧

# 替换传统的cat
sudo apt install bat
mkdir -p ~/.local/bin
ln -s /usr/bin/batcat ~/.local/bin/bat
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc

Basic command for linux

Write what comes to mind

uppercase word such as C is Ctrl

common command

  1. tldr anything
1
tldr -u  # update tldr
  1. Compare two file
1
2
diff a b
md5sum a b
  1. 列出一个 C 语言项目中所有被包含过的头文件
1
find . -name "*.[ch]" | xargs grep "#include" | sort | uniq
  1. 各种工具
  • 文件检索 - cat, more, less, head, tail, file, find
  • 输入输出控制 - 重定向,管道,tee, xargs
  • 文本处理 - vim, grep, awk, sed, sort, wc, uniq, cut, tr
  • 正则
  • 系统监控 - jobs, ps, top, kill, free, dmesg, lsof

vim

  • 左下上右

h j k l ← ↓ ↑ →

  • 保存退出

E-:wq

sed

sed 是一个拿来随手替换文本的工具(有时候 ubuntu 出锅了可以强行更新一波源而玄学地解决

1
sed -i 's/focal/jammy/g' /etc/apt/sources.list

s/要替换的正则/替换为/g

tmux

tmux 是一个终端管理工具,主要作用是保留正在运行的进程和切割终端。

Terminal multiplexer. It allows multiple sessions with windows, panes, and more.See also zellij and screen.More information: https://github.com/tmux/tmux.

  • Start a new named session:

    tmux new -s {{name}}

  • List existing sessions:

    tmux ls

  • Attach to the most recently used session [by name]:

    tmux attach [-t {{name}}]

  • Kill a session by name:

    tmux kill-session -t {{name}}

  • Rename a session by name:

    tmux rename-session -t {{name}} {{new_name}}

  • Delete current session (inside a tmux session):

    C-d

  • Detach from the current session (inside a tmux session):

    C-b d

  • Split a new pane to right (inside a tmux session):

    C-b %

  • Split a new pane to down (inside a tmux session):

    C-b "

  • Create a new window (inside a tmux session):

    C-b c

  • Switch between sessions and windows (inside a tmux session):

    C-b w C-b o C-b C-o

  • Zoom in or zoom out a pane (inside a tmux session):

    C-b z

  • Resize pane in direction of (inside a tmux session):

    C-b C-<arrow key>

  • Rename the current window (inside a tmux session):

    C-b ,

tmux 自动保存与恢复插件

Tmux Resurrect 手动保存恢复 Tmux Continuum 定时保存恢复(依赖 Tmux Resurrect)

mkdir -p ~/.tmux
cd ~/.tmux
git clone git@github.com:tmux-plugins/tmux-resurrect.git
git clone git@github.com:tmux-plugins/tmux-continuum.git

vim ~/.tmux.conf, append to the end.

1
2
run-shell ~/.tmux/tmux-resurrect/resurrect.tmux
run-shell ~/.tmux/tmux-continuum/continuum.tmux
# 重新加载配置文件
tmux source-file ~/.tmux.conf
1
2
C-b + C-s: save
C-b + C-r: reload

Cmake

lscpu 查看 cpu 数 time make 查看运行时间 make -j? ? 为 cpu 数,可以通过多线程加速。

  • ccache 将编译没发生变化的部分加速
    1
    2
    
    # append to .zshrc
    export PATH="/usr/lib/ccache:$PATH"
    

ccache

只输出命令但不执行

make -nB

git

git 可视化练习

精巧的小游戏

gdb

gcc main.c -Wall -Werror -o main -Wall 提示所有 Warning -Werror 将 Warning 视为错误

inside gdb

breakpoint 114514 watch val next continue print val

str

strtok() stores the pointer in static variable where did you last time left off , so on its 2nd call , when we pass the null , strtok() gets the pointer from the static variable .

1
2
//左对齐 20 位并且前导 10 个 0
printf("%-20.010X",0x3f3f3f3f)

正则

元字符 . 将匹配除换行符以外的任意字符;

转义字符 \ 将下一字符标记为特殊字符、原义字符等,如 \d 匹配一个数字,或把 \. 解释为小数点而不是匹配任意字符的元字符;

元字符 \w 将匹配一个字母或数字或下划线或汉字,如 \w 可以匹配 1,也可以匹配 a

元字符 \s 将匹配一个空白字符,如 \s 可以匹配 <space>,也可以匹配 <tab>,甚至可以匹配换行符;

元字符 \d 将匹配一个数字,如 \d 可以匹配 1,也可以匹配 2

元字符 ^$ 分别匹配字符串的开始和结束,如 ^ 匹配字符串 abc 的字符 a 之前的那个位置,$则匹配字符 c 之后的那个位置;

限定符 * 将匹配前子表达式 0-n 次,如 \d*abc 可以匹配 1234abc,亦可以匹配 abc

限定符 + 将匹配前子表达式 1-n 次,如 \d+abc 可以匹配 1234abc,但不能匹配 abc

限定符 {n} 将匹配前子表达式 n 次,其中 n 为非负整数,如 \d{5}abc 可以匹配 12345abc,但不能匹配 123abc

限定符 {n,m} 匹配前子表达式 n-m 次,其中 n,m 为非负整数,n<=m,如 \d{1,3}abc 可以匹配 1abc,13abc,但不能匹配 abc,然而,可以匹配 1234abc234abc 部分;

字符类的表达可以使用一对中括号 [],如 [a-c1-5] 可以匹配 a ,b, c, 1, 2, 3, 4, 5 之中的任意一个字符,如表达式 [a-c1-5]{3} 可以匹配 a1c, 23b 等等字符串;

分枝条件 abc|123 将匹配字符串 abc123

贪婪

a.*b

懒惰

a.*?b匹配最短的,以a开始,以b结束的字符串。如果把它应用于aabab的话,它会匹配aab(第一到第三个字符)和ab(第四到第五个字符)。

. 匹配除换行符以外的任意字符

\w 匹配字母或数字或下划线或汉字

\s 匹配任意的空白符

\d 匹配数字

\b 匹配单词的开始或结束

^ 匹配字符串的开始

$ 匹配字符串的结束

Example of POSIX regex in C

 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
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <sys/types.h>
#include <regex.h>

int regcomp(regex_t *preg, const char *regex, int cflags);
	Prepare your regex for fast processing
int regexec(const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags);
	Do the matching
void regfree(regex_t *preg);
	Free your compiled regex for a new "compiling"
size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size);
	Retrieve some more information on why the regexec() failed

///////////////////////////////////////////
#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  regex_t regex;
  int reti;
  char msgbuf[100];

  /* Compile regular expression */
  reti = regcomp(&regex, "(.*)", REG_EXTENDED);
  if (reti)
  {
    fprintf(stderr, "Could not compile regex\n");
    exit(1);
  }

  /* Execute regular expression */
  regmatch_t pmatch;
  reti = regexec(&regex, "-(asdf)adsf", 1, &pmatch, 0);
  // 第三个参数如果是 0,则仅判断是否匹配
  printf("%d %d",pmatch.rm_so,pmatch.rm_eo);
  if (!reti)
  {
    puts("Match");
  }
  else if (reti == REG_NOMATCH)
  {
    puts("No match");
  }
  else
  {
    regerror(reti, &regex, msgbuf, sizeof(msgbuf));
    fprintf(stderr, "Regex match failed: %s\n", msgbuf);
    exit(1);
  }

  /* Free compiled regular expression if you want to use the regex_t again */
  regfree(&regex);

  return 0;
}
1
2
some examples
((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)

https://learn.microsoft.com/zh-cn/dotnet/standard/base-types/regular-expression-language-quick-reference?redirectedfrom=MSDN

https://deerchao.cn/tutorials/regex/regex.htm#mission

.zshrc

  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
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# alias
alias apigen="goctl api go -api *.api -dir ../  --style=goZero"
alias proxy="source /bin/proxy.sh"
alias ll="ls -alF"
alias la="ls -A"
alias l="ls -CF"
alias say="echo"

. /bin/proxy.sh set

# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# If you come from zsh you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
# ZSH_THEME="robbyrussell"
ZSH_THEME="powerlevel10k/powerlevel10k"

# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in $ZSH/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment one of the following lines to change the auto-update behavior
# zstyle ':omz:update' mode disabled  # disable automatic updates
# zstyle ':omz:update' mode auto      # update automatically without asking
# zstyle ':omz:update' mode reminder  # just remind me to update when it's time

# Uncomment the following line to change how often to auto-update (in days).
# zstyle ':omz:update' frequency 13

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# You can also set it to another string to have that shown instead of the default red dots.
# e.g. COMPLETION_WAITING_DOTS="%F{yellow}waiting...%f"
# Caution: this setting can cause issues with multiline prompts in zsh < 5.7.1 (see #5765)
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(
    z
    docker
    git
    sudo
    zsh-autosuggestions
    # zsh-syntax-highlighting
)

source $ZSH/oh-my-zsh.sh

# User configuration

# export MANPATH="/usr/local/man:$MANPATH"

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='mvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch x86_64"

# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

#ccache
export PATH="/usr/lib/ccache:$PATH"
export NEMU_HOME=/home/loomt/CSLearning/PA/nemu
export AM_HOME=/home/loomt/CSLearning/PA/abstract-machine
# go
#根目录
export GOROOT=/usr/local/go
#工作目录
export GOPATH=/home/loomt/go
#bin目录
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOPATH
export PATH=$PATH:$GOBIN
export PATH=$PATH:$GOROOT/bin

wsl

不访问 windows 路径,但也无法用code .在 vscode 打开当前目录

# /etc/wsl.conf
[interop]
appendWindowsPath = false

reference

tmux nju-PA

updatedupdated2024-04-122024-04-12