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

Check for spaces at the start of the current line

Expand Messages
  • Gerardo Marset
    I m looking for a way of knowing wether or not all the characters up to the cursor position in the current line are spaces. How would I do that? Thanks! -- You
    Message 1 of 5 , Aug 26, 2011
    • 0 Attachment
      I'm looking for a way of knowing wether or not all the characters up to
      the cursor position in the current line are spaces.
      How would I do that?

      Thanks!

      --
      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
    • Tony Mechelynck
      ... Spaces only, or spaces and tabs, or any whitespace including (or not) no-break spaces and including (or not) CJK wide spaces (U+3000 IIRC)? Including or
      Message 2 of 5 , Aug 26, 2011
      • 0 Attachment
        On 26/08/11 19:24, Gerardo Marset wrote:
        > I'm looking for a way of knowing wether or not all the characters up to
        > the cursor position in the current line are spaces.
        > How would I do that?
        >
        > Thanks!
        >

        Spaces only, or spaces and tabs, or any whitespace including (or not)
        no-break spaces and including (or not) CJK wide spaces (U+3000 IIRC)?

        Including or excluding the cursor position?

        For "true" spaces only, and not including the cursor position, what
        about (untested)

        if getline('.') =~ '^ \{' . col('.') - 1 . '}'
        " we are on the first nonspace, or before it
        else
        " we aren't
        endif

        (it is important to use single quotes in the test-expression).

        The other cases are left as an exercise to the reader, but beware of the
        difference between col() and virtcol()

        Also, the example above should intentionally follow the "true" path
        whenever we are in column 1, since in that case the zero characters
        before the cursor contain nothing that isn't a space.


        Best regards,
        Tony.
        --
        John Birch Society -- that pathetic manifestation of organized
        apoplexy.
        -- Edward P. Morgan

        --
        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
      • Andy Wokula
        ... I d use the following test (echos 1 (= true) if cursor is on initial ... Example: a mapping for the key ... -- Andy -- You received this message
        Message 3 of 5 , Sep 1, 2011
        • 0 Attachment
          Am 26.08.2011 19:24, schrieb Gerardo Marset:
          > I'm looking for a way of knowing wether or not all the characters up
          > to the cursor position in the current line are spaces. How would I do
          > that?
          >
          > Thanks!

          I'd use the following test (echos 1 (= true) if cursor is on initial
          white-space):

          :echo virtcol(".")-1 < indent(".")


          Example: a mapping for the <Home> key

          :noremap <expr> <Home> virtcol(".")-1==indent(".")?"<Bar>":"^"

          --
          Andy

          --
          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
        • Gerardo Marset
          ... Nice! However that code takes tabs into account too, as well as, maybe, other whitespace characters I didn t try. Thanks anyway, i ll definitely save it,
          Message 4 of 5 , Sep 1, 2011
          • 0 Attachment
            El 01/09/11 14:47, Andy Wokula escribió:
            > Am 26.08.2011 19:24, schrieb Gerardo Marset:
            >> I'm looking for a way of knowing wether or not all the characters up
            >> to the cursor position in the current line are spaces. How would I do
            >> that?
            >>
            >> Thanks!
            >
            > I'd use the following test (echos 1 (= true) if cursor is on initial
            > white-space):
            >
            > :echo virtcol(".")-1 < indent(".")
            >
            >
            > Example: a mapping for the <Home> key
            >
            > :noremap <expr> <Home> virtcol(".")-1==indent(".")?"<Bar>":"^"
            >
            Nice!
            However that code takes tabs into account too, as well as, maybe, other
            whitespace characters I didn't try.

            Thanks anyway, i'll definitely save it, it migth come in handy later :).

            --
            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
          • Gerardo Marset
            ... Real spaces only. ... Excluding. ... Nice, but it seems to have a little mistake. I had to add brackets like this: if getline( . ) =~ ^ { . (col( . ) -
            Message 5 of 5 , Sep 1, 2011
            • 0 Attachment
              El 27/08/11 00:22, Tony Mechelynck escribió:
              > On 26/08/11 19:24, Gerardo Marset wrote:
              >> I'm looking for a way of knowing wether or not all the characters up to
              >> the cursor position in the current line are spaces.
              >> How would I do that?
              >>
              >> Thanks!
              >>
              >
              > Spaces only, or spaces and tabs, or any whitespace including (or not)
              > no-break spaces and including (or not) CJK wide spaces (U+3000 IIRC)?
              Real spaces only.

              > Including or excluding the cursor position?
              Excluding.

              >
              > For "true" spaces only, and not including the cursor position, what
              > about (untested)
              >
              > if getline('.') =~ '^ \{' . col('.') - 1 . '}'
              > " we are on the first nonspace, or before it
              > else
              > " we aren't
              > endif
              >
              > (it is important to use single quotes in the test-expression).
              >
              > The other cases are left as an exercise to the reader, but beware of
              > the difference between col() and virtcol()
              >
              > Also, the example above should intentionally follow the "true" path
              > whenever we are in column 1, since in that case the zero characters
              > before the cursor contain nothing that isn't a space.
              Nice, but it seems to have a little mistake. I had to add brackets like
              this:

              if getline('.') =~ '^ \{' . (col('.') - 1) . '}'

              Then, it worked.
              Much better than my previous method, which involved a loop and variables
              and whatnot. Thanks!

              --
              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
            Your message has been successfully submitted and would be delivered to recipients shortly.