2

In my codes, I often have to do this kind of text editing:

u(:,:,:) = 0._dp
v(:,:,:) = 0._dp
temp(:,:,:) = 0._dp

To:

grid%u(0:nx,0:ny,0:nz) = 0._dp
grid%v(0:nx,0:ny,0:nz) = 0._dp
grid%temp(0:nx,0:ny,0:nz) = 0._dp

I'm using the record option, which is something that I recently learned how to use and therefore not excel at. Here is how I do it (cursor is placed on "u").

  1. qq / i grid% / ESC (start recording and store in buffer "q", go to insert mode and enter "grid%", escape insert mode)
  2. f: / i 0 / ESC / ;; / . / ;; / . (go to next ":", insert 0, escape insert mode, go to 2nd next ":", repeat last command, go to 2nd next ":", repeat last command)
  3. ; / a nz / ESC / ,, / a ny / ESC / ,, / a nx / ESC (go to next ":", insert after cursor "nz", escape insert mode, go to 2nd previous ":", insert after cursor "ny", escape insert mode, go to 2nd previous ":", insert after cursor "nz", escape insert mode)
  4. j / SHIFT-^ / @q (go down one line, go to first non-blank character, apply macro "q")
  5. j / SHIFT-^ / @q (go down one line, go to first non-blank character, apply macro "q")

The only thing I can think of which can make this code editing more efficient is to use the macro on the two lines in one command. Intuitively, I tried 2@q but that did not work.

1
  • You could visually select your lines and type :'<,'>norm @q. The macro q should be applied once per line. You won't have to insert the visual range '<,'>, Vim will do it automatically for you. So, you will only have to type :norm @q.
    – saginaw
    Jan 24, 2016 at 10:05

1 Answer 1

4

As far as I can tell, your recording doesn't include any vertical motion. This means that 2@q will play your recording twice on the same line.

You could add a j at the end of your macro to move down.

Or you could use 2:norm @q to play your recording on the current line and the following one.

The "count" you give to Vim before running an ex command is transformed into a range. Here, your 2 is transformed into .,.+1 which covers the current line (.) and the one below (.+1).

3
  • If I do that, it inserts grid% before the tab so I end up with grid%<tab>u(0:...
    – solalito
    Jan 24, 2016 at 10:18
  • 1
    @solalito When your begin the recording of your macro, hit _ or ^ to make sure that each time the macro will be replayed it will begin on the first non whitespace of the line.
    – saginaw
    Jan 24, 2016 at 10:53
  • 1
    That's because you use i instead of I: qqIgrid%…
    – romainl
    Jan 24, 2016 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.