Loading ...
Sorry, an error occurred while loading the content.

52197Re: Supplying repeat count for part of a macro

Expand Messages
  • Benji Fisher
    Aug 2, 2004
      On Mon, Aug 02, 2004 at 01:15:32PM -0700, Trebor Sreyb wrote:
      > I use the following definitions:
      > :let e = "kmmj.`m"
      > :map <F12> @e
      > These allow me to apply the same change in the same
      > column over a series of lines (from last line on up).
      > By simply hitting F12 the desired number of times my
      > changes are applied very easily.
      > Because some of my files have lots of column-aligned
      > text, these macros have become extremely useful.
      > Also, by specifying a repeat count before pressing
      > F12, I can easily make the same edit to many lines
      > with very few keystrokes.
      > However, the final enhancement I'd like to make is to
      > mark the initial cursor location (prior to the first
      > edit), apply F12 some specified number of times, and
      > finally return to the original location.
      > In other words, I'd like to do this as follows
      > (example using 15 lines to be edited):
      > 15<F12>
      > When completed, the cursor is at the same spot it was
      > when I first typed the '15'.
      > But I cannot come up with a solution. I thought of
      > defining another macro that calls @e, but it would
      > need to know how many times to call @e.
      > Is there a register that saves the most recent Repeat
      > Count? I can't find such a register.
      > Any suggestions welcome.

      It might be simpler to map <F12> to call a function, rather than
      executing a macro. Something along these lines (untested):

      :nmap <F12> :<C-U>call ColumnRepeat()<CR>

      :fun! ColumnRepeat()
      let l = line(".")
      let c = col(".")
      let cnt = v:count1
      while cnt
      let cnt = cnt - 1
      normal kmmj.`m
      call cursor(l,c)

      If you use <F12> without a count, then <C-U> does nothing and cnt is
      initialized to 1. If you call it with a count, then <C-U> cancels the
      count and cnt is initialized to the count.

      IIRC, there was a bug with using "normal ." in a function, but it
      was fixed before vim 6.3 was released.

      HTH --Benji Fisher
    • Show all 6 messages in this topic