26

Can I fix a buffer so that the only way to remove it from vim window is closing it?

For instance, I'm using the NERDtree plugin, which displays the filesystem in a vim window. Sometimes I forget to change focus to other window before using the quickfix commands and the erroneus file ends up replacing the file tree. (Not to mention that NERDtree's window default width is only 31)

Edit:

What I'm trying to achieve with this question is to simulate Eclipse's notion of views and editors inside vim. In this terms, NERDTree (and other plugins destined to exclusively display information) would be a view while the other windows would be editors.

Pascalius
  • 14,024
  • 4
  • 40
  • 38
freitass
  • 6,542
  • 5
  • 40
  • 44
  • Don't know, but `:b#` will normally jump a window back to the previous file so it might successfully reopen the NERDtree plugin in that window. – El Yobo May 20 '11 at 01:58
  • An easy way to fix NerdTree when you do that is just to close the NerdTree buffer (`ctrl + wq`) and reopen it with `:NERDTree` command. – GWW May 20 '11 at 01:58
  • 4
    I have assigned `` to toggle NERDTree (`nnoremap :NERDTreeToggle`) so when this happens I type `` and NERDTree opens in a new window. Also, El Yobo's suggestion might work too, but I'd rather prefer closing the buffer and reopen NERDTree with `` – freitass May 20 '11 at 13:11
  • Another solution is to always open things in the current window. What I usually do is - create a new window (a duplicate of the current one), - move where I want to open the new file - open the NERDTree (if needed, ie never) - open new file in this new window. – mb14 May 24 '11 at 11:57
  • Thanks for your comment. Please, take a look at the edit above. – freitass May 24 '11 at 12:28
  • Buffers are global and there is literally nothing you can do about it with existing options, modifiers, commands, etc. Any buffer can be displayed in any window, plain and simple. If you don't like how it works and don't want to adapt, then you will have to write your own abstraction *above* the existing model. Good luck with that. – romainl Apr 13 '21 at 17:58

2 Answers2

2

This isn't exactly an answer, but if you screw up, Ctrl-^ undoes a change in a buffer's content.

  • 1
    Actually `Ctrl+6`, right? I've learned that command last week and it saves me a lot of time, but it isn't enough. =/ – freitass Jun 06 '11 at 16:11
2

I've written an autocommand that does this. Adding the following to vimrc will prevent NERDtree buffers from being overwritten:

autocmd BufEnter * if bufname("#") =~ "NERD_tree" && bufname("%") !~ "NERD_tree" | b# | endif

The autocommand runs every time a new buffer is opened -- if it detects that the previous buffer was NERDtree and the current buffer is not NERDtree, then it will go back to the NERDtree buffer, essentially 'locking' NERDtree in place.

eeze
  • 608
  • 1
  • 6
  • 23
  • sweet, works nice. For completeness here's how to do it with NvimTree: `autocmd BufEnter * if bufname("#") =~ "NvimTree" && bufname("%") !~ "NvimTree" | b# | endif` – Pascalius Apr 20 '21 at 14:04