David542
David542

Reputation: 110502

How to toggle window position in vim

To move a window around I can do:

ctrlw shiftH, J, K, or L

Is there a way to just toggle the position of the windows? For example, pressing it five times would do:

Is there something like that in vim, or I have to specify the direction explicitly when repositioning the window?

Upvotes: 2

Views: 196

Answers (1)

Enlico
Enlico

Reputation: 28500

It seems there's no such a thing in Vim (maybe some plugin exists, though). If there was, we would have found it described at :help window-moving.

On the other hand, you can create your own mapping to handle this. The following, for instance, works as you require:

nnoremap <C-W><C-X> :call NextPost()<CR>
let g:mydic = {0: 'K', 1: 'L', 2: 'J', 3: 'H'}
let g:nextPosIndex = -1
function! NextPost()
  if g:nextPosIndex == 3
    let g:nextPosIndex = 0
  else
    let g:nextPosIndex += 1
  endif
  execute "normal! \<C-W>" . g:mydic[g:nextPosIndex]
endfunction

Note that the counter g:nextPosIndex is never reset, so after a K movement happened on a window, if you move to another window, and then move it, it will L-move.¹

(¹) Based on D. Ben Knoble's comment, this limitation seems to be easly removed by using window-local variable w:nextPosIndex instead of a global one, g:nextPosIndex.

Upvotes: 1

Related Questions