Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've gotten used to Ctrl-Shift-T in Firefox to undo the closing of a tab, is it possible to get similar functionality in Vim? Googling didn't point me towards anything useful for this.

share|improve this question
up vote 1 down vote accepted

I'm not aware of an existing solution but it seems relatively easy to save the name of the deleted buffer in a variable on BufDelete and define a simple mapping that uses that variable. Let's see...

autocmd BufDelete * let g:latest_deleted_buffer = expand("<afile>:p")

nnoremap <F6> :e <C-R>=fnameescape(g:latest_deleted_buffer)<CR><CR>
share|improve this answer
    
Thanks, I didn't know about the BufDelete event (and my vimscript-fu is weak anyway). Is it possible to make this into a : command instead of a normal mode map? – sundar Aug 31 '13 at 22:31
    
This works perfectly in Cygwin, but in Windows gVim, after adding the given autocmd I'm getting an error E18: Unexpected characters in :let whenever I do a :bd. I'm guessing it's the backslash characters in Windows paths. Could you please suggest a way around that? – sundar Aug 31 '13 at 22:39
    
Ok, I figured out the second issue, changing the expand command to expand("<afile>:p:gs?\\?/?") fixes that problem. This undo works perfectly now, thanks a lot! – sundar Aug 31 '13 at 23:17
    
Sorry, I was AFK for the night. My solution assumed UNIX because you didn't say much about your environment. I'm happy this simple solution works for you. – romainl Sep 1 '13 at 6:13
1  
Dashes are not allowed in variable names, that causes the E18. Also, you should probably expand to a full path <afile>:p to handle changes of CWD, and use fnameescape() in the mapping. – Ingo Karkat Sep 3 '13 at 15:27

Just to clarify, :bdelete in VIM is used to delete buffers (buffer delete). I am not aware of any way of 'undoing' it, however, maybe the following will help you:

I have a file on my desktop called test.rtf

If I use the following command in the terminal: vim test.rtf followed by: :bdelete + Enter - it opens up a blank screen ready to type into. If you type in: :q followed by Enter to quit, you aren't saving any changes, so you are effectively undoing the mistake.

I hope this may be somewhat helpful.

share|improve this answer
    
It seems to me that you're assuming :bdelete empties the contents of the currently-open file or something, but it actually simply closes the file, so I'm not looking for ways to undo any changes made (and :q wouldn't do that anyway, we'd need either :q! or better :e!). What I'm looking for is a simple shortcut to avoid the bother of having to type :e followed by the whole path of the just-closed file when I wish to reopen it immediately. – sundar Aug 31 '13 at 21:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .