Is there any way to get visual selected text in VIM script.

2,371 views
Skip to first unread message

crimson

unread,
Feb 25, 2008, 9:02:09 AM2/25/08
to vim_use
Hello Vimers

I notice that in vim script I can use getline() to get one line from
buffer
or use <cword> to get current word under cursor.

But I found no way to get the visual selected text in script. Is there
any
way to do this? And can I get text between two marks?


Thanks in advance.

François Ingelrest

unread,
Feb 25, 2008, 9:07:56 AM2/25/08
to vim...@googlegroups.com
Hi,

On Mon, Feb 25, 2008 at 3:02 PM, crimson <faque...@gmail.com> wrote:
> But I found no way to get the visual selected text in script. Is there
> any
> way to do this? And can I get text between two marks?

With '< and '> you can get the start and the end marks of the last
visual selection, maybe that can help you.

Ben Schmidt

unread,
Feb 25, 2008, 9:19:42 AM2/25/08
to vim...@googlegroups.com

I would do it by yanking the text and accessing the register via script.

E.g.

" save register to restore later
let old_a=@a
" yank the text we want, assuming it's currently selected in visual mode
normal "ay
" do whatever we want with @a which now contains the selected text
" restore register
let @a=old_a

To do it with marks, you'd yank via

normal `<y`>

without having visual mode selected. the < and > marks are the previous visual
selection. You can of course use different marks, or do it linewise with ' instead
of `.

Cheers,

Ben.


Send instant messages to your online friends http://au.messenger.yahoo.com

jerbear

unread,
Feb 25, 2008, 9:45:16 AM2/25/08
to vim_use
here's a function that i wrote to do this...

function! GetVisual() range
let reg_save = getreg('"')
let regtype_save = getregtype('"')
let cb_save = &clipboard
set clipboard&
normal! ""gvy
let selection = getreg('"')
call setreg('"', reg_save, regtype_save)
let &clipboard = cb_save
return selection
endfunction

On Feb 25, 9:07 am, "François Ingelrest" <athro...@gmail.com> wrote:
> Hi,
>

crimson

unread,
Feb 26, 2008, 7:37:31 AM2/26/08
to vim_use

Thanks you all.

Seems that we can not achieve this without resorting to normal
commands.

jerbear

unread,
Feb 26, 2008, 4:15:33 PM2/26/08
to vim_use
what's wrong with normal commands?

Charles E. Campbell, Jr.

unread,
Feb 26, 2008, 6:50:37 PM2/26/08
to vim...@googlegroups.com
jerbear wrote:

I think it could be done without normal commands by using getline(),
col(), and substitute()

* getline: to get the lines
* col: to determine the column extents of '< and '>
* substitute: to eliminate the left and right sides of the lines not
in the visual block

but this would be a messy, long way to go about the job when normal
commands will already do the job nicely.

So: like jerbear said, what's wrong with normal commands?

Regards,
Chip Campbell

Tony Mechelynck

unread,
Feb 26, 2008, 7:50:59 PM2/26/08
to vim...@googlegroups.com
jerbear wrote:
> On Feb 26, 7:37 am, crimson <faque.ch...@gmail.com> wrote:
>> Thanks you all.
>>
>> Seems that we can not achieve this without resorting to normal
>> commands.
>
> what's wrong with normal commands?
>
they aren't yet divorced.

Best regards,
Tony.
--
"Never be afraid to tell the world who you are."
-- Anonymous

crimson

unread,
Feb 27, 2008, 7:05:22 AM2/27/08
to vim_use


On 2月27日, 上午5时15分, jerbear <jmcantr...@gmail.com> wrote:
> what's wrong with normal commands?
>

Yes, of course normal commands are one of the most powerful features
of VIM, but I just be sort of curious to know could this be done
without normal commands, and why not provide a function to do this -
more straightforward, isn't it?

Best Regards

A.Politz

unread,
Feb 27, 2008, 8:51:23 AM2/27/08
to vim...@googlegroups.com
jerbear wrote:

-They have a lot of sideeffects (filling up registers and history
for example).
-They are far less expressive in what they are doing and such make
code less understandable.
-They don't evaluate expressions.
-For that you have to use 'execute', which involves micromanagement
of quoting and absence of syntax highlighting.

Just take a closer look at the proposed function :

function! GetVisual() range
let reg_save = getreg('"')
let regtype_save = getregtype('"')
let cb_save = &clipboard
set clipboard&
normal! ""gvy
let selection = getreg('"')
call setreg('"', reg_save, regtype_save)
let &clipboard = cb_save
return selection
endfunction

4 lines are needed to set the editor in a correct state and prepare
for sideeffects. 2 lines for restoring the old state. Only 3 lines
are actually relevant to the task.

I try to avoid normal commands in scripts whenever possible.


-ap

--

:wq

jerbear

unread,
Feb 27, 2008, 12:49:13 PM2/27/08
to vim_use
well, my function actually works. until you can provide a better
alternative, your argument is moot.

A.Politz

unread,
Feb 28, 2008, 11:17:26 AM2/28/08
to vim...@googlegroups.com
jerbear wrote:

>well, my function actually works. until you can provide a better
>alternative, your argument is moot.
>
>
>

You miss the point. I was merely pointing out
"what is wrong with normal commands ?",or why
someone could prefer functions instead.

-ap

--
:wq

Reply all
Reply to author
Forward
0 new messages