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 under 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 (
vnoremaprather thannnoremap) 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
gvfrom 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.