88

I close a tab in vim and immediately realize I need to re-open it again for something. Is there a way to undo close tab in Vim 7.2?

Tim Matthews
  • 5,031
  • 8
  • 38
  • 45
Amjith
  • 22,626
  • 14
  • 43
  • 38
  • Can this be moved to vi.stackexchange.com? – Sukima Jul 16 '15 at 21:01
  • Hi, [this bit of vimscript and the windowlayout plugin](https://www.reddit.com/r/vim/comments/3ke941/undo_close_tab/cux8gh2) does the job: it reopens the tab you just closed and brings back the window layout you had. – Yann Thomas-Gérard Sep 11 '15 at 08:34

6 Answers6

156

Your file is probably still open in a buffer:

:ls " get the buffer number
:tabnew +Nbuf " where N is the buffer number

To reopen buffer 18, for example:

:tabnew +18buf
Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
greyfade
  • 24,948
  • 7
  • 64
  • 80
  • 5
    `:tabnew N` didn't work for me. What I do is `:tabnew` and then `:bN` where N is the buffer number – alf Jul 18 '12 at 21:44
  • @alfonso: The command is `:tabnew +Nbuf` where N is the buffer number. It has to be prepended by a + and suffixed with the word "buf". – greyfade Jul 18 '12 at 23:37
  • 5
    An example of what @greyfade meant: `tabnew +18buf` – Eric Hu Sep 05 '12 at 00:39
  • 3
    you could even do :vsp +[bufferNumber]buf helped me reopen closed split in my vim – 3emad Oct 16 '12 at 18:08
  • 4
    The reason this doesn't work is because the `+` is a line reference NOT a buffer reference use `#` not `+` as in `:tabe #5` to open buffer 5 in a new tab. – Sukima Jul 16 '15 at 21:01
  • `:tabnew` enter `:b 18` (18 - number from `:ls`) – itsnikolay Mar 08 '17 at 15:35
41
:tabnew#

Reopens recently closed file in new tab


Edit: Please use greyfade's answer. I don't like my answer, but I'm keeping it here for references and useful comment info.

konyak
  • 10,818
  • 4
  • 59
  • 65
  • Weird, seems like it worked at first now it is opening unrelated buffers. –  Jan 26 '15 at 03:59
  • `#` is the last edited file in the current window. Closing a tab does not register the file as the alternate (`#`). You have to look it up by buffer number (`:ls`) – Sukima Jul 16 '15 at 20:59
  • It works more than perfectly if you're split-oriented. – Al.G. May 18 '16 at 18:54
  • error prompted: "E194: No alternate file name to substitute for '#' " – Scott Yang Aug 14 '19 at 08:30
12

I'm using an MRU (most recently used files) plugin. So I can edit the last 30 files I've just edited

Here are the MRU plugin metadata:

File: mru.vim
Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com)
Version: 3.2   Last Modified:
September 22, 2008

Usage

To list and edit files from the MRU list, you can use the ":MRU" command. The ":MRU" command displays the MRU file list in a temporary Vim window. If the MRU window is already opened, then the MRU list displayed in the window is refreshed.

icc97
  • 11,395
  • 8
  • 76
  • 90
Oli
  • 15,345
  • 8
  • 30
  • 36
6

Simple answer is no, there is nothing built-in.

But a workable solution would be to use a plug-in like the excellent BufExplorer. Since it defaults to listing the most recently used buffers first, reopening a closed tab would be as simple as pressing \bet

Ben Hoffstein
  • 102,129
  • 8
  • 104
  • 120
1

Use the plug-in Ben Suggested: BufExplorer Github Mirror

In his answer one would have to press <Leader>be<Down>t. Adding a bit shortcut:

map <silent><leader>t <leader>be<Down>t

So that simply <leader>t would do the work.

Lucia
  • 13,033
  • 6
  • 44
  • 50
1

If there were a BufferClose event this would be easy... but it seems that it is not possible since it is not possible for window creation.

But in the case of tabs we can detect if a tab was closed by keeping a tab count and counting the difference between TabLeave and TabEnter.

Usage: <leader>tr reopens the last closed tab on a new tab (supposing the tab had only a single buffer):

let g:reopenbuf = expand('%:p')
function! ReopenLastTabLeave()
  let g:lastbuf = expand('%:p')
  let g:lasttabcount = tabpagenr('$')
endfunction
function! ReopenLastTabEnter()
  if tabpagenr('$') < g:lasttabcount
    let g:reopenbuf = g:lastbuf
  endif
endfunction
function! ReopenLastTab()
  tabnew
  execute 'buffer' . g:reopenbuf
endfunction
augroup ReopenLastTab
  autocmd!
  autocmd TabLeave * call ReopenLastTabLeave()
  autocmd TabEnter * call ReopenLastTabEnter()
augroup END
" Tab Restore
nnoremap <leader>tr :call ReopenLastTab()<CR>
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985