FANDOM


(merge in 201002 from Read (but not write) file in new tab)
(small additions)
Line 31: Line 31:
 
:tabc close current tab
 
:tabc close current tab
 
:tabc {i} close i-th tab
 
:tabc {i} close i-th tab
:tabo show only this tab (close other tabs)
+
:tabo close all other tabs (show only the current tab)
:tab ball show each buffer in a tab
+
:tab ball show each buffer in a tab (up to 'tabpagemax' tabs)
 
</pre>
 
</pre>
   
Line 43: Line 43:
   
 
You can type Ctrl-W <tt>c</tt> 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).
 
You can type Ctrl-W <tt>c</tt> 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 <tt>gf</tt> to edit the file ([[Open file under cursor|goto file]]). If you type Ctrl-W <tt>gf</tt> 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&nbsp;Tab, and Open&nbsp;Tab... items.
   
 
==Navigation==
 
==Navigation==
Line 64: Line 68:
 
</pre>
 
</pre>
   
In normal mode and in insert mode, you can type:
+
When using gvim, in normal mode and in insert mode, you can type:
 
<pre>
 
<pre>
 
Ctrl-PgDn go to next tab
 
Ctrl-PgDn go to next tab

Revision as of 10:14, May 19, 2010

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 tabs

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
:tabe {file}  edit specified file in a new tab
:tabf {file}  open a new tab with filename given, searching the 'path' to find it
:tabc         close current tab
:tabc {i}     close i-th tab
:tabo         close all other tabs (show only the current tab)
:tab ball     show each buffer in a tab (up to 'tabpagemax' tabs)

The :tabf 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=.,,**

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.

Navigation

: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

When using gvim, 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.

Shortcuts

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

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 = 1
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 tabnew\|setlocal nomodifiable\|view

Fixing tips on tab pages

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').
  • Perhaps rename to "Using tab pages"?
Relevant tips
Open files in tabs

Comments

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