Vim Tips Wiki

Editing

Using tab pages

1
  • The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit. If you are undoing an edit that is …
Latest revision Your text
Line 1: Line 1:
  +
{{review}}
 
{{TipImported
 
{{TipImported
 
|id=1347
 
|id=1347
 
|previous=1343
 
|previous=1343
 
|next=1348
 
|next=1348
|created=2006
+
|created=September 29, 2006
 
|complexity=basic
 
|complexity=basic
|author=
+
|author=mchenryk
 
|version=7.0
 
|version=7.0
 
|rating=108/42
 
|rating=108/42
|category1=Getting started
+
|category1=Tabs
|category2=Tabs
+
|category2=
 
}}
 
}}
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.
+
Add the following lines to your vimrc:
 
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>
vim -p first.txt second.txt
+
map <C-t> :tabnew<CR>
gvim -p *.txt
+
map <C-left> :tabp<CR>
  +
map<C-right> :tabn<CR>
 
</pre>
 
</pre>
   
Once Vim has been launched, there are many commands that directly create or close tabs:
+
Then in gvim you have the following commands:
<pre>
+
* Ctrl-t &ndash; open a new tab
:tabedit {file} edit specified file in a new tab
+
* Ctrl-left arrow &ndash; move one tab to the left
:tabfind {file} open a new tab with filename given, searching the 'path' to find it
+
* Ctrl-right arrow &ndash; move one tab to the right
:tabclose close current tab
 
:tabclose {i} close i-th tab
 
:tabonly close all other tabs (show only the current tab)
 
</pre>
 
   
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>').
+
==Basic Navigation==
 
<pre>
 
<pre>
:set path=.,,**
+
:tabs list all tabs
  +
: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>
   
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>.
+
For basic tab navigation, it is probably more convenient to use the built-in normal-mode commands:
   
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>
:tab ball show each buffer in a tab (up to 'tabpagemax' tabs)
+
gt move to (view) the next tab
:tab help open a new help window in its own tab page
+
gT move to (view) the previous tab
:tab drop {file} open {file} in a new tab, or jump to a window/tab containing the file if there is one
+
{i}gt move to (view) the tab in the i-th position
:tab split copy the current window to a new tab of its own
 
 
</pre>
 
</pre>
   
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).
+
==Comments==
  +
{{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>
:tabs list all tabs including their displayed windows
+
" Tab mappings
:tabm 0 move current tab to first
+
map &lt;S-Up> :tabclose<CR>
:tabm move current tab to last
+
map &lt;S-Down> :tabnew<CR>
:tabm {i} move current tab to position i+1
+
map &lt;S-Left> gT
+
map &lt;S-Right> gt
:tabn go to next tab
+
map &lt;S-PageUp> :tabfirst<CR>
:tabp go to previous tab
+
map &lt;S-PageDown> :tablast<CR>
:tabfirst go to first tab
 
:tablast go to last tab
 
 
</pre>
 
</pre>
   
In normal mode, you can type:
+
I used down for a new tab because I'm used to that from Opera's mouse gestures.
<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:
+
----
<pre>
+
I prefer <tt>gt</tt> and <tt>gT</tt>. In any case <C-PgUp> and <C-PgDn> are the defaults for the same operations.
Ctrl-PgDn go to next tab
 
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]].
+
Just opening a new tab is kind of useless. I find <tt>:tabe <filename></tt> more useful.
   
==Switching to another buffer==
+
----
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'}}
+
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.
   
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.
+
----
  +
If you are working with tags or cscope, <Ctrl-T> is for popping the stack.
  +
  +
----
  +
I think this can be condensed to:
 
<pre>
 
<pre>
set switchbuf=usetab
+
:tab sp<CR>
nnoremap <F8> :sbnext<CR>
 
nnoremap <S-F8> :sbprevious<CR>
 
 
</pre>
 
</pre>
   
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>.
+
Alternative way to move current window to a new tab is: <Ctrl-w> T (capital).
   
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.
+
===Possibly useful information from tip 1313 (now removed)===
 
Note, that this option affects more than just the commands mapped above. Particularly, it affects the quickfix window. See the help for details.
 
 
==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>
 
<pre>
nnoremap <C-Left> :tabprevious<CR>
+
:tabo " only this tab (close other tabs)
nnoremap <C-Right> :tabnext<CR>
+
:tab ball " show each buffer in a tab
nnoremap <silent> <A-Left> :execute 'silent! tabmove ' . (tabpagenr()-2)<CR>
 
nnoremap <silent> <A-Right> :execute 'silent! tabmove ' . (tabpagenr()+1)<CR>
 
</pre>
 
   
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>).
+
" Set tab label to show buffer number, filename, if modified.
<pre>
+
:set guitablabel=%n/\ %t\ %M
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>
   
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.
+
When starting Vim, the <tt>-p</tt> option opens each file in a tab, for example:
 
<pre>
 
<pre>
cabbrev tabv tab sview +setlocal\ nomodifiable
+
gvim -p *.txt
 
</pre>
 
</pre>
 
==See also==
 
*We have ''many'' tips about [[:Category:Tabs|using tab pages]]
 
 
 
==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==
 
  Loading editor
Below are some commonly used wiki markup codes. Simply click on what you want to use and it will appear in the edit box above.

View this template