Vim close buffer with nerdtree - vim

My current use case is:
I have a buffer open with a NERDTree (Left) and 1 with a file being edited (Right)
Lets also assume 2 other files in background buffers are open as well.
If i press c (shortcut for closing buffer, basically :bd), the Right side is gone and only the left side is displayed. But when closing a buffer, i would expect that vim will close the current file buffer and keep the right side to open one of the buffers that are in the background.
How do I achieve the expected behavior?

You can use
nnoremap c :bp\|bd #<CR>
Which will first go to the previous buffer, then delete the last buffer. This way it will both close the buffer and go to the previous one.

May be bufkill can solve this issue for you.

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.

Keep NERDTree on right side when closing buffer

I have NERDTree configured to open on the right side with
let g:NERDTreeWinPos = "right"
This works when I open a new buffer. However, when I close a buffer on the left side with :bd, NERDTree takes the whole window and loses it's split. It looks like this.
Is there a way to keep NERDTree at the right side?
And secondly, can I force that new buffers are always opened at the left side with Ctrl-P? Sometimes it pretty annoying when the focus is inside NERDTree on the left side, I press Ctrl-P, and the file is displayed within the small buffer in which NERDTree was displayed.
As a Nerdtree user myself, I'd say, that this is normal behaviour. If you close the last open buffer, the remaining buffer (in this case Nerdtree) will take the full screen.
I imagine, this is annoying and the remedy is usually something like this:
map <F2> :NERDTreeToggle<CR> to toggle Nerdtree only when needed.
The other "trick" is to close it automatically, when opening a file via
let NERDTreeQuitOnOpen = 1.

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.

VIM: How can i open a file at right side as vsplit from a left side NerdTree panel?

I installed NerdTree plugin.
How can i open a file at right side as vsplit from a left side NerdTree panel ?
To make vsplit put the new buffer on the right of the current buffer:
set splitright
Similarly, to make split put the new buffer below the current buffer:
set splitbelow
I haven't tried this with NerdTree, however.
There's a s command, but it opens a file split to the left of current buffer. Though you can press Ctrl+W r to swap windows then.
This is a bit of a hack, but how I do it is this:
Put cursor in window I want to open file into
Hit <leader>n<leader>n (this closes NERDtree and then opens it again with the cursor in NERDtree)
Select the file
On my system this opens it on the last window I was just on if the file isn't already open on my screen.
Start in the window you want to open your file into.
Hit <leader>n<leader>n to close and reopen NERDTree
Select the file you want!
A quick C-w = will get your windows back to proper proportions.
Not sure if anyone else is still struggling with this, but here's how I dealt with it.
You can use
:ls
to list the available buffers. Which would look something like:
1 "foo.txt"
2 "blame_the_user.java"
:b1 to select foo.txt
:b2 for blame_the_user.java
This method can be done from any window setup using :sp or :vs.
Open the buffers you want to split first.

Resources