I have opened several files in vim (particularly, in vimdiff). They are opened in multiple (vertically split) windows.

Is there a simple way, in which I can duplicate all this to another tab?

I could write a function, which will open a new tab & loop through all buffers in current tab to open in new tab using split/diffsplit. But I wanted to know, if there is an easier way.

  • 1
    As far as I can tell that's pretty much the only way.
    – lcd047
    Jul 8 '15 at 6:32
  • OK... I will try to write one such script...
    – anishsane
    Jul 8 '15 at 8:48

You can use :mksession with a specialized 'sessionoptions' to make this easier.

:set sessionoptions=blank,help,folds,winsize,localoptions
:source Session.vim

Using this technique we can create an easy to use command. Put the following command in your vimrc:

command! -bar DuplicateTabpane
      \ let s:sessionoptions = &sessionoptions |
      \ try |
      \   let &sessionoptions = 'blank,help,folds,winsize,localoptions' |
      \   let s:file = tempname() |
      \   execute 'mksession ' . s:file |
      \   tabnew |
      \   execute 'source ' . s:file |
      \ finally |
      \   silent call delete(s:file) |
      \   let &sessionoptions = s:sessionoptions |
      \   unlet! s:file s:sessionoptions |
      \ endtry

Now you can just call :DuplicateTabpane.

For more information see:

:h :mksession
:h :mkview
:h 'sessionoptions'
:h :tabnew
:h :source
  • Good enough... Plus, it will most likely maintain window positions for horizontally split windows too. I will accept this answer, after some more testing... Thanks
    – anishsane
    Jul 9 '15 at 4:02
  • Works great. I also added the &diff logic from my answer to this answer in my .exrc..
    – anishsane
    Jul 9 '15 at 7:05
  • Diff should work when 'sessionoptions' has the localoptions value. Jul 9 '15 at 14:34

Wrote this function. Worked for me.

Note: This will only work for windows split vertically. This was sufficient for me. :-)

set splitright
function! DuplicateTab()
    "Note current position & view settings.
    let cur_win=winnr() - 1
    let view=winsaveview()
    "Are we running under vimdiff?
    let use_diff=&diff
    "Open new tab
    "Go to previous tab
    " For every window, open window in next tab
    windo exec "let localview=winsaveview() | let x=bufnr(\"\")|tabnext|vnew|exec 'buf ' . x | call winrestview(localview) | tabprev"

    " Reset first tab's position.
    exec winnr(). "wincmd h"
    exec cur_win . "wincmd l"
    call winrestview(view)

    "Go to next tab & close first (empty) window
    exec winnr() . "wincmd h"
    "Go to the same window being used in current (previous) tab
    exec cur_win . "wincmd l"

    " If running under vimdiff, start diff view
        windo diffthis

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.