Vim --remote-silent always opens [No Name] buffer for first file - vim

I want to be able to open files using the same instance so I added --remote-silent when opening files. But the first time it loads, Vim will open an empty buffer, then my file. So now I have 2 buffers.
Upon further investigation, I noticed that setting nohidden will solve this problem. BUT, not only is it against my liking, it will cause the first buffer to have no syntax highlighting.
This doesn't happen without the --remote-silent option.
Any help appreciated. Thanks!

Doing $ vim --servername FOO --remote[-silent] filename without an instance already running launches a new instance first then opens the file: it is not like $vim filename. You have to find a way to completely remove the first empty buffer.
From my limited testing, adding set bufhidden=wipe to your ~/.vimrc may solve the problem.
set bufhidden=wipe, being local to a buffer, is applied only to the first empty buffer and reset afterwards.
See :h bufhidden.
This will certainly cause some problems when you run Vim normally, though.
edit
Yes, set bufhidden=wipe causes obvious problems. When launched "normally" (with $vim file1) the first buffer is wiped when you edit a second file which is not what you want.
A simple check on the name of the buffer resolves that problem:
if bufname('%') == ''
set bufhidden=wipe
endif
Syntax highlighting works in every situation, here. Could you post the content of your ~/.vim/ and ~/.vimrc somewhere?

The --remote family of options allows for an additional Ex command to be performed after opening the file.
What you can do here is add :bd1 which deletes buffer #1 which is the [No Name] buffer.
example: --remote-silent +:bd1 {file}

Related

automatically save the current buffer when is going to edit a new file

I've made some changes in the current buffer, and I want vim to automatically save the current buffer when I'm going to edit a new file with the following command:
:e another_file_which_is_not_a_buffer_in_vim_yet
I added the following line in my .vimrc file, but it didn't work.
autocmd BufLeave * update
Vim still prompted me No write since last change, why? How can I make it work? By the way, I only want to save the current buffer instead of all buffers, because saving all buffers seems to mess up the order of the buffers, which would bring trouble when I run :bp or :bn.
Vim has an option to save also for, among others, :edit in addition to what triggers autowrite:
set autowriteall
Relevant manual excerpts:
autowrite:
'autowrite' 'aw' boolean (default off)
global
Write the contents of the file, if it has been modified, on each
:next, :rewind, :last, :first, :previous, :stop, :suspend, :tag, :!,
:make, CTRL-] and CTRL-^ command; and when a :buffer, CTRL-O, CTRL-I,
'{A-Z0-9}, or `{A-Z0-9} command takes one to another file.
Note that for some commands the 'autowrite' option is not used, see
'autowriteall' for that.
and autowriteall:
'autowriteall' 'awa' boolean (default off)
global
{not in Vi}
Like 'autowrite', but also used for commands ":edit", ":enew", ":quit",
":qall", ":exit", ":xit", ":recover" and closing the Vim window.
Setting this option also implies that Vim behaves like 'autowrite' has
been set.
This line of code shold do the trick. Place it inside .vimrc file. Location of this .vimrc file can be found if you type :version inside of vim.
set autowrite
More about this topic can be found on this link

How to :bufdo only on modifiable buffers in vim?

