Vim: Close All Buffers But This One

前端 未结 13 2231
别那么骄傲
别那么骄傲 2020-12-22 15:04

How can I close all buffers in Vim except the one I am currently editing?

相关标签:
13条回答
  • 2020-12-22 15:35

    I like 1,100bd (suggested by juananruiz) which seems to work for me.

    I added a quit! to my mapping to give me

    nnoremap <leader>bd :1,100bd<CR>
    nnoremap <leader>bdq :1,100bd<CR>:q!<CR>
    

    This kills all the buffers and shuts down Vim, which is what I was looking for mostly.

    0 讨论(0)
  • 2020-12-22 15:42

    I was able to do this pretty easily like this:

    :%bd|e#
    
    0 讨论(0)
  • 2020-12-22 15:44

    If you don´t care the current one, is more simple to do something like (no script needing):

    1,100bd
    
    0 讨论(0)
  • 2020-12-22 15:49

    Building on juananruiz's answer.

    Make a small change in the buffer you want to keep, then

    :1,1000bd

    The command bd (buffer delete) will not delete any buffers with unsaved changes. This way you can keep the current (changed) file in the buffer list.

    Edit: Please notice that this will also delete your NERDTreeBuffer. You can get it back with :NERDTree

    0 讨论(0)
  • 2020-12-22 15:50

    You could use this script from vim.org:

    http://www.vim.org/scripts/script.php?script_id=1071

    Just put it to your .vim/plugin directory and then use :BufOnly command to close all buffers but the active one. You could also map it elsewhere you like in your .vimrc.

    Source on Github (via vim-scripts mirror): https://github.com/vim-scripts/BufOnly.vim/blob/master/plugin/BufOnly.vim

    0 讨论(0)
  • 2020-12-22 15:52

    Here's what I do. So I like to keep my cursor position after removing all buffers and most of the solutions above just ignores this fact. I also think remapping the command is better than typing it so Here I use <leader>bd to remove all buffers and jump back to my original cursor position.

    noremap <leader>bd :%bd\|e#\|bd#<cr>\|'"
    

    %bd = delete all buffers.

    e# = open the last buffer for editing (Which Is the buffer I'm working on).

    bd# to delete the [No Name] buffer that gets created when you use %bd.

    The pipe in between just does one command after another. You've gotta escape it though using \|

    '" = keep my cursor position.

    0 讨论(0)
提交回复
热议问题