There are several ways possible to do what you want, but all involve some kind of scripting.
You could use the already mentioned bufdo
like this (suppose you want to save the buffer number 4):
:4,4bufdo :w
However, this will internally switch to buffer 4 and the cursor will remain in that buffer. So you would need to add some more scripting to restore the buffer afterwards. Also, if you don't know the buffer number (see :ls
to find out), but only know the buffer name you need a little bit of scripting, something like this:
let bufnr=bufnr('name')
if bufnr > 0
let curbuf=bufnr('%')
try
exe bufnr.','.bufnr.'bufdo :w'
catch
finally
exe curbuf.'b'
endtry
endif
This is totally untested, but the idea should be clear. You could wrap it into a function and create a custom command to call this function.
If you would only like to save a couple of buffers, say buffer 4 and 6 and reset all other buffers, you could do it like this:
bufdo if index([4,6], bufnr('%')) > -1 | :w | else :e! |endif
This will save only buffer number 4 or 6 and reload the other buffers from disk (discarding their changes [depending on your undo settings, this is even undoable]). Note, also untested.
Another alternative is to use the writefile()
function. That would work like this:
call writefile(getbufline('name', 1, '$'), fnamemodify(bufname('name'), ':p'))
This will get the content of the buffer that matches 'name' and write it. Note, if name matches several buffers, it will probably fail. So it would be good here to add some error handling here as well.