今日已更新 80 条资讯 | 累计 20052 条内容
关于我们

标签:#zsh

找到 2 篇相关文章

AI 资讯

My favourite zsh/bash shortcuts (functions and aliases)

Introduction My zsh profile is over 1000 lines at this point. A lot of that is functions I asked AI to generate for me, since it's fast, portable, and saves me a ton of typing. Here's the thing though: the shortcuts that save me the most time aren't the clever ones. They're the dumb ones. Things like clone instead of git clone && cd , or dir instead of mkdir -p && cd . Each one only saves a second or two, but I run them so often that it adds up fast. These are in no particular order, just the ones I reach for constantly. Git aliases for common commands A few one-liners I have set up as plain aliases: alias gcp = "git cherry-pick" alias git-append = "git commit --amend --no-edit -a" gcp is self-explanatory. git-append amends the last commit with your currently staged (and unstaged, thanks to -a ) changes without touching the commit message. Great for fixing up a commit you just made before you push. Create a branch or switch to it if it already exists One of my most-used functions. Normally you have to remember whether a branch exists before deciding between git checkout <branch> and git checkout -b <branch> . This just does the right thing either way: gb () { if git rev-parse --verify --quiet " $1 " > /dev/null ; then git checkout " $1 " else git checkout -b " $1 " fi } Nuke all local changes to reset the working tree When an experiment goes sideways or I just want to throw everything away and start clean, I run nah : nah () { git reset --hard git clean -df if [ -d ".git/rebase-apply" ] || [ -d ".git/rebase-merge" ] ; then git rebase --abort fi } This resets tracked changes, removes untracked files and directories. No confirmation prompt, so use it carefully. Print recent commits as ready-to-paste cherry-pick commands Useful when you need to cherry-pick a batch of commits from one branch onto another in order: logs () { if [[ -z " $1 " || " $1 " = ~ [ ^0-9] ]] ; then echo "Usage: logs <number_of_commits>" return 1 fi git log -n " $1 " --reverse --pretty = format: "g

2026-07-09 原文 →
开发者

一個很小但很好用的 zsh 技巧:修改上一個指令

有時候我剛跑完一個指令,馬上發現其實只需要改其中一小部分。 例如我剛用 ffmpeg 轉了一個影片: ffmpeg -i calligraphy01.mp4 -c :v libx264 -c :a aac calligraphy_good_01.mp4 然後我想用同樣的指令處理下一個檔案: ffmpeg -i calligraphy02.mp4 -c :v libx264 -c :a aac calligraphy_good_02.mp4 如果用傳統方法,我可能會按上箭頭,然後手動把兩個地方的 01 改成 02 。 但在 zsh 裡,可以直接輸入: !! :gs/01/02/ 意思是: 拿上一個指令,把所有的 01 都換成 02 ,然後執行。 所以這個: !! :gs/01/02/ 會展開成: ffmpeg -i calligraphy02.mp4 -c :v libx264 -c :a aac calligraphy_good_02.mp4 語法是什麼意思? !! 代表「上一個指令」。 :gs/01/02/ 代表「把所有 01 全部替換成 02 」。 所以完整的: !! :gs/01/02/ 意思就是: 使用上一個指令,把所有 01 改成 02 ,然後執行。 先預覽,不要馬上執行 有時候我不想讓它立刻執行,尤其是指令比較長、比較重要,或者執行成本比較高的時候。 這時可以加上 :p : !! :gs/01/02/:p 它會只印出修改後的指令,不會執行: ffmpeg -i calligraphy02.mp4 -c :v libx264 -c :a aac calligraphy_good_02.mp4 如果看起來沒問題,就按一下 上箭頭 ,把剛剛印出的指令帶回命令列,再檢查一次,然後按 Enter 執行。 比較安全的流程就是: !! :gs/01/02/:p 確認輸出結果後: 上箭頭 → 再看一次 → Enter 這對比較長、比較容易打錯、或不想立刻執行的指令很有用。 只替換第一個地方 如果你只想替換第一個出現的地方,也可以用: ^01^02 不過像上面的 ffmpeg 例子,輸入檔名和輸出檔名裡都有 01 ,所以通常會比較適合用: !! :gs/01/02/ 這種小技巧每次可能只省幾秒鐘,但如果你常常在 command line 裡重複處理檔案、日期、編號、影片、圖片或 migration,累積起來真的會讓工作順很多。

2026-06-13 原文 →