Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Delete Buffer in CtrlPBuf Mode #280

Closed
amadeus opened this issue Sep 17, 2012 · 27 comments
Closed

Feature Request: Delete Buffer in CtrlPBuf Mode #280

amadeus opened this issue Sep 17, 2012 · 27 comments

Comments

@amadeus
Copy link

amadeus commented Sep 17, 2012

I searched through the documentation but I couldn't find it anywhere...

I tend to be pretty anal about my buffer management and thought it would be nice to press <c-d> and delete the currently selected buffer in the list (and keep the buffer list open).

Thoughts?

@kien
Copy link
Owner

kien commented Sep 18, 2012

ctrlp doesn't have this built-in. I'm not going to add it because it's not essential, and you can just be creative with the options. Use something like this in your vimrc, for example:

let g:ctrlp_buffer_func = { 'enter': 'MyCtrlPMappings' }

func! MyCtrlPMappings()
    nnoremap <buffer> <silent> <c-@> :call <sid>DeleteBuffer()<cr>
endfunc

func! s:DeleteBuffer()
    let line = getline('.')
    let bufid = line =~ '\[\d\+\*No Name\]$' ? str2nr(matchstr(line, '\d\+'))
        \ : fnamemodify(line[2:], ':p')
    exec "bd" bufid
    exec "norm \<F5>"
endfunc

Then pressing <c-@> in Buffer mode will delete the selected buffer.

@kien kien closed this as completed Sep 18, 2012
@d11wtq
Copy link

d11wtq commented Sep 18, 2012

I might take another stab at this. My extremely limited knowledge of Vimscript and of the source code in ctrlp tripped me up last time. Good to have some example code to go by :) I was aiming for something where you could <c-z> to mark a number of buffers (this already works out-of-the-box), then use some combination to delete those buffers. Starting with handling just a single buffer would be a good start though!

I'm pretty bad at opening lots of buffers and then having the tedious task of closing the ones I'm no longer using.

@amadeus
Copy link
Author

amadeus commented Sep 18, 2012

Dude, @kien, that works awesomely. Thanks so much!

@d11wtq If you get something worked out, let me know!

@Tinche
Copy link

Tinche commented Dec 25, 2012

I'm going to respectfully disagree; so what if it's non essential? It sounds like a very useful feature to me: it fits naturally within the use case and looks like it'd be very easy to add.

I realize the author is probably worried about bloat but making your users extend your addon through their .vimrc for simple things like this doesn't sound like a good design trade off to me.

@kien
Copy link
Owner

kien commented Dec 25, 2012

It's partly because there aren't many usable mappings left to use. I'd also have to make something to allow changing the deletion command between :bd, :bw, :bun, otherwise people would be requesting it. Lastly, I think people should be using plugins dedicated to buffer management instead.

@wting
Copy link

wting commented Jun 11, 2013

I'm also interested in this feature. CtrlP has replaced BufExplorer for switching between buffers. The only reason I keep BufExplorer is to wipe buffers. I imagine I'm not alone in this common use case.

It would be awesome if I could run :CtrlPBuffer, select a bunch of buffers and with <c-z> and then delete/wipe with <c-d> or <c-w>.

@saamalik
Copy link

@wting that's exactly what I was thinking, <c-z> to select buffers, and then delete/wipe using some fancy keyboard shortcut.

@kien soo how can we entice you to reconsider? =)

@milushov
Copy link

plus one 👍 for this feature request 😃 (yep, i have read entire thread)

@nicklasos
Copy link

+1

3 similar comments
@451F
Copy link

451F commented Jun 17, 2013

👍

@martco
Copy link

martco commented Jul 14, 2013

👍

@rradonic
Copy link

+1

@gdso
Copy link

gdso commented Jul 19, 2013

+1 - I'd also like to avoid installing yet another buffer explorer plugin for this use case. That being said, CtrlP is an awesome plugin, great work!

@amadeus
Copy link
Author

amadeus commented Aug 2, 2013

@kien the script you provided has been working great, however I have found one issue with it. If I attempt to delete default blank buffers, it seems to trip up.

