In Vim, the :enew command has a really irritating behavior - after I create a new file, type something and save it, an empty buffer/file is still present. Why is that and is there any way to prevent it? I'm tired of having to close all these empty buffers all the time.
Thanks!
Your question doesn't make any sense, :enew only creates one new empty buffer and you say you are already writing text and saving it. It sounds like you are starting up vim without specifying a file, and using :enew as your first command, in which case you would then have two empty buffers. Why not use the new buffer that vim creates on startup?
Btw, :set confirm makes it easier to discard unused buffers.
Related
I'm making a plugin where the idea is that if the buffer is empty then you'll enter insert mode, otherwise you'll stay in normal mode.
Of course this is all nice and well but you come across some problems, what if you just opened Vim and you're in the default empty buffer and you don't want to do anything in that buffer? You gotta first go to normal mode and then do your :e magic. Which is undesirable as it takes more work.
So my question is, how does one detect if the current buffer is a real file whether it exists in the file system or not.
For example, I'd like to detect if the buffer is a JS file, whether new or pre-existing. Or if the buffer is a NERDTree buffer or a plugin buffer, that kind of stuff.
Thank you. Any help is appreciated. :)
P.S.: For those interested, this is the plugin: https://github.com/Greduan/vim-empty-insert
if expand('%:p') != ''
" do stuff
endif
See :h %:p and :h expand().
BTW, sorry if this sounds rude, but I'd guess many long-time Vim users would automatically press a/i when they enter a buffer, without thinking whether it exists on the drive or not. So you might want to consider keeping the functionality more consistent.
Problem:
If the file "tmp.rb" has been modified, the command "e tmp.rb" will fail.
I'm seeking for a command to succeed in both following conditions:
If "tmp.rb" is not in the buffer list, the file will be loaded;
If "tmp.rb" is already in the buffer list and been modified, the
buffer will be loaded(in current window).
Clarification:
Sorry for all the misleadings. Indeed a single command e tmp.rb will achive both as long as the modified "tmp.rb" is not loaded into the current window, in which situation I prefer e tmp.rb does not cause an error and has the same effect as b tmp.rb. Maybe this little concern will make scripting around buffers, windows etc. a little more easily.
The :edit command fails when the buffer is already loaded and modified to prevent you from accidentally overwriting the changes. If you do want to override, use :edit! instead. In many Vim commands, an appended bang (!) forces the command. Alternatively, if you want Vim to ask you for confirmation, use :confirm edit.
I couldn't reproduce your problem.
What I do to try reproduce your problem is
Open tmp.rb with vim, write some thing and save.
In other shell,
$ echo > tmp.rb
Enter :e or e: tmp.rb in vim and it reopen the file without any problem.
If other program is accessing the tmp.rb at the same time this other program maybe are locking the file and because of it you are having the trouble you describe.
The title of your question says "OR" but its body says "AND".
Vim's commands are usually very precise, it's rare to see an ambiguous command that does multiple mutually exclusive things. The number of keys you can use to enter insert mode (iaIAsScCoO) is a perfect example of this.
You should use the appropriate command for each situation:
:e tmp.rb
:b tmp.rb
If you don't mind installing plugins, CtrlP's :CtrlPMixed may help you with its nice abstraction.
I'm editing a file :e foo.
Now I'd like to save it, close the buffer (so I can go back to the state before I opened foo), using a command.
I have this right now:
command! GC silent execute ":wq" | silent execute ":close"
But if I didn't have any buffer open before, I get this error:
E444: Cannot close last window
How can I close the window, such that if it's the last window, it still closes it, and returns me back to the empty screen that you get when you just run vim?
I think you're looking for the :update + :bdelete command combination. The first persists changes if there are any, and the latter removes the buffer. If there are other split windows, the current one is closed. Otherwise, if you have other arguments / hidden buffers, the next one is displayed in the single window. If there is no other window, an empty buffer (like :enew) is shown.
What you see when you start Vim without a file as argument, $ vim, is just an empty buffer. If its state is not altered in any way it is discarded when you do :e file.
You can see that with :ls: the only buffer available is file.
If you want an empty buffer (and I have no idea why you would want that) you'll need to create one explicitly with:
:enew
unless there's an option that I don't know about.
Anyway, I'd suggest you to do:
$ vim file
rather than:
$ vim
:e file
Do you want to do save all files, and exit vim ? Then just type ZZ.
Or you want to do save all files if updated, and close all windows ? Then
command! GC bufdo update | windo bw
If I close a buffer in VIM with :bd and then later on decide to open the same file again.. it will not be listed in the contents of the :ls command because it is marked as unlisted. Is there anyway to close a buffer such that if I do decide to reopen it later it is still available in the buffer list via :ls?
I understand I can see it in the 'hidden' list with :ls! but that isn't what I'm looking for here...
If I really want to get rid of a buffer, I do :bw. Usually, I simply do :q.
You can use :ls! to see it.
Instead, :bw is the right way to wipe the buffer, so that it will never be listed any more.
This IS what :bd and :ls mean to do. Accept it.
On the other hand, if you have multiple windows, what you need may be ^Wc or :clo, which close the current window so that the buffer is not displayed while still being listed.
I open several files in Vim by, for example, running
vim a/*.php
which opens 23 files.
I then make my edit and run the following twice
:q
which closes all my buffers.
How can you close only one buffer in Vim?
A word of caution: “the w in bw does not stand for write but for wipeout!”
More from manuals:
:bd
Unload buffer [N] (default: current
buffer) and delete it from
the buffer list. If the buffer was changed, this fails,
unless when [!] is specified, in which case changes are
lost.
The file remains unaffected.
If you know what you’re doing, you can also use :bw
:bw
Like |:bdelete|, but really delete the
buffer.
If this isn't made obvious by the the previous answers:
:bd will close the current buffer. If you don't want to grab the buffer list.
Check your buffer id using
:buffers
you will see list of buffers there like
1 a.php
2 b.php
3 c.php
if you want to remove b.php from buffer
:2bw
if you want to remove/close all from buffers
:1,3bw
Rather than browse the ouput of the :ls command and delete (unload, wipe..) a buffer by specifying its number, I find that using file names is often more effective.
For instance, after I opened a couple of .txt file to refresh my memories of some fine point.. copy and paste a few lines of text to use as a template of sorts.. etc. I would type the following:
:bd txt <Tab>
Note that the matching string does not have to be at the start of the file name.
The above displays the list of file names that match 'txt' at the bottom of the screen and keeps the :bd command I initially typed untouched, ready to be completed.
Here's an example:
doc1.txt doc2.txt
:bd txt
I could backspace over the 'txt' bit and type in the file name I wish to delete, but where this becomes really convenient is that I don't have to: if I hit the Tab key a second time, Vim automatically completes my command with the first match:
:bd doc1.txt
If I want to get rid of this particular buffer I just need to hit Enter.
And if the buffer I want to delete happens to be the second (third.. etc.) match, I only need to keep hitting the Tab key to make my :bd command cycle through the list of matches.
Naturally, this method can also be used to switch to a given buffer via such commands as :b.. :sb.. etc.
This approach is particularly useful when the 'hidden' Vim option is set, because the buffer list can quickly become quite large, covering several screens, and making it difficult to spot the particular buffer I am looking for.
To make the most of this feature, it's probably best to read the following Vim help file and tweak the behavior of Tab command-line completion accordingly so that it best suits your workflow:
:help wildmode
The behavior I described above results from the following setting, which I chose for consistency's sake in order to emulate bash completion:
:set wildmode=list:longest,full
As opposed to using buffer numbers, the merit of this approach is that I usually remember at least part of a given file name letting me target the buffer directly rather than having to first look up its number via the :ls command.
Use:
:ls - to list buffers
:bd#n - to close buffer where #n is the buffer number (use ls to get it)
Examples:
to delete buffer 2:
:bd2
You can map next and previous to function keys too, making cycling through buffers a breeze
map <F2> :bprevious<CR>
map <F3> :bnext<CR>
from my vimrc
Close buffer without closing the window
If you want to close a buffer without destroying your window layout (current layout based on splits), you can use a Plugin like bbye. Based on this, you can just use
:Bdelete (instead of :bdelete)
:Bwipeout (instead of :bwipeout)
Or just create a mapping in your .vimrc for easier access like
:nnoremap <Leader>q :Bdelete<CR>
Advantage over vim's :bdelete and :bwipeout
From the plugin's documentation:
Close and remove the buffer.
Show another file in that window.
Show an empty file if you've got no other files open.
Do not leave useless [no file] buffers if you decide to edit another file in that window.
Work even if a file's open in multiple windows.
Work a-okay with various buffer explorers and tabbars.
:bdelete vs :bwipeout
From the plugin's documentation:
Vim has two commands for closing a buffer: :bdelete and :bwipeout. The former removes the file from the buffer list, clears its options, variables and mappings. However, it remains in the jumplist, so Ctrl-o takes you back and reopens the file. If that's not what you want, use :bwipeout or Bbye's equivalent :Bwipeout where you would've used :bdelete.
How about
vim -O a a
That way you can edit a single file on your left and navigate the whole dir on your right...
Just a thought, not the solution...
[EDIT: this was a stupid suggestion from a time I did not know Vim well enough. Please don't use tabs instead of buffers; tabs are Vim's "window layouts"]
Maybe switch to using tabs?
vim -p a/*.php opens the same files in tabs
gt and gT switch tabs back and forth
:q closes only the current tab
:qa closes everything and exits
:tabo closes everything but the current tab
Those using a buffer or tree navigation plugin, like Buffergator or NERDTree, will need to toggle these splits before destroying the current buffer - else you'll send your splits into wonkyville
I use:
"" Buffer Navigation
" Toggle left sidebar: NERDTree and BufferGator
fu! UiToggle()
let b = bufnr("%")
execute "NERDTreeToggle | BuffergatorToggle"
execute ( bufwinnr(b) . "wincmd w" )
execute ":set number!"
endf
map <silent> <Leader>w <esc>:call UiToggle()<cr>
Where "NERDTreeToggle" in that list is the same as typing :NERDTreeToggle. You can modify this function to integrate with your own configuration.