Opening the most recently hidden buffer in Vim - vim

Assuming that I have two buffers. One is NERDTree, the other is an actually file I'm editing. I accidentally open an another file from NERDTree and the new file hides the previous file I was editing.
A similar case occurs whether I use NERDTree or not.
How can I bring back the previous state by opening the mostly recently hidden file? I'm using the word "hide" because this is probably not "closing".

I use ctrl-6 to switch back to the previous buffer in the window.

Related

How to stop vim from splitting horizontally when opening a new file?

Usually when I open a file in Vim when there's already one open Vim opens the file in a whole new... buffer? Screen thing? In other words, it takes up the full vim client area so that I have to :bn through to see the next file, which I prefer. But sometimes when I open a file Vim decides to split the window horizontally and put it in one of those horizontal buffer things.
When does it decide to split the window as opposed to giving me the whole thing? How can I prevent that behavior? And perhaps most important, what is the name for that kind of behavior so I can do better searches in the future?
Quoting from :help window:
A buffer is an in-memory text of a file
A window is a viewport of a buffer
Buffers are enumerated. When you run :bufnext or :bn, you go to the next buffer; you can also use :b<num> to go to a specific buffer, say :b2 to go to buffer 2. You can get the list of buffers with :ls.
When you :split or :vsplit, you open a new window, i.e. a new viewport for a buffer. You can have two windows show the same buffer, and so on.
Also, a tabpage is a collection of buffers; open a new tab with :tabnew and cycle through them with gt and gT to go forward and backward respectively.
I highly recommend you read the helpfile; it's all there.

Find file in vim, switch to next file without saving current file

Vim newbie here.
I use :find command to open a file and make some changes.
Suppose I want to open another file without saving the current changes, then how should I do it.
What is the best way to list all open files and quickly open them again for editing.
Thanks in advance.
:e! filename
will allow you to open a named file in a new buffer (note the exclamation mark overrides the warning re. an unsaved file).
CTRL-W + cursor
will move between visible buffers and does not enforce the saving of your file as you navigate.
:buffers
will list your open buffers and allow you to navigate to each one (albeit with some cryptic flags indicating which buffers are visible/hidden/amended etc.)
:help buffers
will tell you about all the buffer-related features.
:help :wincmd
will tell you all about windows navigations
See also the VIM buffer FAQ

In Vim, how can I undo opening?

I started using NERDTree recently.
When I opened a file while having split window, the window of a new file was not I intended.
Consequently, I had lost my unsaved buffer by opening a file.
Is there any way to undo opening files or reopen my unsaved buffer?
You can go back to the previous file in a buffer using Ctrl6.
You haven't lost anything (yet), the buffer was hidden. Since it is the alternate buffer of that window, you can just switch back via <C-^>. You'll also find it in the buffer list of :ls (with a h prefix for hidden), from which you can re-edit it via its buffer number, e.g. :buffer 42 or :sbuf 42.
If there are unsaved changes, Vim will prompt you on :quit (unless you provide a ! to it).

make nerdtree never open a third window

When using NERDTree, I often have the situation where I close all existing buffers (except the NERDTree buffer), and then start opening fils from NERDTree again.
When doing this, NERDTree opens the files in a new split window, i.e. I have NERDTree at the left side, and on the right side two windows on top of each other, an emtpy buffer and the file I wanted to open.
What I want to happen is that NERDTree opens the file in the right window without splitting it.
Is there a way to tell NERDTree to never open a new window, if there is already one it could use? I tried using the "o" command, but that works exactly like the Enter command.
Have you checked all the mappings from the documentation
o.......Open files, directories and bookmarks....................|NERDTree-o|
go......Open selected file, but leave cursor in the NERDTree.....|NERDTree-go|
t.......Open selected node/bookmark in a new tab.................|NERDTree-t|
T.......Same as 't' but keep the focus on the current tab........|NERDTree-T|
i.......Open selected file in a split window.....................|NERDTree-i|
gi......Same as i, but leave the cursor on the NERDTree..........|NERDTree-gi|
s.......Open selected file in a new vsplit.......................|NERDTree-s|
gs......Same as s, but leave the cursor on the NERDTree..........|NERDTree-gs|
O.......Recursively open the selected directory..................|NERDTree-O|
x.......Close the current nodes parent...........................|NERDTree-x|
X.......Recursively close all children of the current node.......|NERDTree-X|
e.......Edit the current dif.....................................|NERDTree-e|
I finally got around to look at the nerdtree code, and I found the fix for my problem: I had to change the plugin.
I created a pull request for my changes on github:
https://github.com/scrooloose/nerdtree/pull/102
Update: Actually, the problem was a script I was using that set buftype=nofile for an empty buffer (it should set buftype=). After changing that, my problem went away.

How to make Vim's file browser open a file in a new buffer?

Sometimes I start Vim by pointing it at a directory, but I'm not certain yet which file I'll need to change, so I end up looking through several files.
Is there a way to make the file browser open a selected file into a new buffer so that the file browser is still available without having to reopen it with :e path/to/directory ?
I'm not sure if you can have it open in a new buffer, but you can have it open in a new split using o or v for a horizontal or vertical split respectively.
You could also preview the file using p which runs :pedit <fname> where <fname> is the file under the cursor. This opens a new split window but doesn't change the cursor focus or position. You can close this window with :pclose or simply :pc.
See :help netrw-browse-maps for more information.
This is not strictly speaking what you are asking for, but I think it's equivalent:
If you open a file from the file browser you can use CTRL-^ (and/or CTRL-6 ?) to return to the browser. This results in the file open in one buffer and the file browser open at the directory you started in.
Hope it helps...

Resources