History
Article Edit this page Discussion

Move current window between tabs

From Vim Tips Wiki

Jump to: navigation, search

Tip 1554 Previous Next Created: April 18, 2008 Complexity: basic Author: Paluh Version: 7.0


These two functions allow you to move window between tabs. Unlike the :tabmove command, which moves an entire tab to a new position, these functions will move a window in the current tab into another existing tab (or a new tab if there are no other existing tabs).

For example, assume you are editing files in three tabs: [1],[2],[3] – where [] indicates a tab page, and the list of numbers inside brackets shows the windows open in that tab (when there is more than one window in one tab, the script splits it horizontally).

Assume that we are in the first tab (so we are editing window with buffer with file 1 – bold marks current window). After :call MoveToNextTab() there will be [1,2],[3]. After the next :call MoveToNexTab() the windows will be arranged thusly: [2][1,3]. And after next one: [2],[3],[1].

Of course MoveToPrevTab() works in opposite direction.

function MoveToPrevTab()
  "there is only one window
  if tabpagenr('$') == 1 && winnr('$') == 1
    return
  endif
  "preparing new window
  let l:tab_nr = tabpagenr('$')
  let l:cur_buf = bufnr('%')
  if tabpagenr() != 1
    close!
    if l:tab_nr == tabpagenr('$')
      tabprev
    endif
    sp
  else
    close!
    exe "0tabnew"
  endif
  "opening current buffer in new window
  exe "b".l:cur_buf
endfunc

function MoveToNextTab()
  "there is only one window
  if tabpagenr('$') == 1 && winnr('$') == 1
    return
  endif
  "preparing new window
  let l:tab_nr = tabpagenr('$')
  let l:cur_buf = bufnr('%')
  if tabpagenr() < tab_nr
    close!
    if l:tab_nr == tabpagenr('$')
      tabnext
    endif
    sp
  else
    close!
    tabnew
  endif
  "opening current buffer in new window
  exe "b".l:cur_buf
endfunc

My mapping for them:

nnoremap <A-.> :call MoveToNextTab()<CR>
nnoremap <A-,> :call MoveToPrevTab()<CR>

[edit] Comments

Absolutelly love this! Thanks!

Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto Wiki
Doctor Who
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Mystery Science Theater 3000
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions
Send this article to a friend
"Move current window between tabs"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation