5

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.

3 Answers3

5
  1. Press qX, where X is any of the writable registers (typically: pick any lowercase letter).
  2. Do whatever actions you want to record.
  3. Press q again to stop recording.
  4. Press @X (where X is the same register) to play it back (count times, if used with a count).
  5. Press @@ 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.

Kevin
  • 28,963
  • 9
  • 62
  • 81
2

Another approach would be "block select then edit" approach:

  1. ctrl + v - block select

  2. then go down j or down-arrow

  3. 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.

  4. esc twice will show/repeat that change you made on the line one.

amacharla
  • 31
  • 3
0

If you have a big range of similar lines and want to put // at the beginning of it, you can do something like:

:15,25norm! I//<space>

You can also use visual area (vip selects an entire paragraph)

:'<,'>norm! I//<space>

using a pattern

:g/TODO/norm! I//<space>
SergioAraujo
  • 11,069
  • 3
  • 50
  • 40