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

Count and Mapping question

Expand Messages
  • guido
    Hi, File ~/tmp/kbrdMacroTest.vim contains: set nocompatible set number nnoremap H :call MyFun( h ) nnoremap J :call MyFun( j ) nnoremap K :call
    Message 1 of 3 , Nov 1, 2007
    • 0 Attachment
      Hi,

      File ~/tmp/kbrdMacroTest.vim contains:

      set nocompatible
      set number
      nnoremap H :call MyFun("h")<CR>
      nnoremap J :call MyFun("j")<CR>
      nnoremap K :call MyFun("k")<CR>
      nnoremap L :call MyFun("l")<CR>
      function! MyFun(s)
      "echo line('.')
      let @q = a:s
      :normal @q
      endfunction

      When I run vim -u NONE ~/tmp/kbrdMacroTest.vim and source it with :so%
      the four mappings H, J, K and L work as expected: they behave as h, j, k
      and l if and only if used without a count.

      Hoever,
      gg4J brings the cursor on pos 5,1 as expected
      gg4L however brings the cursor to pos 4,2 rather than 1,5
      5G$4H brings the cursor to pos 8,1 instead of 5,27
      5G4K brings the cursor to pos 7,1 instead of 1,1

      I can see on the statusline that the count is really turned into a range
      Apparently there is no way to specify that a mapping to a function call
      should take a count rather than a range. Or did I miss it?

      So I really have to questions:
      1) Can one specify a count for a function called from a mapping?
      2) Why do the L, H and K mappings do not behave as I expected?

      (Needless to say that the simple mappings are only useful for testing
      this)

      Guido.

      --
      > : Any porters out there should feel happier knowing that DEC is shipping
      > : me an AlphaPC that I intend to try getting linux running on: this will
      > : definitely help flush out some of the most flagrant unportable stuff.
      > : The Alpha is much more different from the i386 than the 68k stuff is, so
      > : it's likely to get most of the stuff fixed.
      >
      > It's posts like this that almost convince us non-believers that there
      > really is a god.
      -- Anthony Lovell, to Linus's remarks about porting

      http://vanhoecke.org ... and go2 places!


      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • A.Politz
      ... I believe this is a commonly used solution : nnoremap H : call MyFun( h ) function! MyFun(s) exec :normal .v:count.a:s endfunction gets rid
      Message 2 of 3 , Nov 1, 2007
      • 0 Attachment
        guido wrote:

        >Hi,
        >
        >File ~/tmp/kbrdMacroTest.vim contains:
        >
        >set nocompatible
        >set number
        >nnoremap H :call MyFun("h")<CR>
        >nnoremap J :call MyFun("j")<CR>
        >nnoremap K :call MyFun("k")<CR>
        >nnoremap L :call MyFun("l")<CR>
        >function! MyFun(s)
        > "echo line('.')
        > let @q = a:s
        > :normal @q
        >endfunction
        >
        >When I run vim -u NONE ~/tmp/kbrdMacroTest.vim and source it with :so%
        >the four mappings H, J, K and L work as expected: they behave as h, j, k
        >and l if and only if used without a count.
        >
        >Hoever,
        >gg4J brings the cursor on pos 5,1 as expected
        >gg4L however brings the cursor to pos 4,2 rather than 1,5
        >5G$4H brings the cursor to pos 8,1 instead of 5,27
        >5G4K brings the cursor to pos 7,1 instead of 1,1
        >
        >I can see on the statusline that the count is really turned into a range
        >Apparently there is no way to specify that a mapping to a function call
        >should take a count rather than a range. Or did I miss it?
        >
        >So I really have to questions:
        >1) Can one specify a count for a function called from a mapping?
        >2) Why do the L, H and K mappings do not behave as I expected?
        >
        >(Needless to say that the simple mappings are only useful for testing
        >this)
        >
        >Guido.
        >
        >--
        >
        >

        I believe this is a commonly used solution :

        nnoremap H :<C-U>call MyFun("h")<CR>
        function! MyFun(s)
        exec ":normal ".v:count.a:s
        endfunction

        <C-U> gets rid of the range and 'v:count',
        well it holds the count.

        -ap


        --
        Ich hab geträumt, der Krieg wär vorbei.


        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • guido
        ... Thanks a lot! I had been reading about functions and mappings but didn t meet v:count. My bad luck :) Guido.
        Message 3 of 3 , Nov 1, 2007
        • 0 Attachment
          A.Politz said on 01-11-07 23:51:
          > guido wrote:
          ...
          >> 1) Can one specify a count for a function called from a mapping?
          ...
          > I believe this is a commonly used solution :
          ...
          > <C-U> gets rid of the range and 'v:count',
          > well it holds the count.

          Thanks a lot!

          I had been reading about functions and mappings
          but didn't meet v:count. My bad luck :)

          Guido.


          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        Your message has been successfully submitted and would be delivered to recipients shortly.