created 2003 · complexity basic · author Mark Stosberg · version 5.7
I use Vim's session file feature a lot to switch between projects. Preserving all that context is nice. Usually my process goes like this:
1. Save the current session:
:mks! ~/v/project1.vim
2. Quit vim
:xa
3. Restart with a different session file:
vim -S ~/v/project2.vim
I was exiting and re-opening vim because if I just loaded the second project file, a list of both project buffers would appear in my buffer list. That was not what I wanted -- I wanted to replace the first buffer list completely wtih the second.
There is a simple way to do this. At the top of a session file, add this:
1,999bdelete
That will delete the first 999 existing buffers, effectively allowing any new buffer definitions in the rest of the session file to replace what is currently there. Now you can switch directly to a new session without exiting. Just use:
:so ~/v/project2.vim
Comments[]
Use %bdelete
instead of 1,999bdelete
.
That won't always work.
%bdelete
only deletes as many buffers as there are lines in the current buffer.
How about this:
:execute "1," . bufnr("$") . "bdelete"
That should delete all the buffers. When I say delete, it will only unlist them. The check this execute the above command then run this command.
:ls!