How to get visually selected text in VimScript

后端 未结 11 1869
北荒
北荒 2020-11-27 14:30

I\'m able to get the cursor position with getpos(), but I want to retrieve the selected text within a line, that is \'<,\'>.

11条回答
  •  无人及你
    2020-11-27 14:53

    On Linux, there is a cheap but effective alternative to programming such a GetVisualSelection() function yourself: use the * register!

    The * register contains the content of the most recent Visual selection. See :h x11-selection.

    In your script you could then simply access @* to get the Visual selection.

    let v = @*
    

    Incidentally, * is also a neat little helper in interactive use. For example, in insert mode you can use CTRL-R * to insert what you had selected earlier. No explicit yanking involved.

    This works only on operating systems that support the X11 selection mechanism.

提交回复
热议问题