The first buffer showing here: [123*No Name] as show here:

example1

If I attempt to delete it using the <c-@> hotkey, I get the following error:

screen shot 2013-08-02 at 4 29 01 pm

I am not too hot with Vim script, but if you know a quick and easy fix for this, that would be awesome. I can select the buffer itself and manually :bd it no problem, so not sure what the problem is from the snippet's perspective.

@kien
Copy link
Owner

kien commented Aug 3, 2013

@amadeus Yeah, the snippet was written before the addition of No Name buffers. I've updated it a bit.

@amadeus
Copy link
Author

amadeus commented Aug 3, 2013

Thanks, I'll check it out.

@dhiemstra
Copy link

+1

2 similar comments
@kristijanhusak
Copy link

+1

@ptrin
Copy link

ptrin commented Apr 10, 2014

+1

@d11wtq
Copy link

d11wtq commented Apr 11, 2014

I'm currently taking this snippet at looking to provide it as a plugin with the C-z functionality I was after too. Has anybody already done this and not linked to it here?

@kien doing let g:ctrlp_buffer_func = .... in a plugin seems dangerous, since either the plugin will override a user setting, or the user will effectively disable the plugin by overriding this setting. Is there another way to let ctrlp know about the mapping function that is safe to do from shared code?

@d11wtq
Copy link

d11wtq commented May 8, 2014

We're doing Hack Day for the next couple of days at work and I'm keen to use that time to add this functionality to ctrlp via an extension in a separate plugin. Some problems I need to solve are:

  1. Deleting multiple buffers that have been marked (solved).
  2. Finding a way to make sure let g:ctrlp_buffer_func = 'MyPluginFunction' doesn't break any user expectations with what they have in their own vimrc.

Here's a work in progress version of the snippet that allows the use of C-z to mark multiple buffers and close them.

let g:ctrlp_buffer_func = { 'enter': 'CtrlPBDelete' }

function! CtrlPBDelete()
  nnoremap <buffer> <silent> <c-@> :call <sid>DeleteMarkedBuffers()<cr>
endfunction

function! s:DeleteMarkedBuffers()
  " list all marked buffers
  let marked = ctrlp#getmarkedlist()

  " the file under the cursor is implicitly marked
  if empty(marked)
    call add(marked, fnamemodify(ctrlp#getcline(), ':p'))
  endif

  " call bdelete on all marked buffers
  for fname in marked
    let bufid = fname =~ '\[\d\+\*No Name\]$' ? str2nr(matchstr(fname, '\d\+'))
          \ : fnamemodify(fname[2:], ':p')
    exec "silent! bdelete" bufid
  endfor

  " refresh ctrlp
  exec "normal \<F5>"
endfunction

I'm sure there are some bugs with it, but I have another day and half to work on this anyway.

d11wtq added a commit to d11wtq/ctrlp_bdelete.vim that referenced this issue May 9, 2014
@d11wtq
Copy link

d11wtq commented May 9, 2014

Improved further and turned into a plugin. Pull requests welcomed. https://github.com/d11wtq/ctrlp_bdelete.vim

@lukeorland
Copy link

Excellent. The plugin works for me!

@oopsitmelted
Copy link

Very cool! Thanks for the plugin. Works great!

@j5shi
Copy link

j5shi commented Jan 10, 2016

+1, please, add it to ctrlp or accept a pull request for it.

@bravoecho
Copy link

@kien
I've added this to the bouncing-vim distro with a key combo that works also in tmux and mimics the line deletion already provided by the plugin (https://github.com/lucabelmondo/bouncing-vim/commit/4e296557288a38c128ba714bebc059a57389509e) but I would like to +1 😀

@zQueal
Copy link

zQueal commented May 17, 2016

@j5shi Thanks for the PR. Just added it to my Vim install on Windows. Working great.

liskin pushed a commit to liskin/ctrlp.vim that referenced this issue Oct 7, 2016
liskin pushed a commit to liskin/ctrlp.vim that referenced this issue Oct 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests