Is there a way to take an existing window (split) and put it into a new tab?
4 Answers
As well as the previously suggested :tabedit
approach, a quicker way of doing it is (in normal mode) to hit Ctrl-W Shift-T. Ctrl-W is the general prefix for a wide variety of window manipulation commands.
See:
:help Ctrl-W_T
:help Ctrl-W
-
4Note that
help Ctrl-W_T
takes you to the help entry ofCtrl-W_t
(lower case), however, further down the page is theCtrl-W_T
entry (with capital T), saying "Move the current window to a new tab page...." Oct 19, 2012 at 10:04 -
1
-
@RamizUddin There's no simple way of doing this (as it isn't obvious which window you want to split into). However, you can find out the buffer number for your current tab page with
:echo bufnr("")
and using this number you can split a window with:sp #3
or:sb 3
(assuming the number was 3). You may be able to come up with some mappings or functions to simplify this.– DrAlFeb 25, 2013 at 10:08 -
1How do you move the buffer to a new tab, keeping it's undo/redo state. In other words, I don't want to open the same file in a new tab, I want to literally move the buffer to a new tab, so the edit history can be used in the new tab. If there is no default way to do this, I bet a plugin can be made that adds a new shortcut for doing this that writes the undo history to the swap file for the new buffer.– trusktrMar 22, 2013 at 23:50
-
2@trusktr I'd imagine that opening the buffer in a new tab with
:sb
would work, but with recent versions of Vim (>= 7.3) you can also use persistent undo by settingset undodir=/path/to/dir
where /path/to/dir is a directory you've created for the purpose and thenset undofile
. This will allow you quit vim and then restart and still retain the undo/redo state.– DrAlMar 25, 2013 at 8:31
Try
:tabedit %<CR>
-
3
-
19For the same reason I did include ":". This is the complete vim keys combination in normal mode and you can use this in 'nnoremap' and in 'nmap' Nov 18, 2009 at 19:49
-
6This works as well except it clones/duplicates the window to a new tab as opposed to closing it first and then opening it in a new tab. Not a big deal. Just something to be aware of.– A-DubbSep 15, 2012 at 1:08
-
6This doesn't move the buffer to a new tab, it just opens the same file in a new tab, but you will not have undo/redo history, etc.– trusktrMar 22, 2013 at 23:48
-
6
This moves the newest buffer in a new tab and restores the previous buffer in the current tab. I use this after dragging a new file into my Gvim
:sbp |wincmd p| wincmd T
You can map it like that to Ctrl-Backspace
:nnoremap <C-BS> :sbp<bar>wincmd p<bar>wincmd T<CR>
it performs especially well with
:set switchbuf=usetab
I have been using this which gives you two functions which can be bound to a pair of hotkeys or commands, and which works quite intuitively. I am pretty sure it offers behavior even more friendly than e.g. Ctrl-W_T
.
For example, when multiple windows are open in multiple tabs, using this function allows you to specifically move the current window to the next or previous tab, and if you move something to before the first tab or to after the last tab, then it turns into a whole new tab.
This means if you have 2 tabs, each having a single window, then moving the first tab's window to the right will combine it with the second tab to result in one single tab with two windows. I don't know how convoluted this operation is to achieve using traditional commands.
What this means is that a single pair of move commands allows for both shifting windows around the tabs, splitting windows out into tabs (by pushing a window out to the end) and joining separate tabs into windows inside of a single tab, pretty much everything you could possibly want, short of positional arrangement (which is a separate topic and which the built in Ctrl-W
+Shift-HJKL
commands work fine for).
-
Very elegant. Moved windows maintain history. Tabs are created if there’s none at the requested end. Thanks for sharing. Feb 10 at 15:41
-
1I'll note that 9 years later I transitioned to a lua config and tried to port this vimscript into neovim lua code, but I failed. Never got it even close to working properly. So i gave up and shoved these into the VimL execution string that I already have (and is still pretty big) and I got this feature back again. It sucked not having it working right for a while. Nov 20 at 6:11
-
My Neovim just inherited the large init.vim I had before with vanilla Vim, including this. I am guessing it could be faster if I go all lua with lazy loading but it works in meantime… Nov 22 at 6:37
-
yeah and if you ever make a robust lua port of this functionality, i'd love to know about it, the lua api isnt great in this area of window manipulation. Dec 3 at 0:40