4

Often when coding I have this window layout - which is mapped to the flow of the program I am editing.

Tab1

+------------+--------------+
|            |              |
|            |      B       |
|            |              |
|            |              |
|  A         +--------------+
|            |              |
|            |              |
|            |       C      |
|            |              |
+------------+--------------+

Now I want to open B in a new tab to work on it, but I want to leave the layout of tab1 intact.
Here is my desired outcome.

        tab1                         tab2
+------------+--------------+ +---------------------------+ 
|            |              | |                           | 
|            |      B       | |                           | 
|            |              | |                           | 
|            |              | |            B              | 
|  A         +--------------+ |                           | 
|            |              | |                           | 
|            |              | |                           | 
|            |       C      | |                           | 
|            |              | |                           | 
+------------+--------------+ +---------------------------+ 

What I have tried using vim

  • From inside B, open it in a new tab with <C-w>T.
    The problem is that destroys the B window in tab1.
  • tabe <full path to filename> - this works, but I am trying to find a "shortcut"

What I have tried using ctrlp

  • <C-p>, type in few characters of filename, <C-t>

Unfortunately ctrlp excludes the current buffer from its results, I know I can move to another window, buy I always forget this when I am coding, so I just assume ctrlp cant find the file and resort to some other way to open the file.
Is there a shortcut to open a file in a new tab, leaving the current one intact or to enable ctrlp to include the current buffer in its results?

8

:tab split will create a new tab displaying the current buffer, since :tab modifies any command that would normally create a split to instead create a tab page.

If you want to override <C-w>T to do that instead of its default behavior, you can remap it.

nnoremap <silent> <C-w>T :tab split<CR>

You may be wondering why I used :tab split instead of :tabedit %. According to the documentation, :tabedit {file} is just like creating a new tab and then running :edit {file}.

Therefore, it would be a bad idea to use since :edit {file} on a modified buffer would try to reload the buffer. :tab split better fits the behavior you're describing of just opening a new view of the existing buffer.

All that being said, the implementation of :tabedit {file} apparently doesn't act just like :edit {file} since it doesn't actually reload the buffer.

  • Ah very cool, I just tested now, yes so - if you wanted to open the current buffer in a new tab using :tabe %, but the buffer had unsaved changes - vim blocks the action and throws E:37 no write change - add ! to override – the_velour_fog Mar 3 '16 at 1:13
6

After some research I found a solution.
If you are focused on the buffer/window:

:tabe %

This opens the current buffer in new tab and leaves the current window intact.
why does it leave the old buffer intact when <C-w>T destroys it? I guess its because tabe generally creates a new tab, whereas <C-w>T seems to create a new tab and move the current window and buffer to it.

2

Another way, just for the fun of it…

:tabnew|b#
  • create a new tab page, containing a new window, containing a new empty buffer.
  • then switch to the "alternate file".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.