Vim Tips Wiki
(start cleanup)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
  +
{{TipNew
{{TipProposed
 
|id=0
+
|id=1630
|previous=0
+
|previous=1629
|next=0
+
|next=1631
|created=November 30, 2009
+
|created=2009
 
|complexity=basic
 
|complexity=basic
 
|author=Fritzophrenic
 
|author=Fritzophrenic
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
 
Vim provides tab pages as one way to organize your workspace, alongside (but '''''not''''' in place of) a buffer list and splittable windows. For basic tab use, see our [[Using tab pages]]. Once you know how to navigate Vim's tab pages, you may be confused as to why they act so differently from the tabs in other editors. Here are some quick ideas, no scripts required, that can get you started using tab pages to their full potential.
{{dodgy|This tip is still in a draft version, not yet ready for general consumption. You can probably get some good use out of it, but ''caveat emptor''. Please feel free to improve this tip as much as you can!}}
 
 
Vim provides tab pages as one way to organize your workspace, alongside a buffer list and splittable windows. For basic tab use, see our [[Introduction to using tab pages]]. Once you know how to navigate Vim's tab pages, you may be confused as to why they act so differently from the tabs in other editors. Here are some quick ideas, no scripts required, that can get you started using tab pages to their full potential.
 
   
 
===Full-page reference documents===
 
===Full-page reference documents===
By simply prefacing a <tt>:help</tt> command with the <tt>:tab</tt> modifier, you can have multiple full-page Vim help topics, each in their own conveniently-labeled tab page, without messing up your window layout.
+
By simply prefacing a <code>:help</code> command with the <code>:tab</code> modifier, you can have multiple full-page Vim help topics, each in its own conveniently-labeled tab page, without messing up your window layout. For example, instead of entering <code>:help toc</code>, type <code>:tab help toc</code>. If you have already opened a document in a window and would like it in a tab, press Ctrl-W then <code>T</code>.
   
You don't need to limit this to Vim's help. You can open other reference materials (API docs, framework source code, project notes, etc.) that you need to refer to often, but don't necessarily want taking up screen real estate adjacent to whatever I'm working on, in a new tab for quick and easy access.
+
You don't need to limit this to Vim's help. You can open other reference material (API docs, framework source code, project notes, etc.), each in a new tab for quick and easy access. This is useful when you need to refer to a document, but you do not want it taking up screen real estate next to the text you are currently working on.
   
 
===Quick changes outside normal workflow===
 
===Quick changes outside normal workflow===
Tabs are great places to work on small tasks that fall outside your normal workflow or tasks. Open a new tab page for [[vimrc]] changes or scratchpads to hold external command output, chunks of text, etc., and you can very quickly switch back to what you were working on before, without messing up your window layout or anything else. Just close the tab, and you're automatically back to whatever you were working on.
+
Tabs are great places to work on small tasks that fall outside your normal workflow. Open a new tab page for [[vimrc]] changes or for scratchpads to hold external command output, chunks of text, etc., and you can very quickly switch back to what you were working on before, without messing up your window layout or anything else. Just close the tab, and you're automatically back to whatever you were working on.
  +
 
===Alternate views of a file===
 
If you are working in a buffer with most folds open, you can keep a second tab page showing the buffer with all folds closed for an "at a glance summary". The folded version serves as an overview of the entire file, often visible on a single screen. You can take advantage of the "at a glance" view to rapidly find and mark a place in a file (typing <code>ma</code> creates a mark that applies to the buffer), then jump to the mark (<code>'a</code>) in a different tab showing the same buffer.
  +
 
You can also go the other way. Instead of keeping a tab page for a "summarized" view, you could open a tab page for a detailed "x-ray" view of a buffer. In a separate tab page, you can enable useful but visually noisy options like <code>'list'</code>, <code>'cursorcolumn'</code>, <code>'cursorline'</code>, and <code>'number'</code>, and you may have several <code>:match</code> patterns active, or similar. This way, you can keep your regular working view clean and uncluttered, but quickly flip over to the information-overloaded version when needed. This idea works for the settings in {{help|option-summary}} that are labeled 'local to window'.
  +
  +
===Separate 'diff' views===
  +
Along the same lines as the "x-ray" tab above, you can take advantage of the fact that a <code>'diff'</code> view of two or more buffers is local to the tab page. Knowing this, you can open up a new tab on the current buffer with <code>:tab split</code>, and then in the new tab use the {{help|:DiffOrig|prefix=no}} command to create a tab containing a diff of the current state of the buffer with the original state on disk. Now, you can go back to your original tab, make and even save changes, and your <code>'diff'</code> tab will remain open for you to quickly see changes you have made since you started.
  +
  +
You can go further than this. Using tabs, you are not limited to a single diff. If you have two portions of a single file that are very similar, and you want to see exactly how similar, you can open a new tab with two new buffers, then copy and paste the two sections of the original buffer which you want to compare, one in each window. Then run <code>:windo diffthis</code> in the new tab and you have a quick comparison of just parts of the buffer.
  +
  +
You don't even need to close any other diffs you may have in progress to do this. Using tab pages, you can have multiple diff tabs open at once, alongside tabs for normal editing.
  +
  +
===Redirected command output===
 
Sometimes, you may want to use the output from an ex command, instead of just viewing it in the status area. Using the {{help|:redir|prefix=no}} command, you can run a command, capture the output, then open a new tab page to paste the captured text. This could also be done with a new window, or even within the current window, but using a new tab page can be less disruptive, as it will not mess up your current window layout or buffer contents. If you do this often enough, you could create a simple command or mapping.
  +
  +
You can do the same sort of thing with external commands. The method is even easier. Just open a new tab with <code>:tabnew</code>, then use the {{help|:r!|prefix=no}} command to read in the external command. If you find yourself doing this a lot, you can [[Capture ex command output|use a script]].
  +
  +
===Mini-sessions===
 
If you remove <code>'tabpages'</code> from {{help|'sessionoptions'|prefix=no}}, then {{help|:mksession|prefix=no}} will only pay attention to the current tab page. This makes it easy to use tab pages as a lightweight project-organization tool&ndash;just open a set of files in whatever window layout you like, then run <code>:mksession some-project.vim</code> to save the current tab page as a session. To work on that set of files again, open a new tab page and :source the appropriate session file.
  +
  +
===Repeat a command on a selection of buffers===
  +
It is easy to run a command on all files you have loaded (though not necessarily visible) in Vim, using {{help|:bufdo|prefix=no}}. It is possible, using {{help|:argdo|prefix=no}} to run a command on a selection of files. But, the argument list can be tedious to work with, and additionally it is limited to real files; you can't use it on buffers that don't yet or will never have an associated file. You can use a new tab to open a window on every buffer you wish to run a command on, then use {{help|:windo|prefix=no}} to run the command on every buffer ''in the tab''. If you take care and pay attention to your buffer list instead of relying exclusively on tabs for your organization, you can even do something like:
   
  +
<pre>
===Differing views of a file===
 
  +
:tab sp
If you usually work in a buffer with most or all folds open, you can keep a second tab page buffer with all folds closed for an "at a glance summary". The folded version serves as an overview of the entire file, usually visible on a single screen. You can take advantage of this "at a glance" view to rapidly find a place in a file, set a mark (local to the buffer), and jump to it in another window/tab.
 
  +
:sball
  +
</pre>
   
  +
to open a tab with a window for ALL currently loaded buffers, and then close any buffers you wish to exclude from your command. Then proceed with the <code>:windo</code> and close the tab to return to what you were doing before.
You can also go the other way. Instead of keeping a tab page for a "summarized" view, you could open a tab page for a detailed "X-ray" view of a buffer. In a separate tab page, you can enable useful but visually noisy options like 'list', 'cursorcolumn', 'cursorline', and 'number', several :match patterns active, or other such things. This way, you can keep your regular working view clean and uncluttered, but quickly flip over to the information-overload version when you need it. This idea works for pretty much any setting in `:help option-summary` labeled 'local to window'.
 
   
  +
This method can be especially nice when working with the {{help|quickfix|prefix=no}} list, because you can use the {{help|'switchbuf'|prefix=no}} option to automatically split a window for a result, and the {{help|:cnfile|prefix=no}} command repeatedly (potentially by using {{help|@:|prefix=no}} with a count) to open every file selected by your {{help|:make|prefix=no}}, {{help|:grep|prefix=no}}, or other command.
   
  +
===More ideas===
* Redirected Ex command output: I often want to use the output from an Ex command (vs. just viewing it in the status area), so I have a routine that runs a command, captures the output, then opens a new tab page containing the captured text. As with most of these techniques, this could also be done with a new window, or even within the current window, but I find using a new tab page less disruptive.
 
  +
There are plenty more creative things you can do with Vim's tab pages. If you don't try to limit yourself to one-file-per-tab and one-tab-per-file, you will only be limited by our own imagination. Take a look at the other tips in the [[:Category:Tabs|Tabs category]] for more ideas and some helpful scripts.
* Mini-sessions: If you remove 'tabpages' from 'sessionoptions', :mksession will only pay attention to the current tab page. This makes it easy to use tab pages as a lightweight project-organization tool -- just open a set of files in whatever window layout you like, then run `:mksession some-random-project.vim` to save the current tab page as a session. To work on that set of files again, open a new tab page and :source the appropriate session file.
 
::This one would probably be best as a section in a "using sessions" tip, if we have one.
 
* [[Capture ex command output]] without messing up your window layout.
 
   
 
==Comments==
 
==Comments==
  +
{{todo}}
 
* Link to related tips, e.g. a "sessions" tip, if we have one.
  +
::[[VimTip238]] has a list of what we have (possibly not very helpful). [[User:JohnBeckett|JohnBeckett]] 04:02, March 20, 2010 (UTC)

Latest revision as of 06:41, 13 July 2012

Tip 1630 Printable Monobook Previous Next

created 2009 · complexity basic · author Fritzophrenic · version 7.0


Vim provides tab pages as one way to organize your workspace, alongside (but not in place of) a buffer list and splittable windows. For basic tab use, see our Using tab pages. Once you know how to navigate Vim's tab pages, you may be confused as to why they act so differently from the tabs in other editors. Here are some quick ideas, no scripts required, that can get you started using tab pages to their full potential.

Full-page reference documents[]

By simply prefacing a :help command with the :tab modifier, you can have multiple full-page Vim help topics, each in its own conveniently-labeled tab page, without messing up your window layout. For example, instead of entering :help toc, type :tab help toc. If you have already opened a document in a window and would like it in a tab, press Ctrl-W then T.

You don't need to limit this to Vim's help. You can open other reference material (API docs, framework source code, project notes, etc.), each in a new tab for quick and easy access. This is useful when you need to refer to a document, but you do not want it taking up screen real estate next to the text you are currently working on.

Quick changes outside normal workflow[]

Tabs are great places to work on small tasks that fall outside your normal workflow. Open a new tab page for vimrc changes or for scratchpads to hold external command output, chunks of text, etc., and you can very quickly switch back to what you were working on before, without messing up your window layout or anything else. Just close the tab, and you're automatically back to whatever you were working on.

Alternate views of a file[]

If you are working in a buffer with most folds open, you can keep a second tab page showing the buffer with all folds closed for an "at a glance summary". The folded version serves as an overview of the entire file, often visible on a single screen. You can take advantage of the "at a glance" view to rapidly find and mark a place in a file (typing ma creates a mark that applies to the buffer), then jump to the mark ('a) in a different tab showing the same buffer.

You can also go the other way. Instead of keeping a tab page for a "summarized" view, you could open a tab page for a detailed "x-ray" view of a buffer. In a separate tab page, you can enable useful but visually noisy options like 'list', 'cursorcolumn', 'cursorline', and 'number', and you may have several :match patterns active, or similar. This way, you can keep your regular working view clean and uncluttered, but quickly flip over to the information-overloaded version when needed. This idea works for the settings in :help option-summary that are labeled 'local to window'.

Separate 'diff' views[]

Along the same lines as the "x-ray" tab above, you can take advantage of the fact that a 'diff' view of two or more buffers is local to the tab page. Knowing this, you can open up a new tab on the current buffer with :tab split, and then in the new tab use the :DiffOrig command to create a tab containing a diff of the current state of the buffer with the original state on disk. Now, you can go back to your original tab, make and even save changes, and your 'diff' tab will remain open for you to quickly see changes you have made since you started.

You can go further than this. Using tabs, you are not limited to a single diff. If you have two portions of a single file that are very similar, and you want to see exactly how similar, you can open a new tab with two new buffers, then copy and paste the two sections of the original buffer which you want to compare, one in each window. Then run :windo diffthis in the new tab and you have a quick comparison of just parts of the buffer.

You don't even need to close any other diffs you may have in progress to do this. Using tab pages, you can have multiple diff tabs open at once, alongside tabs for normal editing.

Redirected command output[]

Sometimes, you may want to use the output from an ex command, instead of just viewing it in the status area. Using the :redir command, you can run a command, capture the output, then open a new tab page to paste the captured text. This could also be done with a new window, or even within the current window, but using a new tab page can be less disruptive, as it will not mess up your current window layout or buffer contents. If you do this often enough, you could create a simple command or mapping.

You can do the same sort of thing with external commands. The method is even easier. Just open a new tab with :tabnew, then use the :r! command to read in the external command. If you find yourself doing this a lot, you can use a script.

Mini-sessions[]

If you remove 'tabpages' from 'sessionoptions', then :mksession will only pay attention to the current tab page. This makes it easy to use tab pages as a lightweight project-organization tool–just open a set of files in whatever window layout you like, then run :mksession some-project.vim to save the current tab page as a session. To work on that set of files again, open a new tab page and :source the appropriate session file.

Repeat a command on a selection of buffers[]

It is easy to run a command on all files you have loaded (though not necessarily visible) in Vim, using :bufdo. It is possible, using :argdo to run a command on a selection of files. But, the argument list can be tedious to work with, and additionally it is limited to real files; you can't use it on buffers that don't yet or will never have an associated file. You can use a new tab to open a window on every buffer you wish to run a command on, then use :windo to run the command on every buffer in the tab. If you take care and pay attention to your buffer list instead of relying exclusively on tabs for your organization, you can even do something like:

:tab sp
:sball

to open a tab with a window for ALL currently loaded buffers, and then close any buffers you wish to exclude from your command. Then proceed with the :windo and close the tab to return to what you were doing before.

This method can be especially nice when working with the quickfix list, because you can use the 'switchbuf' option to automatically split a window for a result, and the :cnfile command repeatedly (potentially by using @: with a count) to open every file selected by your :make, :grep, or other command.

More ideas[]

There are plenty more creative things you can do with Vim's tab pages. If you don't try to limit yourself to one-file-per-tab and one-tab-per-file, you will only be limited by our own imagination. Take a look at the other tips in the Tabs category for more ideas and some helpful scripts.

Comments[]

 TO DO 

  • Link to related tips, e.g. a "sessions" tip, if we have one.
VimTip238 has a list of what we have (possibly not very helpful). JohnBeckett 04:02, March 20, 2010 (UTC)