vim open file in a new tab

70,517

Solution 1

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>

Solution 2

Just open the two files at the same time :

vim a.txt b.txt

Then use :b# to switch between the tabs.

Solution 3

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.

Share:
70,517

Related videos on Youtube

The Vivandiere
Author by

The Vivandiere

Updated on September 18, 2022

Comments

  • The Vivandiere
    The Vivandiere over 1 year

    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.

  • sjas
    sjas over 5 years
    this is how to use different buffers, not different tabs.
  • icedwater
    icedwater over 4 years
    May I know why you recommend buffers rather than tabs, please?
  • Nick Bull
    Nick Bull about 4 years
  • icedwater
    icedwater about 4 years
    @NickBull thanks. I prefer split panes myself, I was just exploring tabs at this point. Some kind of justification either way is always helpful.
  • Nick Bull
    Nick Bull about 4 years
    As long as it works (and doesn't cause an issue)! You can split buffers too if you ever change your mind vi.stackexchange.com/questions/76/…