How to get the charactor under cursor in vimscript?

66 views
Skip to first unread message

anhnmncb

unread,
Nov 24, 2008, 5:15:00 AM11/24/08
to vim...@googlegroups.com
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

Jürgen Krämer

unread,
Nov 24, 2008, 5:20:56 AM11/24/08
to vim...@googlegroups.com

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)

Marc Weber

unread,
Nov 24, 2008, 5:45:12 AM11/24/08
to vim...@googlegroups.com
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

Jürgen Krämer

unread,
Nov 24, 2008, 6:01:47 AM11/24/08
to vim...@googlegroups.com

Hi,

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.

Marc Weber

unread,
Nov 24, 2008, 7:04:32 AM11/24/08
to vim...@googlegroups.com
> 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

Jürgen Krämer

unread,
Nov 24, 2008, 7:56:00 AM11/24/08
to vim...@googlegroups.com

Hi,

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

anhnmncb

unread,
Nov 24, 2008, 6:22:12 PM11/24/08
to vim...@googlegroups.com
Jürgen Krämer <jott...@googlemail.com> writes:

Yes, I need yl method, thanks :)

Cyril Slobin

unread,
Nov 24, 2008, 7:18:56 PM11/24/08
to vim...@googlegroups.com
On 11/24/08, Jürgen Krämer <jott...@googlemail.com> 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,
<cy...@slobin.pp.ru> `it means just what I choose it to mean'

anhnmncb

unread,
Nov 24, 2008, 7:58:28 PM11/24/08
to vim...@googlegroups.com
"Cyril Slobin" <cy...@slobin.pp.ru> writes:

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

Reply all
Reply to author
Forward
0 new messages