mtnr

A tech blog with fries on the side

Category: software

  • 5 Terminal Tricks That Will Save You Time Every Day

    Whether you’re on macOS or Linux, your shell is hiding some surprisingly powerful shortcuts. Here are five tricks — spanning both zsh and bash — that are easy to learn and genuinely useful.


    1. !$ and $_ — Reuse the Last Argument

    How often do you run a command and immediately need to use the same argument again?

    mkdir -p /some/deeply/nested/directory
    cd !$
    

    !$ expands to the last argument of the previous command, so you don’t have to type that long path twice. In interactive shells you can also use $_, which does the same thing but works more reliably inside scripts.

    Note: !$ is a history expansion — it’s evaluated before the command runs and will be visible if you press Tabto expand it first. $_ is a special shell variable set after each command completes.


    2. ^old^new — Quick Command Substitution

    Made a typo in the last command? Instead of pressing  and hunting for the mistake, use caret substitution:

    git comit -m "fix typo"
    ^comit^commit
    # Runs: git commit -m "fix typo"
    

    This replaces the first occurrence of old with new in the previous command and re-runs it. It’s a bash and zsh history expansion, so it works in both.


    3. cd - — Jump Back to the Previous Directory

    cd - switches you to whichever directory you were in before the current one. It’s like a back button for your filesystem.

    cd /var/log
    cd /etc/nginx
    cd -         # Back to /var/log
    cd -         # Back to /etc/nginx
    

    zsh takes this a step further with a built-in directory stack. Running cd - followed by Tab in zsh gives you a numbered list of recent directories to jump to directly — handy when you’re hopping between more than two locations.


    4. pbcopy / pbpaste (macOS) and xclip / xsel (Linux)

    These commands pipe data into and out of your system clipboard, which is incredibly useful for moving output between the terminal and other apps.

    macOS:

    cat ~/.ssh/id_ed25519.pub | pbcopy   # Copy your public key to clipboard
    pbpaste > notes.txt                  # Paste clipboard content into a file
    

    Linux (install xclip first):

    cat ~/.ssh/id_ed25519.pub | xclip -selection clipboard
    xclip -selection clipboard -o > notes.txt
    

    A convenient alias to make Linux feel like macOS:

    alias pbcopy='xclip -selection clipboard'
    alias pbpaste='xclip -selection clipboard -o'
    

    5. Background Jobs — Don’t Let Long Tasks Hold Your Terminal Hostage

    Running a long process? You don’t have to open a new terminal tab. The shell has built-in job control for exactly this.

    Send a running process to the background:

    1. Press Ctrl+Z to suspend it.
    2. Run bg to resume it in the background.

    Start a process directly in the background:

    npm run build &
    

    Manage your jobs:

    jobs          # List all background jobs
    fg            # Bring the most recent job back to the foreground
    fg %2         # Bring job number 2 to the foreground
    

    Heads up: A backgrounded job is still tied to your terminal session. If you close it, the process gets killed. To truly detach a process so it survives after you log out, use nohup:

    nohup ./my-script.sh &
    

    Or reach for tmux or screen for a more complete session management experience.


    These shortcuts take only minutes to learn but add up to real time savings over a day of terminal work. Try adding the pbcopy/pbpaste aliases to your .zshrc or .bashrc today — your future self will thank you.

  • Install Java on macOS Using SDKMAN!

    Update: I previously recommended using Homebrew and jEnv for managing Java installations, but I’ve since discovered SDKMAN! and find it to be a superior solution. It’s more streamlined, handles multiple SDKs beyond just Java, and doesn’t require Homebrew as a dependency.

    Managing multiple Java versions on macOS can be challenging, especially when different projects require different JDK versions. SDKMAN! (Software Development Kit Manager) offers an elegant solution that goes beyond just Java management.

    What is SDKMAN!?

    SDKMAN! is a tool for managing parallel versions of multiple Software Development Kits on Unix-based systems. Originally known as GVM (Groovy enVironment Manager), it provides a simple command-line interface for installing, switching, and managing various JVM-related tools. What makes SDKMAN! particularly attractive is that it works seamlessly on macOS, Linux, and Windows Subsystem for Linux (WSL).

    Installing SDKMAN!

    Installation is straightforward. Open your terminal and run:

    curl -s "https://get.sdkman.io" | bash

    After installation completes, either restart your terminal or run:

    source "$HOME/.sdkman/bin/sdkman-init.sh"

    Verify the installation by checking the version:

    sdk version

    Installing Java

    With SDKMAN! installed, you can now easily install Java. First, let’s see what versions are available:

    sdk list java

    This command displays all available Java distributions and versions. To install a specific version, use:

    sdk install java 17.0.13-tem

    You can install multiple versions and switch between them easily:

    sdk install java 21.0.5-tem
    sdk use java 21.0.5-tem

    To set a default Java version globally:

    sdk default java 17.0.13-tem

    Beyond Java: Other Development Tools

    One of SDKMAN!’s biggest advantages over jEnv is its ability to manage numerous other development tools. You can install build tools, frameworks, and other SDKs with the same simple commands:

    • Gradle: sdk install gradle
    • Maven: sdk install maven
    • Kotlin: sdk install kotlin
    • Scala: sdk install scala
    • Spring Boot CLI: sdk install springboot
    • Groovy: sdk install groovy

    To see all available SDKs:

    sdk list

    Useful Commands

    Here are some handy SDKMAN! commands to know:

    • sdk current – Show currently active SDK versions
    • sdk current java – Show currently active Java version
    • sdk upgrade – Upgrade all installed SDKs
    • sdk uninstall java 17.0.13-tem – Remove a specific version
    • sdk env – Switch to project-specific versions defined in .sdkmanrc

    Why SDKMAN! Over jEnv?

    While jEnv served me well, SDKMAN! offers several advantages:

    • No Homebrew dependency – SDKMAN! is self-contained
    • Manages more than just Java – one tool for your entire JVM ecosystem
    • Simpler installation and configuration
    • Active development and community support
    • Built-in update mechanism for both the tool and SDKs

    SDKMAN! has become my go-to tool for managing Java and related development tools on macOS. Its simplicity and comprehensive SDK support make it an excellent choice for Java developers working across multiple projects with varying requirements.

    Happy coding!