FANDOM


(From comment in tip 259 (now removed): we do have a statement like that in the category)
(Navigation: update comment about 0gt, which jumps to the next tab)
 
(31 intermediate revisions by 12 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=1347
 
|id=1347
 
|previous=1343
 
|previous=1343
 
|next=1348
 
|next=1348
|created=September 29, 2006
+
|created=2006
 
|complexity=basic
 
|complexity=basic
|author=mchenryk
+
|author=
 
|version=7.0
 
|version=7.0
 
|rating=108/42
 
|rating=108/42
|category1=Tabs
+
|category1=Getting started
|category2=
+
|category2=Tabs
 
}}
 
}}
Add the following lines to your vimrc:
+
This tip provides an introduction to opening, navigating, and working with tab pages. In Vim, each file is loaded into a buffer, which can be displayed in any number of windows, in any number of tabs. The easiest way to think about tab pages in Vim is to consider them to be viewports, layouts, or workspaces.
  +
  +
In many editors (not Vim), each file is opened in a new tab, and one tab can show only one file, and one file cannot appear in more than one tab. Vim's tab pages do not have these limitations, and tabs are a convenient way to organize your work. See [[Quick tips for using tab pages]] for examples of how tabs can be used to their full potential.
  +
  +
Trying to configure Vim to always have ''one file per tab'' will not be successful, and would remove much of the power of Vim. However, when you want to edit a file, it is easy to use <code>:tabe</code> instead of <code>:e</code> so that ''usually'' there is one file per tab. You can also launch files in new tabs under [[Launch files in new tabs under Windows|Windows]] and [[Launch files in new tabs under Unix|Unix]].
  +
  +
An alternative to ''one file per tab'' is to learn to use the {{help|prefix=no|id='hidden'}} option combined with efficient use of the buffer list via a plugin such as {{script|id=1984|text=FuzzyFinder}}, {{script|id=1890|text=LustyExplorer}} or {{script|id=42|text=BufExplorer}}.
  +
  +
==Opening and closing tabs==
  +
When starting Vim, the <code>-p</code> option opens each specified file in a separate tab (up to the value of the <code>'tabpagemax'</code> option). Examples:
 
<pre>
 
<pre>
map <C-t> :tabnew<CR>
+
vim -p first.txt second.txt
map <C-left> :tabp<CR>
+
gvim -p *.txt
map<C-right> :tabn<CR>
 
 
</pre>
 
</pre>
   
Then in gvim you have the following commands:
+
Once Vim has been launched, there are many commands that directly create or close tabs:
* Ctrl-t &ndash; open a new tab
+
<pre>
* Ctrl-left arrow &ndash; move one tab to the left
+
:tabedit {file} edit specified file in a new tab
* Ctrl-right arrow &ndash; move one tab to the right
+
:tabfind {file} open a new tab with filename given, searching the 'path' to find it
  +
:tabclose close current tab
  +
:tabclose {i} close i-th tab
  +
:tabonly close all other tabs (show only the current tab)
  +
</pre>
   
==Basic Navigation==
+
The <code>:tabfind</code> command uses Vim's <code>'path'</code> option to determine which directories should be searched when opening the specified file. For example, the following tells Vim to look in the directory containing the current file (<code>.</code>), then the current directory (empty text between two commas), then each directory under the current directory ('<code>**</code>').
 
<pre>
 
<pre>
:tabs list all tabs
+
:set path=.,,**
:tabm 0 move the current tab first
 
:tabm {i} move the current tab to the i+1 position
 
:tabn move to (view) the next tab
 
:tabp move to (view) the previous tab
 
:tabfirst move to the first tab
 
:tabf {file} open a new tab with the filename given, searching the 'path' to find it
 
:tabc close the current tab
 
:tabc {i} close the i-th tab
 
:tabo close other tabs
 
 
</pre>
 
</pre>
   
For basic tab navigation, it is probably more convenient to use the built-in normal-mode commands:
+
Remember, as with any Vim commands, you only need type enough characters in the command for Vim to be able to unambiguously identify it. For example, you could use <code>:tabe</code> and <code>:tabf</code> instead of <code>:tabedit</code> and <code>:tabfind</code>.
   
  +
In addition to these commands, because Vim already has a plethora of commands for working with split windows, Vim provides the {{help|prefix=no|:tab}} command-line modifier, to use a new tab instead of a new window for commands that would normally split a window. For example:
 
