8

I've recently started using spacemacs with ivy, and what drives me nuts is that I instinctively press RET when a directory is selected to descend into it, which opens dired instead. Is there a way to prevent counsel from doing that and only have RET work on files and not directories?

Drew
  • 75,699
  • 9
  • 109
  • 225

1 Answers1

8

By default (I am not familiar with Spacemacs overrides), Ivy binds C-m/RET to the command ivy-done and C-j to ivy-alt-done. As described in the manual, the difference is that the former ends the completion session with the currently selected candidate, whereas the latter enters a directory in the case of filename completion. See also the manual section on filename completion.

I have personally become accustomed to this distinction and use the default keybinding for ivy-alt-done most of the time. If you tend to instinctively go for the RET key (i.e. ivy-done), however, and don't rely on the ivy-alt-done binding as much, I would recommend swapping the bindings of the two commands:

(with-eval-after-load 'counsel
  (let ((done (where-is-internal #'ivy-done     ivy-minibuffer-map t))
        (alt  (where-is-internal #'ivy-alt-done ivy-minibuffer-map t)))
    (define-key counsel-find-file-map done #'ivy-alt-done)
    (define-key counsel-find-file-map alt  #'ivy-done)))

You can replace counsel-find-file-map with ivy-minibuffer-map if you want the swap to apply to all ivy-completed commands, not just counsel-find-file.

You can also replace (where-is-internal <cmd> ivy-minibuffer-map t) with whichever keybinding you prefer, otherwise this returns the default ivy binding for the given <cmd>.

Basil
  • 12,019
  • 43
  • 69