Vim: Get the Text from Visual Selection
Store the selected text in a variable in Vim Script#vim
We can get the selected text in a variable in Vim Script using registers.
normal gv"xy let context = getreg("x")
Lets break down the command
normal mode -> gv -> (y)ank text -> to the "x" register | | | Copy the contents into x register(or any register you like) | Select the previously selected text
Here, we are entering normal mode and selecting text which was previously selected and yank the contents into a register in this case we are using (x) register, it can be any register.
Now to get the contents of that register we can use the function
to paste the contents of the
register or more generally for any register(
Hence we can store the contents of the selected text in a variable for further processing or manipulation.
To quickly test this snippet from command mode, you can try the following steps:
Select a text and press Escape, we just want the
command to refresh and get it's contents to the latest visual selection.
:let foo = getreg("x")
The echo command will simply print the text which we have selected in the file.