vim open file in a new tab

I would like to do this -

Open a file, say a.txt in vim. Then, do ctrl+z, which will take me back to the terminal, and hide vim in background. While I am in the terminal, now I would like to open b.txt in a new tab, right next to a.txt. Then, I could do fg to go back into vim, and have both a.txt and b.txt opened for me.

Any ideas how this can be done? When I open b.txt from the terminal, it launches vim in its own window.


When inside vim, I use

:tabnew filename

to open a file in a new tab.

From the terminal, you can do vim -p filename1 filename2 to open the two files in tabs.

I have added the following lines to my .vimrc that allow me to switch between tabs easily.

nnoremap <C-Left> :tabprevious<CR>                                                                            
nnoremap <C-Right> :tabnext<CR>
nnoremap <C-j> :tabprevious<CR>                                                                            
nnoremap <C-k> :tabnext<CR>

C stands for the Ctrl key. Thus, I can do Ctrl-Right or Ctrl-k to switch to the next tab, and likewise for the previous.

This works for me.

For those using tmux - I have mapped Ctrl-h and Ctrl-l for switching windows in tmux. Thus, using the Ctrl key, and h,j,k,l, I am able to switch between all of tmux windows and vim tabs.

EDIT : I did not know this when I asked this question, but you really should avoid tabs to simply manage switching between multiple open files. Use buffers instead. Today, I have

nnoremap <C-j> :bprev<CR>                                                                            
nnoremap <C-k> :bnext<CR>

Just open the two files at the same time :

vim a.txt b.txt

Then use :b# to switch between the tabs.


First, the command for suspending Vim is <C-z>, not <C-x>.

It doesn't work everywhere but it could be possible to do something like this to achieve your goal, if your Vim is compiled with the clientserver feature:

$ vim --remote-tab b.txt

See :help clientserver.

But…

  • You don't need to suspend Vim to open another file to edit. You can do that from Vim itself very easily:

    :e filename
    
  • Tab pages are not at all equivalent to other editors' tabs. Use buffers instead.