The . key can be used to repeat the last insert command. However, we might do some navigation that is not part of the insert, but we want it repeated.
Imagine commenting out lines like so:
// line of text
// line of text
line of text
line of text
The insert command is to put the two forward slashes and a space. That can be repeated using the . key. The navigation would be to navigate down one line and then left some number of characters. That part is not captured by the . key command.
How can we achieve this functionality? I read that it was not available in Vi some years ago, but I'm wondering if it exists now in the latest version of Vim.
Move the cursor to a new location, then type S<C-R>0<Esc> ( S then Ctrl-R then 0 then Escape). After moving the cursor to a new location, press . to repeat the operation (the current line will be replaced with the line that was originally copied).
Replay macros To replay the macro once, move the cursor to the next line and press @h where h represents the register on which you saved the macro. Notice that the macro automatically moves the cursor to the next line as required. This allows you to repeat its execution. To repeat the macro execution, press @@.
In command mode, to undo the last command u is used and if you want to repeat the last command use dot (.).
qX
, where X
is any of the writable registers (typically: pick any lowercase letter).q
again to stop recording.@X
(where X
is the same register) to play it back (count times, if used with a count).@@
to replay the most recently used macro (count times).I read that it was not available in Vi some years ago, but I'm wondering if it exists now in the latest version of Vim.
If the Vim docs are to be believed, Vi did not support recording (steps 1-3), but did support @
. Then you would have to manually yank the characters into the target register with "Xy<motion>
or some other register-writing command. That also works under Vim, but I can't recommend it because it is much more error prone.
Another approach would be "block select then edit" approach:
ctrl + v
- block select
then go down j
or down-arrow
shift + i
will put you in insert mode. Make the change here where you want it to be reflected on all the other lines you've selected.
esc
twice will show/repeat that change you made on the line one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With