13

Sometimes I need to move windows into an already existing tab, and I was wondering if there was a command/keyboard shortcut that could do this. I know I can close the window, switch to the tab, and then open it in a new split, but I was wondering if there was a shorter way of accomplishing this. Note that I want to move it to an existing tab, not a new tab like <C-w>T does.

  • It isn't possible to move windows across tabs. – lcd047 May 25 '15 at 5:26
  • Hmm, that's too bad. I could probably create some mapping or command which could accomplish it though. – EvergreenTree May 25 '15 at 12:08
  • I think the best you can do is to save all of the window settings and then recreate it on the existing tab. – tommcdo May 25 '15 at 14:24
10

Though there are commands to move existing windows around in the current tab page (i.e. affect the window layout by rotating, resizing, and moving), there are no commands to move a window to another tab.

As a window is just a viewport into a loaded buffer, you have to:

  1. Note the buffer number displayed in the current window.
  2. :close! the window.
  3. Switch to the existing target tab page.
  4. :sbuffer the buffer number to re-open it.

You'll lose any window-specific options by this. You could save and restore those in a script, too.

  • I figured that this was the case. I could probably make a small script which does exactly that. – EvergreenTree May 26 '15 at 12:17
2
  1. go to the existing target tab
  2. split it however you want
  3. :b ab(tab-complete the rest)

ab are just random letters, replace with the beginning of your desired file name. :b tab-completion has performed very well for me, it can usually find the right source buffer based on just one letter and 1-2 Tab keypresses.

The benefit of this approach over Ingo's is the lack of noting or remembering anything, meaning less cognitive load. File name you already know anyway.

2

The two commands defined below move all buffers from the right hand tab to the current tab and then close the right hand tab. Mtabsp moves them by splitting horizontally and Mtabvsp splits vertically. You need to move the merged tab before using the commands. Use +tabm, -tabm or <n>tabm.

Setup

Add the below vim script to your vimrc:

function! Mtab(vert)
    if tabpagenr() == tabpagenr("$")
        return 0
    endif
    +tabnext
    let s:killed_tab = tabpagenr()
    let s:buf_list = tabpagebuflist()
    -tabnext
    for item in s:buf_list
        if a:vert
            vsp
        else
            sp
        endif
        execute "buf " . item
    endfor
    execute "tabclose " . s:killed_tab
endfunction

command! Mtabsp call Mtab(0)
command! Mtabvsp call Mtab(1)

Technical details

The function Mtab receives one parameter vert. It determines if the current tab is split horizontally or vertically.

The desired outcome is achieved with the following steps:

  1. We move to the next tab temporarily
  2. Use two variables to reference its tab number and buffers
  3. Return to the current tab
  4. We split the tab and open each buffer
  5. Finally, we close the right hand tab

If our current tab is last, the commands are ignored.

New contributor
evpo is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1

If it's a file buffer (won't work for scratch buffers etc.), you can:

  1. :let @f=@%ENTER — in the source window;
    • This replaces contents of regular register f with contents of special register %, which contains current file name.
    • Note: I chose register f as in file, but you can use any other register you like.
    • You can verify that the register contains correct path with: :echo @f
  2. Optionally: Ctrl-WCtrl-Q — to close the old window;
  3. gt / gT — navigate to the target tab (and window);
  4. Ctrl-Ws — split the tab any way you like, to open a new window;
  5. :e␣Ctrl-RfENTER
    • in the newly opened window, loads a file from the path stored in register f;
    • Note: the ␣ character here marks a regular space.

Important note: the final step won't work correctly if the path contains spaces or some special characters! :-( As a workaround, the following, slightly longer command should work for paths with spaces (though it may still fail for some other special characters):

  • :e␣"Ctrl-Rf"ENTER

(that is, quoting the result of Ctrl-R, f )

Inspired by answers from lkraav and ingo-karkat.

  • This is fantastic, I need to set up a mapping for "cut and paste" of Windows into Tabs. – dragon788 May 15 '18 at 17:13

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.