6

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.

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

2 Answers 2

6

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

:set sessionoptions=blank,help,folds,winsize,localoptions
:mksession
:tabnew
: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
3
  • 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
    Commented Jul 9, 2015 at 4:02
  • Works great. I also added the &diff logic from my answer to this answer in my .exrc..
    – anishsane
    Commented Jul 9, 2015 at 7:05
  • Diff should work when 'sessionoptions' has the localoptions value. Commented Jul 9, 2015 at 14:34
2

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
    tabnew
    "Go to previous tab
    tabprev
    " 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
    tabnext
    exec winnr() . "wincmd h"
    q
    "Go to the same window being used in current (previous) tab
    exec cur_win . "wincmd l"

    " If running under vimdiff, start diff view
    if(use_diff)
        windo diffthis
    endif
endfunction

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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