59

I know I can open the last closed (:q) file in Vim by using :e#. This will open the file in the current tab. How would I perform the same task but open the file in a new Vim tab. Also I am interested in how to open the file in a new split instead of the current tab.

ib.
  • 27,830
  • 11
  • 80
  • 100
Matthew Kirkley
  • 4,138
  • 5
  • 31
  • 33

2 Answers2

76

# 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
pb2q
  • 58,613
  • 19
  • 146
  • 147
  • If I get the buffer number from :ls of the file I want to load, what would be the tabnew format to load that file in a tab? – Matthew Kirkley Jul 27 '12 at 18:35
  • 1
    The `#` Ex special character is *not* a register! It is one of special characters which is subject to expansion at places where a file name can be used in Ex commands. See also `:help cmdline-special`. – ib. Jul 28 '12 at 02:13
  • By the way, why do use `` to format Ex command lines instead of `\``? It is better to use `` for typesetting separate keys and shortcuts. – ib. Jul 28 '12 at 02:16
  • The `:sb1` command will open a buffer in a new tab page, if the `switchbuf` option contains the `newtab` specifier. See `:help switchbuf`. – ib. Jul 28 '12 at 02:20
  • @ib thanks for the info and suggestion. I've updated the answer and added info about `#N`. You'll notice from the edit history that I initially had the ex commands in backticks `; I'm still developing formatting habits here, particularly for vim questions, and your point re: kbd is a good one. – pb2q Jul 28 '12 at 06:09
  • `:tabe` can also be used, which is shorter than `:tabnew` – jaques-sam Apr 17 '19 at 13:35
  • Thanks @DrumM I edited the answer to shorten the commands and reference the equivalence of `:tabe` and `:tabnew` – pb2q Apr 17 '19 at 18:57
6

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).

glts
  • 21,808
  • 12
  • 73
  • 94