Vim Tips Wiki
No edit summary
 
(18 intermediate revisions by 7 users not shown)
Line 1: Line 1:
  +
{{TipNew
This two functions allows to move window between tabs. It is not equal to moving tabs (it's some-how similar to moving window between workspaces in X system).
 
  +
|id=1554
  +
|previous=1553
  +
|next=1555
  +
|created=2008
  +
|complexity=intermediate
  +
|author=Paluh
  +
|version=7.0
  +
|subpage=/200804
  +
|category1=Tabs
  +
|category2=
  +
}}
  +
These two functions allow you to move window between tabs. Unlike the <code>:tabmove</code> 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).
   
Assume that there are three tabs which edits files: [1],[2],[3] - where [] mean tab and list of numbers inside mean windows (splited horizontaly if there is more then one in tab). Assume that we are in first tab (so we are editing window with buffer with file '1'). After '':call MoveToNextTab()'' there will be [1,2],[3]. After next :call ''MoveToNexTab()'' sitiuation will be like that: [2][1,3]. And after next one: [2],[3],[1].
+
For example, assume you are editing files in three tabs: ['''1'''],[2],[3] &ndash; 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''' &ndash; bold marks current window). After '''<code>:call MoveToNextTab()</code>''' there will be ['''1''',2],[3]. After the next '''<code>:call MoveToNextTab()</code>''' the windows will be arranged thusly: [2]['''1''',3]. And after next one: [2],[3],['''1'''].
  +
  +
Of course '''<code>MoveToPrevTab()</code>''' works in opposite direction.
   
 
<pre>
 
<pre>
 
function MoveToPrevTab()
 
function MoveToPrevTab()
"there is only one window
+
"there is only one window
if tabpagenr('$') == 1 && winnr('$') == 1
+
if tabpagenr('$') == 1 && winnr('$') == 1
return
+
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
 
endif
 
sp
 
 
else
"preparing new window
 
 
close!
let l:tab_nr = tabpagenr('$')
 
 
exe "0tabnew"
let l:cur_buf = bufnr('%')
 
 
endif
if tabpagenr() != 1
 
 
"opening current buffer in new window
close!
 
 
exe "b".l:cur_buf
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
 
endfunc
   
 
function MoveToNextTab()
 
function MoveToNextTab()
"there is only one window
+
"there is only one window
if tabpagenr('$') == 1 && winnr('$') == 1
+
if tabpagenr('$') == 1 && winnr('$') == 1
return
+
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
 
endif
 
sp
 
 
else
"preparing new window
 
 
close!
let l:tab_nr = tabpagenr('$')
 
 
tabnew
let l:cur_buf = bufnr('%')
 
 
endif
if tabpagenr() < tab_nr
 
 
"opening current buffer in new window
close!
 
 
exe "b".l:cur_buf
if l:tab_nr == tabpagenr('$')
 
tabnext
 
endif
 
sp
 
else
 
close!
 
tabnew
 
endif
 
"opening current buffer in new window
 
exe "b".l:cur_buf
 
 
endfunc
 
endfunc
 
 
</pre>
 
</pre>
  +
 
 
My mapping for them:
 
My mapping for them:
 
 
<pre>
 
<pre>
map <A-.> :call MoveToNextTab()<CR>
+
nnoremap <A-.> :call MoveToNextTab()<CR>
map <A-,> :call MoveToPrevTab()<CR>
+
nnoremap <A-,> :call MoveToPrevTab()<CR>
 
</pre>
 
</pre>
  +
  +
==Related plugins==
  +
* {{script|id=1961|text=Tabmerge}} will merge all the windows of one tab into another
   
 
==Comments==
 
==Comments==
  +
If you prefer vertically-split windows, you can add <C-w>H to the mappings to make the moved buffer vertical. My mappings look like this:<br />
This looks like it might be promising, but you should explain better what it does, and how it's different from/why it can't use {{help|:tabmove}} or {{help|CTRL-W_T}}. --[[User:Fritzophrenic|Fritzophrenic]] 16:06, 18 April 2008 (UTC)
 
  +
<code>
 
  +
map <C-m> :call MoveToNextTab()<CR><C-w>H<br />
One more way:
 
 
map <C-n> :call MoveToPrevTab()<CR><C-w>H
<pre>
 
 
</code>
function MoveTabLeft()
 
let current_tab = tabpagenr()
 
if current_tab > 1
 
let current_tab = current_tab - 2
 
execute 'tabmove' current_tab
 
endif
 
endfunction
 
 
function MoveTabRight()
 
let current_tab = tabpagenr()
 
execute 'tabmove' current_tab
 
endfunction
 
 
map <Leader>tl :call MoveTabLeft()<CR>
 
map <Leader>tr :call MoveTabRight()<CR>
 
</pre>
 
<small>--Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[User:77.232.5.123|77.232.5.123]] ([[User talk:77.232.5.123|talk]] • [[Special:Contributions/77.232.5.123|contribs]]) 20:44 Apr 1 2008</small><!-- Template:Unsigned -->
 
----
 
 
 
   
  +
Alternately, replace the <code>sp</code> lines with
[[Category:Tabs]]
 
  +
<code>
  +
vert topleft split
  +
</code>

Latest revision as of 15:10, 4 September 2013

Tip 1554 Printable Monobook Previous Next

created 2008 · complexity intermediate · 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 MoveToNextTab() 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>

Related plugins[]

  • Tabmerge will merge all the windows of one tab into another

Comments[]

If you prefer vertically-split windows, you can add <C-w>H to the mappings to make the moved buffer vertical. My mappings look like this:
map <C-m> :call MoveToNextTab()<CR><C-w>H
map <C-n> :call MoveToPrevTab()<CR><C-w>H

Alternately, replace the sp lines with vert topleft split