How do I toggle between a Vertical and a Horizontal split in vimdiff?

后端 未结 3 1502
被撕碎了的回忆
被撕碎了的回忆 2021-02-01 01:10

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 file

相关标签:
3条回答
  • 2021-02-01 01:31

    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
    
    0 讨论(0)
  • 2021-02-01 01:32

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

    0 讨论(0)
  • 2021-02-01 01:34

    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.

    0 讨论(0)
提交回复
热议问题