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

[scripting] discover if I'm in visual mode or not

Expand Messages
  • Stefano Zacchiroli
    Hi all, I need to define a function that acts in two different ways depending on the current vim mode (actually visual or ). The function will
    Message 1 of 3 , Oct 1, 2003
    • 0 Attachment
      Hi all,
      I need to define a function that acts in two different ways depending
      on the current vim mode (actually "visual" or <anything_else>). The
      function will be bound, thru a command, to an F-* key and thus can be
      invoked both in visual and other modes.

      I've tried something that looks like:

      fun Funny()
      if (mode() == "v") || (mode() == "V") || ...
      <some stuff>
      else
      <some other stuff>
      endif
      endfun
      command! -range Funny :call Funny()
      map <F3> :Funny<RETURN>

      Unfortunately the only branch that is ever executed is the "else" one,
      because (accordingly to the "mode()" help):

      This is useful in the 'statusline' option. In most other places it
      always returns "c" or "n".

      I've tried also with visualmode() function, but again this doesn't work
      because it does remember the last used visual mode ...

      How can I discover if I currently am in visual mode or not?

      TIA,
      Cheers.

      --
      Stefano Zacchiroli -- Master in Computer Science @ Uni. Bologna, Italy
      zack@{cs.unibo.it,debian.org,bononia.it} - http://www.bononia.it/zack/
      " I know you believe you understood what you think I said, but I am not
      sure you realize that what you heard is not what I meant! " -- G.Romney
    • mikmach@wp.pl
      ... The easiest way is to create _two_ mappings: map :Funny( normal ) vmap :Funny( visual ) And in function test for argument. m. -- Vim +
      Message 2 of 3 , Oct 1, 2003
      • 0 Attachment
        On 1 Oct 2003 at 17:22, Stefano Zacchiroli wrote:

        > Hi all,
        > I need to define a function that acts in two different ways depending
        > on the current vim mode (actually "visual" or <anything_else>). The
        > function will be bound, thru a command, to an F-* key and thus can be
        > invoked both in visual and other modes.
        >
        > I've tried something that looks like:
        >
        > fun Funny()
        > if (mode() == "v") || (mode() == "V") || ...
        > <some stuff>
        > else
        > <some other stuff>
        > endif
        > endfun
        > command! -range Funny :call Funny()
        > map <F3> :Funny<RETURN>
        >
        >
        > How can I discover if I currently am in visual mode or not?
        >
        The easiest way is to create _two_ mappings:

        map <F3> :Funny("normal")<cr>
        vmap <F3> :Funny("visual")<cr>

        And in function test for argument.

        m.
        --
        Vim + LaTeX = http://vim-latex.sourceforge.net
      • Charles E. Campbell
        ... AFAIK one can t determine if vim is currently in visual mode or not. ... fun! Test() range if mode() == n && visualmode() =~ [vV ] &&
        Message 3 of 3 , Oct 1, 2003
        • 0 Attachment
          On Wed, Oct 01, 2003 at 05:22:57PM +0200, Stefano Zacchiroli wrote:
          > I need to define a function that acts in two different ways depending
          > on the current vim mode...
          > How can I discover if I currently am in visual mode or not?
          ---------------------------------------------------------------------

          AFAIK one can't determine if vim is currently in visual mode or not.
          The following function does about as well as can be currently done:

          ---------------------------------------------------------------------
          fun! Test() range
          if mode() == 'n' && visualmode() =~ "[vV\<c-v>]" &&
          \ line("'<") == a:firstline && line("'>") == a:lastline
          echomsg "I think we've got an active visualmode!"
          endif
          endfun
          ---------------------------------------------------------------------

          One thing that can be used occasionally is

          let @_ = visualmode(1)

          which will clear the visual mode string. For example, in my
          <Align.vim> package I have AlignCtrl clear the visual mode and Align()
          query it. AlignCtrl() often precedes use of Align() but does not use
          visual-selection, whereas Align() may use visual block.

          Regards,
          Chip Campbell

          --
          Charles E Campbell, Jr, PhD _ __ __
          Goddard Space Flight Center / /_/\_\_/ /
          cec@... /_/ \/_//_/
          PGP public key: http://www.erols.com/astronaut/pgp.html
        Your message has been successfully submitted and would be delivered to recipients shortly.