0

Is it possible to make <C-W><C-L> command to make split on the right, if it's the rightmost pane opened? Otherwise, it would behave like regular move. It's something like "conditional splitting" I would say.

Example: I've got two panes opened, split vertically. Cursor is currently on the right pane. I press <C-W><C-L> and I create third pane and my cursor is moved there.

If I press <C-W><C-L> from the leftmost pane, nothing unusual happens, I just move to the already existing pane to the right.

P.S.: which are common bindings for splitting panes among Vim users?

  • As Dedowsdi mentions, Im fine with usual commands. I use either <C-w><C-v/s> if i want to split the same file (not often the case, but useful to work on two parts of the same file), or :[v]split to split to other files files. – D. Ben Knoble May 29 at 12:17
  • D. Ben Knoble, thank you for sharing your experience. Maybe I'll discover the same for myself when I become experienced enough with Vim. But :[v]split will be handy for sure, thanks! – Vladislav Kovechenkov May 29 at 13:27
3

Split and move if window doesn't change after <c-w><c-*>:

nnoremap <c-w><c-j> :call <sid>smart_split('j')<cr>
nnoremap <c-w><c-k> :call <sid>smart_split('k')<cr>
nnoremap <c-w><c-h> :call <sid>smart_split('h')<cr>
nnoremap <c-w><c-l> :call <sid>smart_split('l')<cr>

function! s:smart_split(cmd)
  " original window number
  let wn = winnr()

  " try to move to other window
  exec 'wincmd ' . a:cmd

  " do nothing if window changed
  if wn != winnr() | return | endif

  " otherwise split vertical or horizontal based on a:cmd
  exec 'wincmd ' . (a:cmd =~# '[lh]' ? 'v' : 's')

  " move to new window. You only need this 50% of time.
  exec 'wincmd ' . a:cmd
endfunction

I don't used this, i'm fine with default window splitting command binding.

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.