<pre>
 
<pre>
gt move to (view) the next tab
+
:tab ball show each buffer in a tab (up to 'tabpagemax' tabs)
gT move to (view) the previous tab
+
:tab help open a new help window in its own tab page
{i}gt move to (view) the tab in the i-th position
+
:tab drop {file} open {file} in a new tab, or jump to a window/tab containing the file if there is one
  +
:tab split copy the current window to a new tab of its own
 
</pre>
 
</pre>
   
==Comments==
+
A command like <code>:sp myfile.txt</code> creates a new window in the current tab editing the specified file. That window can be ''moved'' to a new tab by pressing Ctrl-W <code>T</code>, and can be ''copied'' to a new tab with the command <code>:tab sp</code> (split the current window, but open the split in a new tab).
{{Todo}}
 
*No point having a tip change the default keybindings for <tt>:tabn</tt> and <tt>:tabp</tt>. Explain the defaults.
 
*Merge in any useful comments from below.
 
*Perhaps rename to "Using tab pages" (a simpler title that attempts to avoid confusion with the tab key).
 
   
----
+
You can type Ctrl-W <code>c</code> to close the current window. If that window is the last window visible in a tab, the tab is also closed (if another tab page is currently open).
Does not work in [ax]term.
 
   
----
+
If the file you are editing contains the name of another file, you can put the cursor on the name and type <code>gf</code> to edit the file ([[Open file under cursor|goto file]]). If you type Ctrl-W <code>gf</code> the file is displayed in a new tab.
Or you could use <tt>gt</tt> and <tt>gT</tt> without having to move your hands across the keyboard. Also, <tt>gt</tt> can take the tab number to jump directly to a tab.
 
   
----
+
In gvim, you can right click the tab label bar for a popup menu with Close, New&nbsp;Tab, and Open&nbsp;Tab... items.
I use Vim in Windows, so remapping C-Left isn't great for me. These are the mappings I use:
+
  +
==Navigation==
 
<pre>
 
<pre>
" Tab mappings
+
:tabs list all tabs including their displayed windows
map &lt;S-Up> :tabclose<CR>
+
:tabm 0 move current tab to first
map &lt;S-Down> :tabnew<CR>
+
:tabm move current tab to last
map &lt;S-Left> gT
+
:tabm {i} move current tab to position i+1
map &lt;S-Right> gt
+
map &lt;S-PageUp> :tabfirst<CR>
+
:tabn go to next tab
map &lt;S-PageDown> :tablast<CR>
+
:tabp go to previous tab
  +
:tabfirst go to first tab
  +
:tablast go to last tab
 
</pre>
 
</pre>
   
I used down for a new tab because I'm used to that from Opera's mouse gestures.
+
In normal mode, you can type:
  +
<pre>
  +
gt go to next tab
  +
gT go to previous tab
  +
{i}gt go to tab in position i
  +
</pre>
  +
Note that the gt command counts from one. That means 3gt will jump to the third tab. Also note is 0gt and gt mean the same thing: jumping to the next tab.
   
----
+
Using recent vim versions, in normal mode and in insert mode, you can type:
I prefer <tt>gt</tt> and <tt>gT</tt>. In any case <C-PgUp> and <C-PgDn> are the defaults for the same operations.
+
<pre>
+
Ctrl-PgDn go to next tab
Just opening a new tab is kind of useless. I find <tt>:tabe <filename></tt> more useful.
+
Ctrl-PgUp go to previous tab
  +
</pre>
   
----
+
Jumping to a specific tab with <code>{i}gt</code> is easier if you set up your tabline to [[Show tab number in your tab line|show the tab number]].
Instead of <tt>:tabe filename</tt>, I like <tt>:tabf filename</tt>, because it walks the path to find the name, instead of relying on an required explicit path/filename.
 
   
----
+
==Switching to another buffer==
If you are working with tags or cscope, <Ctrl-T> is for popping the stack.
+
By default, when switching to another buffer, the current buffer is hidden and its window is used to display the requested buffer. However, some commands can be configured with the <code>'switchbuf'</code> option to switch to another window, possibly in another tab, if the buffer is currently displayed in another window. {{help|'switchbuf'}}
   
----
+
With the following in your [[vimrc]], you can switch to the next buffer by pressing F8, or the previous buffer by pressing Shift-F8. If the target buffer is already displayed in a window in one of the tabs, that window will be displayed. Otherwise, the current window will be split, and the target buffer will be displayed in the new window.
I think this can be condensed to:
 
 
<pre>
 
