basforge

Search titles, text and the “If it doesn’t work” cases.

    RU
    Cheat sheetLinuxCommand line

    Repeating commands in Bash: !!, !$, Alt+. and inline edits

    Rerun the last command with sudo, reuse its last argument, fix a typo, or run a line from history by number. How history and Ctrl+R search work — Command history.

    Repeat a whole command

    Command What it does
    !! last command again
    sudo !! last command with sudo
    !-2 second-to-last command (!-3 — third from last)
    !1042 command number 1042 from history output
    !docker most recent command starting with docker
    !?nginx? most recent command containing nginx

    Bash prints the expanded line right before running it, but doesn’t pause for confirmation — the command executes immediately. To inspect the line first, append :p or enable histverify below.

    Reuse arguments from the last command

    Command or keys What it does
    !$ last argument of the previous command (mkdir /opt/app && cd !$)
    !^ first argument of the previous command
    !* all arguments of the previous command, without the command name
    !:2 second argument of the previous command (!:0 — the command name itself)
    Alt+. (or Esc, then .) insert the last argument into the prompt; repeat to walk further back in history

    Alt+. is safer than !$: the text lands in your prompt right away, so you see it and can edit it before hitting Enter. All ! forms expand only after you press Enter.

    Fix a typo inline

    Command What it does
    ^stauts^status replace the first stauts with status in the last command and run it
    ^--dry-run^ strip --dry-run from the last command and run it
    !!:gs/web1/web2/ replace every web1 with web2 in the last command

    ^old^new is shorthand for !!:s^old^new^: it changes only the first match. When the word appears twice in the command, use the g modifier (!!:gs/…/…/).

    Path parts from the last argument

    Expression Result for /opt/traefik/traefik.yml
    !$:h directory without filename: /opt/traefik
    !$:t filename only: traefik.yml
    !$:r path without extension: /opt/traefik/traefik

    Modifiers work with any history expansion, including !^: for example, cd !^:h changes to the directory of the previous command’s first argument.

    Preview without running

    Command or keys What it does
    !docker:p print the matching command and place it last in history without running it
    shopt -s histverify load the expanded line into the prompt on Enter instead of executing immediately
    Ctrl+Alt+E (or Esc, then Ctrl+E) expand !!, !$, and aliases in place before pressing Enter
    set +H disable ! history expansion for the rest of the session

    Put shopt -s histverify in ~/.bashrc or ~/.bash_aliases, and sudo !! or !docker shows the ready command on the first Enter and runs it on the second. set +H helps when you need exclamation marks on the command line without single quotes.

    If it doesn’t work

    echo "done!" prints bash: !": event not found

    Cause
    Inside double quotes, Bash expands ! as history expansion. The " after it matches no command.
    Fix
    Use single quotes (echo 'done!') or escape the exclamation mark: echo "done\!".

    sudo !! prints sudo: ll: command not found, even though ll just worked

    Cause
    !! expanded to the alias name, and sudo doesn’t expand aliases by default.
    Fix
    Add alias sudo='sudo ' to ~/.bash_aliases (Bash aliases).

    Alt+. inserts nothing or types a strange character

    Cause
    Your terminal isn’t sending Alt as Meta/Esc (common on macOS with the Option key), or the shell is in set -o vi mode.
    Fix
    Press Esc, release it, then . — that works in every terminal. For Alt+., configure your terminal to send Option/Alt as Esc.

    !docker immediately ran docker compose down instead of docker ps

    Cause
    !prefix picks the most recent command starting with that prefix and executes it without asking.
    Fix
    Preview it first with :p (!docker:p), search visually with Ctrl+R (Command history), or turn on shopt -s histverify.