61

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?

4
  • 4
    Which ctrl+w commands did you use? Did you try ctrl+w J? Commented Apr 15, 2011 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
    Commented Apr 15, 2011 at 22:33
  • 2
    @Peter, you led me to ctrl+w J and ctrl+w H from the Vim doc., 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
    Commented Apr 15, 2011 at 22:40
  • 1
    @drapkin11: Thanks for the mention of diffopt, that was what I was looking for!
    – Flimm
    Commented Aug 12, 2014 at 13:06

3 Answers 3

109

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

ctrl+w then shift+j

To change back to a vertical split use either:

  • ctrl+w then shift+h
  • or ctrl+w then shift+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
5
  • This is not that convenient when you have a three-way diff.
    – Flimm
    Commented Aug 12, 2014 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. Commented Aug 12, 2014 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
    Commented Aug 12, 2014 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
    Commented Sep 11, 2019 at 16:22
  • 2
    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. Commented Jun 10, 2020 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.

-1

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

1
  • This helped me though this answer was not for the asked question. I wanted to switch between files in a vertical split. Thanks! Commented Oct 17, 2022 at 10:58

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