<pre>
:tab sp<CR>
+
set switchbuf=usetab
  +
nnoremap <F8> :sbnext<CR>
  +
nnoremap <S-F8> :sbprevious<CR>
 
</pre>
 
</pre>
   
Alternative way to move current window to a new tab is: <Ctrl-w> T (capital).
+
If you prefer to open a new ''tab'' instead of a new split window, you can set <code>switchbuf</code> to <code>usetab,newtab</code>.
   
===Possibly useful information from tip 1313 (now removed)===
+
Using this technique will cycle between buffers without disturbing the current window or tab layout, providing each buffer is already in a window in a tab. Each listed buffer ({{help|'buflisted'}}) is visited in order of buffer number.
<pre>
 
:tabo " only this tab (close other tabs)
 
:tab ball " show each buffer in a tab
 
   
" Set tab label to show buffer number, filename, if modified.
+
Note, that this option affects more than just the commands mapped above. Particularly, it affects the quickfix window. See the help for details.
:set guitablabel=%n/\ %t\ %M
+
  +
==Shortcuts==
  +
Here are some ideas for entries you may want to add to your [[vimrc]].
  +
  +
With the following mappings (which require gvim), you can press Ctrl-Left or Ctrl-Right to go to the previous or next tabs, and can press Alt-Left or Alt-Right to move the current tab to the left or right.
  +
<pre>
  +
nnoremap <C-Left> :tabprevious<CR>
  +
nnoremap <C-Right> :tabnext<CR>
  +
nnoremap <silent> <A-Left> :execute 'silent! tabmove ' . (tabpagenr()-2)<CR>
  +
nnoremap <silent> <A-Right> :execute 'silent! tabmove ' . (tabpagenr()+1)<CR>
 
</pre>
 
</pre>
   
When starting Vim, the <tt>-p</tt> option opens each file in a tab, for example:
+
With the following, you can press F8 to show all buffers in tabs, or to close all tabs (toggle: it alternately executes <code>:tab ball</code> and <code>:tabo</code>).
 
<pre>
 
<pre>
gvim -p *.txt
+
let notabs = 0
  +
nnoremap <silent> <F8> :let notabs=!notabs<Bar>:if notabs<Bar>:tabo<Bar>:else<Bar>:tab ball<Bar>:tabn<Bar>:endif<CR>
 
</pre>
 
</pre>
   
===From comment in tip 259 (now removed)===
+
The following command abbreviation allows typing <code>:tabv myfile.txt</code> to view the specified file in a new tab; the buffer is read-only and nomodifiable so you cannot accidentally change it.
Press F8 to show all buffers in tabs, or to close all tabs (toggle).
 
 
<pre>
 
<pre>
let g:toggleTabs = 0
+
cabbrev tabv tab sview +setlocal\ nomodifiable
nnoremap <silent> <F8> :if g:toggleTabs<Bar>:tabo<Bar>:let g:toggleTabs = 0<Bar>:else<Bar>:tab ball<Bar>:let g:toggleTabs = 1<Bar>:endif<CR>
 
 
</pre>
 
