Open last closed file in a new tab in Vim

# 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

Leave a Comment