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

Calling a user function via a map cancels visual mode

Expand Messages
  • skeept
    I have the following function and mappings: function! JumpToNextNonMatching(direction) let flags = if a:direction == -1 let flags = flags . b endif let
    Message 1 of 4 , Sep 26, 2013
    • 0 Attachment
      I have the following function and mappings:

      function! JumpToNextNonMatching(direction)
      let flags = ''
      if a:direction == -1
      let flags = flags . 'b'
      endif
      let ntimes = max([v:count, 1])
      let i = 1
      while i <= ntimes
      let i = i + 1
      let curr_line = getline('.')
      let spattern = '^[^' . curr_line[0] . '][^' . curr_line[1] . ']'
      call search(spattern, flags)
      endwhile
      endfunction
      noremap ,n :<C-U>call JumpToNextNonMatching(1)<CR><C-L>
      noremap ,N :<C-U>call JumpToNextNonMatching(-1)<CR><C-L>

      The idea is simply to jump to a previous or next line not matching the beggining of current line.

      But if I am in visual mode and use one of the mappings then visual mode is cancelled.
      Any suggestions on howto properly define the function of the mapping so when I use it I stay in visual mode (with the selection resulting of applying the map, of course).

      Thanks,
      Jorge

      --
      --
      You received this message from the "vim_use" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php

      ---
      You received this message because you are subscribed to the Google Groups "vim_use" group.
      To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
      For more options, visit https://groups.google.com/groups/opt_out.
    • Ben Fritz
      ... The : in your mappings is what ends visual mode. You can t avoid this, but you can go back into visual mode with the same region as you had, simply by
      Message 2 of 4 , Sep 27, 2013
      • 0 Attachment
        On Thursday, September 26, 2013 10:40:27 PM UTC-5, skeept wrote:
        > noremap ,n :<C-U>call JumpToNextNonMatching(1)<CR><C-L>
        > noremap ,N :<C-U>call JumpToNextNonMatching(-1)<CR><C-L>
        >
        > The idea is simply to jump to a previous or next line not matching the beggining of current line.
        >
        > But if I am in visual mode and use one of the mappings then visual mode is cancelled.
        > Any suggestions on howto properly define the function of the mapping so when I use it I stay in visual mode (with the selection resulting of applying the map, of course).
        >

        The : in your mappings is what ends visual mode.

        You can't avoid this, but you can go back into visual mode with the same region as you had, simply by adding 'gv' to the end of your mapping.

        :help gv

        --
        --
        You received this message from the "vim_use" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php

        ---
        You received this message because you are subscribed to the Google Groups "vim_use" group.
        To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
        For more options, visit https://groups.google.com/groups/opt_out.
      • skeept
        ... Thanks Ben, I modified my function and mappings to the following: function! JumpToNextNonMatching(direction) let flags = if a:direction
        Message 3 of 4 , Sep 28, 2013
        • 0 Attachment
          >
          > The : in your mappings is what ends visual mode.
          >
          > You can't avoid this, but you can go back into visual mode with the same region as you had, simply by adding 'gv' to the end of your mapping.
          >
          > :help gv


          Thanks Ben,
          I modified my function and mappings to the following:

          function! JumpToNextNonMatching(direction)
          let flags = ''
          if a:direction < 0
          let flags = flags . 'b'
          endif
          let ntimes = max([v:count, 1])
          let i = 1
          while i <= ntimes
          let i = i + 1
          let curr_line = getline('.')
          let spattern = '^[^' . curr_line[0] . '][^' . curr_line[1] . ']'
          call search(spattern, flags)
          endwhile
          nohlsearch
          if a:direction == 2
          mark >
          endif
          if a:direction == -2
          mark <
          endif
          endfunction
          nnoremap ,n :<C-U>call JumpToNextNonMatching(1)<CR>
          nnoremap ,N :<C-U>call JumpToNextNonMatching(-1)<CR>
          vnoremap ,n :<C-U>call JumpToNextNonMatching(2)<CR>gv
          vnoremap ,N :<C-U>call JumpToNextNonMatching(-2)<CR>gv

          This almost works, if I am moving backwards it progressively increases the visual selected area, but if I am moving forward the visually selected area remains the same because it seems that `gv` brings the cursor back to the beginning of the selection...
          Any idea on how to make this work also for the forward movement?

          Regards,
          Jorge

          --
          --
          You received this message from the "vim_use" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php

          ---
          You received this message because you are subscribed to the Google Groups "vim_use" group.
          To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
          For more options, visit https://groups.google.com/groups/opt_out.
        • Ben Fritz
          ... Yes, you can jump to the other side of the visual selection when in visual mode by pressing o , so append one more letter to your forward mapping. ... --
          Message 4 of 4 , Sep 29, 2013
          • 0 Attachment
            On Saturday, September 28, 2013 5:42:46 PM UTC-5, skeept wrote:
            > vnoremap ,n :<C-U>call JumpToNextNonMatching(2)<CR>gv
            > vnoremap ,N :<C-U>call JumpToNextNonMatching(-2)<CR>gv
            >
            > This almost works, if I am moving backwards it progressively increases the visual selected area, but if I am moving forward the visually selected area remains the same because it seems that `gv` brings the cursor back to the beginning of the selection...
            > Any idea on how to make this work also for the forward movement?
            >

            Yes, you can jump to the other side of the visual selection when in visual mode by pressing 'o', so append one more letter to your forward mapping.

            :help v_o

            --
            --
            You received this message from the "vim_use" maillist.
            Do not top-post! Type your reply below the text you are replying to.
            For more information, visit http://www.vim.org/maillist.php

            ---
            You received this message because you are subscribed to the Google Groups "vim_use" group.
            To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
            For more options, visit https://groups.google.com/groups/opt_out.
          Your message has been successfully submitted and would be delivered to recipients shortly.