Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open last closed file in a new tab in Vim

Tags:

linux

vim

vi

macvim

People also ask

How do I open a vim file in a new window?

To open a new VIM window next to the existing one, press <Ctrl>+<w> then press <v>. You can move to the left window again by pressing <Crtl>+<w> and then pressing <h>. To open a new VIM window on the bottom of the currently selected window, press <Ctrl>+<w> then press <s>.

How do I use tabs in Vim?

To directly move to first tab or last tab, you can enter the following in command mode: :tabfirst or :tablast for first or last tab respectively. To move back and forth : :tabn for next tab and :tabp for previous tab. You can list all the open tabs using : :tabs. To open multiple files in tabs: $ vim -p source.

How do I open multiple tabs in Vim?

With vim, you can use tabs also, just like you would in gvim or any other GUI editor. This will open three tabs, each containing the specified file. The second way to open files is to create new tabs within vim and open files using :tabnew file.


# is simply an Ex special character that will be replaced with the name of the alternate file. Do an :ls, and the alternate file will be marked with a # there also.

# can similarly be used with :tabnew and split. In the examples below I'll use :tabe in place of :tabnew as :tabe is a shorter alias for :tabnew (search for either in the help docs):

  • To open the alternate file in a new tab: :tabe#
  • To open the file in a new split: :split#; this can be abbreviated to :sp#, and :vsp# for a vertical split.

Using a buffer number from :ls, e.g. buffer number 5 you can also:

  • open the buffer in a split with :sp#5; alternately :sb5 if the switchbuf option contains the newtab specifier - see :help switchbuf
  • open the buffer in a vertical split with :vsp #5 (there is no :vsb)
  • open the buffer in a new tab with :tabe #5

You don't necessarily have to leave normal mode to open the alternate buffer in a new window:

CTRL-W ^ opens the alternate buffer in a horizontal split.

CTRL-W T opens the current buffer in a new tab (Shift-T, that is).

So, one solution to your title question is the following combo.

CTRL-W ^, CTRL-W T: opens the alternate buffer in a new tab.

Note that for the caret "^" in the first command you don't have to release the Control key and you don't have to press Shift, just hold down CTRL then strike W and 6 (where the caret is located on many English keyboard layouts).