How to get visually selected text in VimScript

后端 未结 11 1843
北荒
北荒 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:44

    Overview:

    visually select, press mapping, reselect original selection with gv and copy it to a register, finally paste from the register

    Use case:

    1. Add function Test() to your vimrc:

    function! Test() range
    exe 'sp temp.tmp'
    exe 'norm p'
    endfunction

    1. Open a new file
    2. Create the mapping ,m
      :vmap ,m :norm gvy:call Test()
    3. visually select some text
    4. press ,m (the selection is gone, but 'norm gv' reselects it and 'y' yanks it to current register)
    5. Test() is called: file temp.tmp is opened and 'norm p' pastes from current register, which is the original visual selection

提交回复
热议问题