</pre>
----
+
Whereas it's definitely useful to have a tip covering '''tab pages''', I'd like to see a disclaimer explaining what tab pages are not, i.e. the same as tabs in a tabbed editor like Eclipse or Visual Studio. This (admittedly rather bold) text is what #vim uses to help "misguided" users realize what tab pages are not:
+
==See also==
:''Tabs are not buffers, don't try to force them to act like buffers. Consider tabs like viewports, layouts or workspaces. Tabs is a terrible name. Trying to setup 1 tab == 1 buffer is an exercise in futility. Do :set hidden and get started. Get FuzzyFinder, LustyExplorer or BufExplorer to make getting around your buffers easier.''
+
*We have ''many'' tips about [[:Category:Tabs|using tab pages]]
We also recommend that new users read [http://vim.pastey.net/115548 godlygeek's introduction to buffers, windows and tab pages]. That we have created these quick responses might give you a clue as to how many questions we get concerning the 1 tab == 1 buffer setup. :) ([[User:Spiiph|Spiiph]] 22:32, 8 August 2009 (UTC))
+
::There is a small disclaimer in [[:Category:Tabs]]. Feel free to make the wording stronger if you feel it needs it. --[[User:Fritzophrenic|Fritzophrenic]] 00:28, 9 August 2009 (UTC)
+
  +
==Fixing tips on tab pages==
  +
:''This section is a temporary area to plan merging/fixing existing tips on tab pages.''
  +
{{Todo}}
  +
*Perhaps merge in some information from tips listed below (or add as 'see also').
  +
*'''I think the tip should be renamed to "Using tab pages"? Any comments?'''
  +
:
  +
:- Put some basic commands up top (move between tabs, open a new tab in VIM etc). This aught to answer 90% of the questions, then go into :tabedit {file} commands which is much more details than most people want or care about.
  +
::Yes, this tip is much less of an introduction and covers much more detail now. I was thinking about this tip as a "How to use tab pages" and [[Quick tips for using tab pages]] as a "Why to use tab pages" but I don't think there's that much reason to make those the names. The pages really don't have that much to do with each other. --[[User:Fritzophrenic|Fritzophrenic]] 15:59, May 20, 2010 (UTC)
  +
  +
;Navigation mappings
  +
*[[VimTip1221|1221 Alternative tab navigation]] mappings to jump to a tab
  +
*[[VimTip1295|1295 Select tab page with a key]] tab navigation mappings
  +
*[[VimTip1309|1309 Safari style keybindings for tab navigation]] more tab navigation mappings
  +
  +
;Miscellaneous
  +
*[[VimTip1247|1247 Tabclose instead of quit-all]] something about closing tab pages
  +
*[[VimTip1317|1317 Open every buffer in its own tabpage]] open each buffer in a tab page
  +
*[[VimTip1333|1333 Have focus on left tab after tabclose]] close tab and focus on previous tab
  +
  +
==Comments==

Latest revision as of 23:12, January 6, 2019

Tip 1347 Printable Monobook Previous Next

created 2006 · complexity basic · version 7.0


This tip provides an introduction to opening, navigating, and working with tab pages. In Vim, each file is loaded into a buffer, which can be displayed in any number of windows, in any number of tabs. The easiest way to think about tab pages in Vim is to consider them to be viewports, layouts, or workspaces.

In many editors (not Vim), each file is opened in a new tab, and one tab can show only one file, and one file cannot appear in more than one tab. Vim's tab pages do not have these limitations, and tabs are a convenient way to organize your work. See Quick tips for using tab pages for examples of how tabs can be used to their full potential.

Trying to configure Vim to always have one file per tab will not be successful, and would remove much of the power of Vim. However, when you want to edit a file, it is easy to use :tabe instead of :e so that usually there is one file per tab. You can also launch files in new tabs under Windows and Unix.

An alternative to one file per tab is to learn to use the 'hidden' option combined with efficient use of the buffer list via a plugin such as FuzzyFinder, LustyExplorer or BufExplorer.

Opening and closing tabsEdit

When starting Vim, the -p option opens each specified file in a separate tab (up to the value of the 'tabpagemax' option). Examples:

vim -p first.txt second.txt
gvim -p *.txt

Once Vim has been launched, there are many commands that directly create or close tabs:

:tabedit {file}   edit specified file in a new tab
:tabfind {file}   open a new tab with filename given, searching the 'path' to find it
:tabclose         close current tab
:tabclose {i}     close i-th tab
:tabonly          close all other tabs (show only the current tab)

The :tabfind command uses Vim's 'path' option to determine which directories should be searched when opening the specified file. For example, the following tells Vim to look in the directory containing the current file (.), then the current directory (empty text between two commas), then each directory under the current directory ('**').

:set path=.,,**

Remember, as with any Vim commands, you only need type enough characters in the command for Vim to be able to unambiguously identify it. For example, you could use :tabe and :tabf instead of :tabedit and :tabfind.

In addition to these commands, because Vim already has a plethora of commands for working with split windows, Vim provides the :tab command-line modifier, to use a new tab instead of a new window for commands that would normally split a window. For example:

:tab ball         show each buffer in a tab (up to 'tabpagemax' tabs)
:tab help         open a new help window in its own tab page
:tab drop {file}  open {file} in a new tab, or jump to a window/tab containing the file if there is one
:tab split        copy the current window to a new tab of its own

A command like :sp myfile.txt creates a new window in the current tab editing the specified file. That window can be moved to a new tab by pressing Ctrl-W T, and can be copied to a new tab with the command :tab sp (split the current window, but open the split in a new tab).

You can type Ctrl-W c to close the current window. If that window is the last window visible in a tab, the tab is also closed (if another tab page is currently open).

If the file you are editing contains the name of another file, you can put the cursor on the name and type gf to edit the file (goto file). If you type Ctrl-W gf the file is displayed in a new tab.

In gvim, you can right click the tab label bar for a popup menu with Close, New Tab, and Open Tab... items.

NavigationEdit

:tabs         list all tabs including their displayed windows
:tabm 0       move current tab to first
:tabm         move current tab to last
:tabm {i}     move current tab to position i+1

:tabn         go to next tab
:tabp         go to previous tab
:tabfirst     go to first tab
:tablast      go to last tab

In normal mode, you can type:

gt            go to next tab
gT            go to previous tab
{i}gt         go to tab in position i

Note that the gt command counts from one. That means 3gt will jump to the third tab. Also note is 0gt and gt mean the same thing: jumping to the next tab.

Using recent vim versions, in normal mode and in insert mode, you can type:

Ctrl-PgDn     go to next tab
Ctrl-PgUp     go to previous tab

Jumping to a specific tab with {i}gt is easier if you set up your tabline to show the tab number.

Switching to another bufferEdit

By default, when switching to another buffer, the current buffer is hidden and its window is used to display the requested buffer. However, some commands can be configured with the 'switchbuf' option to switch to another window, possibly in another tab, if the buffer is currently displayed in another window. :help 'switchbuf'

With the following in your vimrc, you can switch to the next buffer by pressing F8, or the previous buffer by pressing Shift-F8. If the target buffer is already displayed in a window in one of the tabs, that window will be displayed. Otherwise, the current window will be split, and the target buffer will be displayed in the new window.

set switchbuf=usetab
nnoremap <F8> :sbnext<CR>
nnoremap <S-F8> :sbprevious<CR>

If you prefer to open a new tab instead of a new split window, you can set switchbuf to usetab,newtab.

Using this technique will cycle between buffers without disturbing the current window or tab layout, providing each buffer is already in a window in a tab. Each listed buffer (:help 'buflisted') is visited in order of buffer number.

Note, that this option affects more than just the commands mapped above. Particularly, it affects the quickfix window. See the help for details.

ShortcutsEdit

Here are some ideas for entries you may want to add to your vimrc.

With the following mappings (which require gvim), you can press Ctrl-Left or Ctrl-Right to go to the previous or next tabs, and can press Alt-Left or Alt-Right to move the current tab to the left or right.

nnoremap <C-Left> :tabprevious<CR>
nnoremap <C-Right> :tabnext<CR>
nnoremap <silent> <A-Left> :execute 'silent! tabmove ' . (tabpagenr()-2)<CR>
nnoremap <silent> <A-Right> :execute 'silent! tabmove ' . (tabpagenr()+1)<CR>

With the following, you can press F8 to show all buffers in tabs, or to close all tabs (toggle: it alternately executes :tab ball and :tabo).

let notabs = 0
nnoremap <silent> <F8> :let notabs=!notabs<Bar>:if notabs<Bar>:tabo<Bar>:else<Bar>:tab ball<Bar>:tabn<Bar>:endif<CR>

The following command abbreviation allows typing :tabv myfile.txt to view the specified file in a new tab; the buffer is read-only and nomodifiable so you cannot accidentally change it.

cabbrev tabv tab sview +setlocal\ nomodifiable

See alsoEdit


Fixing tips on tab pagesEdit

This section is a temporary area to plan merging/fixing existing tips on tab pages.

 TO DO 

  • Perhaps merge in some information from tips listed below (or add as 'see also').
  • I think the tip should be renamed to "Using tab pages"? Any comments?
- Put some basic commands up top (move between tabs, open a new tab in VIM etc). This aught to answer 90% of the questions, then go into :tabedit {file} commands which is much more details than most people want or care about.
Yes, this tip is much less of an introduction and covers much more detail now. I was thinking about this tip as a "How to use tab pages" and Quick tips for using tab pages as a "Why to use tab pages" but I don't think there's that much reason to make those the names. The pages really don't have that much to do with each other. --Fritzophrenic 15:59, May 20, 2010 (UTC)
Navigation mappings
Miscellaneous

CommentsEdit

Community content is available under CC-BY-SA unless otherwise noted.