How do I edit an existing buffer in a new tab in vim?

Suppose I have started vim like this:

vim foo bar

Now I decide that I want each of those files in its own tab. Is there a way to do that without exiting vim and adding the -p option to my command line?


Solution 1:

You wish to open a buffer in a new tab ?

Split up the screen (Ctrl-W s), take up a window, and Ctrl-W T

Solution 2:

When you start vim like that, you don't get a vim client, the text editor is using the terminal or cmd prompt - the two files are in two different buffers. Use :ls to list the buffers:

:ls
  1 %a   "foo"                 line 6
  2      "bar"             line 0

The %a is the active buffer. You can use :b2 to switch to buffer 2 or use :bn to cycle to the next or :bp for previous. I prefer (CTRL-W v) to split windows vertically, rather than (CTRL-W s), which splits horizontally.

If you have 2 files loaded & no tabs (yet), you can, :tabnew and in the new tab type :b2

If you want to always have buffers loaded into their own tabs, check out this article.

Solution 3:

You can accomplish this by combining the tab command with the sb[uffer] command.

First you'll need to know the buffer id of the buffer you wish to open in a new tab. You can find this out with the ls command:

:ls
  1 %a   "foo"                          line 1
  2      "bar"                          line 0

Once you have the id, you can easily open it in a new tab using:

:tab sb 2

The sb command normally opens the given buffer in a new split window, but the tab command causes it to open in a new tab, instead.

The tab command also allows you to specify where in the tab list the new tab should be created. For example, :0tab sb 2 would result in the new ‘bar’ tab appearing at the beginning of the list instead of after the current tab.