A collection of useful command line tricks
To create a file including content (overwriting the file if it exists), use echo with the -e flag (to allow for escape sequences like the \n newline) and redirect to a file with >:
echo -e 'abc\ndef' > 1.txtNow 1.txt will include the following content:
abc
def
To make multi-line content more readable, use cat with a heredoc tag like EOF:
cat << EOF > main.ts
import { add } from './add.js';
console.log(add(2,3));
EOFCombine two video files using FFmpeg
ls video1.mp4 video2.mp4 | while read line; do echo file \'$line\'; done | ffmpeg -protocol_whitelist file,pipe -f concat -i - -c copy output.mp4Source: How to concatenate two MP4 files using FFmpeg? (Answer) | Stack Overflow
Sometimes you may want to search and replace within your Git remote - for example, if you change your username on GitHub. This is a command to do it (replaces OldUsername with NewUsername):
git remote set-url origin $(current_origin=$(git remote get-url origin --push) && echo ${current_origin/OldUsername/NewUsername})Loop over CSS, JS, EOT, SVG and TTF files and gzip them into a folder named gzipped.
gzip-web-assets.sh
#!/usr/bin/env bash
# Usage:
# $ ./gzip-web-assets.sh
mkdir gzipped
for file in $(find . -type f -depth 1 | egrep "\.(css|js|eot|svg|ttf)$") ; do
gzip -c --best $file > gzipped/$file
doneTo set a custom terminal tab title with zsh, configure the zsh precmd and preexec hook functions in .zshrc:
~/.zshrc
# Disable auto-setting terminal title.
DISABLE_AUTO_TITLE="true"
function precmd () {
echo -ne "\033]0;Custom title: $(print -rD $PWD)\007"
}
precmd
function preexec () {
print -Pn "\e]0;🚀 $(print -rD $PWD) $1 🚀\a"
}This will change the tab title as such:
- Show the
Custom title:prefix before the present working directory - When a foreground program is running, rocket emojis will appear at the beginning and end of the prompt to show a visual indicator that something is running
Screen.Recording.2025-02-15.at.17.36.47.mov
To use this with Ghostty, disable Ghostty's built-in shell integration using shell-integration-features = no-title:
~/Library/Application Support/com.mitchellh.ghostty/config
# Disable Ghostty built-in tab title shell integration
# https://ghostty.org/docs/config/reference#shell-integration-features
shell-integration-features = no-titleTo wrap a command and exit based on stdout from the command, loop over each line of the stdout to look for the message. If the message is found, set a variable to true and exit with code 1:
pnpm install | { has_ignored_build_scripts=false; while IFS= read -r line; do echo "$line"; [[ "$line" == *"Ignored build scripts:"* ]] && has_ignored_build_scripts=true; done; [[ "$has_ignored_build_scripts" = false ]]; }