Vim Tips Wiki
(more tweaking)
(merge in 201002 from Read (but not write) file in new tab)
Line 71: Line 71:
   
 
Jumping to a specific tab with <tt>{i}gt</tt> is easier if you set up your tabline to [[Show tab number in your tab line|show the tab number]].
 
Jumping to a specific tab with <tt>{i}gt</tt> is easier if you set up your tabline to [[Show tab number in your tab line|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 <tt>:tab ball</tt> and <tt>:tabo</tt>).
 
<pre>
 
let notabs = 1
 
nnoremap <silent> <F8> :let notabs=!notabs<Bar>:if notabs<Bar>:tabo<Bar>:else<Bar>:tab ball<Bar>:tabn<Bar>:endif<CR>
 
</pre>
  +
  +
The following command abbreviation allows typing <tt>:tabv myfile.txt</tt> to view the specified file in a new tab; the buffer is read-only and nomodifiable so you cannot accidentally change it.
  +
<pre>
  +
cabbrev tabv tabnew\|setlocal nomodifiable\|view
  +
</pre>
   
 
==Fixing tips on tab pages==
 
==Fixing tips on tab pages==
Line 100: Line 114:
   
 
==Comments==
 
==Comments==
With the following in your [[vimrc]], you can press F8 to show all buffers in tabs, or to close all tabs (toggle: it alternately executes <tt>:tab ball</tt> and <tt>:tabo</tt>).
 
<pre>
 
let notabs = 1
 
nnoremap <silent> <F8> :let notabs=!notabs<Bar>:if notabs<Bar>:tabo<Bar>:else<Bar>:tab ball<Bar>:tabn<Bar>:endif<CR>
 
</pre>
 
----
 

Revision as of 08:18, 19 May 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         show only this tab (close other tabs)
:tab ball     show each buffer in a tab

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).

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

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