0

One can create non-rectangular Visual-Block selections using C-v$ on files with lines of non-equal length.

Is it possible to programmatically determine whether the last Visual Block selection was non-rectangular?

2

A little crude but this works...

func! IsDollarBlock()
    let l:mode = char2nr(visualmode(""))
    if l:mode != 22
        echom char2nr(l:mode) . " is not block mode"
        return 0
    endif

    sil norm! gv"aygv$"by
    return @a == @b
endfunc

You can test with...

:echo IsDollarBlock()

(Assuming visual selection was not active)...first this checks whether the last visual selection was even block mode. If not it returns 0 (false). Otherwise it re-selects the visual selection and yanks into register "a", re-selects it again, issues $ and yanks into register "b". Then it compares the two registers and returns the comparison result (0/false or 1/true).

If you wanted to preserve the visual selection you could issue <Esc> or equivalent at the beginning of the function then put norm! gv as last command of the function.

  • Alternate algorithm: if the lengths of the lines differ at all, it must be non-rectangular. Something like len(uniq(sort(map(lines, strlen)))) != 1 – D. Ben Knoble May 25 at 10:54
  • Hey DBK. My first thought was to use line lengths but you can make a non-rectangular selection without $ and the question is specifically about creating selections with $. – B Layer May 25 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.