Vim and NERDTree - Clear [No name] from buffers - vim

I am new to Vim and NERDTree, and I am trying to understand why a buffer appears as [No name] after I delete it using :bd. I want to keep my NERDTree clean so I wonder if there is a way to remove the [No name].
For example,
The l.py file is the one I am working on, and the [No name] ones are the files that I already closed (with :bd). I don't want them to show up at all because it looks messy.
Thanks!

I doubt NerdTree is putting buffers into your status line. I imagine some status line plugin like vim-airline. It would probably be best to look at your status line plugin's documentation for how to make customizations or submit an issue on the plugin's bug tracker.
We need to talk...
The biggest problem I see is that you are trying to use the status line as a makeshift "tab bar". Most other editors use a tab bar as a way to manage documents, but not Vim. This makeshift "tab bar" is probably a bad idea once you start using more files or more complicated workflows with splits or tabs (Vim's tabs are different).
Oh yeah? What about Buftabline, Airline, BufTabs, MiniBufExpl, ...?
All these plugins do is show you your currently listed buffers. Maybe with some kind of positioning information so that you feel comfortable cycling via :bnext and :bprevious (or whatever mappings you might be using) through your buffer list.
Now that is great and all, these plugins have recreated other editor's version of tabs, but Vim already let you cycle through buffers without these plugins. The only thing missing was a menu which :ls will gladly do for you without wasting any screen real-estate.
Imagine having 10, 25, 50, or 100+ buffers open. How is your Buftabline going to handle that? My bet is not well. You need to stop reaching for simple tools like :bnext / :bprev and start reaching for a power tool like :b.
Behold the power of :b
The :b command can take a buffer number to switch directly to a buffer. Far more interesting, :b can take a partial filename.
:b partial-name
Need more power in your :b?
Uses tab completion
Uses <c-d> to list out completions
Accepts globs. e.g. :b *foo
Use ** to descend into directories. e.g. :b foo/**/bar
Don't forget to add set hidden to your vimrc. See :h 'hidden'
Why ride a bike when you can fly?
taken from Bairui's collection of Vim infographics.
You can use a simple mapping to leverage both :ls and :b:
nnoremap <leader>b :ls<cr>:b<space>
Now you can travel directly to you buffer you want. No more cycling.
But I like plugins
Who doesn't like a good plugin. As a matter a fact if you are looking for a nice buffer switching plugin then I would recommend you look into a nice fuzzy finder like CtrlP to aid you in switching between buffers. A fuzzy finder actually adds value to switching between buffers by getting you there faster with less typing.
Conclusion
Eventually you workflow will require you to use more than 3 buffers at a time. At that moment I would suggest you take another look at the :b command or at the very least get a nice fuzzy finder. It will save you time and effort.
So stop riding your bike when you can fly.

Related

Vim NERDTree. How to prohibit duplicate files in tabs?

I use NERDTree with the setting:
""""
" NerdTree
"
Bundle 'scrooloose/nerdtree'
Bundle 'jistr/vim-nerdtree-tabs'
map <F2> :NERDTreeTabsToggle<CR>
I can open any number of tabs with the same file by pressing 't'. For example:
|foo.txt|bar.txt|foo.txt|foo.txt|
How to prevent the opening of duplicate files? I want to open an existing buffer by pressing 't'.
I found the solution here https://github.com/scrooloose/nerdtree/issues/439
Grab the latest version and stick this in
~/.vim/nerdtree_plugin/override_tab_mapping.vim
https://gist.github.com/scrooloose/0495cade24f1f2ebb602
Thanks #moeabdol
From what I understand NerdTree does not have such a behavior. I believe however what you are looking for is either :tab drop like #Ben mentioned or using :sb to switch buffers with the following setting: set swb=useopen,usetabe.
Personally I would suggest you use NerdTree for more of a File Explorer and less of a file/buffer manager. By leveraging Vim's buffer commands you can easily switch between buffers. Additionally by using Vim's buffer commands you can avoid the "one-to-one: file-to-tab relationship trap" that so many new vimmers get stuck on.
Aside about NerdTree
NerdTree is very helpful to explore a complex or unfamiliar file structure, but it comes at the cost of taking up screen real estate and disrupting buffer and window/split workflows. See Oil and vinegar - split windows and the project drawer for more. Using a nice fuzzyfinder plugin like CtrlP often takes the place of NerdTree for many people.
I have a nice post about NerdTree that might be of value: Files, Buffers, and Splits Oh My!
Aside about tabs
Vim's tabs are not like most text editors tab. They are more like viewports into a group of windows/splits. Additionally, Vim is buffer centric, not tab centric like most editors. Therefore using features like the quickfix list is often easier without tabs (See :h 'switchbuf if you must use tabs). Vim's tabs often get in the way of using a splits as there are better window and buffer navigation commands available. I personally have many files open (sometimes 100+) use no tabs and use on average 1-2 splits without any issue. Bottom line: read the following posts:
Why do Vim experts prefer buffers over tabs?
Use buffers effectively
Best practices with Vim mappings
Supply a mode. So :map becomes :nmap
Unless using a <Plug> or <SID> mapping you should probably be using :noremap
By following these 2 rules your mapping will become:
nnoremap <f2> :NERDTreeTabsToggle<cr>
to open a new buffer, just press o

Can Vim NerdTree be used as a buffer explorer

I have been using vim NerdTree for sometime now. One thought came to my mind and i am not sure if it is possible so seeking the help of experts.
Can Nerdtree show the list of open buffers say something like :NerdtreeBuffers
I know there is BufferExplorer but i dont like it for the simple fact that i have to pre-define if i want the buffer in a tab/split/vsplit
Nerdtree is awesome ... i just need one command and then from the list i can open it however i want.
No, NERDTree doesn't have any buffer list/exploration properties. I like Buffergator (GitHub) for quick buffer listing & selection. By default, it operates similarly to NERDTree, opening a left vertical split and allowing you to navigate to a buffer and hit enter to jump to it. It also supports opening buffers in vertical/horizontal splits, and other basic functionality. CtrlP is also handy. It does fuzzy find on files, but can also limit itself to open buffers.

An alternative to minibufexplorer (vim)?

minibufexplorer is a persistent buffer manager for vim that lives in its own window. It shows all the buffers you have open, with color indicators as to which are currently visible in windows and which have unsaved changes.
It's a great plugin which I've been hooked on forever. And without the persistent buffer display I find that I now feel lost.
The problem is, minibufexpl tends to get in the way of other window controls. As it is a normal vim window, it behaves like one, causing issues if you wanted to say, rotate your other working windows. Minibufexplorer's window would rotate as well, which is obviously less than desirable.
What I really need is a plugin that persistently displays the open buffers, but doesn't behave like a window. I don't need file navigation or anything as I use other means for that. Perhaps even something like an extended status bar that was capable of displaying information on buffers like minibufexpl does?
There's a new fork of minibufexpl.vim on GitHub which aims to solve some of these problems. One of its new features is "Prevents resizing of MBE buffer by window resizing commands" and it is immune to commands such as C-WC-R (rotate windows).
https://github.com/fholgado/minibufexpl.vim
While this is not an exact answer to your question, I think you could find it useful.
FuzzyFinder plugin provides good visual representation of your open buffers,
and also offers great way to jump between them - fuzzy match.
Its list of buffers is not "persistent" meaning it's only shown on
demand, but I consider this an advantage: firstly the buffers list doesn't eat
up precious screen estate, and secondly with fuzzy match I don't feel a
need to see it at all.
Consider the below screenshot: in order to switch to ext_gcd.py I just need to press
,bg : ,b invokes buffers list and g fuzzy-matches the only buffer
whose name contains letter g.
P.S: it works with files too. My mappings are as follows:
map <leader>f :FufFileWithCurrentBufferDir **/<C-M>
map <leader>b :FufBuffer<C-M>
,f in this case recursively fuzzy-matches files starting from a directory where current buffer is placed.
Old thread, but for anyone else searching checkout the vim-airline plugin. It's very popular and has great compatibility with other plugins.
Use the g:airline#extensions#bufferline#enabled option to get what your looking for.
I really enjoy buftabs.vim because it keeps the information about which buffers I have open in the status line, which disappear when I need to type there in a nice clear manner. I find this quite preferable to minibufexplorer because my cursor never accidentally ends up in a read-only scratch buffer which takes up more space than necessary. It also does some magic-mapping of :bnext and :blast to allow hopping between buffers for a rather more "familiar" feeling.
You can use ctrlp, a plugin written in pure Vimscript and highly configurable.
:CtrlPBuffer open a window with the list of open buffers
you can begin to type, it is a fuzzy finder.
There is also Tagma Buffer Manager. I am not using it yet, but someday I will give it a try, for sure.
You can try Powerline's tabline!
I tried many plugins but they tend to take up the command line or the status line, or won't show up the whole tabs when there're too many of them.
Here is a screen shot (the top bar is the tabline!):
You need to add set showtabline=2 in .vimrc after install Powerline.
And if you want to go to the 8th buffer, :b 8 will jump to it!
Hope this helps.

How to survive the transition from tabbed-based to buffer-based coding (Vim)

I recently changed from notepad++ to Vim. In notepad++ I used to be aware of my open files by
seeing them as tabs, so when I wanted to close or change them I just pressed Shift-Tab or Ctrl-W. In Vim there are also tabs, but when I use them I feel like I'm just going back to my notepad++ way of managing my files. Is there a good way of list, manage, switch and delete buffers other than splitting them?
Yep. I recommend a buffer explorer plugin as well as learning buffer commands. I use this plugin.
http://www.vim.org/scripts/script.php?script_id=42
The buffer paradigm is quite elegant once you are used to it. Less visual clutter. but you are free with vim to find your own thing
You can use these commands:
ls - list all open buffers
bp, bn - switch to the previous or next buffer
b number - switch to the buffer with that number
b text - switch to the buffer whose name includes the string text
I've tried several setups for Vim. My previous was one where I tried to use tabs instead of buffers. It was not very satisfying.
Now I've returned to buffers and for navigating files and buffers I use only the NERDTree and fuzzyfinder (I guess ex Textmate users may prefer fuzzyfinder_textmate) plugins, both are great.
For locating files I use either NERDTree bound to n or fuzzyfinder's File mode. For navigating open buffers I use fuzzfinder's buffer mode solely, bound to b.
Recently I also discovered that I could switch to the previously open buffer with Ctrl-6 (I think maybe that is Ctrl-^ on most keyboard).
There are so many ways to deal with buffers in vim.
CTRL-^ to switch between buffers.
:q is the same as Ctrl-W
For further details see http://vim.wikia.com/wiki/Easier_buffer_switching
BufferExplorerLight
" quick buffer selection including unlisted
nnoremap <leader>b :buffers!<cr>:buffer<space>
Alternatively
nnoremap <leader>b :buffer<space> <c-d>
But really you have to try FuzzyFinder!

Using Vim's tabs like buffers

I have looked at the ability to use tabs in Vim (with :tabe, :tabnew, etc.) as a replacement for my current practice of having many files open in the same window in hidden buffers.
I would like every distinct file that I have open to always be in its own tab. However, there are some things that get in the way of this. How do I fix these:
When commands like gf and ^] jump to a location in another file, the file opens in a new buffer in the current tab. Is there a way to have all of these sorts of commands open the file in a new tab, or switch to the existing tab with the file if it is already open?
When switching buffers I can use
:b <part of filename><tab>
and it will complete the names of files in existing buffers. <part of filename> can even be the middle of a filename instead of the beginning. Is there an equivalent for switching tabs?
Stop, stop, stop.
This is not how Vim's tabs are designed to be used. In fact, they're misnamed. A better name would be "viewport" or "layout", because that's what a tab is—it's a different layout of windows of all of your existing buffers.
Trying to beat Vim into 1 tab == 1 buffer is an exercise in futility. Vim doesn't know or care and it will not respect it on all commands—in particular, anything that uses the quickfix buffer (:make, :grep, and :helpgrep are the ones that spring to mind) will happily ignore tabs and there's nothing you can do to stop that.
Instead:
:set hidden
If you don't have this set already, then do so. It makes vim work like every other multiple-file editor on the planet. You can have edited buffers that aren't visible in a window somewhere.
Use :bn, :bp, :b #, :b name, and ctrl-6 to switch between buffers. I like ctrl-6 myself (alone it switches to the previously used buffer, or #ctrl-6 switches to buffer number #).
Use :ls to list buffers, or a plugin like MiniBufExpl or BufExplorer.
Bit late to the party here but surprised I didn't see the following in this list:
:tab sball - this opens a new tab for each open buffer.
:help switchbuf - this controls buffer switching behaviour, try :set switchbuf=usetab,newtab. This should mean switching to the existing tab if the buffer is open, or creating a new one if not.
Vim :help window explains the confusion "tabs vs buffers" pretty well.
A buffer is the in-memory text of a file.
A window is a viewport
on a buffer.
A tab page is a collection of windows.
Opening multiple files is achieved in vim with buffers. In other editors (e.g. notepad++) this is done with tabs, so the name tab in vim maybe misleading.
Windows are for the purpose of splitting the workspace and displaying multiple files (buffers) together on one screen. In other editors this could be achieved by opening multiple GUI windows and rearranging them on the desktop.
Finally in this analogy vim's tab pages would correspond to multiple desktops, that is different rearrangements of windows.
As vim help: tab-page explains a tab page can be used, when one wants to temporarily edit a file, but does not want to change anything in the current layout of windows and buffers. In such a case another tab page can be used just for the purpose of editing that particular file.
Of course you have to remember that displaying the same file in many tab pages or windows would result in displaying the same working copy (buffer).
Contrary to some of the other answers here, I say that you can use tabs however you want. vim was designed to be versatile and customizable, rather than forcing you to work according to predefined parameters. We all know how us programmers love to impose our "ethics" on everyone else, so this achievement is certainly a primary feature.
<C-w>gf is the tab equivalent of buffers' gf command. <C-PageUp> and <C-PageDown> will switch between tabs. (In Byobu, these two commands never work for me, but they work outside of Byobu/tmux. Alternatives are gt and gT.) <C-w>T will move the current window to a new tab page.
If you'd prefer that vim use an existing tab if possible, rather than creating a duplicate tab, add :set switchbuf=usetab to your .vimrc file. You can add newtab to the list (:set switchbuf=usetab,newtab) to force QuickFix commands that display compile errors to open in separate tabs. I prefer split instead, which opens the compile errors in a split window.
If you have mouse support enabled with :set mouse=a, you can interact with the tabs by clicking on them. There's also a + button by default that will create a new tab.
For the documentation on tabs, type :help tab-page in normal mode. (After you do that, you can practice moving a window to a tab using <C-w>T.) There's a long list of commands. Some of the window commands have to do with tabs, so you might want to look at that documentation as well via :help windows.
Addition: 2013-12-19
To open multiple files in vim with each file in a separate tab, use vim -p file1 file2 .... If you're like me and always forget to add -p, you can add it at the end, as vim follows the normal command line option parsing rules. Alternatively, you can add a bash alias mapping vim to vim -p.
I ran into the same problem. I wanted tabs to work like buffers and I never quite manage to get them to. The solution that I finally settled on was to make buffers behave like tabs!
Check out the plugin called Mini Buffer Explorer, once installed and configured, you'll be able to work with buffers virtaully the same way as tabs without losing any functionality.
This is an answer for those not familiar with Vim and coming from other text editors (in my case Sublime Text).
I read through all these answers and it still wasn't clear. If you read through them enough things begin to make sense, but it took me hours of going back and forth between questions.
The first thing is, as others have explained:
Tab Pages, sound a lot like tabs, they act like tabs and look a lot like tabs in most other GUI editors, but they're not. I think it's an a bad mental model that was built on in Vim, which unfortunately clouds the extra power that you have within a tab page.
The first description that I understood was from #crenate's answer is that they are the equivalent to multiple desktops. When seen in that regard you'd only ever have a couple of desktops open but have lots of GUI windows open within each one.
I would say they are similar to in other editors/browsers:
Tab groupings
Sublime Text workspaces (i.e. a list of the open files that you have in a project)
When you see them like that you realise the power of them that you can easily group sets of files (buffers) together e.g. your CSS files, your HTML files and your JS files in different tab pages. Which is actually pretty awesome.
Other descriptions that I find confusing
Viewport
This makes no sense to me. A viewport which although it does have a defined dictionary term, I've only heard referring to Vim windows in the :help window doc. Viewport is not a term I've ever heard with regards to editors like Sublime Text, Visual Studio, Atom, Notepad++. In fact I'd never heard about it for Vim until I started to try using tab pages.
If you view tab pages like multiple desktops, then referring to a desktop as a single window seems odd.
Workspaces
This possibly makes more sense, the dictionary definition is:
A memory storage facility for temporary use.
So it's like a place where you store a group of buffers.
I didn't initially sound like Sublime Text's concept of a workspace which is a list of all the files that you have open in your project:
the sublime-workspace file, which contains user specific data, such as the open files and the modifications to each.
However thinking about it more, this does actually agree. If you regard a Vim tab page like a Sublime Text project, then it would seem odd to have just one file open in each project and keep switching between projects. Hence why using a tab page to have open only one file is odd.
Collection of windows
The :help window refers to tab pages this way. Plus numerous other answers use the same concept. However until you get your head around what a vim window is, then that's not much use, like building a castle on sand.
As I referred to above, a vim window is the same as a viewport and quiet excellently explained in this linux.com article:
A really useful feature in Vim is the ability to split the viewable area between one or more files, or just to split the window to view two bits of the same file more easily. The Vim documentation refers to this as a viewport or window, interchangeably.
You may already be familiar with this feature if you've ever used Vim's help feature by using :help topic or pressing the F1 key. When you enter help, Vim splits the viewport and opens the help documentation in the top viewport, leaving your document open in the bottom viewport.
I find it odd that a tab page is referred to as a collection of windows instead of a collection of buffers. But I guess you can have two separate tab pages open each with multiple windows all pointing at the same buffer, at least that's what I understand so far.
You can map commands that normally manipulate buffers to manipulate tabs, as I've done with gf in my .vimrc:
map gf :tabe <cfile><CR>
I'm sure you can do the same with [^
I don't think vim supports this for tabs (yet). I use gt and gT to move to the next and previous tabs, respectively. You can also use Ngt, where N is the tab number. One peeve I have is that, by default, the tab number is not displayed in the tab line. To fix this, I put a couple functions at the end of my .vimrc file (I didn't paste here because it's long and didn't format correctly).
I use buffers like tabs, using the BufExplorer plugin and a few macros:
" CTRL+b opens the buffer list
map <C-b> <esc>:BufExplorer<cr>
" gz in command mode closes the current buffer
map gz :bdelete<cr>
" g[bB] in command mode switch to the next/prev. buffer
map gb :bnext<cr>
map gB :bprev<cr>
With BufExplorer you don't have a tab bar at the top, but on the other hand it saves space on your screen, plus you can have an infinite number of files/buffers open and the buffer list is searchable...
Looking at
:help tabs
it doesn't look like vim wants to work the way you do...
Buffers are shared across tabs, so it doesn't seem possible to lock a given buffer to appear only on a certain tab.
It's a good idea, though.
You could probably get the effect you want by using a terminal that supports tabs, like multi-gnome-terminal, then running vim instances in each terminal tab. Not perfect, though...
If you want buffers to work like tabs, check out the tabline plugin.
That uses a single window, and adds a line on the top to simulate the tabs (just showing the list of buffers). This came out a long time ago when tabs were only supported in GVim but not in the command line vim. Since it is only operating with buffers, everything integrates well with the rest of vim.
relevant:
Vim: can global marks switch tabs instead of the file in the current tab?
maybe useful to solve a little part of OP's problem:
nno \ <Cmd>call To_global_mark()<cr>
fun! To_global_mark()
-tab drop /tmp/useless.md
exe 'normal! `' .. toupper(input("To global mark: "))
endf

Resources