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

Re: Using "strlen" under Visual Mode

Expand Messages
  • jamessan@jamessan.com
    ... You could yank the text into a register and then call strlen on the buffer. For example: ay ... James -- GPG Key: 1024D/61326D40
    Message 1 of 7 , May 1, 2005
    • 0 Attachment
      On Mon, May 02, 2005 at 06:49:19AM +0800, Edward WIJAYA wrote:
      > Suppose I want to measure then length of the
      > string highlighted under visual mode.
      >
      > Is there a way to do it?

      You could yank the text into a register and then call strlen on the
      buffer. For example:

      <select visual range>
      "ay
      :echo strlen(@a)

      :help y
      :help "
      :help @
      :help strlen

      James
      --
      GPG Key: 1024D/61326D40 2003-09-02 James Vega <jamessan@...>
    • Eric Arnold
      ... If you want to restore the selection area, try: ` or V or which-ever. I tried making a function to do it automatically, function! Vislen() let
      Message 2 of 7 , May 1, 2005
      • 0 Attachment
        --- jamessan@... wrote:

        > On Mon, May 02, 2005 at 06:49:19AM +0800, Edward WIJAYA wrote:
        > > Suppose I want to measure then length of the
        > > string highlighted under visual mode.
        > >
        > > Is there a way to do it?
        >
        > You could yank the text into a register and then call strlen on the
        > buffer. For example:
        >
        > <select visual range>
        > "ay
        > :echo strlen(@a)


        If you want to restore the selection area, try:

        `<v`>

        or V or which-ever. I tried making a function to do it automatically,

        function! Vislen()
        let modetype = mode()
        let do = 'normal "by`<' . modetype . '`>'
        echo do
        exe do
        echo modetype . ": strlen(" . @b . ")=" . strlen(@b)
        endfunction
        map ,t :call Vislen()<CR>

        but the "mode()" call returns "n" normal mode when I call the ,t macro
        from a visual selection. The help says:

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

        Sadly, the help for "mode()" doesn't refer you to what actually works,
        getregtype(). It does mean that you can't find out the mode without
        yanking into a register, as far as I can tell.

        This was what ended up working:

        function! Vislen()
        " let modetype = mode() " useless

        " this is too late to yank the @b reg, have to do it from the mapping
        "exe 'normal "by'
        exe 'normal `<' . getregtype('b') . '`>'
        echo 'normal `<' . getregtype('b') . '`>'
        echo "strlen(" . @b . ")=" . strlen(@b)
        endfunction

        map ,t "by:call Vislen()<CR>

        So does anybody know a way around how the "normal" stream gets broken
        by almost everything, meaning that the flow from character to character
        as entered by the user cannot be emulated by "normal" calls, if you
        split up the commands onto separate calls (which I think is the same
        problem as what we see in the above example where the "by command
        only works from the map/macro).
      • Marian Csontos
        If you want to return back to previous visual selection you can simply use gv ... -- Marian On Mon, 02 May 2005 07:45:15 +0200, Eric Arnold
        Message 3 of 7 , May 2, 2005
        • 0 Attachment
          If you want to return back to previous visual selection you can simply use
          gv
          :he gv

          -- Marian

          On Mon, 02 May 2005 07:45:15 +0200, Eric Arnold <eric_p_arnold@...>
          wrote:

          >
          > --- jamessan@... wrote:
          >
          >> On Mon, May 02, 2005 at 06:49:19AM +0800, Edward WIJAYA wrote:
          >> > Suppose I want to measure then length of the
          >> > string highlighted under visual mode.
          >> >
          >> > Is there a way to do it?
          >>
          >> You could yank the text into a register and then call strlen on the
          >> buffer. For example:
          >>
          >> <select visual range>
          >> "ay
          >> :echo strlen(@a)
          >
          >
          > If you want to restore the selection area, try:
          >
          > `<v`>
          >
          > or V or which-ever. I tried making a function to do it automatically,
          >
          > function! Vislen()
          > let modetype = mode()
          > let do = 'normal "by`<' . modetype . '`>'
          > echo do
          > exe do
          > echo modetype . ": strlen(" . @b . ")=" . strlen(@b)
          > endfunction
          > map ,t :call Vislen()<CR>
          >
          > but the "mode()" call returns "n" normal mode when I call the ,t macro
          > from a visual selection. The help says:
          >
          > This is useful in the 'statusline' option. In most other
          > places it always returns "c" or "n".
          >
          > Sadly, the help for "mode()" doesn't refer you to what actually works,
          > getregtype(). It does mean that you can't find out the mode without
          > yanking into a register, as far as I can tell.
          >
          > This was what ended up working:
          >
          > function! Vislen()
          > " let modetype = mode() " useless
          >
          > " this is too late to yank the @b reg, have to do it from the mapping
          > "exe 'normal "by'
          > exe 'normal `<' . getregtype('b') . '`>'
          > echo 'normal `<' . getregtype('b') . '`>'
          > echo "strlen(" . @b . ")=" . strlen(@b)
          > endfunction
          >
          > map ,t "by:call Vislen()<CR>
          >
          > So does anybody know a way around how the "normal" stream gets broken
          > by almost everything, meaning that the flow from character to character
          > as entered by the user cannot be emulated by "normal" calls, if you
          > split up the commands onto separate calls (which I think is the same
          > problem as what we see in the above example where the "by command
          > only works from the map/macro).
          >
          >
          >
          >
          >
          > ________ Information from NOD32 ________
          > This message was checked by NOD32 Antivirus System for Linux Mail Server.
          > http://www.nod32.com
          >
          >
          > __________ Informacia od NOD32 1.1085 (20050501) __________
          >
          > Tato sprava bola preverena antivirusovym systemom NOD32.
          > http://www.eset.sk
          >
          >



          --
          Mgr. Marián Csontos
          programátor POS
          AXA, a.s.
          Radničné námestie 4
          821 05 Bratislava
          tel. +421 2 4829 4218
          mailto:csontos@...
          www.axa.sk



          ________ Information from NOD32 ________
          This message was checked by NOD32 Antivirus System for Linux Mail Server.
          http://www.nod32.com
        • Gary Johnson
          ... If you just want to see the length and not use it in a script, just type g^G (letter g followed by Ctrl-G). See ... HTH, Gary -- Gary Johnson
          Message 4 of 7 , May 2, 2005
          • 0 Attachment
            On 2005-05-02, Edward WIJAYA <ewijaya@...> wrote:
            > Hi,
            >
            > Suppose I want to measure then length of the
            > string highlighted under visual mode.
            >
            > Is there a way to do it?

            If you just want to see the length and not use it in a script, just
            type

            g^G

            (letter 'g' followed by Ctrl-G). See

            :help v_g_CTRL-G

            HTH,
            Gary

            --
            Gary Johnson | Agilent Technologies
            garyjohn@... | Wireless Division
            | Spokane, Washington, USA
          • Arun Easi
            ... and look towards the lower-right-corner of the screen. -Arun
            Message 5 of 7 , May 2, 2005
            • 0 Attachment
              If the selection is within a line and if you just want to see the len:

              :set showcmd

              and look towards the lower-right-corner of the screen.

              -Arun

              On Mon, 2 May 2005, Edward WIJAYA wrote:

              > Hi,
              >
              > Suppose I want to measure then length of the
              > string highlighted under visual mode.
              >
              > Is there a way to do it?
              >
              > --
              > Regards,
              > Edward WIJAYA
              > SINGAPORE
              >
            • Keith Roberts
              ... I think visualmode() would work here ... It doesn t matter if you ve crossed into normal mode, since it returns what the mode was last time visual mode was
              Message 6 of 7 , May 10, 2005
              • 0 Attachment
                >-----Original Message-----
                >From: Eric Arnold [mailto:eric_p_arnold@...]
                >Sent: Sunday, May 01, 2005 9:45 PM
                >To: jamessan@...; vim@...
                >Subject: Re: Using "strlen" under Visual Mode[answer + question]
                >
                >
                >--- jamessan@... wrote:
                >
                >> On Mon, May 02, 2005 at 06:49:19AM +0800, Edward WIJAYA wrote:
                >> > Suppose I want to measure then length of the string highlighted
                >> > under visual mode.
                >> >
                >> > Is there a way to do it?
                >>
                >> You could yank the text into a register and then call strlen on the
                >> buffer. For example:
                >>
                >> <select visual range>
                >> "ay
                >> :echo strlen(@a)
                >
                >
                >If you want to restore the selection area, try:
                >
                > `<v`>
                >
                >or V or which-ever. I tried making a function to do it automatically,
                >
                > function! Vislen()
                > let modetype = mode()
                > let do = 'normal "by`<' . modetype . '`>'
                > echo do
                > exe do
                > echo modetype . ": strlen(" . @b . ")=" . strlen(@b)
                > endfunction
                > map ,t :call Vislen()<CR>
                >
                >but the "mode()" call returns "n" normal mode when I call the
                >,t macro from a visual selection. The help says:
                >
                > This is useful in the 'statusline'
                >option. In most other
                > places it always returns "c" or "n".
                >
                >Sadly, the help for "mode()" doesn't refer you to what
                >actually works, getregtype(). It does mean that you can't
                >find out the mode without yanking into a register, as far as I
                >can tell.

                I think visualmode() would work here ... It doesn't matter if you've
                crossed into normal mode, since it returns what the mode was last time
                visual mode was entered.

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