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

Delete file or kill buffer DWIM [Feature request] #164

Closed
kaushalmodi opened this issue Jun 30, 2015 · 13 comments
Closed

Delete file or kill buffer DWIM [Feature request] #164

kaushalmodi opened this issue Jun 30, 2015 · 13 comments

Comments

@kaushalmodi
Copy link
Contributor

I have switched to using counsel and ivy instead of ido and this is one of the features I miss from ido:

  1. When doing C-x C-f, if the selection is on a file name that is not open in one of the buffers, C-k will delete (or trash, depending on delete-by-moving-to-trash) that file name.
  2. When switching buffers, C-k will kill the selected buffer if open. If the buffer is already closed (virtual buffer) and if that buffer was associated to a file name, that file will be deleted (or trashed).

It will be nice to have these functionalities in counsel-find-file and ivy-switch-buffer.

Currently, I need to switch to that file/buffer and then delete/kill it.

abo-abo added a commit that referenced this issue Jun 30, 2015
* ivy.el (ivy-switch-buffer-map): New defvar.

Re #164
abo-abo added a commit that referenced this issue Jun 30, 2015
* counsel.el (counsel-find-file-map): New defvar.

Re #164
@abo-abo
Copy link
Owner

abo-abo commented Jun 30, 2015

You could add something like this to your config:

(define-key
    ivy-switch-buffer-map
    (kbd "C-k")
  (lambda ()
    (interactive)
    (ivy-set-action 'kill-buffer)
    (ivy-done)))

I'm currently working on multiple actions for a single command. Similar to counsel-describe-variable, except the key bindings aren't hard coded. Maybe that could solve your problem as well:

  1. Call ivy-switch-buffer
  2. C-o to open up options.
  3. w / s to select the buffer kill action, instead of the default switch-to-buffer action.
  4. Use C-M-n to kill a bunch of buffers, while skipping some with C-n.

abo-abo added a commit that referenced this issue Jun 30, 2015
* counsel.el (counsel-find-file): Update.

* ivy.el (ivy--buffer-list): Update.

Re #164
abo-abo added a commit that referenced this issue Jun 30, 2015
While in "C-o":

- Use "s" to make "C-m", "C-j", "C-M-n" and "C-M-p" kill
- Use "w" to switch back to normal.

Re #164
@kaushalmodi
Copy link
Contributor Author

Works great with ivy-switch-buffer. Would like to implement 'delete file' function in counsel-find-file in a similar fashion.

btw, why have the ivy- prefix for ivy-switch-buffer and counsel- for almost everything else?

@kaushalmodi
Copy link
Contributor Author

    (defun counsel-find-file ()
      "Forward to `find-file'."
      (interactive)
      (ivy-read "Find file: " 'read-file-name-internal
                :matcher #'counsel--find-file-matcher
                :action (cons
                         1
                         `(("default"
                            (lambda (x)
                              (find-file
                               (expand-file-name x ivy--directory))))
                           (,(propertize "delete"
                                         'face 'font-lock-warning-face)
                            (lambda (x)
                              (delete-file
                               (expand-file-name x ivy--directory))))))
                :preselect (when counsel-find-file-at-point
                             (require 'ffap)
                             (ffap-guesser))
                :require-match 'confirm-after-completion
                :history 'file-name-history
                :keymap counsel-find-file-map))

@abo-abo
Copy link
Owner

abo-abo commented Jul 3, 2015

btw, why have the ivy- prefix for ivy-switch-buffer and counsel- for almost everything else?

Because most of ivy-switch-buffer is in ivy.el, and it's part of ivy-mode via:

(define-key map [remap switch-to-buffer] 'ivy-switch-buffer)

I tried remapping counsel-find-file, it didn't work for some reason.

@abo-abo
Copy link
Owner

abo-abo commented Jul 3, 2015

Would like to implement 'delete file' function in counsel-find-file in a similar fashion.

I'm afraid of deleting files by accident: by mashing sc you can delete a file by accident. But I don't want to make you redefine counsel-find-file if you choose to have that functionality. Maybe I could add ivy-extra-actions-alist similar to ivy-initial-inputs-alist. What do you think?

@kaushalmodi
Copy link
Contributor Author

Maybe I could add ivy-extra-actions-alist similar to ivy-initial-inputs-alist.

I am fine redefining that function. But adding that variable would also mean easy customizability of any function using ivy; so go for it. Thanks! :)

I also feared of accidental deletion and so I gave font-lock-warning-face to delete option. I can even go further and wrap the delete-file in y-or-n-p.

abo-abo added a commit that referenced this issue Jul 6, 2015
* ivy.el (ivy--actions-list): New defvar. Store the exit points per
  command.
(ivy-set-actions): New defun. Use this to set the extra exit points for
each command.
(ivy-read): Account for `ivy--actions-list'.
(ivy-switch-buffer): Set extra action to kill the buffer. Update the
call to `ivy-read'.

* counsel.el (counsel-locate): Use the single action in the function and
  customize the rest via `ivy-set-actions'.

Re #164
@abo-abo
Copy link
Owner

abo-abo commented Jul 6, 2015

Added the new interface. Give it a try for your file-deleting application.

@kaushalmodi
Copy link
Contributor Author

Works great! Thank you.

(ivy-set-actions
 'counsel-find-file
 `((,(propertize "delete" 'face 'font-lock-warning-face)
    (lambda (x) (delete-file (expand-file-name x ivy--directory))))))

@JohnLunzer
Copy link

I have a question regarding the switch-buffer kill, (ivy-done) closes the ivy minibuffer which is not desirable for me. I changed it to (ivy-done) to (ivy-call) and it performs the action but the ivy minibuffer doesn't update as I would have expected.

What would I have to add to kill a buffer, keep the ivy minibuffer open, and update the ivy-minibuffer to get rid of the killed buffer?

I'm also using ivy-rich, just in case you can't reproduce.

@abo-abo
Copy link
Owner

abo-abo commented Jun 15, 2018

@JohnLunzer I think you're looking for C-c C-k.

@JohnLunzer
Copy link

You know... you just have the answer for everything don't you ;) That's great. With that command and Ivy-rich I feel like I can abandon bs-show

Out of curiosity, so I don't ask stupid questions in the future, what is the prescribed method for showing ivy-x-map keybindings while in an ivy minibuffer?

@abo-abo
Copy link
Owner

abo-abo commented Jun 15, 2018

Out of curiosity, so I don't ask stupid questions in the future, what is the prescribed method for showing ivy-x-map keybindings while in an ivy minibuffer?

Usually, describe-mode should work. But it doesn't show you the C-c C-k binding in this case. We'll need to improve the discoverability in the future.

@basil-conto
Copy link
Collaborator

Usually, describe-mode should work. But it doesn't show you the C-cC-k binding in this case. We'll need to improve the discoverability in the future.

What about C-hb, AKA describe-bindings or, with counsel-mode enabled, counsel-descbinds?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants