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

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.

| improve this answer | |
  • 2
    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

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.


Add the below vim script to your vimrc:

function! Mtab(vert)
    if tabpagenr() == tabpagenr("$")
        return 0
    let s:killed_tab = tabpagenr()
    let s:buf_list = tabpagebuflist()
    for item in s:buf_list
        if a:vert
        execute "buf " . item
    execute "tabclose " . s:killed_tab

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.

| improve this answer | |
  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.

| improve this answer | |

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.

| improve this answer | |
  • 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

I came across the same issue and decided to write tiny (2 sloc) helper commands. https://github.com/gitusp/yanked-buffer

With this plugin, you can just close the buffer that you want to move, and then reopen it wherever you want, like "yanking" a buffer.

| improve this answer | |
  • Welcome to Vi and Vim and thank you for the contribution! – filbranden Jun 4 at 2:30
  • 1
    Important note: scripts must be independent of user settings to become generally useful. In this particular case you really should use hide quit instead of simply q. – Matt Jun 4 at 4:48
  • You're right, set hidden must not be forced to set to use a plugin. Having said that, I happened to realize that set hidden was not required from the beginning, so I rewrote the document. Anyway I really appreciate your advice, thanks @Matt. – usp Jun 4 at 14:03

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.