62

I already know how to use the diffopt variable to start diff mode with horizontal/vertical splits but not how to toggle between the two when I already have two files open for comparison.

I tried the accepted answer found in this older post, but to no avail. The Ctrl+W commands didn't work for me. Perhaps because I'm running gVim in Windows-friendly mode?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
drapkin11
  • 1,205
  • 2
  • 12
  • 24
  • 4
    Which `ctrl+w` commands did you use? Did you try `ctrl+w J`? – Peter Rincker Apr 15 '11 at 22:26
  • Notice I've just updated the link above that was formerly missing. That post doesn't mention your suggestion. `ctrl+w J` changed my vertical split to a horizontal. Now how do I change it back? Please post as answer below if you've got the full solution! Thanks. – drapkin11 Apr 15 '11 at 22:33
  • 1
    @Peter, you led me to `ctrl+w J` and `ctrl+w H` from [the Vim doc.](http://vimdoc.sourceforge.net/htmldoc/windows.html#opening-window), which will do the trick. Feel free to post this answer below, otherwise I'll have to repost this comment as my accepted answer! Thanks again. – drapkin11 Apr 15 '11 at 22:40
  • 1
    @drapkin11: Thanks for the mention of `diffopt`, that was what I was looking for! – Flimm Aug 12 '14 at 13:06

3 Answers3

109

The following command will change a vertical split into a horizontal split:

ctrl+w then J

To change back to a vertical split use either:

ctrl+w H or ctrl+w L

For more information about moving windows:

:h window-moving
:h ctrl-w_J
:h ctrl-w_K
:h ctrl-w_H
:h ctrl-w_L
Peter Rincker
  • 43,539
  • 9
  • 74
  • 101
  • This is not that convenient when you have a three-way diff. – Flimm Aug 12 '14 at 13:01
  • 4
    @Flimm What command are you executing to get a 3-way diff? Have you tried adding `:vert` to the command. e.g. `:vert diffsplit foo.c`. Maybe try `:set diffopt+=vertical`. If you are using fugitive then it will determine to do horizontal or vertical diff splits based on the value of `'diffopt'` and/or if your screen size is large enough. – Peter Rincker Aug 12 '14 at 13:10
  • 2
    I was using Fugitive, and I just figured out the `diffopt` option mentioned in the question post. That solves my problem. – Flimm Aug 12 '14 at 13:12
  • Just one tip: the file where the cursor is determines where the windows will go. From vertical to horizontal, the active file will go to the bottom. From horizontal to vertical, the active file will go to the left – alejandro Sep 11 '19 at 16:22
  • 1
    For the readers: if the command doesn't work for you on the first go, make sure you are pressing `Shift + j` to get `J`. So the command is `ctrl+w` then `shift+j`. – Pushpak Dagade Jun 10 '20 at 11:27
0

I'm rly late, but maybe this is an interesting solution. The solution by @PeterRincker only works if u have just a few windows open without inner windows.
I found this (useful) function in my runtime configuration I like to share with u. It is meant to be mapped as keybinding and let the user switch the current split to a specified one. Mark that it does not toggle between vertical and horizontal, but the user tell which one he likes (Could be currently active one too, also if this scenario doesn't make sense.) The Vim window tree always have two windows as "partners". Effects of this are also observable when resizing windows. What I want to say: Trigger the function, if applies to the currently active window and its "partner" window.

" Switch to a vertical or horizontal split between two windows.
" Switching to currently used split results into the equal split.
" This is between the current window and the one window which is focused, when close the active window.
" This function does not adjust the windows height after the switch, cause this can't work correctly.
" 
" Arguments:
"   horizontal - Boolean to differ between both layouts.
"
function! s:switch_window_split(horizontal) abort
  let l:bufnr = bufnr('%')  " Get current buffer number to restore it in the new window.
  if a:horizontal | let l:vert = '' | else | let l:vert = 'vert ' | endif

  " Close current window and open new split with the cached buffer number.
  wincmd c
  execute l:vert . 'sbuffer ' . l:bufnr
endfunction

" Switch split layout.
nnoremap <leader>wS :<C-u>call <SID>switch_window_split(v:true)<CR>
nnoremap <leader>wV :<C-u>call <SID>switch_window_split(v:false)<CR>

Unfortunately it currently still change the size of the window and don't leave the shape as it is. I'm working on it, but it isn't that easy to achive, cause I have to know the shape of the "partner" window.

weilbith
  • 564
  • 4
  • 20
-1

You can also do ctrl-w + <arrow key> to select the window.