Wednesday, May 23, 2007

Convenient tab commands

I was conversing with a friend about how to turn a split into a new tab, and vice versa this morning. You can call IMing 'conversing', right? Yes. At this day and age, I'd hope so. Anyway. We came up with a couple convenient commands/abbreviations. Take your pick:

" Close a split and then open it in a new tab:
:ca tb let a=bufnr("%") q tabnew exe "b ". a
:command! Tb :let a=bufnr("%") | q | tabnew | exe "b ". a

" Close the current tab and open as a split in another existing tab:
:command! Ts :let a=bufnr("%") | tabprev | sp | exe "b ". a | tabnext | q | tabprev
:ca ts :let a=bufnr("%") tabprev sp exe "b ". a tabnext q tabprev

Note these obviously aren't that smart. If you have no splits, its not gonna act quite right. I've made the order open X, then close Y so that vim still has *something* open. Hopefully that means your Vim session won't disappear out from underneath you.

Yep, I just tested it out. Vim doesn't close, and the 'tb' command works fine regardless of the # of windows open. The 'ts' command acts a little funny when there is more than one split open in a tab. It removes one split, sticks it in another tab but doesn't completely remove the other tab b/c there's still a window in it. Not so bad. Just a little wierd.


Michael Anderson said...

I know this post was from a long time ago.. but it just about does what I'm looking for. So any idea how to make it move an unsaved buffer from a split to a new tab?

DeeSumm said...

I'm pretty sure that the following will do what you want:

" Turn a split into a tab
:command! Tb :let a=bufnr("%") | tabnew | exe "b ". a | tabp | q | tabnext

" Turn a tab into a split
:command! Ts :let a=bufnr("%") | tabprev | sp | exe "b ". a | tabnext | q

The trick is to make the new space before deleting the old.

Rodrigo Rosenfeld Rosas said...

T (capital t) is native to Vim and will open the active split in a new tab, closing the split.

DeeSumm said...

Hi Rodrigo,

Thanks for the comment...I looked around the help files and couldn't find any documentation for the 'T' command you were talking about.

Could you point me in the right direction?

Rodrigo Rosenfeld Rosas said...

I probably meant Ctrl+w, T, but I may have put the ctrl+w under a tag that was escaped by your comment system...

Anonymous said...

to join a split screen into a tab:
C-w T