开发者

vim script: working with the visual selection as well as the word under cursor

I have a small vim script that queries Google to insert links in markdown-formatted text. Currently it only works using the word under cursor, which it retrieves using expand("<cword>") and modifies by executing normal mode commands (norm ciw<new_text>).

How could I modify this script so that it works using the visual selection if it exists?

I need basically three things:

  • know whether text is being selected (otherwise use the word und开发者_运维问答er cursor)
  • retrieve the selected text
  • modify the selected text

Ideally I would like a clean way to do this.

Any hints?

Cheers


You can handle this case cleanly using a separate visual-mode mapping (vnoremap rather than nnoremap) that calls the same underlying function.

To extract the text, the markers `< and `> (for the start and end of the previous visual selection) may be useful.

To modify the text, you may be able to use gv from your script (re-select the previous visual selection) and then delete or replace it as you wish.

Hope this helps for now; I'll be able to spend a bit more time on it later.


Your visual selection extraction function is too complex. The usual way to proceed is the following: lh#visual#selection().

BTW, you can build the normal mode mapping with:

vnoremap µ :call <sid>DoStuff()<cr>
nmap µ viwµ
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