Sunday, August 28, 2016

Re: How to reference characters within visually selected text within a function?

On 2016-08-28 15:33, jfeal@worcester.edu wrote:
> I have a question. I am trying to create a function that uses
> visually selected text. For example:
>
> Let's say I have
>
> 12345
>
> And I highlight the characters
>
> 234
>
> How would I access the selected text '234' within a vim function?
>
> Or another example, let's say I have
>
> 12345
> abcde
>
> And I highlight (using visual block) the characters
>
> 234
> bcd
>
> How would I access the selected text within a vim function?

There are a couple things you can do depending on what you intend to
do with the highlighted content.

- you can yank it to a register (e.g. the "z" register) and then
operate on that register

:vnoremap Q "zy:echo substitute(@z, '[c3]', 'X', 'g')<cr>

- you can use it in a substitution using the \%V token:

:%s/\%V[c3]/X/g

- you can use the mode(), line(), winline(), col(), virtcol(),
screencol(), and/or wincol() functions, passing the '< and '> marks
to the *line() and *col() functions for the beginning/ending of the
block. That will give you the offsets in the various lines,
allowing you to use the getline() function to pull those lines.
Note there are some thorny edge-cases if using mutli-byte
characters.

With a little more detail on what you're trying to do, there might be
other ideas that the list can concoct. :-)

-tim


--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment