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.
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
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
Best regards,
Tony.
--
"Never be afraid to tell the world who you are."
-- Anonymous
-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
>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