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

help with counsel-shell-command #689

Closed
njdan5691 opened this issue Sep 23, 2016 · 13 comments
Closed

help with counsel-shell-command #689

njdan5691 opened this issue Sep 23, 2016 · 13 comments

Comments

@njdan5691
Copy link

@njdan5691 njdan5691 commented Sep 23, 2016

Hi,

I use M-: (shell-command) often, and have turned on the history, I would love to have a counsel interface instead of the default behavior. I'm pretty new to elisp but this snippet was my first attempt
at it, using counsel-describe-function as a start. Well obviously this did not work. Would someone be kind enough to help.

Thanks in advance

(defun counsel-shell-command ()
  "Forward to `shell-command'."
  (interactive)
  (ivy-read "Shell Command: "
            (let (cands)
              (mapatoms
               (lambda (x)
                 (when (fboundp x)
                   (push (symbol-name x) cands))))
              cands)
            :keymap counsel-ag-map
            :history 'counsel-shell-command-symbol-history
            :require-match t
            :sort t
            :action (lambda (x)
                      (shell-command
                       (intern x)))
            :caller 'counsel-shell-command))
@abo-abo
Copy link
Owner

@abo-abo abo-abo commented Sep 24, 2016

Try this:

(defun counsel-shell-command-history ()
  (interactive)
  (ivy-read "cmd: " shell-command-history
            :action 'insert
            :caller 'counsel-shell-command-history))

(define-key minibuffer-local-shell-command-map
    (kbd "C-r") 'counsel-shell-command-history)

Invoke your shell-command as usual. But press C-r to search though history.

@njdan5691
Copy link
Author

@njdan5691 njdan5691 commented Sep 24, 2016

Thanks, after some tweaking i got my function down to this, which works as I expected. I tried your variant but get "Command attempted to use minibuffer while in minebuffer. I'm happy with what I came up with, but would like to get your version to work also. Again thanks for your help.

(defun counsel-shell-command ()
  "Forward to `shell-command'."
  (interactive)
  (ivy-read "Shell Command: "
                        shell-command-history
            :action (lambda (x)
                      (shell-command x))
            :caller 'counsel-shell-command))
@abo-abo
Copy link
Owner

@abo-abo abo-abo commented Sep 24, 2016

You also need (setq enable-recursive-minibuffers t). A very useful setting.

With you command, you have no option to alter the previous command before running it. With mine, you can do it, but it takes an extra RET.

@dieggsy
Copy link
Contributor

@dieggsy dieggsy commented Sep 24, 2016

@njdan5691 I had to add (add-to-list 'shell-command-history x) to the lambda in your version in order for any extra commands executed from within counsel-shell-command to appear on the history list.

Though having the option to edit the command does seem nice. @abo-abo when I try your version, it inserts the command into the current buffer, not the minibuffer? And C-r is starting a recursive search when (setq enable-recursive-minibuffers t)

@abo-abo
Copy link
Owner

@abo-abo abo-abo commented Sep 25, 2016

it inserts the command into the current buffer, not the minibuffer

Do you have the latest ivy version?

@dieggsy
Copy link
Contributor

@dieggsy dieggsy commented Sep 25, 2016

I believe I do

@abo-abo
Copy link
Owner

@abo-abo abo-abo commented Sep 26, 2016

@therockmandolinist It should insert into the current buffer. Was the minibuffer current when you called the command? You need to call it through a binding when already in the minibuffer, not through M-x or a binding from top-level.

@dieggsy
Copy link
Contributor

@dieggsy dieggsy commented Sep 26, 2016

Oh yeah, I see. Nevermind, I was being silly and calling the function by name. Thanks for clearing that up!

@njdan5691
Copy link
Author

@njdan5691 njdan5691 commented Sep 26, 2016

This is what I ended up with, the @abo-abo version wrapped in use-package. Works perfect, thanks.

    (use-package simple
      :commands (shell-command)
      :config
      (defun counsel-shell-command-history ()                                                                 
        (interactive)
        (ivy-read "cmd: " shell-command-history
                  :action 'insert
                  :caller 'counsel-shell-command-history))

      (define-key minibuffer-local-shell-command-map
        (kbd "C-r") 'counsel-shell-command-history))

@abo-abo abo-abo closed this in e7d6dab Sep 26, 2016
@abo-abo
Copy link
Owner

@abo-abo abo-abo commented Sep 26, 2016

The new command counsel-shell-command-history and C-r is now added to counsel-mode, no extra config necessary.

Thanks to all for contributing to the discussion.

@njdan5691
Copy link
Author

@njdan5691 njdan5691 commented Sep 26, 2016

I don't use counsel-mode, I need to use find-file without the counsel extension, because I use environment variables when locating my files, and have not figured out how to do that with counsel yet.

@JohnLunzer
Copy link

@JohnLunzer JohnLunzer commented Jun 17, 2019

@abo-abo, I've been trying, without success to replicate your counsel-shell-command-history for use with dired-do-async-shell-command.

Do you have any idea how to get a similar C-r map to what you've done with the regular shell command? I can't seem to figure out which map to put the define-key into.

EDIT:

This is solved for apparently all minibuffer now via:
(define-key minibuffer-local-map (kbd "C-r") 'counsel-minibuffer-history)

@abo-abo
Copy link
Owner

@abo-abo abo-abo commented Jun 22, 2019

@JohnLunzer glad you found the solution. The code you used is also part of counsel-mode, it binds C-r in minibuffer-local-map.

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

Successfully merging a pull request may close this issue.

None yet
4 participants