Vim Tips Wiki
(merge in from 1223 and 1242)
Tags: Visual edit apiedit
(12 intermediate revisions by 7 users not shown)
Line 8: Line 8:
 
|version=7.0
 
|version=7.0
 
|rating=108/42
 
|rating=108/42
|category1=Getting_started
+
|category1=Getting started
 
|category2=Tabs
 
|category2=Tabs
 
}}
 
}}
Line 15: Line 15:
 
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.
 
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 <tt>:tabe</tt> instead of <tt>:e</tt> 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]].
+
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}}.
 
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==
 
==Opening and closing tabs==
When starting Vim, the <tt>-p</tt> option opens each specified file in a separate tab (up to the value of the <tt>'tabpagemax'</tt> option). Examples:
+
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
 
vim -p first.txt second.txt
Line 26: Line 26:
 
</pre>
 
</pre>
   
  +
Once Vim has been launched, there are many commands that directly create or close tabs:
 
<pre>
 
<pre>
:tabe {file} edit specified file in a new tab
+
:tabedit {file} edit specified file in a new tab
:tabf {file} open a new tab with filename given, searching the 'path' to find it
+
:tabfind {file} open a new tab with filename given, searching the 'path' to find it
:tabc close current tab
+
:tabclose close current tab
:tabc {i} close i-th tab
+
:tabclose {i} close i-th tab
:tabo close all other tabs (show only the current tab)
+
:tabonly close all other tabs (show only the current tab)
:tab ball show each buffer in a tab (up to 'tabpagemax' tabs)
 
 
</pre>
 
</pre>
   
The <tt>:tabf</tt> command uses Vim's <tt>'path'</tt> 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 (<tt>.</tt>), then the current directory (empty text between two commas), then each directory under the current directory ('<tt>**</tt>').
+
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>
 
:set path=.,,**
 
:set path=.,,**
 
</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>.
A command like <tt>:sp myfile.txt</tt> 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 <tt>T</tt>, and can be ''copied'' to a new tab with the command <tt>:tab sp</tt> (split the current window, but open the split in a new tab).
 
   
  +
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:
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).
 
  +
<pre>
 
: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
  +
</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).
  +
 
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).
   
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.
+
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.
   
 
In gvim, you can right click the tab label bar for a popup menu with Close, New&nbsp;Tab, and Open&nbsp;Tab... items.
 
In gvim, you can right click the tab label bar for a popup menu with Close, New&nbsp;Tab, and Open&nbsp;Tab... items.
Line 67: Line 77:
 
{i}gt go to tab in position i
 
{i}gt go to tab in position i
 
</pre>
 
</pre>
  +
Note that the gt command counts from one. That means 3gt will jump to the third tab. Also note is 0gt and 1gt mean the same thing: jumping to the first tab.
   
 
When using gvim, in normal mode and in insert mode, you can type:
 
When using gvim, in normal mode and in insert mode, you can type:
Line 74: Line 85:
 
</pre>
 
</pre>
   
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 <code>{i}gt</code> is easier if you set up your tabline to [[Show tab number in your tab line|show the tab number]].
  +
  +
==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'}}
  +
  +
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.
  +
<pre>
  +
set switchbuf=usetab
  +
nnoremap <F8> :sbnext<CR>
  +
nnoremap <S-F8> :sbprevious<CR>
  +
</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>.
  +
  +
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.
   
 
==Shortcuts==
 
==Shortcuts==
Line 87: Line 114:
 
</pre>
 
</pre>
   
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>).
+
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>
let notabs = 1
+
let notabs = 0
 
nnoremap <silent> <F8> :let notabs=!notabs<Bar>:if notabs<Bar>:tabo<Bar>:else<Bar>:tab ball<Bar>:tabn<Bar>:endif<CR>
 
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 <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.
+
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.
 
<pre>
 
<pre>
cabbrev tabv tabnew\|setlocal nomodifiable\|view
+
cabbrev tabv tab sview +setlocal\ nomodifiable
 
</pre>
 
</pre>
   
 
==See also==
 
==See also==
  +
*We have ''many'' tips about [[:Category:Tabs|using tab pages]]
*[[Move current window between tabs]] move a single window from one tab to the next
 
  +
   
 
==Fixing tips on tab pages==
 
==Fixing tips on tab pages==
Line 105: Line 133:
 
{{Todo}}
 
{{Todo}}
 
*Perhaps merge in some information from tips listed below (or add as 'see also').
 
*Perhaps merge in some information from tips listed below (or add as 'see also').
*Perhaps rename to "Using tab pages"?
+
*'''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
;Relevant tips
 
*[[VimTip95|95 Capture ex command output]] redirect output to a new tab
 
*[[VimTip133|133 Windo and Bufdo]] tabdo and keep current tab
 
 
*[[VimTip1221|1221 Alternative tab navigation]] mappings to jump to a tab
 
*[[VimTip1221|1221 Alternative tab navigation]] mappings to jump to a tab
*[[VimTip1233|1233 Move the current tabpage forward or backward]] move current tab page
 
*[[VimTip1247|1247 Tabclose instead of quit-all]] something about closing tab pages
 
 
*[[VimTip1295|1295 Select tab page with a key]] tab navigation mappings
 
*[[VimTip1295|1295 Select tab page with a key]] tab navigation mappings
 
*[[VimTip1309|1309 Safari style keybindings for tab navigation]] more 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
 
*[[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
 
*[[VimTip1333|1333 Have focus on left tab after tabclose]] close tab and focus on previous tab
*[[VimTip1491|1491 Call TortoiseSVN commands from within Vim]]
 
*[[VimTip1554|1554 Move current window between tabs]] move current window between tabs
 
 
;Open files in tabs
 
*[[VimTip1225|1225 Open files in multiple tabs using the Windows Context Menu]]
 
*[[VimTip1314|1314 Add open-in-tabs context menu for Windows]]
 
*[[VimTip1440|1440 Launch files in new tabs under Windows]] (mentioned in intro)
 
*[[VimTip1552|1552 Launch files in new tabs under Unix]] (mentioned in intro)
 
   
 
==Comments==
 
==Comments==
 
===Rough merge in from 1242===
 
I have too many ways to move between buffers. The <tt>:ls</tt> command lists all the buffers and their buffer numbers; <tt>:b<number></tt> replaces the contents of the current window with the requested buffer; <tt>:gt</tt> moves to the next tab; and I configured <tt><Leader>w</tt> to move to the next window (within a tab). That's a lot of options.
 
 
Generally, I want a way to move between 'files' without disturbing my current window and tab layout. As I finally learned, a combination of <tt>:sb</tt> and 'switchbuf' makes this possible.
 
 
The <tt>:sb <buffer></tt> (split buffer) command usually opens a new window with the requested buffer. You can give the command a buffer number or a filename. With the <tt>set switchbuf=usetab</tt> option, you can tell Vim to first search the tab and window list for the requested buffer - if the buffer exists is in a window or tab, the focus goes right to that window. The <tt>:sbn</tt> (split buffer next), when used with switchbuf, lets me flip through the list of my open files.
 
 
----
 

Revision as of 18:07, 4 November 2015

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

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.

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

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

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.

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

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.

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()<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 also


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

Comments