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

Mapping with count

Expand Messages
  • kbosau@web.de
    Hi, I d like to create a normal mode mapping one can control by a number that has to be typed in prior to hitting the mapped key... function! f() Do something
    Message 1 of 9 , Dec 2, 2002
    • 0 Attachment
      Hi,

      I'd like to create a normal mode mapping one
      can control by a number that has to be typed
      in prior to hitting the mapped key...

      function! f()
      Do something dependent of the
      value of the number that has
      been typed prior to hitting 'z'
      endfunction

      nmap z :call f()<Cr>

      for instance should define a normal mode
      command '34z'.

      How can I achieve this?

      Thanks in advance!

      Klaus
    • Charles E. Campbell
      ... ex. map F 20 F The problem is: you can t get ahold of the repeat number. Instead the mapping will be repeated as in this example, 20 times. ...
      Message 2 of 9 , Dec 4, 2002
      • 0 Attachment
        On Tue, Dec 03, 2002 at 04:36:01AM +0100, kbosau@... wrote:
        > I'd like to create a normal mode mapping one
        > can control by a number that has to be typed
        > in prior to hitting the mapped key...

        ex. map \F <right>
        20\F

        The problem is: you can't get ahold of the repeat number.
        Instead the mapping will be repeated as in this example,
        20 times.

        You can grab the repeat value with calls to functions:

        :20 call SomeFunction()

        Regards,
        C Campbell

        --
        Charles E Campbell, Jr, PhD _ __ __
        Goddard Space Flight Center / /_/\_\_/ /
        cec@... /_/ \/_//_/
        PGP public key: http://www.erols.com/astronaut/pgp.html
      • Jürgen Krämer
        Hi, ... another problem: the count is only applied to the first complete command inside the right hand side of the mapping, e.g. map F 20 F
        Message 3 of 9 , Dec 4, 2002
        • 0 Attachment
          Hi,

          Charles E. Campbell wrote:
          > On Tue, Dec 03, 2002 at 04:36:01AM +0100, kbosau@... wrote:
          >
          >>I'd like to create a normal mode mapping one
          >>can control by a number that has to be typed
          >>in prior to hitting the mapped key...
          >
          >
          > ex. map \F <right>
          > 20\F
          >
          > The problem is: you can't get ahold of the repeat number.
          > Instead the mapping will be repeated as in this example,
          > 20 times.

          another problem: the count is only applied to the first complete
          command inside the right hand side of the mapping, e.g.

          map \F <right><right>
          20\F

          would move the cursor only by 21 characters, not 40.

          Regards,
          Jürgen


          --
          Jürgen Krämer Softwareentwicklung
          Habel GmbH mailto:jkraemer@...
          Hinteres Öschle 2 Tel: (0 74 61) 93 53 15
          78604 Rietheim-Weilheim Fax: (0 74 61) 93 53 99
        • Benji Fisher
          ... Mappings cannot handle counts, nor registers, but user-defined commands can. ... /Range handling /Special cases HTH --Benj Fisher
          Message 4 of 9 , Dec 4, 2002
          • 0 Attachment
            Charles E. Campbell wrote:
            > On Tue, Dec 03, 2002 at 04:36:01AM +0100, kbosau@... wrote:
            >
            >>I'd like to create a normal mode mapping one
            >>can control by a number that has to be typed
            >>in prior to hitting the mapped key...
            >
            >
            > ex. map \F <right>
            > 20\F
            >
            > The problem is: you can't get ahold of the repeat number.
            > Instead the mapping will be repeated as in this example,
            > 20 times.
            >
            > You can grab the repeat value with calls to functions:
            >
            > :20 call SomeFunction()

            Mappings cannot handle counts, nor registers, but user-defined
            commands can.

            :help :command
            /Range handling
            /Special cases

            HTH --Benj Fisher
          • Michael Naumann
            ... Even worse map F 3 20 F would move the cursor by 203 Chacacters, not 60. - Michael
            Message 5 of 9 , Dec 4, 2002
            • 0 Attachment
              On Wednesday 04 December 2002 15:58, Jürgen Krämer wrote:
              > Hi,
              >
              > Charles E. Campbell wrote:
              > > On Tue, Dec 03, 2002 at 04:36:01AM +0100, kbosau@... wrote:
              > >
              > >>I'd like to create a normal mode mapping one
              > >>can control by a number that has to be typed
              > >>in prior to hitting the mapped key...
              > >
              > >
              > > ex. map \F <right>
              > > 20\F
              > >
              > > The problem is: you can't get ahold of the repeat number.
              > > Instead the mapping will be repeated as in this example,
              > > 20 times.
              >
              > another problem: the count is only applied to the first complete
              > command inside the right hand side of the mapping, e.g.
              >
              > map \F <right><right>
              > 20\F
              >
              > would move the cursor only by 21 characters, not 40.

              Even worse
              map \F 3<right>
              20\F

              would move the cursor by 203 Chacacters, not 60.

              - Michael
            • Benji Fisher
              ... Silly me! This can already be done: fun! F() Remember to start the function name with a capital! echo count = v:count , count1 = v:count1 ,
              Message 6 of 9 , Dec 4, 2002
              • 0 Attachment
                kbosau@... wrote:
                > Hi,
                >
                > I'd like to create a normal mode mapping one
                > can control by a number that has to be typed
                > in prior to hitting the mapped key...
                >
                > function! f()
                > Do something dependent of the
                > value of the number that has
                > been typed prior to hitting 'z'
                > endfunction
                >
                > nmap z :call f()<Cr>
                >
                > for instance should define a normal mode
                > command '34z'.
                >
                > How can I achieve this?

                Silly me! This can already be done:

                fun! F()
                " Remember to start the function name with a capital!
                echo "count =" v:count
                \ ", count1 =" v:count1
                \ ", prevcount =" v:prevcount
                endfun

                HTH --Benji Fisher
              • Charles E. Campbell
                ... fun! F() range Remember to start the function name with a capital! echo count = .v:count. , count1 = .v:count1. , prevcount = .v:prevcount endfun nmap
                Message 7 of 9 , Dec 4, 2002
                • 0 Attachment
                  On Wed, Dec 04, 2002 at 11:00:06AM -0500, Benji Fisher wrote:
                  > kbosau@... wrote:
                  > > I'd like to create a normal mode mapping one
                  > > can control by a number that has to be typed
                  > > in prior to hitting the mapped key...
                  > > for instance should define a normal mode
                  > > command '34z'.
                  > >
                  > > How can I achieve this?
                  >
                  > fun! F()
                  > " Remember to start the function name with a capital!
                  > echo "count =" v:count
                  > \ ", count1 =" v:count1
                  > \ ", prevcount =" v:prevcount
                  > endfun

                  You'll want to put "range" as a qualifier on the function:

                  ---------------------------------------------------------------------
                  fun! F() range
                  " Remember to start the function name with a capital!
                  echo "count =".v:count.", count1 =".v:count1.", prevcount =".v:prevcount
                  endfun
                  nmap \F :call F()<Cr>
                  ---------------------------------------------------------------------

                  There's still a problem, however: if the number sent to the function
                  is greater than the number of lines in the file, you'll get an
                  "E16: Invalid Range" error.

                  Regards,
                  C Campbell

                  --
                  Charles E Campbell, Jr, PhD _ __ __
                  Goddard Space Flight Center / /_/\_\_/ /
                  cec@... /_/ \/_//_/
                  PGP public key: http://www.erols.com/astronaut/pgp.html
                • Benji Fisher
                  ... Sorry, I forgot about that. I think the right solution is to cancel the count: fun! EchoCount() echo v:count endfun nnoremap F : call EchoCount()
                  Message 8 of 9 , Dec 4, 2002
                  • 0 Attachment
                    Charles E. Campbell wrote:

                    > ---------------------------------------------------------------------
                    > fun! F() range
                    > " Remember to start the function name with a capital!
                    > echo "count =".v:count.", count1 =".v:count1.", prevcount =".v:prevcount
                    > endfun
                    > nmap \F :call F()<Cr>
                    > ---------------------------------------------------------------------
                    >
                    > There's still a problem, however: if the number sent to the function
                    > is greater than the number of lines in the file, you'll get an
                    > "E16: Invalid Range" error.

                    Sorry, I forgot about that. I think the right solution is to
                    cancel the count:

                    fun! EchoCount()
                    echo v:count
                    endfun
                    nnoremap \F :<C-U>call EchoCount()<CR>

                    I use the "nore" form because some people have :cmap's for <C-U>.

                    HTH --Benji Fisher
                  • Klaus Bosau
                    ... Hi, and thanks a lot to all helpers! I found this very helpful cause I didn t know where to look for information on this topic in Vim s help file... Thanks
                    Message 9 of 9 , Dec 4, 2002
                    • 0 Attachment
                      On Wed, 4 Dec 2002, BenjiFisher wrote:

                      > Charles E. Campbell wrote:
                      >
                      > > ---------------------------------------------------------------------
                      > > fun! F() range
                      > > " Remember to start the function name with a capital!
                      > > echo "count =".v:count.", count1 =".v:count1.", prevcount =".v:prevcount
                      > > endfun
                      > > nmap \F :call F()<Cr>
                      > > ---------------------------------------------------------------------
                      > >
                      > > There's still a problem, however: if the number sent to the function
                      > > is greater than the number of lines in the file, you'll get an
                      > > "E16: Invalid Range" error.
                      >
                      > Sorry, I forgot about that. I think the right solution is to
                      > cancel the count:
                      >
                      > fun! EchoCount()
                      > echo v:count
                      > endfun
                      > nnoremap \F :<C-U>call EchoCount()<CR>
                      >
                      > I use the "nore" form because some people have :cmap's for <C-U>.

                      Hi,

                      and thanks a lot to all helpers! I found this very helpful cause I
                      didn't know where to look for information on this topic in Vim's help
                      file...

                      Thanks again!

                      Klaus
                    Your message has been successfully submitted and would be delivered to recipients shortly.