vim open file in a new tab
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.
Related videos on Youtube
The Vivandiere
Updated on September 18, 2022Comments
-
The Vivandiere over 1 year
I would like to do this -
Open a file, say
a.txt
in vim. Then, doctrl+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 openb.txt
in a new tab, right next toa.txt
. Then, I could dofg
to go back into vim, and have botha.txt
andb.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 over 5 yearsthis is how to use different buffers, not different tabs.
-
icedwater over 4 yearsMay I know why you recommend buffers rather than tabs, please?
-
Nick Bull about 4 years@icedwater stackoverflow.com/a/26710166/2483271
-
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 about 4 yearsAs 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/…