fun myfun()
!cmd <cchar>
endfun
How to do it?
--
Regards,
anhnmncb
gpg key: 44A31344
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)
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 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
that's what I'd call "creative quoting". ;-)
> > 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'
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. :)