Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a selection exists in vimscript

Tags:

vim

I want to write a function in vimscript that echoes the selected text or, if no text is selected, the entire buffer.

How can I distinguish between these two cases?

like image 208
roqvist Avatar asked Jul 11 '26 14:07

roqvist


1 Answers

Define two mappings, an :nmap using the entire buffer, and a :vmap for the selected text. Both can invoke the same function, passing an isVisual boolean flag or a mode argument.

Anything else (custom commands, direct function :call) would require an explicit hint, because in order to invoke them, visual mode as already been left (for command-line mode). You also cannot use the '<,'> marks for the detection, because they will keep the last selection even after it has been removed.

like image 108
Ingo Karkat Avatar answered Jul 13 '26 20:07

Ingo Karkat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!