Is it possible to determine the direction of visual selection in Vim?

In Vim, I want to visually select some lines, and then insert a copy of the selected selection either above or below the original selection, depending on whether the visual selection was created up or down. Therefore, if I start visual mode on line 100 and move the cursor 10 lines, it will be an upward visual selection, whereas if I start from line 100 and move the cursor down 10 lines, it will be a downward selection. line("'<") and line("'>") identical in both cases, so it seems that Vim always selects a smaller number as the start of the selection. Is there a way to determine if the visual selection was up or down?

+4
source share
2 answers

When (in your display / command) you first exit the visual mode via <Esc> , you can compare the position / line of the cursor ( line('.') ) With the marks '< and '> . Then simply use the labels (they retain their values ​​even when the visual mode remains), or re-select via :normal! gv :normal! gv .

+2
source

You can put a mark before you start the selection, and one more when you leave the choice:

 ma V jjj <Esc> mb 

and then see if there is line("'a") < line("'b") .

0
source

Source: https://habr.com/ru/post/1433398/


All Articles