Snippets

SNP 001–148

Loosely organized collection of code snippets. Maybe you'll find something useful here.

Showing 20 of 148 snippets

  1. SNP 048 #31B

    Truncate text after a few lines

    Clamp a description to three lines with an ellipsis, in CSS only.

  2. SNP 047 #459

    Copy files with rsync over SSH

    Like scp, but it only sends what changed, and it can resume.

  3. SNP 046 #B27

    Open a shell in a running container

    Poke around inside a container to see what it actually looks like in there.

  4. SNP 045 #164

    Respect prefers-reduced-motion

    Some people get dizzy from your fancy animations. The browser tells you who, so listen.

  5. SNP 044 #A35

    Latest row per group in SQL

    The newest order of every customer, in one query.

  6. SNP 043 #591

    Create a new user with sudo rights on Ubuntu

    Stop working as root on a fresh server.

  7. SNP 042 #2B4

    Shuffle an array properly

    The sort(() => Math.random() - 0.5) trick is biased. Fisher-Yates is just as short and actually random.

  8. SNP 041 #369

    Create a swap file on Ubuntu

    Small VPS, no swap, and npm install gets killed. Give it some room.

  9. SNP 040 #14B

    Hide the scrollbar but keep scrolling

    For horizontal carousels and tab bars where the scrollbar is just noise.

  10. SNP 039 #952

    MariaDB in Docker for local development

    A database for the project that you can throw away and recreate in a second.

  11. SNP 038 #416

    Read a JSON file in Node

    Two ways, one of them without any fs code at all.

  12. SNP 037 #529

    Redirect www and http to https in nginx

    One canonical address for your site, everything else gets a 301.

  13. SNP 036 #B63

    Sleep in Node without writing a timeout function

    Node ships a promise-based setTimeout, so the little helper from my old snippet isn't needed there.

  14. SNP 035 #397

    Trim a video without re-encoding with ffmpeg

    Cut a clip out of a long recording in a second, with no quality loss.

  15. SNP 034 #6B1

    Watch a nested property in Vue

    Your watcher doesn't fire when something deep inside an object changes. Here is why and what to do.

  16. SNP 033 #953

    Dump and restore a MariaDB database

    Backups, moving a database to another server, or copying production to your machine.

  17. SNP 032 #42B

    Generate a random password in the terminal

    No password generator website needed.

  18. SNP 031 #592

    Copy text to the clipboard

    The modern clipboard API in three lines, with the gotchas.

  19. SNP 030 #13A

    Delete a branch locally and on the remote

    The feature is merged, the branch can go.

  20. SNP 029 #269

    Follow a log file live

    Watch a log as it is being written, with and without systemd.