1
\$\begingroup\$

I've been thinking about it for a while and, after asking a question on vi.stackexchange, I decided to write a small plugin myself and put it on GitHub; basically it offers a mapping to activate a kind of "window movement" mode where Vim waits for inputs that it appends to Ctrl+w, so that one does not have to press this combination before every window-related movement or action; hitting Esc exits this mode.

It's mostly just one main function and one mapping, so I will copy them below.

  • The mapping plus some things I copied from scratch.vim which seemed reasonable:
" plugin/winzoz.vim

if (exists('g:winzoz_disable') && g:winzoz_disable) || &compatible
  finish
endif

nnoremap <silent> <Plug>(go-zoz-navigation) :call winzoz#goZoZ()<cr>

if !exists('g:winzoz_no_mappings')
  nmap <silent> <leader>w <Plug>(go-zoz-navigation)
endif
  • The function with the logic (plus a helper function for coloring the command line while the functionality is active)
" autoload/winzoz.vim

function! winzoz#goZoZ()
  redrawstatus!
  echohl Search
  echo s:make_status_line()
  echohl None
  let l:key = ''
  while l:key !=? "\<Esc>"
    let l:key = nr2char(getchar())
    execute "normal! \<c-w>" . l:key
    redrawstatus!
  endw
  echo ''
  redrawstatus!
endfunction

" drawing function
function! s:make_status_line()
  let l:text = 'Go WinZoZ'
  let l:text .= repeat(' ', (&columns - len(l:text))/2)
  return repeat(' ', &columns - len(l:text) - 1) .  l:text
endfunction

I'd like some feedback in general, but also on the following points.

  • As regards the logic, I'm not checking that the input is correct (with a :try-:catch-:finally), because I'm not inventing any functionality, but only forwarding the keys to the already existing functionality of Ctrl+w+whatever.
  • I'm using echohl Search + echo to print a colored command line to make apparent when the mode is active; is this a good way of doing it?
    • Regarding this part, I had to make a trick (the - 1 in the second to last line of code above) in order to have one character left for the blinking cursor. I really don't like this, but I don't know what else I could do...
  • There are some Ctrl+w-commands that take two key strokes, but my plugin cannot deal with them :( ... I'll fix when I have time.
  • Do you like it?
  • Do you think it could be useful?

(I also plan to add some documentation to it, fwiw :/ )

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

As requested on vi.SE, here is my little view.

First, I see a great quality in most patterns you have used: autoload plugin, plug-mappings, and so on. Good work!

If I really had to nit-pick, it would be that:

  • I find l: prefix to be cumbersome and useless in many situations
  • I like to annotate my functions with abort in order to simplify error messages in case I'd introduce errors in future versions. abort should have been the default, but unfortunately, this is not the case.
  • "\<Esc>" and "\<ESC>" and "\<eSc>" are the same character, using a case-insensitive comparator doesn't really make any sense. !=# would make more sense. I have an old habit, personally I simply use != ^^'

Regarding your question regarding finally, it could make sense to make sure to reset the highlighting used with :echo in case make_status_line() could fail. I don't see any other place where it could make sense.

Regarding catch around <c-w> + {key}, it would depend on the user experience you want to provide. Use your plugin for some time, make some mistakes, and see how you'd like to see your plugin react.

Regarding the highlight color, in my plugin I use StatusLineNC as it's similar to what :substitute/pat/txt/c seems to use. In another plugin, I use Question.

Do you think it could be useful?

Well. I know I could do without it. And yet a few times, I've been bothered by the number of times I had to hit <c-w><down>. In the end I use the mouse. So... Why not. I'd have to make sure though that I never use <c-w> in a non-nore mapping.

\$\endgroup\$
3
  • \$\begingroup\$ As regards finally you are suggesting that the lines around there be try, echo s:make_status_line(), finally, echohl None, endtry. Is it so? \$\endgroup\$
    – Enlico
    Commented Oct 29, 2020 at 8:04
  • 1
    \$\begingroup\$ Indeed, the temporary highlight is the only resource you acquire and that you must be sure to release; this is the only place where finally could make sense. So. In case s:make_status_line() could fail (I don't think this is possible -- unless you introduce a bug), this is what should be done. As only a bug, and not an unexpected but plausible situation, could trigger an error here, IMO there is no need for finally here. \$\endgroup\$ Commented Oct 29, 2020 at 10:47
  • \$\begingroup\$ Not sure if you're interested, but I've updated the plugin to Vim9script, and I've posted it for review on vi.stackexchange. \$\endgroup\$
    – Enlico
    Commented May 8, 2022 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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