Sometimes I need to substitute across multiple buffers. For the purpose I use :bufdo %s/old/new/gec. Recently I noticed that the command fails when there is non-modifiable buffer in the buffer list (in my case it's opened file explorer/netrw). After running the command vim leaves me with E21: Cannot make changes, 'modifiable' is off and opened Netrw window.
Are the ways to :bufdo only on modifiable buffers? I've already tried :bufdo!, but the behaviour was the same (just without showing the error).
UPDATE
I find the line of .vimrc that poses this problematic behaviour:
let g:netrw_liststyle=3
I don't know what the magic here, but when I set this option neither of the suggested solutions/commands work for me. Now, the question is how to keep this line and make the :bufdo behaviour skip the buffer created by Netrw.
Well, if :bufdo sil! :%s/old/new/gec does not work for you (this silently ignores errors). you need to wrap the command into an if statement. Something like this:
:bufdo if &ma | :%s/old/new/gec | endif
which checks for each buffer, if it is modifiable and only then attempts to replace old by new.
Note: You might also want to check for the 'readonly' option in addition to the 'modifiable' setting.

Quit vim, when a buffer has unsaved changes and is un-named?

I created a blank "scratch buffer" (i.e. not associated with a specific file) in vim, by using :vnew. Then I played around with some text, now I simply want to exit the editor - not keeping the contents of this "scratch buffer".
When I type the command:
:q!
Vim gives me:
[No Name][+]
E37: No write since last change (add ! to override)
E162: No write since last change for buffer "[No Name]"
Press ENTER or type command to continue
How can I quit vim from this state?
This happens when you have a modified hidden buffer. Use :qa! to exit anyway.
From :h :quit :
:q[uit]! Quit without writing, also when currently visible
buffers have changes. Does not exit when this is the
last window and there is a changed hidden buffer.
In this case, the first changed hidden buffer becomes
the current buffer.
Use ":qall!" to exit always.
In case someone wants to reproduce it:
Start vim and modify the unnamed buffer
Edit another file (you might need to :set hidden), ie :e README
Try to exit with :q!
At best you could call it a "transient" buffer (in memory but not associated with a specific file) but what you created with :vnew is not a "scratch" buffer.
In Vim, a "scratch" buffer is a normal buffer in which you set a bunch of local options. It can be done with a command like this:
:vnew | setlocal nobuflisted buftype=nofile bufhidden=wipe noswapfile

Programatically execute command to close current buffer and switch to last state

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

vi, vim buffers overrun

I'm losing all previous buffers when by mistake I'm trying to switch behind the last buffer [n:].
If for example I open couple of files in editor
:ls
1 # "/etc/moduli" line 1
2 %a "/etc/motd" line 1
:n
E163: There is only one file to edit
:p
E163: There is only one file to edit
now i can navigate between tabs just using :b [number]
Please advice how to fix this behavior. How can I prevent buffers from closing in this case?
I think you're confusing something there. A buffer is something like an open file. When you switch to the next file in the argument list using :n you close the current buffer and open the next one, so the changes must either be saved or discarded at this point.
Additionally the default behaviour of vim is to display an error message if you try to go beyond the last file in your argument list, so losing anything is not very easy in vim.
Maybe describing your actions (pressed keys) could help here, if this does not answer your question.
[edit]
Ok, now I know what the problem is: There is a difference between a buffer and the list of files to edit that you supply when starting vim. If you start vim with
vim a.txt b.txt
there are 2 files to edit. This does not mean, there are multiple buffers. You can navigate using :n and :p (meaning n(ext) file and p(revious) file). If you have the global flag :hidden set, this means that every buffer you close will become a hidden buffer. The file is still being edited, but it is not shown in any window. This value is possibly set upon startup of vim in your system. Try adding :se nohidden to your .vimrc and try the following:
:help buffer-hidden
[/edit]
:bn
will display the next file in your buffer (in your case "/etc/moduli")
:bp
will display the previous file in your buffer (also "/etc/moduli" because it does a permutation)
One thing that you'll notice is that the file you're editing is marked with
%a
whereas
#
means it's the last file you displayed.
Hope it helps you.
:n and :p doesn't switch between buffers :)
try :bufnext and :bufprev
maybe you'll like:
nmap <LEADER>k :bnext<CR>:redraw<CR>
nmap <LEADER>j :bprevious<CR>:redraw<CR>
nmap <LEADER>d :bd<CR>
nnoremap <LEADER>b :buffers<CR>:buffer<space>
Press ,j for the previous buffer, ,k for the next buffer, ,d to close the current buffer and ,b to list your buffers and select one with number keys.

Resources