← back to latest

Run the last colon command again without retyping it

NORMAL@:Run the last colon command again without retyping it

anatomy

@
Execute the contents of a register as if you typed them. Usually used with a macro register like q.
:
The read-only colon register. It holds the most recent command line you ran, so @: replays that command on the current line.

In the buffer

You ran :s/http:/https:/ on the first line, then pressed j to move down. Before:

api_url = "https://api.example.com/v1"
cdn_url = "http://cdn.example.com/static"
^
auth_url = "http://auth.example.com/login"
docs_url = "http://docs.example.com/"

After typing @::

api_url = "https://api.example.com/v1"
cdn_url = "https://cdn.example.com/static"
^
auth_url = "http://auth.example.com/login"
docs_url = "http://docs.example.com/"

The substitution runs again, this time on the line under the cursor. Press j@@ and it runs on the third line too. The fourth line stays http because you never asked for it.

When you would reach for it

You ran an Ex command that did exactly what you wanted on one line, and now you need it on a few more lines that are not next to each other and do not share a pattern you could hand to :g. Retyping the command is slow and invites typos. Pressing : and <Up> works, but it is three keystrokes plus <CR> every time. @: replays the whole command line in two keys, wherever the cursor happens to be.

Gotchas

  • . does not repeat Ex commands. It repeats the last normal-mode change, and a :s is not one. If you press . after a substitution, vim repeats whatever you changed before it, or nothing. @: is the dot for the command line.
  • The range is stored as text, not as lines. :.,+1s/old/new/ replays relative to wherever the cursor is now, and fails with E16: Invalid range if that runs past the end of the file. An absolute range like :5d hits line 5 every time, no matter where you are.
  • A count repeats the command in place. 3@: runs it three times on the same line without moving, which is useful for :s without g (one more match per run) but surprising if you expected it to walk down three lines.
  • The ": register only stores a command line if you typed at least one character of it. Commands run entirely from a mapping, or passed with vim -c, leave it unchanged. If nothing qualifies, you get E30: No previous command line.

Variants

@@

Repeats the last register execution, so after one @: you can keep going with j@@, j@@, the same way you would with a macro.

:s/$/,/
j2@:

Appends a comma to the next line twice, since a count runs the command repeatedly without moving the cursor.

g&

For substitutions specifically: reruns the last :s on every line in the file with the same pattern and flags. Use it when you tried the substitution on one line and now want it everywhere.

lineage

Vi let you execute a named buffer with @, but it had no register for the command line. Vim added the read-only ": register, which holds the last command line from history, and @: reads from it. Both depend on the +cmdline_hist feature, which every normal build of Vim and Neovim includes.

-- INSERT --vim-hacks.com030-030-repeat-the-last-ex-command.html
utf-81,1All