Today I Learned
A collection of useful things I’ve picked up along the way.
Shell & Terminal
cdwith no args takes you to your home directory.cd -takes you to the previous directory.!!repeats the last command. Useful withsudo !!.!$refers to the last argument of the previous command.ctrl+rlets you search through command history.ctrl+amoves cursor to start of line,ctrl+eto end.ctrl+uclears everything before the cursor.ctrl+kclears everything after the cursor.ctrl+wdeletes the word before the cursor.alt+.inserts the last argument from the previous command.pbcopyandpbpasteon macOS let you use the clipboard from terminal.cat file | pbcopycopies file contents to clipboard.open .on macOS opens Finder in the current directory.command -vchecks if a command exists (portable alternative towhich).- Wrapping a command in
$()captures its output:echo "Today is $(date)". &&runs the next command only if the previous succeeded.||runs the next command only if the previous failed.;runs commands sequentially regardless of success/failure.> fileoverwrites,>> fileappends.2>&1redirects stderr to stdout.teewrites to both stdout and a file:command | tee output.txt.watch -n 2 commandruns a command every 2 seconds.time commandshows how long a command takes to run.history | grep patternsearches your command history.fcopens your last command in an editor.jobslists background processes,fgbrings one to foreground.nohup command &keeps a process running after terminal closes.disowndetaches a running job from the terminal.
Git
git checkout -checks out the previous branch.git switch -also switches to the previous branch (newer syntax).git stashsaves uncommitted changes for later.git stash popapplies and removes the most recent stash.git stash listshows all stashes.git stash show -pshows the diff of the most recent stash.git diff --stagedshows changes that are staged for commit.git log --onelineshows compact commit history.git log --graph --oneline --allvisualises branch history.git blame fileshows who changed each line and when.git show commit:fileshows a file at a specific commit.git cherry-pick commitapplies a single commit to current branch.git rebase -i HEAD~3lets you edit the last 3 commits.git commit --amendupdates the last commit.git reset HEAD~1undoes the last commit, keeping changes.git reset --hard HEAD~1undoes the last commit, discarding changes.git reflogshows history of HEAD movements (useful for recovery).git clean -fdremoves untracked files and directories.git bisecthelps find which commit introduced a bug.git worktreelets you check out multiple branches simultaneously.git shortlog -snshows commit count per author.git config --global alias.co checkoutcreates command aliases.git diff --word-diffshows inline word-level diffs.git log -p -- fileshows the history of changes to a specific file.git remote -vshows remote repository URLs.git fetch --pruneremoves references to deleted remote branches.
JavaScript & TypeScript
Array.at(-1)gets the last element of an array.??(nullish coalescing) only falls back fornullorundefined, not falsy values.?.(optional chaining) safely accesses nested properties.Object.fromEntries()converts an array of key-value pairs to an object.structuredClone()creates a deep copy of an object.Array.from({ length: 5 }, (_, i) => i)creates[0, 1, 2, 3, 4].- Destructuring with defaults:
const { name = 'Anonymous' } = user. Promise.allSettled()waits for all promises regardless of rejection.AbortControllercan cancel fetch requests and other async operations.new Set([...arr])removes duplicates from an array.Object.hasOwn(obj, prop)is safer thanobj.hasOwnProperty(prop).- Template literals support tagged templates for custom processing.
for...ofiterates values,for...initerates keys (usually avoidfor...in).globalThisworks in browsers, Node, and workers.queueMicrotask()schedules code to run after current task but before rendering.- Arrow functions don’t have their own
this,arguments, orsuper. Number.isNaN()is stricter than globalisNaN().Object.freeze()makes an object immutable (shallow only).Reflect.ownKeys()returns all keys including symbols.new URL()andURLSearchParamsmake URL manipulation much easier.
CSS
aspect-ratio: 16/9maintains element proportions without padding hacks.gapworks in flexbox, not just grid.min(),max(), andclamp()enable responsive values without media queries.scroll-behavior: smoothenables smooth scrolling site-wide.scroll-margin-topoffsets scroll position for fixed headers.::markerpseudo-element styles list bullets.:is()and:where()reduce selector repetition.:has()is a parent selector (finally!).inset: 0is shorthand fortop: 0; right: 0; bottom: 0; left: 0.place-items: centercentres in both directions (grid/flexbox).isolation: isolatecreates a new stacking context.accent-colorstyles form controls like checkboxes.color-scheme: light darktells the browser your site supports both modes.prefers-reduced-motionmedia query respects user accessibility settings.content-visibility: autocan significantly improve rendering performance.text-wrap: balancecreates more visually balanced text wrapping.
Node.js
node --watch file.jsauto-restarts on file changes (Node 18+).npm pkg get versionprints the package version.npm outdatedshows packages that need updating.npm lsshows the dependency tree.npm why packageexplains why a package is installed.npxruns packages without installing them globally.process.env.NODE_ENVdistinguishes development from production.import.meta.urlgives the current module’s URL in ES modules.node --inspectenables the Chrome DevTools debugger.NODE_OPTIONS='--max-old-space-size=4096'increases memory limit.
VS Code
cmd+shift+popens the command palette.cmd+popens quick file search.cmd+shift+fsearches across all files.cmd+dselects the next occurrence of current selection.cmd+shift+lselects all occurrences of current selection.alt+clickcreates multiple cursors.cmd+shift+kdeletes the current line.alt+up/downmoves lines up or down.shift+alt+up/downduplicates lines.cmd+/toggles line comments.cmd+btoggles the sidebar.cmd+jtoggles the terminal panel.F2renames a symbol across all references.cmd+.shows quick fixes and refactoring options.cmd+shift+onavigates to symbols in current file.
Docker
docker system pruneremoves unused data.docker compose up -dstarts services in detached mode.docker compose logs -ffollows container logs.docker exec -it container shopens a shell in a running container.docker cp container:/path ./localcopies files from container.COPY --chown=node:nodesets ownership during build.- Multi-stage builds dramatically reduce image size.
.dockerignoreworks like.gitignorefor build context.docker statsshows real-time resource usage.docker history imageshows how an image was built.
General
- Rubber duck debugging actually works - explain the problem out loud.
- Taking a break often solves problems faster than pushing through.
- Writing tests first clarifies what you’re actually trying to build.
- Code is read far more often than it’s written.
- The best code is often the code you delete.
- “Make it work, make it right, make it fast” - in that order.
- Naming is one of the hardest problems in programming.
- If you can’t explain it simply, you don’t understand it well enough.
- Most bugs are caused by incorrect assumptions.
- Version control everything, including configuration.