how to get non-linewise visually selected text

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

how to get non-linewise visually selected text

gmguyx

I want to operate a function on text which I selected in visual mode.
Note that I'm using the visual "v" mode as opposed to the linewise "V"
or blockwise "<C-V>". The problem is that I don't know how to find out
exactly what text was selected.

If I do:

function Foo() range
        ...
endfunction

I have access to "a:firstline" and "a:lastline" so I could operate on
all the lines within the visual selection. However, I only want to
operate on the actual text I selected which may only include parts of
the first and last lines as opposed to the entire line. Is there a way
to access this?


--~--~---------~--~----~------------~-------~--~----~
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
|

RE: how to get non-linewise visually selected text

Larson, David

> I have access to "a:firstline" and "a:lastline" so I could operate on
> all the lines within the visual selection. However, I only want to
> operate on the actual text I selected which may only include parts of
> the first and last lines as opposed to the entire line. Is there a way
> to access this?

You can take advantage of the '< and '> marks, which are set whenever a
visual area is selected, to figure out the exact text selected.

Hope that helps,
David


--~--~---------~--~----~------------~-------~--~----~
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
|

Re: how to get non-linewise visually selected text

Tony Mechelynck
In reply to this post by gmguyx

gmguyx wrote:

> I want to operate a function on text which I selected in visual mode.
> Note that I'm using the visual "v" mode as opposed to the linewise "V"
> or blockwise "<C-V>". The problem is that I don't know how to find out
> exactly what text was selected.
>
> If I do:
>
> function Foo() range
> ...
> endfunction
>
> I have access to "a:firstline" and "a:lastline" so I could operate on
> all the lines within the visual selection. However, I only want to
> operate on the actual text I selected which may only include parts of
> the first and last lines as opposed to the entire line. Is there a way
> to access this?

IIUC, the `< and `> marks correspond to the start and end of the visual area,
respectively, regardless of whether it is characterwise, blockwise, or linewise.

When you hit : in Visual mode, you get :'<,'> which sets the _lines_ of these
marks as the start and end of the range, respectively. But hitting `< or `>
(with backtick) in Normal mode moves to the _character_ where the latest
Visual area started or ended, respectively.

So IIUC you could get the exact coordinates of the visual area as follows:

let y0 = line("'<")
let x0 = col("'<")
let vx0 = virtcol("'<")
let y1 = line("'>")
let x1 = col("'>")
let vx1 = virtcol("'>")

To test (in a function) whether the current selection is characterwise,
linewise or blockwise, use the visualmode() function:

        let mode = visualmode()
        if mode == ""
                " visual mode not yet used
        elseif mode ==# "v"
                " characterwise
        elseif mode ==# "V"
                " linewise
        elseif mode == "\<C-V>"
                " blockwise
        else
                echoerr "Invalid result <<" . mode . ">> from visualmode()"
        endif

Note that ==# differs from == if 'ignorecase' is set and both strings being
compared can contain characters in /[A-Za-z]/.


Best regards,
Tony.
--
Genetics explains why you look like your father, and if you don't, why
you should.

--~--~---------~--~----~------------~-------~--~----~
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
|

Re: how to get non-linewise visually selected text

gmguyx

On Aug 7, 4:38 pm, Tony Mechelynck <[hidden email]>
wrote:
> let y0 = line("'<")
> let x0 = col("'<")
> let vx0 = virtcol("'<")
> let y1 = line("'>")
> let x1 = col("'>")
> let vx1 = virtcol("'>")

Thank you David and Tony!


--~--~---------~--~----~------------~-------~--~----~
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
|

Re: how to get non-linewise visually selected text

Charles E Campbell Jr
In reply to this post by gmguyx

gmguyx wrote:

>I want to operate a function on text which I selected in visual mode.
>Note that I'm using the visual "v" mode as opposed to the linewise "V"
>or blockwise "<C-V>". The problem is that I don't know how to find out
>exactly what text was selected.
>
>If I do:
>
>function Foo() range
> ...
>endfunction
>
>I have access to "a:firstline" and "a:lastline" so I could operate on
>all the lines within the visual selection. However, I only want to
>operate on the actual text I selected which may only include parts of
>the first and last lines as opposed to the entire line. Is there a way
>to access this?
>
>  
>
In addition to Tony and David's notes, may I point out that you can use gv:

  norm! gv

to restart the visual mode selection and follow it with something.  For
example,

  norm! gv"ay

would grab a copy of the visually selected region into register a.

Regards,
Chip Campbell


--~--~---------~--~----~------------~-------~--~----~
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
|

RE: how to get non-linewise visually selected text

Larson, David

> gmguyx wrote:
>
> >I want to operate a function on text which I selected in visual mode.
> >Note that I'm using the visual "v" mode as opposed to the linewise
"V"
> >or blockwise "<C-V>". The problem is that I don't know how to find
out
> >exactly what text was selected.

The @* register has the visually selected text. 'a' must be present in
'guioptions' for this to work, though.

(Sorry that I didn't think of this earlier.)

Cheers!
David

--~--~---------~--~----~------------~-------~--~----~
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
|

Re: how to get non-linewise visually selected text

Tony Mechelynck

Larson, David wrote:

>> gmguyx wrote:
>>
>>> I want to operate a function on text which I selected in visual mode.
>>> Note that I'm using the visual "v" mode as opposed to the linewise
> "V"
>>> or blockwise "<C-V>". The problem is that I don't know how to find
> out
>>> exactly what text was selected.
>
> The @* register has the visually selected text. 'a' must be present in
> 'guioptions' for this to work, though.
>
> (Sorry that I didn't think of this earlier.)
>
> Cheers!
> David

this also means that you are willing to clobber the X-selection or the Windows
clipboard every time you select anything in Vim.



Best regards,
Tony.
--
What color is a chameleon on a mirror?

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