Mac 必装命令行工具 1)
作为一个命令行爱好者,Mac 的终端 terminal 真的是心头爱,各种命令行工具简直不要太爽。一定场景下,生产力直接起飞。
下面列几个我几乎每天都在用的命令行工具。
tldr
TOO LONG ,DON'T READ。
Unix 生存第一法则: 快使用 man
很多命令行工具往往都有各种参数、各种使用说明,往往很难记得清楚。而 man 里面的帮助文档也往往又臭又长,我只是想知道咋用,我不太知道那么多啊!!
tldr 就是个精简版的 man , Displays simple help pages for command-line tools。
Install
brew install tldr
使用
$ tldr grep
grep
Matches patterns in input text.
Supports simple patterns and regular expressions.
- Search for a pattern within a file:
grep search_pattern path/to/file
- Search for an exact string:
grep -F exact_string path/to/file
- Search for a pattern [R]ecursively in the current directory, showing matching line [n]umbers, [I]gnoring non-text files:
grep -RIn search_pattern .
- Use extended regular expressions (supporting `?`, `+`, `{}`, `()` and `|`), in case-insensitive mode:
grep -Ei search_pattern path/to/file
- Print 3 lines of [C]ontext around, [B]efore, or [A]fter each match:
grep -C|B|A 3 search_pattern path/to/file
- Print file name with the corresponding line number for each match:
grep -Hn search_pattern path/to/file
- Use the standard input instead of a file:
cat path/to/file | grep search_pattern
- Invert match for excluding specific strings:
grep -v search_pattern
当然,你也可以: tldr tldr 来看看 tldr 的用法。
tldr 对于我这种记不住各种用法的玩家来说简直太友好了。
ripgrep
R.I.P, grep, plz!
你知道 grep 用法吗?
ripgrep 是一个 rust 编写的可以用来替代 grep 的命令行工具。 ripgrep 相比于 grep 搜索更快,附带了一些默认行为,比如会忽略 .gitignore 里面所配置的一些文件和目录以及隐藏文件之类的。
Install
brew install ripgrep
更多安装方式可以参考GitHub仓库中的README.md的 installation 章节。
使用
# 默认搜索行为,默认keywords 为正则模式搜索
rg 'abc'
# 通过-g 配置搜索路径
rg 'abc' -g '!node_modules/**' -g '!dist/**'
# plain-text 搜索
rg -F 'this.'
更多使用方法既可以 tldr rg 也可以在 GitHub上查看。
fd
「 fd是一种简单ㄡ快速和用户友好的find替代方案.」
fd 同样是一个rust 编写的 find 的替代工具,当然,官方也明说了 不寻求复刻find所有强大的功能,但它提供了明智的 (自定的) 80%的用例 。 相比于 find ,fd 有很多优点。
Install
brew install fd
使用
fd [FLAGS/OPTIONS] [<pattern>] [<path>...]
# 搜索README
fd README
# 在 src 目录下搜索 README
fd README ./src
End
以上三个命令行工具在我的日常工作中扮演了不可缺少的角色。
工具只是辅助,输出还是得靠DPS啊,黑猫白猫抓到老鼠就是好猫。
$ tldr grep > grep.help | fd -e help --exec rg grep --files-with-matches | xargs -n1 sed -i '' -e 's/grep/rg/g'