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 · 11 comments

Comments

Projects
None yet
3 participants
@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

This comment has been minimized.

Owner

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

This comment has been minimized.

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

This comment has been minimized.

Owner

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

This comment has been minimized.

Contributor

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

This comment has been minimized.

Owner

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

This comment has been minimized.

Contributor

dieggsy commented Sep 25, 2016

I believe I do

@abo-abo

This comment has been minimized.

Owner

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

This comment has been minimized.

Contributor

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

This comment has been minimized.

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

This comment has been minimized.

Owner

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

This comment has been minimized.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment