How to get the charactor under cursor in vimscript?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to get the charactor under cursor in vimscript?

anhnmncb-2

I want to get the charactor under cursor(<cchar>?) then run something
like:

fun myfun()
  !cmd <cchar>
endfun

How to do it?
--
Regards,

  anhnmncb
 gpg key: 44A31344

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get the charactor under cursor in vimscript?

Jürgen Krämer-4


Hi,

anhnmncb wrote:
> I want to get the charactor under cursor(<cchar>?) then run something
> like:
>
> fun myfun()
>   !cmd <cchar>
> endfun
>
> How to do it?

  function MyFun()
      normal yl
      execute "!cmd " . @"
  endfunction

Regards,
Jürgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us.     (Calvin)

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get the charactor under cursor in vimscript?

MarcWeber
In reply to this post by anhnmncb-2

You're hitting two problems:
a) How to run a command and pass an args?
  using exec. Exapmle:
  exec "MyCommand ".arg1
  You still have to quote spaces eg using substitute

b) How to get char under cursor. Sure, yanking works. But maybe you
   don't want to change the registers.
  answer:
    getline('.')[col('.')-1]

Sincerly
Marc Weber

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get the charactor under cursor in vimscript?

Jürgen Krämer-4


Hi,

Marc Weber write:

> You're hitting two problems:
> a) How to run a command and pass an args?
>   using exec. Exapmle:
>   exec "MyCommand ".arg1
>   You still have to quote spaces eg using substitute
>
> b) How to get char under cursor. Sure, yanking works. But maybe you
>    don't want to change the registers.
>   answer:
>     getline('.')[col('.')-1]

you cannot use this getline()-expression reliably if encoding is set to
utf8 and your line contains non-ASCII characters, because getline()[i]
only returns *one* byte of a possible multi-byte sequence, e.g. with
this text

  123äöüabc

the commands

  let line = getline(1)

  for i in range(0, strlen(line) - 1)
    echo i . ': ' . line[i]
  endfor

prints

  0: 1
  1: 2
  2: 3
  3: <c3>
  4: <a4>
  5: <c3>
  6: <b6>
  7: <c3>
  8: <bc>
  9: a
  10: b
  11: c

In contrast, yanking the character under the cursor with

  normal yl

works correctly.

Regards,
Jürgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us.     (Calvin)

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get the charactor under cursor in vimscript?

MarcWeber

> you cannot use this getline()-expression reliably if encoding is set to
> utf8 and your line contains non-ASCII characters, because getline()[i]
> works correctly.
I haven't been aware about that. Thanks for correcting me.

Marc

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get the charactor under cursor in vimscript?

Jürgen Krämer-4


Hi,

Marc Weber wrote:
>> you cannot use this getline()-expression reliably if encoding is set to
>> utf8 and your line contains non-ASCII characters, because getline()[i]
>> works correctly.
> I haven't been aware about that. Thanks for correcting me.

that's what I'd call "creative quoting". ;-)

Regards,
Jürgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us.     (Calvin)

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get the charactor under cursor in vimscript?

anhnmncb-2
In reply to this post by Jürgen Krämer-4

Jürgen Krämer <[hidden email]> writes:

> Hi,
>
> Marc Weber write:
>> You're hitting two problems:
>> a) How to run a command and pass an args?
>>   using exec. Exapmle:
>>   exec "MyCommand ".arg1
>>   You still have to quote spaces eg using substitute
>>
>> b) How to get char under cursor. Sure, yanking works. But maybe you
>>    don't want to change the registers.
>>   answer:
>>     getline('.')[col('.')-1]
>
> you cannot use this getline()-expression reliably if encoding is set to
> utf8 and your line contains non-ASCII characters, because getline()[i]
> only returns *one* byte of a possible multi-byte sequence, e.g. with
> this text
>
>   123äöüabc
>
> the commands
>
>   let line = getline(1)
>
>   for i in range(0, strlen(line) - 1)
>     echo i . ': ' . line[i]
>   endfor
>
> prints
>
>   0: 1
>   1: 2
>   2: 3
>   3: <c3>
>   4: <a4>
>   5: <c3>
>   6: <b6>
>   7: <c3>
>   8: <bc>
>   9: a
>   10: b
>   11: c
>
> In contrast, yanking the character under the cursor with
>
>   normal yl
>
> works correctly.
>
> Regards,
> Jürgen

Yes, I need yl method, thanks :)

--
Regards,

  anhnmncb
 gpg key: 44A31344

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get the charactor under cursor in vimscript?

Cyril Slobin-4
In reply to this post by Jürgen Krämer-4
On 11/24/08, Jürgen Krämer <[hidden email]> wrote:

> > b) How to get char under cursor. Sure, yanking works. But maybe you
> >    don't want to change the registers.
> >   answer:
> >     getline('.')[col('.')-1]
>
>
> you cannot use this getline()-expression reliably if encoding is
> set to utf8 and your line contains non-ASCII characters, because
> getline()[i] only returns *one* byte of a possible multi-byte
> sequence,

In my scripts I use this construction -- seems ugly, but works:

function <SID>Process()
  let [buf, row, col, off] = getpos(".")
  let [whole, before, old, after; rest] =
  \ matchlist(getline("."), printf('^\(.\{-}\)\(.\?\)\%%%dc\(.*\)$', col))
  " old now contains the char under cursor, in the following code
  " we are replacing it with something else
  let new = SomethingElse();
  let col = col - strlen(old) + strlen(new)
  call setline(".", before . new . after)
  call setpos(".", [buf, row, col, off])
  return ""
endfunction

The regexp seems complex because it should process even the corner
cases, such the cursor after the last character in insert mode.

--
http://slobin.pp.ru/ `When I use a word,' Humpty Dumpty said,
<[hidden email]> `it means just what I choose it to mean'

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get the charactor under cursor in vimscript?

anhnmncb-2

"Cyril Slobin" <[hidden email]> writes:

> On 11/24/08, Jürgen Krämer <[hidden email]> wrote:
>
>> > b) How to get char under cursor. Sure, yanking works. But maybe you
>> >    don't want to change the registers.
>> >   answer:
>> >     getline('.')[col('.')-1]
>>
>>
>> you cannot use this getline()-expression reliably if encoding is
>> set to utf8 and your line contains non-ASCII characters, because
>> getline()[i] only returns *one* byte of a possible multi-byte
>> sequence,
>
> In my scripts I use this construction -- seems ugly, but works:
>
> function <SID>Process()
>   let [buf, row, col, off] = getpos(".")
>   let [whole, before, old, after; rest] =
>   \ matchlist(getline("."), printf('^\(.\{-}\)\(.\?\)\%%%dc\(.*\)$', col))
>   " old now contains the char under cursor, in the following code
>   " we are replacing it with something else
>   let new = SomethingElse();
>   let col = col - strlen(old) + strlen(new)
>   call setline(".", before . new . after)
>   call setpos(".", [buf, row, col, off])
>   return ""
> endfunction
>
> The regexp seems complex because it should process even the corner
> cases, such the cursor after the last character in insert mode.

Thanks, I have bookmarked it, thought it's too complex for me now, I
hope it would be useful when my vim skill improves to a relative higher
level in future.  :)

--
Regards,

  anhnmncb
 gpg key: 44A31344

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Loading...