
Vim has a lot of commands to close anything and in any combination, but what he doesn’t know how to close it all with one command. To the person who started using Vim recently, this may seem rather strange. Let's try to figure out how to fix this situation.
When I first met Vim, my eyes ran away from the abundance of ways
closing files, windows and the editor itself. Here is a list of commands to delete (close)
buffers and windows (in brackets are the full names of commands):
- : bd [elete] - delete buffer. Actually just makes the buffer invisible, but
leaves it in memory. - : bw [ipeout] - completely erases (wipeout) buffer from memory
- : bun [load] - removes the buffer from memory, but leaves it in the list of buffers.
Useless team, except when you or
giant file size or extremely little RAM. - : close - close the current split. The buffer is not deleted.
- : q [uit] - purely theoretically, the team should close Vim, in practice everything is a bit more complicated
')
I highly recommend setting the
set confirm option to .vimrc, this will allow
avoid annoying error messages when trying to close (delete) the buffer with
not saved changes. A confirmation will appear instead of an error.
close not saved file.
To close a file in the traditional sense (for any other editor)
suitable command
: bw , but unfortunately all the commands for working with buffers
have the ugly property to close the window in which, at the moment
they are displayed (of course, if this is not the only window on the screen).
This problem is solved by installing the
bufkill.vim plugin and using the command
: BW (: BD,: BUN) instead of
: bw (: bd ,: bun) .
The command
: q should close Vim, but its behavior depends on the situation and is far from
intuitively. To close a window, use the command
: close , but close it with
using the last window you can not. It is also not possible to close Vim with
delete buffer commands (even if the buffer is the last).
As a result, the empty buffer cannot be deleted, the last window
you can not close and work with multiple files turns into a game "guess what
the command should be used now -: bw,: BW,: q or: close ”. After
combing forums, I had to reinvent the wheel because
I did not find the one I needed. What I needed was so familiar (for others
editors) and an intuitive way to quickly close files, windows and Vim itself.
The first problem is to determine the algorithm of the “usual” way to produce everything.
above described. By trial and error, the algorithm became like this:

At first glance, a little difficult, but when you start to use - everything rises
their places. Of course, this is not a panacea - windows (not buffers) still need to be closed
using
: close .
Here is the implementation:
function! CountListedBuffers() let cnt = 0 for nr in range(1,bufnr("$")) if buflisted(nr) let cnt += 1 endif endfor return cnt endfunction function! SmartExit() let s:BufferToKill = bufnr('%') let s:EmptyBuffer = 0 if bufname('%') == '' && ! &modified && &modifiable if &buftype == 'nofile' && &swapfile == 0 " Is scratch buffer, not empty else let s:EmptyBuffer = 1 endif endif " Get a list of all windows which have this buffer loaded let s:WindowListWithBufferLoaded = [] let i = 1 let buf = winbufnr(i) while buf != -1 if buf == s:BufferToKill let s:WindowListWithBufferLoaded += [i] endif let i = i + 1 let buf = winbufnr(i) endwhile " Check that the buffer is last if(CountListedBuffers() < 2) let s:LastBuffer = 1 else let s:LastBuffer = 0 endif if s:LastBuffer if len(s:WindowListWithBufferLoaded) > 1 execute "close" else if ! s:EmptyBuffer execute "bw | bw" else execute "q" endif endif else let g:BufKillActionWhenBufferDisplayedInAnotherWindow="kill" execute "BW" let g:BufKillActionWhenBufferDisplayedInAnotherWindow="confirm" endif endfunction
To work, you must have bufkill.vim installed.
As a result, my mapping for “closing everything and everything” looks like this:
"Smart Close"
nmap qq: call SmartExit () "Close the window, but do not delete the buffer
nmap qw <CW> c
I use qq much more often, it does exactly what I understand.
should have done: q, but for some reason does not.
Closing
Total
Vim'om I use recently, but I got the impression
that I will have to write such crutches and bicycles more than once! But what
To be honest - this is extremely exciting. Hope this mini plugin or
The information from this article is useful to someone.
PS Not for holivar's sake, but how is it with Emacs?
PPS I do not use tabs (tabs), splits within one screen are enough for me,
they do not solve problems when working with buffers, they just add them.