Undo Close Tab in Vim

VimTabs

Vim Problem Overview


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?

Vim Solutions


Solution 1 - Vim

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

Solution 2 - Vim

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

Solution 3 - Vim

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.

Solution 4 - Vim

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

Solution 5 - Vim

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.

Solution 6 - Vim

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmjithView Question on Stackoverflow
Solution 1 - VimgreyfadeView Answer on Stackoverflow
Solution 2 - VimkonyakView Answer on Stackoverflow
Solution 3 - VimOliView Answer on Stackoverflow
Solution 4 - VimBen HoffsteinView Answer on Stackoverflow
Solution 5 - VimLuciaView Answer on Stackoverflow
Solution 6 - VimCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow