Post Closed as "Duplicate of…" by Rich, statox of
2 edited tags
| link
1
source | link

get string from visual selection

Having some text selected in visual mode, I need some way of turning it into a string that can be used in an expression.

I have tried this

function! VisualSelection()
    normal! `<""y`>
    return @"
endfunction

and it works by itself, but when I want to use it in a mapping vim complains because of the normal command, which, guess what, can't be used in mappings with <expr>, so I need some different way of doing this.

I've found out that it is possible to find mark positions with regex like \%'< and \%'> but I don't know how to find regex in the file in an expression (match wants a string, and I haven't found any similar function that searches in the file itself)