├── .gitignore ├── doc ├── tags └── close-buffers.txt ├── img └── demo.gif ├── README.md └── plugin └── close-buffers.vim /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /doc/tags: -------------------------------------------------------------------------------- 1 | close-buffers.txt close-buffers.txt /*close-buffers.txt* 2 | -------------------------------------------------------------------------------- /img/demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstacruz/close-buffers.vim/master/img/demo.gif -------------------------------------------------------------------------------- /doc/close-buffers.txt: -------------------------------------------------------------------------------- 1 | *close-buffers.txt* Commands to close buffers 2 | 3 | Please see https://github.com/Asheq/close-buffers.vim 4 | 5 | vim:tw=78:et:ft=help:norl: 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # close-buffers.vim 2 | 3 | This plug-in allows you to quickly `bdelete` several buffers at once. It is useful if you want to 4 | prune your buffer list or free up memory. 5 | 6 | It provides a command called `Bdelete` that takes a single parameter specifying which buffers to 7 | `bdelete`. See the list of possible parameters below. 8 | 9 | ## Demo Video 10 | 11 | ![demo](https://raw.githubusercontent.com/Asheq/close-buffers.vim/master/img/demo.gif) 12 | 13 | ## Commands 14 | 15 | Command | Description | Native version 16 | -------- | ----------- | ------------------ 17 | `:Bdelete other` 🔥 | `bdelete` all buffers *except* the buffer in the current window | 18 | `:Bdelete hidden` 🔥 | `bdelete` buffers not visible in a window | 19 | `:Bdelete all` | `bdelete` all buffers | `:bufdo bdelete` 20 | `:Bdelete this` | `bdelete` buffer in the current window | `:bdelete` 21 | `:Bdelete nameless` | `bdelete` buffers without a name: *[No Name]* | 22 | `:Bdelete select` | Lets you interactively select which buffers to `bdelete` | 23 | `:Bdelete menu` | Lets you interactively choose one of the other commands above | 24 | 25 | ## Recommended key mapping 26 | 27 | I recommend one of the following key mappings: 28 | 29 | ``` 30 | nnoremap :Bdelete menu 31 | ``` 32 | ``` 33 | nnoremap Q :Bdelete menu 34 | ``` 35 | 36 | ## Install 37 | You can use any plugin manager you want. Some popular options: 38 | 39 | - [vim-plug](https://github.com/junegunn/vim-plug): `Plug 'Asheq/close-buffers.vim'` 40 | - [Vundle](https://github.com/VundleVim/Vundle.vim): `Plugin 'Asheq/close-buffers.vim'` 41 | - [pathogen](https://github.com/tpope/vim-pathogen): `cd ~/.vim/bundle && git clone git://github.com/asheq/close-buffers.vim.git` 42 | - Manual: Copy the files into your `.vim` directory. 43 | 44 | ## Other Info 45 | 46 | ### Buffers with unsaved changes 47 | By default, `Bdelete` will fail to `bdelete` modified buffers, i.e., buffers that have unsaved 48 | changes. 49 | 50 | In order to force `Bdelete` to `bdelete` modified buffers, add a bang to the end of the command, 51 | i.e., `Bdelete!`. 52 | 53 | Alternatively, you can configure Vim to ask you to confirm each modified buffer that would be 54 | `bdeleted`. This can be done by setting the native Vim `confirm` option with `:set confirm`. 55 | 56 | ### Bwipeout 57 | If you know what you are doing, you can use `Bwipeout` instead of `Bdelete` to `bwipeout` buffers 58 | instead of `bdelete` them. The `Bwipeout` command has the same [parameters](#commands) as `Bdelete`. 59 | 60 | ### Inspiration 61 | This plugin was inspired by [vim-bufonly](https://github.com/schickling/vim-bufonly), but adds 62 | several related features. 63 | 64 | ### License 65 | Same license as Vim itself. 66 | -------------------------------------------------------------------------------- /plugin/close-buffers.vim: -------------------------------------------------------------------------------- 1 | " close-buffers.vim 2 | " Commands to delete buffers 3 | " Author: Asheq Imran 4 | " License: Same license as Vim itself 5 | " Version: 0.5 6 | 7 | " Setup 8 | " -------------------- 9 | if exists("g:loaded_close_buffers") 10 | finish 11 | endif 12 | let g:loaded_close_buffers = 1 13 | 14 | let s:save_cpo = &cpo 15 | set cpo&vim 16 | 17 | " Helper variables 18 | " -------------------- 19 | let s:options = ["menu", "other", "hidden", "nameless", "all", "select", "this"] 20 | let s:menu_options = ["cancel", "other", "hidden", "nameless", "all", "select", "this"] 21 | 22 | function! s:get_menu_confirm_string() 23 | let menu_options = copy(s:menu_options) 24 | let menu_options_with_amp = map(menu_options, '"&" . v:val') 25 | return join(menu_options_with_amp, "\n") 26 | endfunction 27 | 28 | let s:menu_confirm_string = s:get_menu_confirm_string() 29 | 30 | " Set delete and wipeout commands. 31 | " -------------------- 32 | if !exists('g:close_buffers_bdelete_command') 33 | let g:close_buffers_bdelete_command = 'Bdelete' 34 | endif 35 | 36 | if !exists('g:close_buffers_bwipeout_command') 37 | let g:close_buffers_bwipeout_command = 'Bwipeout' 38 | endif 39 | 40 | " Commands 41 | " -------------------- 42 | if exists(':' . g:close_buffers_bdelete_command) 43 | echoerr 'close-buffers.vim: You already have a ":' . g:close_buffers_bdelete_command . '" command defined' 44 | else 45 | execute 'command -bang -nargs=1 -complete=customlist,s:bclose_completion_options ' . g:close_buffers_bdelete_command . ' call s:bclose("bdelete", 0, )' 46 | endif 47 | 48 | if exists(':' . g:close_buffers_bwipeout_command) 49 | echoerr 'close-buffers.vim: You already have a ":' . g:close_buffers_bwipeout_command . '" command defined' 50 | else 51 | execute 'command -bang -nargs=1 -complete=customlist,s:bclose_completion_options ' . g:close_buffers_bwipeout_command . ' call s:bclose("bwipeout", 0, )' 52 | endif 53 | 54 | " Functions 55 | " -------------------- 56 | function! s:bclose(command, bang, option) 57 | let l:option = trim(a:option) 58 | if index(s:options, l:option) < 0 59 | echoerr 'close-buffers.vim: Invalid option: ' . l:option 60 | endif 61 | 62 | if (l:option == 'menu') 63 | call s:bclose_menu(a:command, a:bang) 64 | elseif (l:option == 'select') 65 | pwd 66 | execute 'ls' . (a:command == 'bwipeout' ? '!' : '') 67 | call feedkeys(':' . s:append_bang(a:command, a:bang) . ' ', 'n') 68 | elseif (l:option == 'this') 69 | execute s:append_bang(a:command, a:bang) 70 | else 71 | if (l:option == 'other') 72 | let filtered_bufinfo = filter(s:get_relevant_bufinfo(a:command), 'v:val.bufnr != bufnr("%")') 73 | elseif (l:option == 'hidden') 74 | let filtered_bufinfo = filter(s:get_relevant_bufinfo(a:command), 'empty(v:val.windows)') 75 | elseif (l:option == 'nameless') 76 | let filtered_bufinfo = filter(s:get_relevant_bufinfo(a:command), 'v:val.name == ""') 77 | elseif (l:option == 'all') 78 | let filtered_bufinfo = s:get_relevant_bufinfo(a:command) 79 | endif 80 | 81 | let buffer_numbers = map(filtered_bufinfo, 'v:val.bufnr') 82 | call s:close_buffers(a:command, a:bang, buffer_numbers) 83 | endif 84 | endfunction 85 | 86 | function! s:bclose_completion_options(ArgLead, CmdLine, CursorPos) abort 87 | let matches = [] 88 | for f in s:options 89 | if f =~ '^' . a:ArgLead 90 | call add(matches, f) 91 | endif 92 | endfor 93 | return matches 94 | endfunction 95 | 96 | function! s:bclose_menu(command, bang) 97 | let word = a:command == 'bdelete' ? 'Delete' : 'Wipeout' 98 | let choice = confirm(word . " Buffers?", s:menu_confirm_string, 1) 99 | if (choice != 1) 100 | let option = s:menu_options[choice - 1] 101 | call s:bclose(a:command, a:bang, option) 102 | endif 103 | endfunction 104 | 105 | " Helper functions 106 | " -------------------- 107 | function! s:close_buffers(command, bang, buffer_numbers) 108 | if !empty(a:buffer_numbers) 109 | execute s:append_bang(a:command, a:bang) . ' ' . join(a:buffer_numbers) 110 | endif 111 | endfunction 112 | 113 | function! s:append_bang(command, bang) 114 | return a:command . (a:bang ? '!' : '') 115 | endfunction 116 | 117 | function! s:get_relevant_bufinfo(command) 118 | if (a:command == 'bdelete') 119 | return filter(getbufinfo(), 'v:val.listed') 120 | elseif (a:command == 'bwipeout') 121 | return getbufinfo() 122 | else 123 | echoerr 'close-buffers.vim: Invalid command: ' . a:command 124 | endif 125 | endfunction 126 | 127 | " Obsolete Commands 128 | " -------------------- 129 | if !exists(':CloseAllBuffers') 130 | command -bang CloseAllBuffers echoerr '":CloseAllBuffers" is obsolete. Use ":Bdelete all" instead.' 131 | endif 132 | 133 | if !exists(':CloseHiddenBuffers') 134 | command -bang CloseHiddenBuffers echoerr '":CloseHiddenBuffers" is obsolete. Use ":Bdelete hidden" instead.' 135 | endif 136 | 137 | if !exists(':CloseNamelessBuffers') 138 | command -bang CloseNamelessBuffers echoerr '":CloseNamelessBuffers" is obsolete. Use ":Bdelete nameless" instead.' 139 | endif 140 | 141 | if !exists(':CloseOtherBuffers') 142 | command -bang CloseOtherBuffers echoerr '":CloseOtherBuffers" is obsolete. Use ":Bdelete other" instead.' 143 | endif 144 | 145 | if !exists(':CloseSelectedBuffers') 146 | command -bang CloseSelectedBuffers echoerr '":CloseSelectedBuffers" is obsolete. Use ":Bdelete select" instead.' 147 | endif 148 | 149 | if !exists(':CloseThisBuffer') 150 | command -bang CloseThisBuffer echoerr '":CloseThisBuffer" is obsolete. Use ":Bdelete this" instead.' 151 | endif 152 | 153 | if !exists(':CloseBuffers') 154 | command -bang CloseBuffers echoerr '":CloseBuffers" is obsolete. Use ":Bdelete menu" instead.' 155 | endif 156 | 157 | if !exists(':CloseBuffersMenu') 158 | command -bang CloseBuffersMenu echoerr '":CloseBuffersMenu" is obsolete. Use ":Bdelete menu" instead.' 159 | endif 160 | 161 | " Teardown 162 | " -------------------- 163 | let &cpo = s:save_cpo 164 | unlet s:save_cpo 165 | --------------------------------------------------------------------------------