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

Actual cursor position.

Expand Messages
  • Gerardo Marset
    I m looking for a way to get the current cursor position in the line (that is, the column) in *characters*. col is no use, because it returns the position in
    Message 1 of 3 , Aug 30 3:12 PM
    • 0 Attachment
      I'm looking for a way to get the current cursor position in the line
      (that is, the column) in *characters*.
      col is no use, because it returns the position in bytes. I can't use
      virtcol either because it returns the position on the screen: a tab
      counts as 8 characters (or whatever tabstop is set to), the line can be
      wrapped, etc.

      This could be a reasonably compatible solution:

      function! Column()
      let l:ot=&tabstop
      let l:ow=&wrap
      set tabstop=1
      set nowrap

      let l:ret = virtcol(".")

      let &tabstop=l:ot
      let &wrap=l:ow

      return l:ret
      endfunction

      But it looks sloppy and bogus. There's got to be a better way.

      Any suggestions or tips?

      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
    • ZyX
      Reply to message «Actual cursor position.», sent 02:12:13 31 August 2011, Wednesday ... Why do you need it? You can use echo
      Message 2 of 3 , Aug 30 9:35 PM
      • 0 Attachment
        Reply to message «Actual cursor position.»,
        sent 02:12:13 31 August 2011, Wednesday
        by Gerardo Marset:

        > I'm looking for a way to get the current cursor position in the line
        > (that is, the column) in *characters*.
        > col is no use, because it returns the position in bytes. I can't use
        > virtcol either because it returns the position on the screen: a tab
        > counts as 8 characters (or whatever tabstop is set to), the line can be
        > wrapped, etc.
        Why do you need it?

        You can use
        echo ((col('.')>1)?(strchars(getline(line('.'))[:col('.')-2])):(0))
        or (if you need to count composing characters as a part of previous character)
        replace `strchars(...)' with `len(split(..., '\v.@='))'.

        Note that you solution does not work: you forgot that one character can occupy
        two display cells if it either fullwidth (U+FF00..U+FF60, U+FFE0..U+FFE6,
        U+3000) or with ambigious width and &ambiwidth is set.

        > This could be a reasonably compatible solution:
        >
        > function! Column()
        > let l:ot=&tabstop
        > let l:ow=&wrap
        > set tabstop=1
        > set nowrap
        >
        > let l:ret = virtcol(".")
        >
        > let &tabstop=l:ot
        > let &wrap=l:ow
        >
        > return l:ret
        > endfunction
        Original message:
        > I'm looking for a way to get the current cursor position in the line
        > (that is, the column) in *characters*.
        > col is no use, because it returns the position in bytes. I can't use
        > virtcol either because it returns the position on the screen: a tab
        > counts as 8 characters (or whatever tabstop is set to), the line can be
        > wrapped, etc.
        >
        > This could be a reasonably compatible solution:
        >
        > function! Column()
        > let l:ot=&tabstop
        > let l:ow=&wrap
        > set tabstop=1
        > set nowrap
        >
        > let l:ret = virtcol(".")
        >
        > let &tabstop=l:ot
        > let &wrap=l:ow
        >
        > return l:ret
        > endfunction
        >
        > But it looks sloppy and bogus. There's got to be a better way.
        >
        > Any suggestions or tips?
        >
        > Thanks!
      • Gerardo Marset
        ... I don t want my code to break if a line has multibyte characters. ... Works like a charm! Thank you! ... That s why I thougth my solution was sloppy: there
        Message 3 of 3 , Aug 31 10:50 AM
        • 0 Attachment
          El 31/08/11 01:35, ZyX escribió:
          > Reply to message «Actual cursor position.»,
          > sent 02:12:13 31 August 2011, Wednesday
          > by Gerardo Marset:
          >
          >> I'm looking for a way to get the current cursor position in the line
          >> (that is, the column) in *characters*.
          >> col is no use, because it returns the position in bytes. I can't use
          >> virtcol either because it returns the position on the screen: a tab
          >> counts as 8 characters (or whatever tabstop is set to), the line can be
          >> wrapped, etc.
          > Why do you need it?
          I don't want my code to break if a line has multibyte characters.

          > You can use
          > echo ((col('.')>1)?(strchars(getline(line('.'))[:col('.')-2])):(0))
          > or (if you need to count composing characters as a part of previous character)
          > replace `strchars(...)' with `len(split(..., '\v.@='))'.
          Works like a charm! Thank you!

          > Note that you solution does not work: you forgot that one character can occupy
          > two display cells if it either fullwidth (U+FF00..U+FF60, U+FFE0..U+FFE6,
          > U+3000) or with ambigious width and&ambiwidth is set.
          That's why I thougth my solution was sloppy: there was bound to be a way
          of breaking it.

          --
          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.